m920x.c 12 KB

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