m920x.c 12 KB

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