m920x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. /* debug */
  16. static int dvb_usb_m920x_debug;
  17. module_param_named(debug,dvb_usb_m920x_debug, int, 0644);
  18. MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
  19. static struct dvb_usb_rc_key megasky_rc_keys [] = {
  20. { 0x0, 0x12, KEY_POWER },
  21. { 0x0, 0x1e, KEY_CYCLEWINDOWS }, /* min/max */
  22. { 0x0, 0x02, KEY_CHANNELUP },
  23. { 0x0, 0x05, KEY_CHANNELDOWN },
  24. { 0x0, 0x03, KEY_VOLUMEUP },
  25. { 0x0, 0x06, KEY_VOLUMEDOWN },
  26. { 0x0, 0x04, KEY_MUTE },
  27. { 0x0, 0x07, KEY_OK }, /* TS */
  28. { 0x0, 0x08, KEY_STOP },
  29. { 0x0, 0x09, KEY_MENU }, /* swap */
  30. { 0x0, 0x0a, KEY_REWIND },
  31. { 0x0, 0x1b, KEY_PAUSE },
  32. { 0x0, 0x1f, KEY_FASTFORWARD },
  33. { 0x0, 0x0c, KEY_RECORD },
  34. { 0x0, 0x0d, KEY_CAMERA }, /* screenshot */
  35. { 0x0, 0x0e, KEY_COFFEE }, /* "MTS" */
  36. };
  37. static inline int m9206_read(struct usb_device *udev, u8 request, u16 value, u16 index, void *data, int size)
  38. {
  39. int ret;
  40. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  41. request, USB_TYPE_VENDOR | USB_DIR_IN,
  42. value, index, data, size, 2000);
  43. if (ret < 0)
  44. return ret;
  45. if (ret != size)
  46. return -EIO;
  47. return 0;
  48. }
  49. static inline int m9206_write(struct usb_device *udev, u8 request, u16 value, u16 index)
  50. {
  51. int ret;
  52. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  53. request, USB_TYPE_VENDOR | USB_DIR_OUT,
  54. value, index, NULL, 0, 2000);
  55. return ret;
  56. }
  57. static int m9206_rc_init(struct usb_device *udev)
  58. {
  59. int ret = 0;
  60. /* Remote controller init. */
  61. if ((ret = m9206_write(udev, M9206_CORE, 0xa8, M9206_RC_INIT2)) != 0)
  62. return ret;
  63. if ((ret = m9206_write(udev, M9206_CORE, 0x51, M9206_RC_INIT1)) != 0)
  64. return ret;
  65. return ret;
  66. }
  67. static int m9206_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  68. {
  69. struct m9206_state *m = d->priv;
  70. int i, ret = 0;
  71. u8 rc_state[2];
  72. if ((ret = m9206_read(d->udev, M9206_CORE, 0x0, M9206_RC_STATE, rc_state, 1)) != 0)
  73. goto unlock;
  74. if ((ret = m9206_read(d->udev, M9206_CORE, 0x0, M9206_RC_KEY, rc_state + 1, 1)) != 0)
  75. goto unlock;
  76. for (i = 0; i < ARRAY_SIZE(megasky_rc_keys); i++)
  77. if (megasky_rc_keys[i].data == rc_state[1]) {
  78. *event = megasky_rc_keys[i].event;
  79. switch(rc_state[0]) {
  80. case 0x80:
  81. *state = REMOTE_NO_KEY_PRESSED;
  82. goto unlock;
  83. case 0x93:
  84. case 0x92:
  85. m->rep_count = 0;
  86. *state = REMOTE_KEY_PRESSED;
  87. goto unlock;
  88. case 0x91:
  89. /* For comfort. */
  90. if (++m->rep_count > 2)
  91. *state = REMOTE_KEY_REPEAT;
  92. goto unlock;
  93. default:
  94. deb_rc("Unexpected rc response %x\n", rc_state[0]);
  95. *state = REMOTE_NO_KEY_PRESSED;
  96. goto unlock;
  97. }
  98. }
  99. if (rc_state[1] != 0)
  100. deb_rc("Unknown rc key %x\n", rc_state[1]);
  101. *state = REMOTE_NO_KEY_PRESSED;
  102. unlock:
  103. return ret;
  104. }
  105. /* I2C */
  106. static int m9206_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
  107. {
  108. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  109. int i;
  110. int ret = 0;
  111. /* Need to access d->adapter[0] */
  112. if (d->num_adapters_initialized != 1) {
  113. deb_rc("Impossible happened!\n");
  114. return -EINVAL;
  115. }
  116. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  117. return -EAGAIN;
  118. if (num > 2)
  119. return -EINVAL;
  120. for (i = 0; i < num; i++) {
  121. u8 w_len;
  122. if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].addr, 0x80)) != 0)
  123. goto unlock;
  124. if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].buf[0], 0x0)) != 0)
  125. goto unlock;
  126. if (i + 1 < num && msg[i + 1].flags & I2C_M_RD) {
  127. /* Possibly device dependant */
  128. if (msg[i].addr == d->adapter[0].pll_addr)
  129. w_len = 0xc5;
  130. else
  131. w_len = 0x1f;
  132. if ((ret = m9206_write(d->udev, M9206_I2C, w_len, 0x80)) != 0)
  133. goto unlock;
  134. if ((ret = m9206_read(d->udev, M9206_I2C, 0x0, 0x60, msg[i + 1].buf, msg[i + 1].len)) != 0)
  135. goto unlock;
  136. i++;
  137. } else {
  138. if (msg[i].len != 2)
  139. return -EINVAL;
  140. if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].buf[1], 0x40)) != 0)
  141. goto unlock;
  142. }
  143. }
  144. ret = i;
  145. unlock:
  146. mutex_unlock(&d->i2c_mutex);
  147. return ret;
  148. }
  149. static u32 m9206_i2c_func(struct i2c_adapter *adapter)
  150. {
  151. return I2C_FUNC_I2C;
  152. }
  153. static struct i2c_algorithm m9206_i2c_algo = {
  154. .master_xfer = m9206_i2c_xfer,
  155. .functionality = m9206_i2c_func,
  156. };
  157. /* Callbacks for DVB USB */
  158. static int megasky_identify_state(struct usb_device *udev,
  159. struct dvb_usb_device_properties *props,
  160. struct dvb_usb_device_description **desc,
  161. int *cold)
  162. {
  163. struct usb_host_interface *alt;
  164. alt = usb_altnum_to_altsetting(usb_ifnum_to_if(udev, 0), 1);
  165. *cold = (alt == NULL) ? 1 : 0;
  166. return 0;
  167. }
  168. static int megasky_mt352_demod_init(struct dvb_frontend *fe)
  169. {
  170. u8 config[] = { CONFIG, 0x3d };
  171. u8 clock[] = { CLOCK_CTL, 0x30 };
  172. u8 reset[] = { RESET, 0x80 };
  173. u8 adc_ctl[] = { ADC_CTL_1, 0x40 };
  174. u8 agc[] = { AGC_TARGET, 0x1c, 0x20 };
  175. u8 sec_agc[] = { 0x69, 0x00, 0xff, 0xff, 0x40, 0xff, 0x00, 0x40, 0x40 };
  176. u8 unk1[] = { 0x93, 0x1a };
  177. u8 unk2[] = { 0xb5, 0x7a };
  178. mt352_write(fe, config, ARRAY_SIZE(config));
  179. mt352_write(fe, clock, ARRAY_SIZE(clock));
  180. mt352_write(fe, reset, ARRAY_SIZE(reset));
  181. mt352_write(fe, adc_ctl, ARRAY_SIZE(adc_ctl));
  182. mt352_write(fe, agc, ARRAY_SIZE(agc));
  183. mt352_write(fe, sec_agc, ARRAY_SIZE(sec_agc));
  184. mt352_write(fe, unk1, ARRAY_SIZE(unk1));
  185. mt352_write(fe, unk2, ARRAY_SIZE(unk2));
  186. deb_rc("Demod init!\n");
  187. return 0;
  188. }
  189. static struct mt352_config megasky_mt352_config = {
  190. .demod_address = 0x1e,
  191. .no_tuner = 1,
  192. .demod_init = megasky_mt352_demod_init,
  193. };
  194. static int megasky_frontend_attach(struct dvb_usb_adapter *adap)
  195. {
  196. deb_rc("megasky_frontend_attach!\n");
  197. if ((adap->fe = dvb_attach(mt352_attach, &megasky_mt352_config, &adap->dev->i2c_adap)) != NULL) {
  198. return 0;
  199. }
  200. return -EIO;
  201. }
  202. static int megasky_tuner_attach(struct dvb_usb_adapter *adap)
  203. {
  204. adap->pll_addr = 0xc4;
  205. adap->pll_desc = NULL;
  206. adap->fe->ops.tuner_ops.set_params = qt1010_set_params;
  207. return 0;
  208. }
  209. static int m9206_set_filter(struct dvb_usb_adapter *adap, int type, int idx, int pid)
  210. {
  211. int ret = 0;
  212. if (pid >= 0x8000)
  213. return -EINVAL;
  214. pid |= 0x8000;
  215. if ((ret = m9206_write(adap->dev->udev, M9206_FILTER, pid, (type << 8) | (idx * 4) )) != 0)
  216. return ret;
  217. if ((ret = m9206_write(adap->dev->udev, M9206_FILTER, 0, (type << 8) | (idx * 4) )) != 0)
  218. return ret;
  219. return ret;
  220. }
  221. static int m9206_update_filters(struct dvb_usb_adapter *adap)
  222. {
  223. struct m9206_state *m = adap->dev->priv;
  224. int enabled = m->filtering_enabled;
  225. int i, ret = 0, filter = 0;
  226. for (i = 0; i < M9206_MAX_FILTERS; i++)
  227. if (m->filters[i] == 8192)
  228. enabled = 0;
  229. /* Disable all filters */
  230. if ((ret = m9206_set_filter(adap, 0x81, 1, enabled)) != 0)
  231. return ret;
  232. for (i = 0; i < M9206_MAX_FILTERS; i++)
  233. if ((ret = m9206_set_filter(adap, 0x81, i + 2, 0)) != 0)
  234. return ret;
  235. if ((ret = m9206_set_filter(adap, 0x82, 0, 0x0)) != 0)
  236. return ret;
  237. /* Set */
  238. if (enabled) {
  239. for (i = 0; i < M9206_MAX_FILTERS; i++) {
  240. if (m->filters[i] == 0)
  241. continue;
  242. if ((ret = m9206_set_filter(adap, 0x81, filter + 2, m->filters[i])) != 0)
  243. return ret;
  244. filter++;
  245. }
  246. }
  247. if ((ret = m9206_set_filter(adap, 0x82, 0, 0x02f5)) != 0)
  248. return ret;
  249. return ret;
  250. }
  251. static int m9206_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
  252. {
  253. struct m9206_state *m = adap->dev->priv;
  254. m->filtering_enabled = onoff ? 1 : 0;
  255. return m9206_update_filters(adap);
  256. }
  257. static int m9206_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
  258. {
  259. struct m9206_state *m = adap->dev->priv;
  260. m->filters[index] = onoff ? pid : 0;
  261. return m9206_update_filters(adap);
  262. }
  263. static int m9206_firmware_download(struct usb_device *udev, const struct firmware *fw)
  264. {
  265. u16 value, index, size;
  266. u8 read[4], *buff;
  267. int i, pass, ret = 0;
  268. buff = kmalloc(65536, GFP_KERNEL);
  269. if ((ret = m9206_read(udev, M9206_FILTER, 0x0, 0x8000, read, 4)) != 0)
  270. goto done;
  271. deb_rc("%x %x %x %x\n", read[0], read[1], read[2], read[3]);
  272. if ((ret = m9206_read(udev, M9206_FW, 0x0, 0x0, read, 1)) != 0)
  273. goto done;
  274. deb_rc("%x\n", read[0]);
  275. for (pass = 0; pass < 2; pass++) {
  276. for (i = 0; i + (sizeof(u16) * 3) < fw->size;) {
  277. value = le16_to_cpu(*(u16 *)(fw->data + i));
  278. i += sizeof(u16);
  279. index = le16_to_cpu(*(u16 *)(fw->data + i));
  280. i += sizeof(u16);
  281. size = le16_to_cpu(*(u16 *)(fw->data + i));
  282. i += sizeof(u16);
  283. if (pass == 1) {
  284. /* Will stall if using fw->data ... */
  285. memcpy(buff, fw->data + i, size);
  286. ret = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
  287. M9206_FW,
  288. USB_TYPE_VENDOR | USB_DIR_OUT,
  289. value, index, buff, size, 20);
  290. if (ret != size) {
  291. deb_rc("error while uploading fw!\n");
  292. ret = -EIO;
  293. goto done;
  294. }
  295. msleep(3);
  296. }
  297. i += size;
  298. }
  299. if (i != fw->size) {
  300. ret = -EINVAL;
  301. goto done;
  302. }
  303. }
  304. msleep(36);
  305. /* m9206 will disconnect itself from the bus after this. */
  306. (void) m9206_write(udev, M9206_CORE, 0x01, M9206_FW_GO);
  307. deb_rc("firmware uploaded!\n");
  308. done:
  309. kfree(buff);
  310. return ret;
  311. }
  312. /* DVB USB Driver stuff */
  313. static struct dvb_usb_device_properties megasky_properties;
  314. static int m920x_probe(struct usb_interface *intf, const struct usb_device_id *id)
  315. {
  316. struct dvb_usb_device *d;
  317. struct usb_host_interface *alt;
  318. int ret;
  319. if ((ret = dvb_usb_device_init(intf, &megasky_properties, THIS_MODULE, &d)) == 0) {
  320. deb_rc("probed!\n");
  321. alt = usb_altnum_to_altsetting(intf, 1);
  322. if (alt == NULL) {
  323. deb_rc("not alt found!\n");
  324. return -ENODEV;
  325. }
  326. ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber, alt->desc.bAlternateSetting);
  327. if (ret < 0)
  328. return ret;
  329. deb_rc("Changed to alternate setting!\n");
  330. if ((ret = m9206_rc_init(d->udev)) != 0)
  331. return ret;
  332. }
  333. return ret;
  334. }
  335. static struct usb_device_id m920x_table [] = {
  336. { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580) },
  337. { } /* Terminating entry */
  338. };
  339. MODULE_DEVICE_TABLE (usb, m920x_table);
  340. static struct dvb_usb_device_properties megasky_properties = {
  341. .usb_ctrl = DEVICE_SPECIFIC,
  342. .firmware = "dvb-usb-megasky-02.fw",
  343. .download_firmware = m9206_firmware_download,
  344. .rc_interval = 100,
  345. .rc_key_map = megasky_rc_keys,
  346. .rc_key_map_size = ARRAY_SIZE(megasky_rc_keys),
  347. .rc_query = m9206_rc_query,
  348. .size_of_priv = sizeof(struct m9206_state),
  349. .identify_state = megasky_identify_state,
  350. .num_adapters = 1,
  351. .adapter = {{
  352. .caps = DVB_USB_IS_AN_I2C_ADAPTER | DVB_USB_ADAP_HAS_PID_FILTER |
  353. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
  354. .pid_filter_count = 8,
  355. .pid_filter = m9206_pid_filter,
  356. .pid_filter_ctrl = m9206_pid_filter_ctrl,
  357. .frontend_attach = megasky_frontend_attach,
  358. .tuner_attach = megasky_tuner_attach,
  359. .stream = {
  360. .type = USB_BULK,
  361. .count = 8,
  362. .endpoint = 0x81,
  363. .u = {
  364. .bulk = {
  365. .buffersize = 512,
  366. }
  367. }
  368. },
  369. }},
  370. .i2c_algo = &m9206_i2c_algo,
  371. .generic_bulk_ctrl_endpoint = 0x01,
  372. .num_device_descs = 1,
  373. .devices = {
  374. { "MSI Mega Sky 580 DVB-T USB2.0",
  375. { &m920x_table[0], NULL },
  376. { NULL },
  377. },
  378. { NULL },
  379. }
  380. };
  381. static struct usb_driver m920x_driver = {
  382. .name = "dvb_usb_m920x",
  383. .probe = m920x_probe,
  384. .disconnect = dvb_usb_device_exit,
  385. .id_table = m920x_table,
  386. };
  387. /* module stuff */
  388. static int __init m920x_module_init(void)
  389. {
  390. int ret;
  391. if ((ret = usb_register(&m920x_driver))) {
  392. err("usb_register failed. Error number %d", ret);
  393. return ret;
  394. }
  395. return 0;
  396. }
  397. static void __exit m920x_module_exit(void)
  398. {
  399. /* deregister this driver from the USB subsystem */
  400. usb_deregister(&m920x_driver);
  401. }
  402. module_init (m920x_module_init);
  403. module_exit (m920x_module_exit);
  404. MODULE_AUTHOR("Aapo Tahkola <aet@rasterburn.org>");
  405. MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / Uli m920x");
  406. MODULE_VERSION("0.1");
  407. MODULE_LICENSE("GPL");