digitv.c 8.8 KB

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