m920x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  112. return -EAGAIN;
  113. if (num > 2)
  114. return -EINVAL;
  115. for (i = 0; i < num; i++) {
  116. u8 w_len;
  117. if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].addr, 0x80)) != 0)
  118. goto unlock;
  119. if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].buf[0], 0x0)) != 0)
  120. goto unlock;
  121. if (i + 1 < num && msg[i + 1].flags & I2C_M_RD) {
  122. /* Possibly device dependant */
  123. if (msg[i].addr == d->adapter[0].pll_addr)
  124. w_len = 0xc5;
  125. else
  126. w_len = 0x1f;
  127. if ((ret = m9206_write(d->udev, M9206_I2C, w_len, 0x80)) != 0)
  128. goto unlock;
  129. if ((ret = m9206_read(d->udev, M9206_I2C, 0x0, 0x60, msg[i + 1].buf, msg[i + 1].len)) != 0)
  130. goto unlock;
  131. i++;
  132. } else {
  133. if (msg[i].len != 2)
  134. return -EINVAL;
  135. if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].buf[1], 0x40)) != 0)
  136. goto unlock;
  137. }
  138. }
  139. ret = i;
  140. unlock:
  141. mutex_unlock(&d->i2c_mutex);
  142. return ret;
  143. }
  144. static u32 m9206_i2c_func(struct i2c_adapter *adapter)
  145. {
  146. return I2C_FUNC_I2C;
  147. }
  148. static struct i2c_algorithm m9206_i2c_algo = {
  149. .master_xfer = m9206_i2c_xfer,
  150. .functionality = m9206_i2c_func,
  151. };
  152. /* Callbacks for DVB USB */
  153. static int megasky_identify_state(struct usb_device *udev,
  154. struct dvb_usb_device_properties *props,
  155. struct dvb_usb_device_description **desc,
  156. int *cold)
  157. {
  158. struct usb_host_interface *alt;
  159. alt = usb_altnum_to_altsetting(usb_ifnum_to_if(udev, 0), 1);
  160. *cold = (alt == NULL) ? 1 : 0;
  161. return 0;
  162. }
  163. static int megasky_mt352_demod_init(struct dvb_frontend *fe)
  164. {
  165. u8 config[] = { CONFIG, 0x3d };
  166. u8 clock[] = { CLOCK_CTL, 0x30 };
  167. u8 reset[] = { RESET, 0x80 };
  168. u8 adc_ctl[] = { ADC_CTL_1, 0x40 };
  169. u8 agc[] = { AGC_TARGET, 0x1c, 0x20 };
  170. u8 sec_agc[] = { 0x69, 0x00, 0xff, 0xff, 0x40, 0xff, 0x00, 0x40, 0x40 };
  171. u8 unk1[] = { 0x93, 0x1a };
  172. u8 unk2[] = { 0xb5, 0x7a };
  173. mt352_write(fe, config, ARRAY_SIZE(config));
  174. mt352_write(fe, clock, ARRAY_SIZE(clock));
  175. mt352_write(fe, reset, ARRAY_SIZE(reset));
  176. mt352_write(fe, adc_ctl, ARRAY_SIZE(adc_ctl));
  177. mt352_write(fe, agc, ARRAY_SIZE(agc));
  178. mt352_write(fe, sec_agc, ARRAY_SIZE(sec_agc));
  179. mt352_write(fe, unk1, ARRAY_SIZE(unk1));
  180. mt352_write(fe, unk2, ARRAY_SIZE(unk2));
  181. deb_rc("Demod init!\n");
  182. return 0;
  183. }
  184. static struct mt352_config megasky_mt352_config = {
  185. .demod_address = 0x1e,
  186. .no_tuner = 1,
  187. .demod_init = megasky_mt352_demod_init,
  188. };
  189. static int megasky_frontend_attach(struct dvb_usb_adapter *adap)
  190. {
  191. deb_rc("megasky_frontend_attach!\n");
  192. if ((adap->fe = dvb_attach(mt352_attach, &megasky_mt352_config, &adap->dev->i2c_adap)) != NULL)
  193. return 0;
  194. return -EIO;
  195. }
  196. static int m9206_set_filter(struct dvb_usb_adapter *adap, int type, int idx, int pid)
  197. {
  198. int ret = 0;
  199. if (pid >= 0x8000)
  200. return -EINVAL;
  201. pid |= 0x8000;
  202. if ((ret = m9206_write(adap->dev->udev, M9206_FILTER, pid, (type << 8) | (idx * 4) )) != 0)
  203. return ret;
  204. if ((ret = m9206_write(adap->dev->udev, M9206_FILTER, 0, (type << 8) | (idx * 4) )) != 0)
  205. return ret;
  206. return ret;
  207. }
  208. static int m9206_update_filters(struct dvb_usb_adapter *adap)
  209. {
  210. struct m9206_state *m = adap->dev->priv;
  211. int enabled = m->filtering_enabled;
  212. int i, ret = 0, filter = 0;
  213. for (i = 0; i < M9206_MAX_FILTERS; i++)
  214. if (m->filters[i] == 8192)
  215. enabled = 0;
  216. /* Disable all filters */
  217. if ((ret = m9206_set_filter(adap, 0x81, 1, enabled)) != 0)
  218. return ret;
  219. for (i = 0; i < M9206_MAX_FILTERS; i++)
  220. if ((ret = m9206_set_filter(adap, 0x81, i + 2, 0)) != 0)
  221. return ret;
  222. if ((ret = m9206_set_filter(adap, 0x82, 0, 0x0)) != 0)
  223. return ret;
  224. /* Set */
  225. if (enabled) {
  226. for (i = 0; i < M9206_MAX_FILTERS; i++) {
  227. if (m->filters[i] == 0)
  228. continue;
  229. if ((ret = m9206_set_filter(adap, 0x81, filter + 2, m->filters[i])) != 0)
  230. return ret;
  231. filter++;
  232. }
  233. }
  234. if ((ret = m9206_set_filter(adap, 0x82, 0, 0x02f5)) != 0)
  235. return ret;
  236. return ret;
  237. }
  238. static int m9206_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
  239. {
  240. struct m9206_state *m = adap->dev->priv;
  241. m->filtering_enabled = onoff ? 1 : 0;
  242. return m9206_update_filters(adap);
  243. }
  244. static int m9206_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
  245. {
  246. struct m9206_state *m = adap->dev->priv;
  247. m->filters[index] = onoff ? pid : 0;
  248. return m9206_update_filters(adap);
  249. }
  250. static int m9206_firmware_download(struct usb_device *udev, const struct firmware *fw)
  251. {
  252. u16 value, index, size;
  253. u8 read[4], *buff;
  254. int i, pass, ret = 0;
  255. buff = kmalloc(65536, GFP_KERNEL);
  256. if ((ret = m9206_read(udev, M9206_FILTER, 0x0, 0x8000, read, 4)) != 0)
  257. goto done;
  258. deb_rc("%x %x %x %x\n", read[0], read[1], read[2], read[3]);
  259. if ((ret = m9206_read(udev, M9206_FW, 0x0, 0x0, read, 1)) != 0)
  260. goto done;
  261. deb_rc("%x\n", read[0]);
  262. for (pass = 0; pass < 2; pass++) {
  263. for (i = 0; i + (sizeof(u16) * 3) < fw->size;) {
  264. value = le16_to_cpu(*(u16 *)(fw->data + i));
  265. i += sizeof(u16);
  266. index = le16_to_cpu(*(u16 *)(fw->data + i));
  267. i += sizeof(u16);
  268. size = le16_to_cpu(*(u16 *)(fw->data + i));
  269. i += sizeof(u16);
  270. if (pass == 1) {
  271. /* Will stall if using fw->data ... */
  272. memcpy(buff, fw->data + i, size);
  273. ret = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
  274. M9206_FW,
  275. USB_TYPE_VENDOR | USB_DIR_OUT,
  276. value, index, buff, size, 20);
  277. if (ret != size) {
  278. deb_rc("error while uploading fw!\n");
  279. ret = -EIO;
  280. goto done;
  281. }
  282. msleep(3);
  283. }
  284. i += size;
  285. }
  286. if (i != fw->size) {
  287. ret = -EINVAL;
  288. goto done;
  289. }
  290. }
  291. msleep(36);
  292. /* m9206 will disconnect itself from the bus after this. */
  293. (void) m9206_write(udev, M9206_CORE, 0x01, M9206_FW_GO);
  294. deb_rc("firmware uploaded!\n");
  295. done:
  296. kfree(buff);
  297. return ret;
  298. }
  299. /* DVB USB Driver stuff */
  300. static struct dvb_usb_device_properties megasky_properties;
  301. static int m920x_probe(struct usb_interface *intf, const struct usb_device_id *id)
  302. {
  303. struct dvb_usb_device *d;
  304. struct usb_host_interface *alt;
  305. int ret;
  306. if ((ret = dvb_usb_device_init(intf, &megasky_properties, THIS_MODULE, &d)) == 0) {
  307. deb_rc("probed!\n");
  308. alt = usb_altnum_to_altsetting(intf, 1);
  309. if (alt == NULL) {
  310. deb_rc("not alt found!\n");
  311. return -ENODEV;
  312. }
  313. ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber, alt->desc.bAlternateSetting);
  314. if (ret < 0)
  315. return ret;
  316. deb_rc("Changed to alternate setting!\n");
  317. if ((ret = m9206_rc_init(d->udev)) != 0)
  318. return ret;
  319. }
  320. return ret;
  321. }
  322. static struct usb_device_id m920x_table [] = {
  323. { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580) },
  324. { } /* Terminating entry */
  325. };
  326. MODULE_DEVICE_TABLE (usb, m920x_table);
  327. static struct dvb_usb_device_properties megasky_properties = {
  328. .usb_ctrl = DEVICE_SPECIFIC,
  329. .firmware = "dvb-usb-megasky-02.fw",
  330. .download_firmware = m9206_firmware_download,
  331. .rc_interval = 100,
  332. .rc_key_map = megasky_rc_keys,
  333. .rc_key_map_size = ARRAY_SIZE(megasky_rc_keys),
  334. .rc_query = m9206_rc_query,
  335. .size_of_priv = sizeof(struct m9206_state),
  336. .identify_state = megasky_identify_state,
  337. .num_adapters = 1,
  338. .adapter = {{
  339. .caps = DVB_USB_IS_AN_I2C_ADAPTER | DVB_USB_ADAP_HAS_PID_FILTER |
  340. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
  341. .pid_filter_count = 8,
  342. .pid_filter = m9206_pid_filter,
  343. .pid_filter_ctrl = m9206_pid_filter_ctrl,
  344. .frontend_attach = megasky_frontend_attach,
  345. .tuner_attach = qt1010_tuner_attach,
  346. .stream = {
  347. .type = USB_BULK,
  348. .count = 8,
  349. .endpoint = 0x81,
  350. .u = {
  351. .bulk = {
  352. .buffersize = 512,
  353. }
  354. }
  355. },
  356. }},
  357. .i2c_algo = &m9206_i2c_algo,
  358. .num_device_descs = 1,
  359. .devices = {
  360. { "MSI Mega Sky 580 DVB-T USB2.0",
  361. { &m920x_table[0], NULL },
  362. { NULL },
  363. },
  364. { NULL },
  365. }
  366. };
  367. static struct usb_driver m920x_driver = {
  368. .name = "dvb_usb_m920x",
  369. .probe = m920x_probe,
  370. .disconnect = dvb_usb_device_exit,
  371. .id_table = m920x_table,
  372. };
  373. /* module stuff */
  374. static int __init m920x_module_init(void)
  375. {
  376. int ret;
  377. if ((ret = usb_register(&m920x_driver))) {
  378. err("usb_register failed. Error number %d", ret);
  379. return ret;
  380. }
  381. return 0;
  382. }
  383. static void __exit m920x_module_exit(void)
  384. {
  385. /* deregister this driver from the USB subsystem */
  386. usb_deregister(&m920x_driver);
  387. }
  388. module_init (m920x_module_init);
  389. module_exit (m920x_module_exit);
  390. MODULE_AUTHOR("Aapo Tahkola <aet@rasterburn.org>");
  391. MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / Uli m920x");
  392. MODULE_VERSION("0.1");
  393. MODULE_LICENSE("GPL");