m920x.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /* DVB USB compliant linux driver for MSI Mega Sky 580 DVB-T USB2.0 receiver
  2. *
  3. * Copyright (C) 2006 Aapo Tahkola (aet@rasterburn.org)
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation, version 2.
  8. *
  9. * see Documentation/dvb/README.dvb-usb for more information
  10. */
  11. #include "m920x.h"
  12. #include "mt352.h"
  13. #include "mt352_priv.h"
  14. #include "qt1010.h"
  15. #include "tda1004x.h"
  16. #include "tda827x.h"
  17. /* debug */
  18. static int dvb_usb_m920x_debug;
  19. module_param_named(debug,dvb_usb_m920x_debug, int, 0644);
  20. MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
  21. static struct dvb_usb_rc_key megasky_rc_keys [] = {
  22. { 0x0, 0x12, KEY_POWER },
  23. { 0x0, 0x1e, KEY_CYCLEWINDOWS }, /* min/max */
  24. { 0x0, 0x02, KEY_CHANNELUP },
  25. { 0x0, 0x05, KEY_CHANNELDOWN },
  26. { 0x0, 0x03, KEY_VOLUMEUP },
  27. { 0x0, 0x06, KEY_VOLUMEDOWN },
  28. { 0x0, 0x04, KEY_MUTE },
  29. { 0x0, 0x07, KEY_OK }, /* TS */
  30. { 0x0, 0x08, KEY_STOP },
  31. { 0x0, 0x09, KEY_MENU }, /* swap */
  32. { 0x0, 0x0a, KEY_REWIND },
  33. { 0x0, 0x1b, KEY_PAUSE },
  34. { 0x0, 0x1f, KEY_FASTFORWARD },
  35. { 0x0, 0x0c, KEY_RECORD },
  36. { 0x0, 0x0d, KEY_CAMERA }, /* screenshot */
  37. { 0x0, 0x0e, KEY_COFFEE }, /* "MTS" */
  38. };
  39. static inline int m9206_read(struct usb_device *udev, u8 request, u16 value,\
  40. u16 index, void *data, int size)
  41. {
  42. int ret;
  43. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  44. request, USB_TYPE_VENDOR | USB_DIR_IN,
  45. value, index, data, size, 2000);
  46. if (ret < 0) {
  47. printk(KERN_INFO "m920x_read = error: %d\n", ret);
  48. return ret;
  49. }
  50. if (ret != size) {
  51. deb_rc("m920x_read = no data\n");
  52. return -EIO;
  53. }
  54. return 0;
  55. }
  56. static inline int m9206_write(struct usb_device *udev, u8 request,
  57. u16 value, u16 index)
  58. {
  59. int ret;
  60. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  61. request, USB_TYPE_VENDOR | USB_DIR_OUT,
  62. value, index, NULL, 0, 2000);
  63. return ret;
  64. }
  65. static int m9206_init(struct dvb_usb_device *d)
  66. {
  67. int ret = 0;
  68. /* Remote controller init. */
  69. if (d->props.rc_query) {
  70. if ((ret = m9206_write(d->udev, M9206_CORE, 0xa8, M9206_RC_INIT2)) != 0)
  71. return ret;
  72. if ((ret = m9206_write(d->udev, M9206_CORE, 0x51, M9206_RC_INIT1)) != 0)
  73. return ret;
  74. }
  75. return ret;
  76. }
  77. static int m9206_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  78. {
  79. struct m9206_state *m = d->priv;
  80. int i, ret = 0;
  81. u8 rc_state[2];
  82. if ((ret = m9206_read(d->udev, M9206_CORE, 0x0, M9206_RC_STATE, rc_state, 1)) != 0)
  83. goto unlock;
  84. if ((ret = m9206_read(d->udev, M9206_CORE, 0x0, M9206_RC_KEY, rc_state + 1, 1)) != 0)
  85. goto unlock;
  86. for (i = 0; i < d->props.rc_key_map_size; i++)
  87. if (d->props.rc_key_map[i].data == rc_state[1]) {
  88. *event = d->props.rc_key_map[i].event;
  89. switch(rc_state[0]) {
  90. case 0x80:
  91. *state = REMOTE_NO_KEY_PRESSED;
  92. goto unlock;
  93. case 0x93:
  94. case 0x92:
  95. m->rep_count = 0;
  96. *state = REMOTE_KEY_PRESSED;
  97. goto unlock;
  98. case 0x91:
  99. /* For comfort. */
  100. if (++m->rep_count > 2)
  101. *state = REMOTE_KEY_REPEAT;
  102. goto unlock;
  103. default:
  104. deb_rc("Unexpected rc response %x\n", rc_state[0]);
  105. *state = REMOTE_NO_KEY_PRESSED;
  106. goto unlock;
  107. }
  108. }
  109. if (rc_state[1] != 0)
  110. deb_rc("Unknown rc key %x\n", rc_state[1]);
  111. *state = REMOTE_NO_KEY_PRESSED;
  112. unlock:
  113. return ret;
  114. }
  115. /* I2C */
  116. static int m9206_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  117. int num)
  118. {
  119. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  120. int i, j;
  121. int ret = 0;
  122. if (!num)
  123. return -EINVAL;
  124. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  125. return -EAGAIN;
  126. for (i = 0; i < num; i++) {
  127. if (msg[i].flags & (I2C_M_NO_RD_ACK|I2C_M_IGNORE_NAK|I2C_M_TEN) ||
  128. msg[i].len == 0) {
  129. /* For a 0 byte message, I think sending the address to index 0x80|0x40
  130. * would be the correct thing to do. However, zero byte messages are
  131. * only used for probing, and since we don't know how to get the slave's
  132. * ack, we can't probe. */
  133. ret = -ENOTSUPP;
  134. goto unlock;
  135. }
  136. /* Send START & address/RW bit */
  137. if (!(msg[i].flags & I2C_M_NOSTART)) {
  138. if ((ret = m9206_write(d->udev, M9206_I2C, (msg[i].addr<<1)|(msg[i].flags&I2C_M_RD?0x01:0), 0x80)) != 0)
  139. goto unlock;
  140. /* Should check for ack here, if we knew how. */
  141. }
  142. if (msg[i].flags & I2C_M_RD) {
  143. for (j = 0; j < msg[i].len; j++) {
  144. /* Last byte of transaction? Send STOP, otherwise send ACK. */
  145. int stop = (i+1 == num && j+1 == msg[i].len)?0x40:0x01;
  146. if ((ret = m9206_read(d->udev, M9206_I2C, 0x0, 0x20|stop, &msg[i].buf[j], 1)) != 0)
  147. goto unlock;
  148. }
  149. } else {
  150. for (j = 0; j < msg[i].len; j++) {
  151. /* Last byte of transaction? Then send STOP. */
  152. int stop = (i+1 == num && j+1 == msg[i].len)?0x40:0x00;
  153. if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].buf[j], stop)) != 0)
  154. goto unlock;
  155. /* Should check for ack here too. */
  156. }
  157. }
  158. }
  159. ret = num;
  160. unlock:
  161. mutex_unlock(&d->i2c_mutex);
  162. return ret;
  163. }
  164. static u32 m9206_i2c_func(struct i2c_adapter *adapter)
  165. {
  166. return I2C_FUNC_I2C;
  167. }
  168. static struct i2c_algorithm m9206_i2c_algo = {
  169. .master_xfer = m9206_i2c_xfer,
  170. .functionality = m9206_i2c_func,
  171. };
  172. static int m9206_set_filter(struct dvb_usb_adapter *adap, int type, int idx,
  173. int pid)
  174. {
  175. int ret = 0;
  176. if (pid >= 0x8000)
  177. return -EINVAL;
  178. pid |= 0x8000;
  179. if ((ret = m9206_write(adap->dev->udev, M9206_FILTER, pid, (type << 8) | (idx * 4) )) != 0)
  180. return ret;
  181. if ((ret = m9206_write(adap->dev->udev, M9206_FILTER, 0, (type << 8) | (idx * 4) )) != 0)
  182. return ret;
  183. return ret;
  184. }
  185. static int m9206_update_filters(struct dvb_usb_adapter *adap)
  186. {
  187. struct m9206_state *m = adap->dev->priv;
  188. int enabled = m->filtering_enabled;
  189. int i, ret = 0, filter = 0;
  190. for (i = 0; i < M9206_MAX_FILTERS; i++)
  191. if (m->filters[i] == 8192)
  192. enabled = 0;
  193. /* Disable all filters */
  194. if ((ret = m9206_set_filter(adap, 0x81, 1, enabled)) != 0)
  195. return ret;
  196. for (i = 0; i < M9206_MAX_FILTERS; i++)
  197. if ((ret = m9206_set_filter(adap, 0x81, i + 2, 0)) != 0)
  198. return ret;
  199. if ((ret = m9206_set_filter(adap, 0x82, 0, 0x0)) != 0)
  200. return ret;
  201. /* Set */
  202. if (enabled) {
  203. for (i = 0; i < M9206_MAX_FILTERS; i++) {
  204. if (m->filters[i] == 0)
  205. continue;
  206. if ((ret = m9206_set_filter(adap, 0x81, filter + 2, m->filters[i])) != 0)
  207. return ret;
  208. filter++;
  209. }
  210. }
  211. if ((ret = m9206_set_filter(adap, 0x82, 0, 0x02f5)) != 0)
  212. return ret;
  213. return ret;
  214. }
  215. static int m9206_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
  216. {
  217. struct m9206_state *m = adap->dev->priv;
  218. m->filtering_enabled = onoff ? 1 : 0;
  219. return m9206_update_filters(adap);
  220. }
  221. static int m9206_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
  222. int onoff)
  223. {
  224. struct m9206_state *m = adap->dev->priv;
  225. m->filters[index] = onoff ? pid : 0;
  226. return m9206_update_filters(adap);
  227. }
  228. static int m9206_firmware_download(struct usb_device *udev,
  229. const struct firmware *fw)
  230. {
  231. u16 value, index, size;
  232. u8 read[4], *buff;
  233. int i, pass, ret = 0;
  234. buff = kmalloc(65536, GFP_KERNEL);
  235. if ((ret = m9206_read(udev, M9206_FILTER, 0x0, 0x8000, read, 4)) != 0)
  236. goto done;
  237. deb_rc("%x %x %x %x\n", read[0], read[1], read[2], read[3]);
  238. if ((ret = m9206_read(udev, M9206_FW, 0x0, 0x0, read, 1)) != 0)
  239. goto done;
  240. deb_rc("%x\n", read[0]);
  241. for (pass = 0; pass < 2; pass++) {
  242. for (i = 0; i + (sizeof(u16) * 3) < fw->size;) {
  243. value = le16_to_cpu(*(u16 *)(fw->data + i));
  244. i += sizeof(u16);
  245. index = le16_to_cpu(*(u16 *)(fw->data + i));
  246. i += sizeof(u16);
  247. size = le16_to_cpu(*(u16 *)(fw->data + i));
  248. i += sizeof(u16);
  249. if (pass == 1) {
  250. /* Will stall if using fw->data ... */
  251. memcpy(buff, fw->data + i, size);
  252. ret = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
  253. M9206_FW,
  254. USB_TYPE_VENDOR | USB_DIR_OUT,
  255. value, index, buff, size, 20);
  256. if (ret != size) {
  257. deb_rc("error while uploading fw!\n");
  258. ret = -EIO;
  259. goto done;
  260. }
  261. msleep(3);
  262. }
  263. i += size;
  264. }
  265. if (i != fw->size) {
  266. deb_rc("bad firmware file!\n");
  267. ret = -EINVAL;
  268. goto done;
  269. }
  270. }
  271. msleep(36);
  272. /* m9206 will disconnect itself from the bus after this. */
  273. (void) m9206_write(udev, M9206_CORE, 0x01, M9206_FW_GO);
  274. deb_rc("firmware uploaded!\n");
  275. done:
  276. kfree(buff);
  277. return ret;
  278. }
  279. /* Callbacks for DVB USB */
  280. static int m920x_identify_state(struct usb_device *udev,
  281. struct dvb_usb_device_properties *props,
  282. struct dvb_usb_device_description **desc,
  283. int *cold)
  284. {
  285. struct usb_host_interface *alt;
  286. alt = usb_altnum_to_altsetting(usb_ifnum_to_if(udev, 0), 1);
  287. *cold = (alt == NULL) ? 1 : 0;
  288. return 0;
  289. }
  290. static int megasky_mt352_demod_init(struct dvb_frontend *fe)
  291. {
  292. u8 config[] = { CONFIG, 0x3d };
  293. u8 clock[] = { CLOCK_CTL, 0x30 };
  294. u8 reset[] = { RESET, 0x80 };
  295. u8 adc_ctl[] = { ADC_CTL_1, 0x40 };
  296. u8 agc[] = { AGC_TARGET, 0x1c, 0x20 };
  297. u8 sec_agc[] = { 0x69, 0x00, 0xff, 0xff, 0x40, 0xff, 0x00, 0x40, 0x40 };
  298. u8 unk1[] = { 0x93, 0x1a };
  299. u8 unk2[] = { 0xb5, 0x7a };
  300. mt352_write(fe, config, ARRAY_SIZE(config));
  301. mt352_write(fe, clock, ARRAY_SIZE(clock));
  302. mt352_write(fe, reset, ARRAY_SIZE(reset));
  303. mt352_write(fe, adc_ctl, ARRAY_SIZE(adc_ctl));
  304. mt352_write(fe, agc, ARRAY_SIZE(agc));
  305. mt352_write(fe, sec_agc, ARRAY_SIZE(sec_agc));
  306. mt352_write(fe, unk1, ARRAY_SIZE(unk1));
  307. mt352_write(fe, unk2, ARRAY_SIZE(unk2));
  308. deb_rc("Demod init!\n");
  309. return 0;
  310. }
  311. static struct mt352_config megasky_mt352_config = {
  312. .demod_address = 0x0f,
  313. .no_tuner = 1,
  314. .demod_init = megasky_mt352_demod_init,
  315. };
  316. static int megasky_mt352_frontend_attach(struct dvb_usb_adapter *adap)
  317. {
  318. deb_rc("megasky_frontend_attach!\n");
  319. if ((adap->fe = dvb_attach(mt352_attach, &megasky_mt352_config, &adap->dev->i2c_adap)) == NULL)
  320. return -EIO;
  321. return 0;
  322. }
  323. static struct qt1010_config megasky_qt1010_config = {
  324. .i2c_address = 0x62
  325. };
  326. static int megasky_qt1010_tuner_attach(struct dvb_usb_adapter *adap)
  327. {
  328. if (dvb_attach(qt1010_attach, adap->fe, &adap->dev->i2c_adap,
  329. &megasky_qt1010_config) == NULL)
  330. return -ENODEV;
  331. return 0;
  332. }
  333. static struct tda1004x_config digivox_tda10046_config = {
  334. .demod_address = 0x08,
  335. .invert = 0,
  336. .invert_oclk = 0,
  337. .ts_mode = TDA10046_TS_SERIAL,
  338. .xtal_freq = TDA10046_XTAL_16M,
  339. .if_freq = TDA10046_FREQ_045,
  340. .agc_config = TDA10046_AGC_TDA827X,
  341. .gpio_config = TDA10046_GPTRI,
  342. .request_firmware = NULL,
  343. };
  344. static int digivox_tda10046_frontend_attach(struct dvb_usb_adapter *adap)
  345. {
  346. deb_rc("digivox_tda10046_frontend_attach!\n");
  347. if ((adap->fe = dvb_attach(tda10046_attach, &digivox_tda10046_config,
  348. &adap->dev->i2c_adap)) == NULL)
  349. return -EIO;
  350. return 0;
  351. }
  352. static int digivox_tda8275_tuner_attach(struct dvb_usb_adapter *adap)
  353. {
  354. if (dvb_attach(tda827x_attach, adap->fe, 0x60, &adap->dev->i2c_adap,
  355. NULL) == NULL)
  356. return -ENODEV;
  357. return 0;
  358. }
  359. /* DVB USB Driver stuff */
  360. static struct dvb_usb_device_properties megasky_properties;
  361. static struct dvb_usb_device_properties digivox_mini_ii_properties;
  362. static int m920x_probe(struct usb_interface *intf,
  363. const struct usb_device_id *id)
  364. {
  365. struct dvb_usb_device *d;
  366. struct usb_host_interface *alt;
  367. int ret;
  368. deb_rc("Probed!\n");
  369. if (((ret = dvb_usb_device_init(intf, &megasky_properties, THIS_MODULE, &d)) == 0) ||
  370. ((ret = dvb_usb_device_init(intf, &digivox_mini_ii_properties, THIS_MODULE, &d)) == 0))
  371. goto found;
  372. return ret;
  373. found:
  374. alt = usb_altnum_to_altsetting(intf, 1);
  375. if (alt == NULL) {
  376. deb_rc("No alt found!\n");
  377. return -ENODEV;
  378. }
  379. ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber,
  380. alt->desc.bAlternateSetting);
  381. if (ret < 0)
  382. return ret;
  383. if ((ret = m9206_init(d)) != 0)
  384. return ret;
  385. return ret;
  386. }
  387. static struct usb_device_id m920x_table [] = {
  388. { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580) },
  389. { USB_DEVICE(USB_VID_ANUBIS_ELECTRONIC,
  390. USB_PID_MSI_DIGI_VOX_MINI_II) },
  391. { } /* Terminating entry */
  392. };
  393. MODULE_DEVICE_TABLE (usb, m920x_table);
  394. static struct dvb_usb_device_properties megasky_properties = {
  395. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  396. .usb_ctrl = DEVICE_SPECIFIC,
  397. .firmware = "dvb-usb-megasky-02.fw",
  398. .download_firmware = m9206_firmware_download,
  399. .rc_interval = 100,
  400. .rc_key_map = megasky_rc_keys,
  401. .rc_key_map_size = ARRAY_SIZE(megasky_rc_keys),
  402. .rc_query = m9206_rc_query,
  403. .size_of_priv = sizeof(struct m9206_state),
  404. .identify_state = m920x_identify_state,
  405. .num_adapters = 1,
  406. .adapter = {{
  407. .caps = DVB_USB_ADAP_HAS_PID_FILTER |
  408. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
  409. .pid_filter_count = 8,
  410. .pid_filter = m9206_pid_filter,
  411. .pid_filter_ctrl = m9206_pid_filter_ctrl,
  412. .frontend_attach = megasky_mt352_frontend_attach,
  413. .tuner_attach = megasky_qt1010_tuner_attach,
  414. .stream = {
  415. .type = USB_BULK,
  416. .count = 8,
  417. .endpoint = 0x81,
  418. .u = {
  419. .bulk = {
  420. .buffersize = 512,
  421. }
  422. }
  423. },
  424. }},
  425. .i2c_algo = &m9206_i2c_algo,
  426. .num_device_descs = 1,
  427. .devices = {
  428. { "MSI Mega Sky 580 DVB-T USB2.0",
  429. { &m920x_table[0], NULL },
  430. { NULL },
  431. }
  432. }
  433. };
  434. static struct dvb_usb_device_properties digivox_mini_ii_properties = {
  435. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  436. .usb_ctrl = DEVICE_SPECIFIC,
  437. .firmware = "dvb-usb-digivox-02.fw",
  438. .download_firmware = m9206_firmware_download,
  439. .size_of_priv = sizeof(struct m9206_state),
  440. .identify_state = m920x_identify_state,
  441. .num_adapters = 1,
  442. .adapter = {{
  443. .caps = DVB_USB_ADAP_HAS_PID_FILTER |
  444. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
  445. .pid_filter_count = 8,
  446. .pid_filter = m9206_pid_filter,
  447. .pid_filter_ctrl = m9206_pid_filter_ctrl,
  448. .frontend_attach = digivox_tda10046_frontend_attach,
  449. .tuner_attach = digivox_tda8275_tuner_attach,
  450. .stream = {
  451. .type = USB_BULK,
  452. .count = 8,
  453. .endpoint = 0x81,
  454. .u = {
  455. .bulk = {
  456. .buffersize = 0x4000,
  457. }
  458. }
  459. },
  460. }},
  461. .i2c_algo = &m9206_i2c_algo,
  462. .num_device_descs = 1,
  463. .devices = {
  464. { "MSI DIGI VOX mini II DVB-T USB2.0",
  465. { &m920x_table[1], NULL },
  466. { NULL },
  467. },
  468. }
  469. };
  470. static struct usb_driver m920x_driver = {
  471. .name = "dvb_usb_m920x",
  472. .probe = m920x_probe,
  473. .disconnect = dvb_usb_device_exit,
  474. .id_table = m920x_table,
  475. };
  476. /* module stuff */
  477. static int __init m920x_module_init(void)
  478. {
  479. int ret;
  480. if ((ret = usb_register(&m920x_driver))) {
  481. err("usb_register failed. Error number %d", ret);
  482. return ret;
  483. }
  484. return 0;
  485. }
  486. static void __exit m920x_module_exit(void)
  487. {
  488. /* deregister this driver from the USB subsystem */
  489. usb_deregister(&m920x_driver);
  490. }
  491. module_init (m920x_module_init);
  492. module_exit (m920x_module_exit);
  493. MODULE_AUTHOR("Aapo Tahkola <aet@rasterburn.org>");
  494. MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / Uli m920x");
  495. MODULE_VERSION("0.1");
  496. MODULE_LICENSE("GPL");