m920x.c 12 KB

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