digitv.c 8.8 KB

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