cxusb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /* DVB USB compliant linux driver for Conexant USB reference design.
  2. *
  3. * The Conexant reference design I saw on their website was only for analogue
  4. * capturing (using the cx25842). The box I took to write this driver (reverse
  5. * engineered) is the one labeled Medion MD95700. In addition to the cx25842
  6. * for analogue capturing it also has a cx22702 DVB-T demodulator on the main
  7. * board. Besides it has a atiremote (X10) and a USB2.0 hub onboard.
  8. *
  9. * Maybe it is a little bit premature to call this driver cxusb, but I assume
  10. * the USB protocol is identical or at least inherited from the reference
  11. * design, so it can be reused for the "analogue-only" device (if it will
  12. * appear at all).
  13. *
  14. * TODO: Use the cx25840-driver for the analogue part
  15. *
  16. * Copyright (C) 2005 Patrick Boettcher (patrick.boettcher@desy.de)
  17. * Copyright (C) 2005 Michael Krufky (mkrufky@m1k.net)
  18. * Copyright (C) 2006 Chris Pascoe (c.pascoe@itee.uq.edu.au)
  19. *
  20. * This program is free software; you can redistribute it and/or modify it
  21. * under the terms of the GNU General Public License as published by the Free
  22. * Software Foundation, version 2.
  23. *
  24. * see Documentation/dvb/README.dvb-usb for more information
  25. */
  26. #include "cxusb.h"
  27. #include "cx22702.h"
  28. #include "lgdt330x.h"
  29. #include "mt352.h"
  30. #include "mt352_priv.h"
  31. /* debug */
  32. int dvb_usb_cxusb_debug;
  33. module_param_named(debug,dvb_usb_cxusb_debug, int, 0644);
  34. MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
  35. static int cxusb_ctrl_msg(struct dvb_usb_device *d,
  36. u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
  37. {
  38. int wo = (rbuf == NULL || rlen == 0); /* write-only */
  39. u8 sndbuf[1+wlen];
  40. memset(sndbuf,0,1+wlen);
  41. sndbuf[0] = cmd;
  42. memcpy(&sndbuf[1],wbuf,wlen);
  43. if (wo)
  44. dvb_usb_generic_write(d,sndbuf,1+wlen);
  45. else
  46. dvb_usb_generic_rw(d,sndbuf,1+wlen,rbuf,rlen,0);
  47. return 0;
  48. }
  49. /* GPIO */
  50. static void cxusb_gpio_tuner(struct dvb_usb_device *d, int onoff)
  51. {
  52. struct cxusb_state *st = d->priv;
  53. u8 o[2],i;
  54. if (st->gpio_write_state[GPIO_TUNER] == onoff)
  55. return;
  56. o[0] = GPIO_TUNER;
  57. o[1] = onoff;
  58. cxusb_ctrl_msg(d,CMD_GPIO_WRITE,o,2,&i,1);
  59. if (i != 0x01)
  60. deb_info("gpio_write failed.\n");
  61. st->gpio_write_state[GPIO_TUNER] = onoff;
  62. }
  63. /* I2C */
  64. static int cxusb_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
  65. {
  66. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  67. int i;
  68. if (down_interruptible(&d->i2c_sem) < 0)
  69. return -EAGAIN;
  70. if (num > 2)
  71. warn("more than 2 i2c messages at a time is not handled yet. TODO.");
  72. for (i = 0; i < num; i++) {
  73. switch (msg[i].addr) {
  74. case 0x63:
  75. cxusb_gpio_tuner(d,0);
  76. break;
  77. default:
  78. cxusb_gpio_tuner(d,1);
  79. break;
  80. }
  81. /* read request */
  82. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  83. u8 obuf[3+msg[i].len], ibuf[1+msg[i+1].len];
  84. obuf[0] = msg[i].len;
  85. obuf[1] = msg[i+1].len;
  86. obuf[2] = msg[i].addr;
  87. memcpy(&obuf[3],msg[i].buf,msg[i].len);
  88. if (cxusb_ctrl_msg(d, CMD_I2C_READ,
  89. obuf, 3+msg[i].len,
  90. ibuf, 1+msg[i+1].len) < 0)
  91. break;
  92. if (ibuf[0] != 0x08)
  93. deb_info("i2c read could have been failed\n");
  94. memcpy(msg[i+1].buf,&ibuf[1],msg[i+1].len);
  95. i++;
  96. } else { /* write */
  97. u8 obuf[2+msg[i].len], ibuf;
  98. obuf[0] = msg[i].addr;
  99. obuf[1] = msg[i].len;
  100. memcpy(&obuf[2],msg[i].buf,msg[i].len);
  101. if (cxusb_ctrl_msg(d,CMD_I2C_WRITE, obuf, 2+msg[i].len, &ibuf,1) < 0)
  102. break;
  103. if (ibuf != 0x08)
  104. deb_info("i2c write could have been failed\n");
  105. }
  106. }
  107. up(&d->i2c_sem);
  108. return i;
  109. }
  110. static u32 cxusb_i2c_func(struct i2c_adapter *adapter)
  111. {
  112. return I2C_FUNC_I2C;
  113. }
  114. static struct i2c_algorithm cxusb_i2c_algo = {
  115. .master_xfer = cxusb_i2c_xfer,
  116. .functionality = cxusb_i2c_func,
  117. };
  118. static int cxusb_power_ctrl(struct dvb_usb_device *d, int onoff)
  119. {
  120. u8 b = 0;
  121. if (onoff)
  122. return cxusb_ctrl_msg(d, CMD_POWER_ON, &b, 1, NULL, 0);
  123. else
  124. return cxusb_ctrl_msg(d, CMD_POWER_OFF, &b, 1, NULL, 0);
  125. }
  126. static int cxusb_streaming_ctrl(struct dvb_usb_device *d, int onoff)
  127. {
  128. u8 buf[2] = { 0x03, 0x00 };
  129. if (onoff)
  130. cxusb_ctrl_msg(d,CMD_STREAMING_ON, buf, 2, NULL, 0);
  131. else
  132. cxusb_ctrl_msg(d,CMD_STREAMING_OFF, NULL, 0, NULL, 0);
  133. return 0;
  134. }
  135. static int cxusb_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  136. {
  137. struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
  138. u8 ircode[4];
  139. int i;
  140. cxusb_ctrl_msg(d, CMD_GET_IR_CODE, NULL, 0, ircode, 4);
  141. *event = 0;
  142. *state = REMOTE_NO_KEY_PRESSED;
  143. for (i = 0; i < d->props.rc_key_map_size; i++) {
  144. if (keymap[i].custom == ircode[2] &&
  145. keymap[i].data == ircode[3]) {
  146. *event = keymap[i].event;
  147. *state = REMOTE_KEY_PRESSED;
  148. return 0;
  149. }
  150. }
  151. return 0;
  152. }
  153. struct dvb_usb_rc_key dvico_mce_rc_keys[] = {
  154. { 0xfe, 0x02, KEY_TV },
  155. { 0xfe, 0x0e, KEY_MP3 },
  156. { 0xfe, 0x1a, KEY_DVD },
  157. { 0xfe, 0x1e, KEY_FAVORITES },
  158. { 0xfe, 0x16, KEY_SETUP },
  159. { 0xfe, 0x46, KEY_POWER2 },
  160. { 0xfe, 0x0a, KEY_EPG },
  161. { 0xfe, 0x49, KEY_BACK },
  162. { 0xfe, 0x4d, KEY_MENU },
  163. { 0xfe, 0x51, KEY_UP },
  164. { 0xfe, 0x5b, KEY_LEFT },
  165. { 0xfe, 0x5f, KEY_RIGHT },
  166. { 0xfe, 0x53, KEY_DOWN },
  167. { 0xfe, 0x5e, KEY_OK },
  168. { 0xfe, 0x59, KEY_INFO },
  169. { 0xfe, 0x55, KEY_TAB },
  170. { 0xfe, 0x0f, KEY_PREVIOUSSONG },/* Replay */
  171. { 0xfe, 0x12, KEY_NEXTSONG }, /* Skip */
  172. { 0xfe, 0x42, KEY_ENTER }, /* Windows/Start */
  173. { 0xfe, 0x15, KEY_VOLUMEUP },
  174. { 0xfe, 0x05, KEY_VOLUMEDOWN },
  175. { 0xfe, 0x11, KEY_CHANNELUP },
  176. { 0xfe, 0x09, KEY_CHANNELDOWN },
  177. { 0xfe, 0x52, KEY_CAMERA },
  178. { 0xfe, 0x5a, KEY_TUNER }, /* Live */
  179. { 0xfe, 0x19, KEY_OPEN },
  180. { 0xfe, 0x0b, KEY_1 },
  181. { 0xfe, 0x17, KEY_2 },
  182. { 0xfe, 0x1b, KEY_3 },
  183. { 0xfe, 0x07, KEY_4 },
  184. { 0xfe, 0x50, KEY_5 },
  185. { 0xfe, 0x54, KEY_6 },
  186. { 0xfe, 0x48, KEY_7 },
  187. { 0xfe, 0x4c, KEY_8 },
  188. { 0xfe, 0x58, KEY_9 },
  189. { 0xfe, 0x13, KEY_ANGLE }, /* Aspect */
  190. { 0xfe, 0x03, KEY_0 },
  191. { 0xfe, 0x1f, KEY_ZOOM },
  192. { 0xfe, 0x43, KEY_REWIND },
  193. { 0xfe, 0x47, KEY_PLAYPAUSE },
  194. { 0xfe, 0x4f, KEY_FASTFORWARD },
  195. { 0xfe, 0x57, KEY_MUTE },
  196. { 0xfe, 0x0d, KEY_STOP },
  197. { 0xfe, 0x01, KEY_RECORD },
  198. { 0xfe, 0x4e, KEY_POWER },
  199. };
  200. static int cxusb_dee1601_demod_init(struct dvb_frontend* fe)
  201. {
  202. static u8 clock_config [] = { CLOCK_CTL, 0x38, 0x38 };
  203. static u8 reset [] = { RESET, 0x80 };
  204. static u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
  205. static u8 agc_cfg [] = { AGC_TARGET, 0x28, 0x20 };
  206. static u8 gpp_ctl_cfg [] = { GPP_CTL, 0x33 };
  207. static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
  208. mt352_write(fe, clock_config, sizeof(clock_config));
  209. udelay(200);
  210. mt352_write(fe, reset, sizeof(reset));
  211. mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
  212. mt352_write(fe, agc_cfg, sizeof(agc_cfg));
  213. mt352_write(fe, gpp_ctl_cfg, sizeof(gpp_ctl_cfg));
  214. mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
  215. return 0;
  216. }
  217. struct cx22702_config cxusb_cx22702_config = {
  218. .demod_address = 0x63,
  219. .output_mode = CX22702_PARALLEL_OUTPUT,
  220. .pll_init = dvb_usb_pll_init_i2c,
  221. .pll_set = dvb_usb_pll_set_i2c,
  222. };
  223. struct lgdt330x_config cxusb_lgdt330x_config = {
  224. .demod_address = 0x0e,
  225. .demod_chip = LGDT3303,
  226. .pll_set = dvb_usb_pll_set_i2c,
  227. };
  228. struct mt352_config cxusb_dee1601_config = {
  229. .demod_address = 0x0f,
  230. .demod_init = cxusb_dee1601_demod_init,
  231. .pll_set = dvb_usb_pll_set,
  232. };
  233. /* Callbacks for DVB USB */
  234. static int cxusb_fmd1216me_tuner_attach(struct dvb_usb_device *d)
  235. {
  236. u8 bpll[4] = { 0x0b, 0xdc, 0x9c, 0xa0 };
  237. d->pll_addr = 0x61;
  238. memcpy(d->pll_init, bpll, 4);
  239. d->pll_desc = &dvb_pll_fmd1216me;
  240. return 0;
  241. }
  242. static int cxusb_lgh064f_tuner_attach(struct dvb_usb_device *d)
  243. {
  244. u8 bpll[4] = { 0x00, 0x00, 0x18, 0x50 };
  245. /* bpll[2] : unset bit 3, set bits 4&5
  246. bpll[3] : 0x50 - digital, 0x20 - analog */
  247. d->pll_addr = 0x61;
  248. memcpy(d->pll_init, bpll, 4);
  249. d->pll_desc = &dvb_pll_tdvs_tua6034;
  250. return 0;
  251. }
  252. static int cxusb_dee1601_tuner_attach(struct dvb_usb_device *d)
  253. {
  254. d->pll_addr = 0x61;
  255. d->pll_desc = &dvb_pll_thomson_dtt7579;
  256. return 0;
  257. }
  258. static int cxusb_cx22702_frontend_attach(struct dvb_usb_device *d)
  259. {
  260. u8 b;
  261. if (usb_set_interface(d->udev,0,6) < 0)
  262. err("set interface failed");
  263. cxusb_ctrl_msg(d,CMD_DIGITAL, NULL, 0, &b, 1);
  264. if ((d->fe = cx22702_attach(&cxusb_cx22702_config, &d->i2c_adap)) != NULL)
  265. return 0;
  266. return -EIO;
  267. }
  268. static int cxusb_lgdt330x_frontend_attach(struct dvb_usb_device *d)
  269. {
  270. if (usb_set_interface(d->udev,0,7) < 0)
  271. err("set interface failed");
  272. cxusb_ctrl_msg(d,CMD_DIGITAL, NULL, 0, NULL, 0);
  273. if ((d->fe = lgdt330x_attach(&cxusb_lgdt330x_config, &d->i2c_adap)) != NULL)
  274. return 0;
  275. return -EIO;
  276. }
  277. static int cxusb_dee1601_frontend_attach(struct dvb_usb_device *d)
  278. {
  279. if (usb_set_interface(d->udev,0,0) < 0)
  280. err("set interface failed");
  281. cxusb_ctrl_msg(d,CMD_DIGITAL, NULL, 0, NULL, 0);
  282. if ((d->fe = mt352_attach(&cxusb_dee1601_config, &d->i2c_adap)) != NULL)
  283. return 0;
  284. return -EIO;
  285. }
  286. /*
  287. * DViCO bluebird firmware needs the "warm" product ID to be patched into the
  288. * firmware file before download.
  289. */
  290. #define BLUEBIRD_01_ID_OFFSET 6638
  291. static int bluebird_patch_dvico_firmware_download(struct usb_device *udev, const struct firmware *fw)
  292. {
  293. if (fw->size < BLUEBIRD_01_ID_OFFSET + 4)
  294. return -EINVAL;
  295. if (fw->data[BLUEBIRD_01_ID_OFFSET] == (USB_VID_DVICO & 0xff) &&
  296. fw->data[BLUEBIRD_01_ID_OFFSET + 1] == USB_VID_DVICO >> 8) {
  297. /* FIXME: are we allowed to change the fw-data ? */
  298. fw->data[BLUEBIRD_01_ID_OFFSET + 2] = udev->descriptor.idProduct + 1;
  299. fw->data[BLUEBIRD_01_ID_OFFSET + 3] = udev->descriptor.idProduct >> 8;
  300. return usb_cypress_load_firmware(udev,fw,CYPRESS_FX2);
  301. }
  302. return -EINVAL;
  303. }
  304. /* DVB USB Driver stuff */
  305. static struct dvb_usb_properties cxusb_medion_properties;
  306. static struct dvb_usb_properties cxusb_bluebird_lgh064f_properties;
  307. static struct dvb_usb_properties cxusb_bluebird_dee1601_properties;
  308. static int cxusb_probe(struct usb_interface *intf,
  309. const struct usb_device_id *id)
  310. {
  311. if (dvb_usb_device_init(intf,&cxusb_medion_properties,THIS_MODULE,NULL) == 0 ||
  312. dvb_usb_device_init(intf,&cxusb_bluebird_lgh064f_properties,THIS_MODULE,NULL) == 0 ||
  313. dvb_usb_device_init(intf,&cxusb_bluebird_dee1601_properties,THIS_MODULE,NULL) == 0) {
  314. return 0;
  315. }
  316. return -EINVAL;
  317. }
  318. static struct usb_device_id cxusb_table [] = {
  319. { USB_DEVICE(USB_VID_MEDION, USB_PID_MEDION_MD95700) },
  320. { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LG064F_COLD) },
  321. { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LG064F_WARM) },
  322. { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DEE1601_COLD) },
  323. { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DEE1601_WARM) },
  324. {} /* Terminating entry */
  325. };
  326. MODULE_DEVICE_TABLE (usb, cxusb_table);
  327. static struct dvb_usb_properties cxusb_medion_properties = {
  328. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  329. .usb_ctrl = CYPRESS_FX2,
  330. .size_of_priv = sizeof(struct cxusb_state),
  331. .streaming_ctrl = cxusb_streaming_ctrl,
  332. .power_ctrl = cxusb_power_ctrl,
  333. .frontend_attach = cxusb_cx22702_frontend_attach,
  334. .tuner_attach = cxusb_fmd1216me_tuner_attach,
  335. .i2c_algo = &cxusb_i2c_algo,
  336. .generic_bulk_ctrl_endpoint = 0x01,
  337. /* parameter for the MPEG2-data transfer */
  338. .urb = {
  339. .type = DVB_USB_BULK,
  340. .count = 5,
  341. .endpoint = 0x02,
  342. .u = {
  343. .bulk = {
  344. .buffersize = 8192,
  345. }
  346. }
  347. },
  348. .num_device_descs = 1,
  349. .devices = {
  350. { "Medion MD95700 (MDUSBTV-HYBRID)",
  351. { NULL },
  352. { &cxusb_table[0], NULL },
  353. },
  354. }
  355. };
  356. static struct dvb_usb_properties cxusb_bluebird_lgh064f_properties = {
  357. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  358. .usb_ctrl = DEVICE_SPECIFIC,
  359. .firmware = "dvb-usb-bluebird-01.fw",
  360. .download_firmware = bluebird_patch_dvico_firmware_download,
  361. /* use usb alt setting 0 for EP4 transfer (dvb-t),
  362. use usb alt setting 7 for EP2 transfer (atsc) */
  363. .size_of_priv = sizeof(struct cxusb_state),
  364. .streaming_ctrl = cxusb_streaming_ctrl,
  365. .power_ctrl = cxusb_power_ctrl,
  366. .frontend_attach = cxusb_lgdt330x_frontend_attach,
  367. .tuner_attach = cxusb_lgh064f_tuner_attach,
  368. .i2c_algo = &cxusb_i2c_algo,
  369. .generic_bulk_ctrl_endpoint = 0x01,
  370. /* parameter for the MPEG2-data transfer */
  371. .urb = {
  372. .type = DVB_USB_BULK,
  373. .count = 5,
  374. .endpoint = 0x02,
  375. .u = {
  376. .bulk = {
  377. .buffersize = 8192,
  378. }
  379. }
  380. },
  381. .num_device_descs = 1,
  382. .devices = {
  383. { "DViCO FusionHDTV5 USB Gold",
  384. { &cxusb_table[1], NULL },
  385. { &cxusb_table[2], NULL },
  386. },
  387. }
  388. };
  389. static struct dvb_usb_properties cxusb_bluebird_dee1601_properties = {
  390. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  391. .usb_ctrl = DEVICE_SPECIFIC,
  392. .firmware = "dvb-usb-bluebird-01.fw",
  393. .download_firmware = bluebird_patch_dvico_firmware_download,
  394. /* use usb alt setting 0 for EP4 transfer (dvb-t),
  395. use usb alt setting 7 for EP2 transfer (atsc) */
  396. .size_of_priv = sizeof(struct cxusb_state),
  397. .streaming_ctrl = cxusb_streaming_ctrl,
  398. .power_ctrl = cxusb_power_ctrl,
  399. .frontend_attach = cxusb_dee1601_frontend_attach,
  400. .tuner_attach = cxusb_dee1601_tuner_attach,
  401. .i2c_algo = &cxusb_i2c_algo,
  402. .rc_interval = 150,
  403. .rc_key_map = dvico_mce_rc_keys,
  404. .rc_key_map_size = ARRAY_SIZE(dvico_mce_rc_keys),
  405. .rc_query = cxusb_rc_query,
  406. .generic_bulk_ctrl_endpoint = 0x01,
  407. /* parameter for the MPEG2-data transfer */
  408. .urb = {
  409. .type = DVB_USB_BULK,
  410. .count = 5,
  411. .endpoint = 0x04,
  412. .u = {
  413. .bulk = {
  414. .buffersize = 8192,
  415. }
  416. }
  417. },
  418. .num_device_descs = 1,
  419. .devices = {
  420. { "DViCO FusionHDTV DVB-T Dual USB",
  421. { &cxusb_table[3], NULL },
  422. { &cxusb_table[4], NULL },
  423. },
  424. }
  425. };
  426. static struct usb_driver cxusb_driver = {
  427. .name = "dvb_usb_cxusb",
  428. .probe = cxusb_probe,
  429. .disconnect = dvb_usb_device_exit,
  430. .id_table = cxusb_table,
  431. };
  432. /* module stuff */
  433. static int __init cxusb_module_init(void)
  434. {
  435. int result;
  436. if ((result = usb_register(&cxusb_driver))) {
  437. err("usb_register failed. Error number %d",result);
  438. return result;
  439. }
  440. return 0;
  441. }
  442. static void __exit cxusb_module_exit(void)
  443. {
  444. /* deregister this driver from the USB subsystem */
  445. usb_deregister(&cxusb_driver);
  446. }
  447. module_init (cxusb_module_init);
  448. module_exit (cxusb_module_exit);
  449. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  450. MODULE_DESCRIPTION("Driver for Conexant USB2.0 hybrid reference design");
  451. MODULE_VERSION("1.0-alpha");
  452. MODULE_LICENSE("GPL");