dib0700_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* Linux driver for devices based on the DiBcom DiB0700 USB bridge
  2. *
  3. * This program is free software; you can redistribute it and/or modify it
  4. * under the terms of the GNU General Public License as published by the Free
  5. * Software Foundation, version 2.
  6. *
  7. * Copyright (C) 2005-6 DiBcom, SA
  8. */
  9. #include "dib0700.h"
  10. /* debug */
  11. int dvb_usb_dib0700_debug;
  12. module_param_named(debug,dvb_usb_dib0700_debug, int, 0644);
  13. MODULE_PARM_DESC(debug, "set debugging level (1=info,2=fw,4=fwdata,8=data (or-able))." DVB_USB_DEBUG_STATUS);
  14. int dvb_usb_dib0700_ir_proto = 1;
  15. module_param(dvb_usb_dib0700_ir_proto, int, 0644);
  16. MODULE_PARM_DESC(dvb_usb_dib0700_ir_proto, "set ir protocol (0=NEC, 1=RC5 (default), 2=RC6).");
  17. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  18. /* expecting rx buffer: request data[0] data[1] ... data[2] */
  19. static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen)
  20. {
  21. int status;
  22. deb_data(">>> ");
  23. debug_dump(tx,txlen,deb_data);
  24. status = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev,0),
  25. tx[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, tx, txlen,
  26. USB_CTRL_GET_TIMEOUT);
  27. if (status != txlen)
  28. deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
  29. return status < 0 ? status : 0;
  30. }
  31. /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
  32. int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
  33. {
  34. u16 index, value;
  35. int status;
  36. if (txlen < 2) {
  37. err("tx buffer length is smaller than 2. Makes no sense.");
  38. return -EINVAL;
  39. }
  40. if (txlen > 4) {
  41. err("tx buffer length is larger than 4. Not supported.");
  42. return -EINVAL;
  43. }
  44. deb_data(">>> ");
  45. debug_dump(tx,txlen,deb_data);
  46. value = ((txlen - 2) << 8) | tx[1];
  47. index = 0;
  48. if (txlen > 2)
  49. index |= (tx[2] << 8);
  50. if (txlen > 3)
  51. index |= tx[3];
  52. status = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev,0), tx[0],
  53. USB_TYPE_VENDOR | USB_DIR_IN, value, index, rx, rxlen,
  54. USB_CTRL_GET_TIMEOUT);
  55. if (status < 0)
  56. deb_info("ep 0 read error (status = %d)\n",status);
  57. deb_data("<<< ");
  58. debug_dump(rx,rxlen,deb_data);
  59. return status; /* length in case of success */
  60. }
  61. int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
  62. {
  63. u8 buf[3] = { REQUEST_SET_GPIO, gpio, ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6) };
  64. return dib0700_ctrl_wr(d,buf,3);
  65. }
  66. /*
  67. * I2C master xfer function (supported in 1.20 firmware)
  68. */
  69. static int dib0700_i2c_xfer_new(struct i2c_adapter *adap, struct i2c_msg *msg,
  70. int num)
  71. {
  72. /* The new i2c firmware messages are more reliable and in particular
  73. properly support i2c read calls not preceded by a write */
  74. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  75. uint8_t bus_mode = 1; /* 0=eeprom bus, 1=frontend bus */
  76. uint8_t gen_mode = 0; /* 0=master i2c, 1=gpio i2c */
  77. uint8_t en_start = 0;
  78. uint8_t en_stop = 0;
  79. uint8_t buf[255]; /* TBV: malloc ? */
  80. int result, i;
  81. /* Ensure nobody else hits the i2c bus while we're sending our
  82. sequence of messages, (such as the remote control thread) */
  83. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  84. return -EAGAIN;
  85. for (i = 0; i < num; i++) {
  86. if (i == 0) {
  87. /* First message in the transaction */
  88. en_start = 1;
  89. } else if (!(msg[i].flags & I2C_M_NOSTART)) {
  90. /* Device supports repeated-start */
  91. en_start = 1;
  92. } else {
  93. /* Not the first packet and device doesn't support
  94. repeated start */
  95. en_start = 0;
  96. }
  97. if (i == (num - 1)) {
  98. /* Last message in the transaction */
  99. en_stop = 1;
  100. }
  101. if (msg[i].flags & I2C_M_RD) {
  102. /* Read request */
  103. u16 index, value;
  104. uint8_t i2c_dest;
  105. i2c_dest = (msg[i].addr << 1);
  106. value = ((en_start << 7) | (en_stop << 6) |
  107. (msg[i].len & 0x3F)) << 8 | i2c_dest;
  108. /* I2C ctrl + FE bus; */
  109. index = ((gen_mode<<6)&0xC0) | ((bus_mode<<4)&0x30);
  110. result = usb_control_msg(d->udev,
  111. usb_rcvctrlpipe(d->udev, 0),
  112. REQUEST_NEW_I2C_READ,
  113. USB_TYPE_VENDOR | USB_DIR_IN,
  114. value, index, msg[i].buf,
  115. msg[i].len,
  116. USB_CTRL_GET_TIMEOUT);
  117. if (result < 0) {
  118. err("i2c read error (status = %d)\n", result);
  119. break;
  120. }
  121. } else {
  122. /* Write request */
  123. buf[0] = REQUEST_NEW_I2C_WRITE;
  124. buf[1] = (msg[i].addr << 1);
  125. buf[2] = (en_start << 7) | (en_stop << 6) |
  126. (msg[i].len & 0x3F);
  127. /* I2C ctrl + FE bus; */
  128. buf[3] = ((gen_mode<<6)&0xC0) | ((bus_mode<<4)&0x30);
  129. /* The Actual i2c payload */
  130. memcpy(&buf[4], msg[i].buf, msg[i].len);
  131. result = usb_control_msg(d->udev,
  132. usb_sndctrlpipe(d->udev, 0),
  133. REQUEST_NEW_I2C_WRITE,
  134. USB_TYPE_VENDOR | USB_DIR_OUT,
  135. 0, 0, buf, msg[i].len + 4,
  136. USB_CTRL_GET_TIMEOUT);
  137. if (result < 0) {
  138. err("i2c write error (status = %d)\n", result);
  139. break;
  140. }
  141. }
  142. }
  143. mutex_unlock(&d->i2c_mutex);
  144. return i;
  145. }
  146. /*
  147. * I2C master xfer function (pre-1.20 firmware)
  148. */
  149. static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap,
  150. struct i2c_msg *msg, int num)
  151. {
  152. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  153. int i,len;
  154. u8 buf[255];
  155. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  156. return -EAGAIN;
  157. for (i = 0; i < num; i++) {
  158. /* fill in the address */
  159. buf[1] = (msg[i].addr << 1);
  160. /* fill the buffer */
  161. memcpy(&buf[2], msg[i].buf, msg[i].len);
  162. /* write/read request */
  163. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  164. buf[0] = REQUEST_I2C_READ;
  165. buf[1] |= 1;
  166. /* special thing in the current firmware: when length is zero the read-failed */
  167. if ((len = dib0700_ctrl_rd(d, buf, msg[i].len + 2, msg[i+1].buf, msg[i+1].len)) <= 0) {
  168. deb_info("I2C read failed on address %x\n", msg[i].addr);
  169. break;
  170. }
  171. msg[i+1].len = len;
  172. i++;
  173. } else {
  174. buf[0] = REQUEST_I2C_WRITE;
  175. if (dib0700_ctrl_wr(d, buf, msg[i].len + 2) < 0)
  176. break;
  177. }
  178. }
  179. mutex_unlock(&d->i2c_mutex);
  180. return i;
  181. }
  182. static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  183. int num)
  184. {
  185. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  186. struct dib0700_state *st = d->priv;
  187. if (st->fw_use_new_i2c_api == 1) {
  188. /* User running at least fw 1.20 */
  189. return dib0700_i2c_xfer_new(adap, msg, num);
  190. } else {
  191. /* Use legacy calls */
  192. return dib0700_i2c_xfer_legacy(adap, msg, num);
  193. }
  194. }
  195. static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
  196. {
  197. return I2C_FUNC_I2C;
  198. }
  199. struct i2c_algorithm dib0700_i2c_algo = {
  200. .master_xfer = dib0700_i2c_xfer,
  201. .functionality = dib0700_i2c_func,
  202. };
  203. int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
  204. struct dvb_usb_device_description **desc, int *cold)
  205. {
  206. u8 b[16];
  207. s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev,0),
  208. REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);
  209. deb_info("FW GET_VERSION length: %d\n",ret);
  210. *cold = ret <= 0;
  211. deb_info("cold: %d\n", *cold);
  212. return 0;
  213. }
  214. static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
  215. u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
  216. u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
  217. {
  218. u8 b[10];
  219. b[0] = REQUEST_SET_CLOCK;
  220. b[1] = (en_pll << 7) | (pll_src << 6) | (pll_range << 5) | (clock_gpio3 << 4);
  221. b[2] = (pll_prediv >> 8) & 0xff; // MSB
  222. b[3] = pll_prediv & 0xff; // LSB
  223. b[4] = (pll_loopdiv >> 8) & 0xff; // MSB
  224. b[5] = pll_loopdiv & 0xff; // LSB
  225. b[6] = (free_div >> 8) & 0xff; // MSB
  226. b[7] = free_div & 0xff; // LSB
  227. b[8] = (dsuScaler >> 8) & 0xff; // MSB
  228. b[9] = dsuScaler & 0xff; // LSB
  229. return dib0700_ctrl_wr(d, b, 10);
  230. }
  231. int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
  232. {
  233. switch (clk_MHz) {
  234. case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
  235. default: return -EINVAL;
  236. }
  237. return 0;
  238. }
  239. static int dib0700_jumpram(struct usb_device *udev, u32 address)
  240. {
  241. int ret, actlen;
  242. u8 buf[8] = { REQUEST_JUMPRAM, 0, 0, 0,
  243. (address >> 24) & 0xff,
  244. (address >> 16) & 0xff,
  245. (address >> 8) & 0xff,
  246. address & 0xff };
  247. if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
  248. deb_fw("jumpram to 0x%x failed\n",address);
  249. return ret;
  250. }
  251. if (actlen != 8) {
  252. deb_fw("jumpram to 0x%x failed\n",address);
  253. return -EIO;
  254. }
  255. return 0;
  256. }
  257. int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
  258. {
  259. struct hexline hx;
  260. int pos = 0, ret, act_len;
  261. u8 buf[260];
  262. while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
  263. deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",hx.addr, hx.len, hx.chk);
  264. buf[0] = hx.len;
  265. buf[1] = (hx.addr >> 8) & 0xff;
  266. buf[2] = hx.addr & 0xff;
  267. buf[3] = hx.type;
  268. memcpy(&buf[4],hx.data,hx.len);
  269. buf[4+hx.len] = hx.chk;
  270. ret = usb_bulk_msg(udev,
  271. usb_sndbulkpipe(udev, 0x01),
  272. buf,
  273. hx.len + 5,
  274. &act_len,
  275. 1000);
  276. if (ret < 0) {
  277. err("firmware download failed at %d with %d",pos,ret);
  278. return ret;
  279. }
  280. }
  281. if (ret == 0) {
  282. /* start the firmware */
  283. if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
  284. info("firmware started successfully.");
  285. msleep(500);
  286. }
  287. } else
  288. ret = -EIO;
  289. return ret;
  290. }
  291. int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  292. {
  293. struct dib0700_state *st = adap->dev->priv;
  294. u8 b[4];
  295. b[0] = REQUEST_ENABLE_VIDEO;
  296. b[1] = (onoff << 4) | 0x00; /* this bit gives a kind of command, rather than enabling something or not */
  297. b[2] = (0x01 << 4); /* Master mode */
  298. b[3] = 0x00;
  299. deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
  300. if (onoff)
  301. st->channel_state |= 1 << adap->id;
  302. else
  303. st->channel_state &= ~(1 << adap->id);
  304. b[2] |= st->channel_state;
  305. deb_info("data for streaming: %x %x\n",b[1],b[2]);
  306. return dib0700_ctrl_wr(adap->dev, b, 4);
  307. }
  308. int dib0700_rc_setup(struct dvb_usb_device *d)
  309. {
  310. u8 rc_setup[3] = {REQUEST_SET_RC, dvb_usb_dib0700_ir_proto, 0};
  311. int i = dib0700_ctrl_wr(d, rc_setup, 3);
  312. if (i<0) {
  313. err("ir protocol setup failed");
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. static int dib0700_probe(struct usb_interface *intf,
  319. const struct usb_device_id *id)
  320. {
  321. int i;
  322. struct dvb_usb_device *dev;
  323. for (i = 0; i < dib0700_device_count; i++)
  324. if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE,
  325. &dev, adapter_nr) == 0)
  326. {
  327. dib0700_rc_setup(dev);
  328. return 0;
  329. }
  330. return -ENODEV;
  331. }
  332. static struct usb_driver dib0700_driver = {
  333. .name = "dvb_usb_dib0700",
  334. .probe = dib0700_probe,
  335. .disconnect = dvb_usb_device_exit,
  336. .id_table = dib0700_usb_id_table,
  337. };
  338. /* module stuff */
  339. static int __init dib0700_module_init(void)
  340. {
  341. int result;
  342. info("loaded with support for %d different device-types", dib0700_device_count);
  343. if ((result = usb_register(&dib0700_driver))) {
  344. err("usb_register failed. Error number %d",result);
  345. return result;
  346. }
  347. return 0;
  348. }
  349. static void __exit dib0700_module_exit(void)
  350. {
  351. /* deregister this driver from the USB subsystem */
  352. usb_deregister(&dib0700_driver);
  353. }
  354. module_init (dib0700_module_init);
  355. module_exit (dib0700_module_exit);
  356. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  357. MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
  358. MODULE_VERSION("1.0");
  359. MODULE_LICENSE("GPL");