dib0700_core.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /* expecting rx buffer: request data[0] data[1] ... data[2] */
  15. static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen)
  16. {
  17. int status;
  18. deb_data(">>> ");
  19. debug_dump(tx,txlen,deb_data);
  20. status = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev,0),
  21. tx[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, tx, txlen,
  22. USB_CTRL_GET_TIMEOUT);
  23. if (status != txlen)
  24. deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
  25. return status < 0 ? status : 0;
  26. }
  27. /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
  28. static int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
  29. {
  30. u16 index, value;
  31. int status;
  32. if (txlen < 2) {
  33. err("tx buffer length is smaller than 2. Makes no sense.");
  34. return -EINVAL;
  35. }
  36. if (txlen > 4) {
  37. err("tx buffer length is larger than 4. Not supported.");
  38. return -EINVAL;
  39. }
  40. deb_data(">>> ");
  41. debug_dump(tx,txlen,deb_data);
  42. value = ((txlen - 2) << 8) | tx[1];
  43. index = 0;
  44. if (txlen > 2)
  45. index |= (tx[2] << 8);
  46. if (txlen > 3)
  47. index |= tx[3];
  48. /* think about swapping here */
  49. value = le16_to_cpu(value);
  50. index = le16_to_cpu(index);
  51. status = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev,0), tx[0],
  52. USB_TYPE_VENDOR | USB_DIR_IN, value, index, rx, rxlen,
  53. USB_CTRL_GET_TIMEOUT);
  54. if (status < 0)
  55. deb_info("ep 0 read error (status = %d)\n",status);
  56. deb_data("<<< ");
  57. debug_dump(rx,rxlen,deb_data);
  58. return status; /* length in case of success */
  59. }
  60. int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
  61. {
  62. u8 buf[3] = { REQUEST_SET_GPIO, gpio, ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6) };
  63. return dib0700_ctrl_wr(d,buf,3);
  64. }
  65. /*
  66. * I2C master xfer function
  67. */
  68. static int dib0700_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg *msg,int num)
  69. {
  70. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  71. int i,len;
  72. u8 buf[255];
  73. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  74. return -EAGAIN;
  75. for (i = 0; i < num; i++) {
  76. /* fill in the address */
  77. buf[1] = (msg[i].addr << 1);
  78. /* fill the buffer */
  79. memcpy(&buf[2], msg[i].buf, msg[i].len);
  80. /* write/read request */
  81. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  82. buf[0] = REQUEST_I2C_READ;
  83. buf[1] |= 1;
  84. /* special thing in the current firmware: when length is zero the read-failed */
  85. if ((len = dib0700_ctrl_rd(d, buf, msg[i].len + 2, msg[i+1].buf, msg[i+1].len)) <= 0) {
  86. deb_info("I2C read failed on address %x\n", msg[i].addr);
  87. break;
  88. }
  89. msg[i+1].len = len;
  90. i++;
  91. } else {
  92. buf[0] = REQUEST_I2C_WRITE;
  93. if (dib0700_ctrl_wr(d, buf, msg[i].len + 2) < 0)
  94. break;
  95. }
  96. }
  97. mutex_unlock(&d->i2c_mutex);
  98. return i;
  99. }
  100. static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
  101. {
  102. return I2C_FUNC_I2C;
  103. }
  104. struct i2c_algorithm dib0700_i2c_algo = {
  105. .master_xfer = dib0700_i2c_xfer,
  106. .functionality = dib0700_i2c_func,
  107. };
  108. int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
  109. struct dvb_usb_device_description **desc, int *cold)
  110. {
  111. u8 buf[3] = { REQUEST_SET_GPIO, 4, (GPIO_IN << 7) | (0 << 6) }; // GPIO4 is save - used for I2C
  112. *cold = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
  113. buf[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, buf, 3, USB_CTRL_GET_TIMEOUT) != 3;
  114. deb_info("cold: %d\n", *cold);
  115. return 0;
  116. }
  117. static int dib0700_jumpram(struct usb_device *udev, u32 address)
  118. {
  119. int ret, actlen;
  120. u8 buf[8] = { REQUEST_JUMPRAM, 0, 0, 0,
  121. (address >> 24) & 0xff,
  122. (address >> 16) & 0xff,
  123. (address >> 8) & 0xff,
  124. address & 0xff };
  125. if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
  126. deb_fw("jumpram to 0x%x failed\n",address);
  127. return ret;
  128. }
  129. if (actlen != 8) {
  130. deb_fw("jumpram to 0x%x failed\n",address);
  131. return -EIO;
  132. }
  133. return 0;
  134. }
  135. int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
  136. {
  137. struct hexline hx;
  138. int pos = 0, ret, act_len;
  139. u8 buf[260];
  140. while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
  141. deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",hx.addr, hx.len, hx.chk);
  142. buf[0] = hx.len;
  143. buf[1] = (hx.addr >> 8) & 0xff;
  144. buf[2] = hx.addr & 0xff;
  145. buf[3] = hx.type;
  146. memcpy(&buf[4],hx.data,hx.len);
  147. buf[4+hx.len] = hx.chk;
  148. ret = usb_bulk_msg(udev,
  149. usb_sndbulkpipe(udev, 0x01),
  150. buf,
  151. hx.len + 5,
  152. &act_len,
  153. 1000);
  154. if (ret < 0) {
  155. err("firmware download failed at %d with %d",pos,ret);
  156. return ret;
  157. }
  158. }
  159. if (ret == 0) {
  160. /* start the firmware */
  161. if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
  162. info("firmware started successfully.");
  163. msleep(100);
  164. }
  165. } else
  166. ret = -EIO;
  167. return ret;
  168. }
  169. int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  170. {
  171. struct dib0700_state *st = adap->dev->priv;
  172. u8 b[4];
  173. b[0] = REQUEST_ENABLE_VIDEO;
  174. b[1] = 0x00;
  175. b[2] = (0x01 << 4); /* Master mode */
  176. b[3] = 0x00;
  177. deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
  178. if (onoff)
  179. st->channel_state |= 1 << adap->id;
  180. else
  181. st->channel_state &= ~(1 << adap->id);
  182. b[2] |= st->channel_state;
  183. if (st->channel_state) /* if at least one channel is active */
  184. b[1] = (0x01 << 4) | 0x00;
  185. deb_info("data for streaming: %x %x\n",b[1],b[2]);
  186. return dib0700_ctrl_wr(adap->dev, b, 4);
  187. }
  188. static int dib0700_probe(struct usb_interface *intf,
  189. const struct usb_device_id *id)
  190. {
  191. int i;
  192. for (i = 0; i < dib0700_device_count; i++)
  193. if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE, NULL) == 0)
  194. return 0;
  195. return -ENODEV;
  196. }
  197. static struct usb_driver dib0700_driver = {
  198. .name = "dvb_usb_dib0700",
  199. .probe = dib0700_probe,
  200. .disconnect = dvb_usb_device_exit,
  201. .id_table = dib0700_usb_id_table,
  202. };
  203. /* module stuff */
  204. static int __init dib0700_module_init(void)
  205. {
  206. int result;
  207. info("loaded with support for %d different device-types", dib0700_device_count);
  208. if ((result = usb_register(&dib0700_driver))) {
  209. err("usb_register failed. Error number %d",result);
  210. return result;
  211. }
  212. return 0;
  213. }
  214. static void __exit dib0700_module_exit(void)
  215. {
  216. /* deregister this driver from the USB subsystem */
  217. usb_deregister(&dib0700_driver);
  218. }
  219. module_init (dib0700_module_init);
  220. module_exit (dib0700_module_exit);
  221. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  222. MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
  223. MODULE_VERSION("1.0");
  224. MODULE_LICENSE("GPL");