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