digitv.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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, struct dvb_frontend_parameters *fep)
  100. {
  101. struct dvb_usb_adapter *adap = fe->dvb->priv;
  102. u8 b[5];
  103. fe->ops.tuner_ops.calc_regs(fe, fep, 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. if ((adap->fe = dvb_attach(mt352_attach, &digitv_mt352_config, &adap->dev->i2c_adap)) != NULL) {
  115. st->is_nxt6000 = 0;
  116. return 0;
  117. }
  118. if ((adap->fe = dvb_attach(nxt6000_attach, &digitv_nxt6000_config, &adap->dev->i2c_adap)) != NULL) {
  119. st->is_nxt6000 = 1;
  120. return 0;
  121. }
  122. return -EIO;
  123. }
  124. static int digitv_tuner_attach(struct dvb_usb_adapter *adap)
  125. {
  126. struct digitv_state *st = adap->dev->priv;
  127. if (!dvb_attach(dvb_pll_attach, adap->fe, 0x60, NULL, DVB_PLL_TDED4))
  128. return -ENODEV;
  129. if (st->is_nxt6000)
  130. adap->fe->ops.tuner_ops.set_params = digitv_nxt6000_tuner_set_params;
  131. return 0;
  132. }
  133. static struct rc_map_table rc_map_digitv_table[] = {
  134. { 0x5f55, KEY_0 },
  135. { 0x6f55, KEY_1 },
  136. { 0x9f55, KEY_2 },
  137. { 0xaf55, KEY_3 },
  138. { 0x5f56, KEY_4 },
  139. { 0x6f56, KEY_5 },
  140. { 0x9f56, KEY_6 },
  141. { 0xaf56, KEY_7 },
  142. { 0x5f59, KEY_8 },
  143. { 0x6f59, KEY_9 },
  144. { 0x9f59, KEY_TV },
  145. { 0xaf59, KEY_AUX },
  146. { 0x5f5a, KEY_DVD },
  147. { 0x6f5a, KEY_POWER },
  148. { 0x9f5a, KEY_CAMERA }, /* labelled 'Picture' */
  149. { 0xaf5a, KEY_AUDIO },
  150. { 0x5f65, KEY_INFO },
  151. { 0x6f65, KEY_F13 }, /* 16:9 */
  152. { 0x9f65, KEY_F14 }, /* 14:9 */
  153. { 0xaf65, KEY_EPG },
  154. { 0x5f66, KEY_EXIT },
  155. { 0x6f66, KEY_MENU },
  156. { 0x9f66, KEY_UP },
  157. { 0xaf66, KEY_DOWN },
  158. { 0x5f69, KEY_LEFT },
  159. { 0x6f69, KEY_RIGHT },
  160. { 0x9f69, KEY_ENTER },
  161. { 0xaf69, KEY_CHANNELUP },
  162. { 0x5f6a, KEY_CHANNELDOWN },
  163. { 0x6f6a, KEY_VOLUMEUP },
  164. { 0x9f6a, KEY_VOLUMEDOWN },
  165. { 0xaf6a, KEY_RED },
  166. { 0x5f95, KEY_GREEN },
  167. { 0x6f95, KEY_YELLOW },
  168. { 0x9f95, KEY_BLUE },
  169. { 0xaf95, KEY_SUBTITLE },
  170. { 0x5f96, KEY_F15 }, /* AD */
  171. { 0x6f96, KEY_TEXT },
  172. { 0x9f96, KEY_MUTE },
  173. { 0xaf96, KEY_REWIND },
  174. { 0x5f99, KEY_STOP },
  175. { 0x6f99, KEY_PLAY },
  176. { 0x9f99, KEY_FASTFORWARD },
  177. { 0xaf99, KEY_F16 }, /* chapter */
  178. { 0x5f9a, KEY_PAUSE },
  179. { 0x6f9a, KEY_PLAY },
  180. { 0x9f9a, KEY_RECORD },
  181. { 0xaf9a, KEY_F17 }, /* picture in picture */
  182. { 0x5fa5, KEY_KPPLUS }, /* zoom in */
  183. { 0x6fa5, KEY_KPMINUS }, /* zoom out */
  184. { 0x9fa5, KEY_F18 }, /* capture */
  185. { 0xafa5, KEY_F19 }, /* web */
  186. { 0x5fa6, KEY_EMAIL },
  187. { 0x6fa6, KEY_PHONE },
  188. { 0x9fa6, KEY_PC },
  189. };
  190. static int digitv_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  191. {
  192. int i;
  193. u8 key[5];
  194. u8 b[4] = { 0 };
  195. *event = 0;
  196. *state = REMOTE_NO_KEY_PRESSED;
  197. digitv_ctrl_msg(d,USB_READ_REMOTE,0,NULL,0,&key[1],4);
  198. /* Tell the device we've read the remote. Not sure how necessary
  199. this is, but the Nebula SDK does it. */
  200. digitv_ctrl_msg(d,USB_WRITE_REMOTE,0,b,4,NULL,0);
  201. /* if something is inside the buffer, simulate key press */
  202. if (key[1] != 0)
  203. {
  204. for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
  205. if (rc5_custom(&d->props.rc.legacy.rc_map_table[i]) == key[1] &&
  206. rc5_data(&d->props.rc.legacy.rc_map_table[i]) == key[2]) {
  207. *event = d->props.rc.legacy.rc_map_table[i].keycode;
  208. *state = REMOTE_KEY_PRESSED;
  209. return 0;
  210. }
  211. }
  212. }
  213. if (key[0] != 0)
  214. deb_rc("key: %x %x %x %x %x\n",key[0],key[1],key[2],key[3],key[4]);
  215. return 0;
  216. }
  217. /* DVB USB Driver stuff */
  218. static struct dvb_usb_device_properties digitv_properties;
  219. static int digitv_probe(struct usb_interface *intf,
  220. const struct usb_device_id *id)
  221. {
  222. struct dvb_usb_device *d;
  223. int ret = dvb_usb_device_init(intf, &digitv_properties, THIS_MODULE, &d,
  224. adapter_nr);
  225. if (ret == 0) {
  226. u8 b[4] = { 0 };
  227. if (d != NULL) { /* do that only when the firmware is loaded */
  228. b[0] = 1;
  229. digitv_ctrl_msg(d,USB_WRITE_REMOTE_TYPE,0,b,4,NULL,0);
  230. b[0] = 0;
  231. digitv_ctrl_msg(d,USB_WRITE_REMOTE,0,b,4,NULL,0);
  232. }
  233. }
  234. return ret;
  235. }
  236. static struct usb_device_id digitv_table [] = {
  237. { USB_DEVICE(USB_VID_ANCHOR, USB_PID_NEBULA_DIGITV) },
  238. { } /* Terminating entry */
  239. };
  240. MODULE_DEVICE_TABLE (usb, digitv_table);
  241. static struct dvb_usb_device_properties digitv_properties = {
  242. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  243. .usb_ctrl = CYPRESS_FX2,
  244. .firmware = "dvb-usb-digitv-02.fw",
  245. .size_of_priv = sizeof(struct digitv_state),
  246. .num_adapters = 1,
  247. .adapter = {
  248. {
  249. .frontend_attach = digitv_frontend_attach,
  250. .tuner_attach = digitv_tuner_attach,
  251. /* parameter for the MPEG2-data transfer */
  252. .stream = {
  253. .type = USB_BULK,
  254. .count = 7,
  255. .endpoint = 0x02,
  256. .u = {
  257. .bulk = {
  258. .buffersize = 4096,
  259. }
  260. }
  261. },
  262. }
  263. },
  264. .identify_state = digitv_identify_state,
  265. .rc.legacy = {
  266. .rc_interval = 1000,
  267. .rc_map_table = rc_map_digitv_table,
  268. .rc_map_size = ARRAY_SIZE(rc_map_digitv_table),
  269. .rc_query = digitv_rc_query,
  270. },
  271. .i2c_algo = &digitv_i2c_algo,
  272. .generic_bulk_ctrl_endpoint = 0x01,
  273. .num_device_descs = 1,
  274. .devices = {
  275. { "Nebula Electronics uDigiTV DVB-T USB2.0)",
  276. { &digitv_table[0], NULL },
  277. { NULL },
  278. },
  279. { NULL },
  280. }
  281. };
  282. static struct usb_driver digitv_driver = {
  283. .name = "dvb_usb_digitv",
  284. .probe = digitv_probe,
  285. .disconnect = dvb_usb_device_exit,
  286. .id_table = digitv_table,
  287. };
  288. /* module stuff */
  289. static int __init digitv_module_init(void)
  290. {
  291. int result;
  292. if ((result = usb_register(&digitv_driver))) {
  293. err("usb_register failed. Error number %d",result);
  294. return result;
  295. }
  296. return 0;
  297. }
  298. static void __exit digitv_module_exit(void)
  299. {
  300. /* deregister this driver from the USB subsystem */
  301. usb_deregister(&digitv_driver);
  302. }
  303. module_init (digitv_module_init);
  304. module_exit (digitv_module_exit);
  305. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  306. MODULE_DESCRIPTION("Driver for Nebula Electronics uDigiTV DVB-T USB2.0");
  307. MODULE_VERSION("1.0-alpha");
  308. MODULE_LICENSE("GPL");