digitv.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* DVB USB compliant linux driver for Nebula Electronics uDigiTV DVB-T USB2.0
  2. * receiver
  3. *
  4. * Copyright (C) 2005 Patrick Boettcher (patrick.boettcher@desy.de)
  5. *
  6. * partly based on the SDK published by Nebula Electronics
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation, version 2.
  11. *
  12. * see Documentation/dvb/README.dvb-usb for more information
  13. */
  14. #include "digitv.h"
  15. #include "mt352.h"
  16. #include "nxt6000.h"
  17. /* debug */
  18. static int dvb_usb_digitv_debug;
  19. module_param_named(debug,dvb_usb_digitv_debug, int, 0644);
  20. MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
  21. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  22. #define deb_rc(args...) dprintk(dvb_usb_digitv_debug,0x01,args)
  23. static int digitv_ctrl_msg(struct dvb_usb_device *d,
  24. u8 cmd, u8 vv, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
  25. {
  26. int wo = (rbuf == NULL || rlen == 0); /* write-only */
  27. u8 sndbuf[7],rcvbuf[7];
  28. memset(sndbuf,0,7); memset(rcvbuf,0,7);
  29. sndbuf[0] = cmd;
  30. sndbuf[1] = vv;
  31. sndbuf[2] = wo ? wlen : rlen;
  32. if (wo) {
  33. memcpy(&sndbuf[3],wbuf,wlen);
  34. dvb_usb_generic_write(d,sndbuf,7);
  35. } else {
  36. dvb_usb_generic_rw(d,sndbuf,7,rcvbuf,7,10);
  37. memcpy(rbuf,&rcvbuf[3],rlen);
  38. }
  39. return 0;
  40. }
  41. /* I2C */
  42. static int digitv_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
  43. {
  44. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  45. int i;
  46. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  47. return -EAGAIN;
  48. if (num > 2)
  49. warn("more than 2 i2c messages at a time is not handled yet. TODO.");
  50. for (i = 0; i < num; i++) {
  51. /* write/read request */
  52. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  53. if (digitv_ctrl_msg(d, USB_READ_COFDM, msg[i].buf[0], NULL, 0,
  54. msg[i+1].buf,msg[i+1].len) < 0)
  55. break;
  56. i++;
  57. } else
  58. if (digitv_ctrl_msg(d,USB_WRITE_COFDM, msg[i].buf[0],
  59. &msg[i].buf[1],msg[i].len-1,NULL,0) < 0)
  60. break;
  61. }
  62. mutex_unlock(&d->i2c_mutex);
  63. return i;
  64. }
  65. static u32 digitv_i2c_func(struct i2c_adapter *adapter)
  66. {
  67. return I2C_FUNC_I2C;
  68. }
  69. static struct i2c_algorithm digitv_i2c_algo = {
  70. .master_xfer = digitv_i2c_xfer,
  71. .functionality = digitv_i2c_func,
  72. };
  73. /* Callbacks for DVB USB */
  74. static int digitv_identify_state (struct usb_device *udev, struct
  75. dvb_usb_device_properties *props, struct dvb_usb_device_description **desc,
  76. int *cold)
  77. {
  78. *cold = udev->descriptor.iManufacturer == 0 && udev->descriptor.iProduct == 0;
  79. return 0;
  80. }
  81. static int digitv_mt352_demod_init(struct dvb_frontend *fe)
  82. {
  83. static u8 reset_buf[] = { 0x89, 0x38, 0x8a, 0x2d, 0x50, 0x80 };
  84. static u8 init_buf[] = { 0x68, 0xa0, 0x8e, 0x40, 0x53, 0x50,
  85. 0x67, 0x20, 0x7d, 0x01, 0x7c, 0x00, 0x7a, 0x00,
  86. 0x79, 0x20, 0x57, 0x05, 0x56, 0x31, 0x88, 0x0f,
  87. 0x75, 0x32 };
  88. int i;
  89. for (i = 0; i < ARRAY_SIZE(reset_buf); i += 2)
  90. mt352_write(fe, &reset_buf[i], 2);
  91. msleep(1);
  92. for (i = 0; i < ARRAY_SIZE(init_buf); i += 2)
  93. mt352_write(fe, &init_buf[i], 2);
  94. return 0;
  95. }
  96. static struct mt352_config digitv_mt352_config = {
  97. .demod_init = digitv_mt352_demod_init,
  98. };
  99. static int digitv_nxt6000_tuner_set_params(struct dvb_frontend *fe)
  100. {
  101. struct dvb_usb_adapter *adap = fe->dvb->priv;
  102. u8 b[5];
  103. fe->ops.tuner_ops.calc_regs(fe, b, sizeof(b));
  104. if (fe->ops.i2c_gate_ctrl)
  105. fe->ops.i2c_gate_ctrl(fe, 1);
  106. return digitv_ctrl_msg(adap->dev, USB_WRITE_TUNER, 0, &b[1], 4, NULL, 0);
  107. }
  108. static struct nxt6000_config digitv_nxt6000_config = {
  109. .clock_inversion = 1,
  110. };
  111. static int digitv_frontend_attach(struct dvb_usb_adapter *adap)
  112. {
  113. struct digitv_state *st = adap->dev->priv;
  114. adap->fe_adap[0].fe = dvb_attach(mt352_attach, &digitv_mt352_config,
  115. &adap->dev->i2c_adap);
  116. if ((adap->fe_adap[0].fe) != NULL) {
  117. st->is_nxt6000 = 0;
  118. return 0;
  119. }
  120. adap->fe_adap[0].fe = dvb_attach(nxt6000_attach,
  121. &digitv_nxt6000_config,
  122. &adap->dev->i2c_adap);
  123. if ((adap->fe_adap[0].fe) != NULL) {
  124. st->is_nxt6000 = 1;
  125. return 0;
  126. }
  127. return -EIO;
  128. }
  129. static int digitv_tuner_attach(struct dvb_usb_adapter *adap)
  130. {
  131. struct digitv_state *st = adap->dev->priv;
  132. if (!dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60, NULL, DVB_PLL_TDED4))
  133. return -ENODEV;
  134. if (st->is_nxt6000)
  135. adap->fe_adap[0].fe->ops.tuner_ops.set_params = digitv_nxt6000_tuner_set_params;
  136. return 0;
  137. }
  138. static struct rc_map_table rc_map_digitv_table[] = {
  139. { 0x5f55, KEY_0 },
  140. { 0x6f55, KEY_1 },
  141. { 0x9f55, KEY_2 },
  142. { 0xaf55, KEY_3 },
  143. { 0x5f56, KEY_4 },
  144. { 0x6f56, KEY_5 },
  145. { 0x9f56, KEY_6 },
  146. { 0xaf56, KEY_7 },
  147. { 0x5f59, KEY_8 },
  148. { 0x6f59, KEY_9 },
  149. { 0x9f59, KEY_TV },
  150. { 0xaf59, KEY_AUX },
  151. { 0x5f5a, KEY_DVD },
  152. { 0x6f5a, KEY_POWER },
  153. { 0x9f5a, KEY_CAMERA }, /* labelled 'Picture' */
  154. { 0xaf5a, KEY_AUDIO },
  155. { 0x5f65, KEY_INFO },
  156. { 0x6f65, KEY_F13 }, /* 16:9 */
  157. { 0x9f65, KEY_F14 }, /* 14:9 */
  158. { 0xaf65, KEY_EPG },
  159. { 0x5f66, KEY_EXIT },
  160. { 0x6f66, KEY_MENU },
  161. { 0x9f66, KEY_UP },
  162. { 0xaf66, KEY_DOWN },
  163. { 0x5f69, KEY_LEFT },
  164. { 0x6f69, KEY_RIGHT },
  165. { 0x9f69, KEY_ENTER },
  166. { 0xaf69, KEY_CHANNELUP },
  167. { 0x5f6a, KEY_CHANNELDOWN },
  168. { 0x6f6a, KEY_VOLUMEUP },
  169. { 0x9f6a, KEY_VOLUMEDOWN },
  170. { 0xaf6a, KEY_RED },
  171. { 0x5f95, KEY_GREEN },
  172. { 0x6f95, KEY_YELLOW },
  173. { 0x9f95, KEY_BLUE },
  174. { 0xaf95, KEY_SUBTITLE },
  175. { 0x5f96, KEY_F15 }, /* AD */
  176. { 0x6f96, KEY_TEXT },
  177. { 0x9f96, KEY_MUTE },
  178. { 0xaf96, KEY_REWIND },
  179. { 0x5f99, KEY_STOP },
  180. { 0x6f99, KEY_PLAY },
  181. { 0x9f99, KEY_FASTFORWARD },
  182. { 0xaf99, KEY_F16 }, /* chapter */
  183. { 0x5f9a, KEY_PAUSE },
  184. { 0x6f9a, KEY_PLAY },
  185. { 0x9f9a, KEY_RECORD },
  186. { 0xaf9a, KEY_F17 }, /* picture in picture */
  187. { 0x5fa5, KEY_KPPLUS }, /* zoom in */
  188. { 0x6fa5, KEY_KPMINUS }, /* zoom out */
  189. { 0x9fa5, KEY_F18 }, /* capture */
  190. { 0xafa5, KEY_F19 }, /* web */
  191. { 0x5fa6, KEY_EMAIL },
  192. { 0x6fa6, KEY_PHONE },
  193. { 0x9fa6, KEY_PC },
  194. };
  195. static int digitv_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  196. {
  197. int i;
  198. u8 key[5];
  199. u8 b[4] = { 0 };
  200. *event = 0;
  201. *state = REMOTE_NO_KEY_PRESSED;
  202. digitv_ctrl_msg(d,USB_READ_REMOTE,0,NULL,0,&key[1],4);
  203. /* Tell the device we've read the remote. Not sure how necessary
  204. this is, but the Nebula SDK does it. */
  205. digitv_ctrl_msg(d,USB_WRITE_REMOTE,0,b,4,NULL,0);
  206. /* if something is inside the buffer, simulate key press */
  207. if (key[1] != 0)
  208. {
  209. for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
  210. if (rc5_custom(&d->props.rc.legacy.rc_map_table[i]) == key[1] &&
  211. rc5_data(&d->props.rc.legacy.rc_map_table[i]) == key[2]) {
  212. *event = d->props.rc.legacy.rc_map_table[i].keycode;
  213. *state = REMOTE_KEY_PRESSED;
  214. return 0;
  215. }
  216. }
  217. }
  218. if (key[0] != 0)
  219. deb_rc("key: %*ph\n", 5, key);
  220. return 0;
  221. }
  222. /* DVB USB Driver stuff */
  223. static struct dvb_usb_device_properties digitv_properties;
  224. static int digitv_probe(struct usb_interface *intf,
  225. const struct usb_device_id *id)
  226. {
  227. struct dvb_usb_device *d;
  228. int ret = dvb_usb_device_init(intf, &digitv_properties, THIS_MODULE, &d,
  229. adapter_nr);
  230. if (ret == 0) {
  231. u8 b[4] = { 0 };
  232. if (d != NULL) { /* do that only when the firmware is loaded */
  233. b[0] = 1;
  234. digitv_ctrl_msg(d,USB_WRITE_REMOTE_TYPE,0,b,4,NULL,0);
  235. b[0] = 0;
  236. digitv_ctrl_msg(d,USB_WRITE_REMOTE,0,b,4,NULL,0);
  237. }
  238. }
  239. return ret;
  240. }
  241. static struct usb_device_id digitv_table [] = {
  242. { USB_DEVICE(USB_VID_ANCHOR, USB_PID_NEBULA_DIGITV) },
  243. { } /* Terminating entry */
  244. };
  245. MODULE_DEVICE_TABLE (usb, digitv_table);
  246. static struct dvb_usb_device_properties digitv_properties = {
  247. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  248. .usb_ctrl = CYPRESS_FX2,
  249. .firmware = "dvb-usb-digitv-02.fw",
  250. .size_of_priv = sizeof(struct digitv_state),
  251. .num_adapters = 1,
  252. .adapter = {
  253. {
  254. .num_frontends = 1,
  255. .fe = {{
  256. .frontend_attach = digitv_frontend_attach,
  257. .tuner_attach = digitv_tuner_attach,
  258. /* parameter for the MPEG2-data transfer */
  259. .stream = {
  260. .type = USB_BULK,
  261. .count = 7,
  262. .endpoint = 0x02,
  263. .u = {
  264. .bulk = {
  265. .buffersize = 4096,
  266. }
  267. }
  268. },
  269. }},
  270. }
  271. },
  272. .identify_state = digitv_identify_state,
  273. .rc.legacy = {
  274. .rc_interval = 1000,
  275. .rc_map_table = rc_map_digitv_table,
  276. .rc_map_size = ARRAY_SIZE(rc_map_digitv_table),
  277. .rc_query = digitv_rc_query,
  278. },
  279. .i2c_algo = &digitv_i2c_algo,
  280. .generic_bulk_ctrl_endpoint = 0x01,
  281. .num_device_descs = 1,
  282. .devices = {
  283. { "Nebula Electronics uDigiTV DVB-T USB2.0)",
  284. { &digitv_table[0], NULL },
  285. { NULL },
  286. },
  287. { NULL },
  288. }
  289. };
  290. static struct usb_driver digitv_driver = {
  291. .name = "dvb_usb_digitv",
  292. .probe = digitv_probe,
  293. .disconnect = dvb_usb_device_exit,
  294. .id_table = digitv_table,
  295. };
  296. module_usb_driver(digitv_driver);
  297. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  298. MODULE_DESCRIPTION("Driver for Nebula Electronics uDigiTV DVB-T USB2.0");
  299. MODULE_VERSION("1.0-alpha");
  300. MODULE_LICENSE("GPL");