dib0700_core.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. break;
  87. msg[i+1].len = len;
  88. i++;
  89. } else {
  90. buf[0] = REQUEST_I2C_WRITE;
  91. if (dib0700_ctrl_wr(d, buf, msg[i].len + 2) < 0)
  92. break;
  93. }
  94. }
  95. mutex_unlock(&d->i2c_mutex);
  96. return i;
  97. }
  98. static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
  99. {
  100. return I2C_FUNC_I2C;
  101. }
  102. struct i2c_algorithm dib0700_i2c_algo = {
  103. .master_xfer = dib0700_i2c_xfer,
  104. .functionality = dib0700_i2c_func,
  105. };
  106. int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
  107. struct dvb_usb_device_description **desc, int *cold)
  108. {
  109. u8 buf[3] = { REQUEST_SET_GPIO, 4, (GPIO_IN << 7) | (0 << 6) }; // GPIO4 is save - used for I2C
  110. *cold = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
  111. buf[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, buf, 3, USB_CTRL_GET_TIMEOUT) != 3;
  112. deb_info("cold: %d\n", *cold);
  113. return 0;
  114. }
  115. static int dib0700_jumpram(struct usb_device *udev, u32 address)
  116. {
  117. int ret, actlen;
  118. u8 buf[8] = { REQUEST_JUMPRAM, 0, 0, 0,
  119. (address >> 24) & 0xff,
  120. (address >> 16) & 0xff,
  121. (address >> 8) & 0xff,
  122. address & 0xff };
  123. if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
  124. deb_fw("jumpram to 0x%x failed\n",address);
  125. return ret;
  126. }
  127. if (actlen != 8) {
  128. deb_fw("jumpram to 0x%x failed\n",address);
  129. return -EIO;
  130. }
  131. return 0;
  132. }
  133. int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
  134. {
  135. struct hexline hx;
  136. int pos = 0, ret, act_len;
  137. u8 buf[260];
  138. while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
  139. deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",hx.addr, hx.len, hx.chk);
  140. buf[0] = hx.len;
  141. buf[1] = (hx.addr >> 8) & 0xff;
  142. buf[2] = hx.addr & 0xff;
  143. buf[3] = hx.type;
  144. memcpy(&buf[4],hx.data,hx.len);
  145. buf[4+hx.len] = hx.chk;
  146. ret = usb_bulk_msg(udev,
  147. usb_sndbulkpipe(udev, 0x01),
  148. buf,
  149. hx.len + 5,
  150. &act_len,
  151. 1000);
  152. if (ret < 0) {
  153. err("firmware download failed at %d with %d",pos,ret);
  154. return ret;
  155. }
  156. }
  157. if (ret == 0) {
  158. /* start the firmware */
  159. if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
  160. info("firmware started successfully.");
  161. msleep(100);
  162. }
  163. } else
  164. ret = -EIO;
  165. return ret;
  166. }
  167. int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  168. {
  169. struct dib0700_state *st = adap->dev->priv;
  170. u8 b[4];
  171. b[0] = REQUEST_ENABLE_VIDEO;
  172. b[1] = 0x00;
  173. b[2] = (0x01 << 4); /* Master mode */
  174. b[3] = 0x00;
  175. deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
  176. if (onoff)
  177. st->channel_state |= 1 << adap->id;
  178. else
  179. st->channel_state &= ~(1 << adap->id);
  180. b[2] |= st->channel_state;
  181. if (st->channel_state) /* if at least one channel is active */
  182. b[1] = (0x01 << 4) | 0x00;
  183. deb_info("data for streaming: %x %x\n",b[1],b[2]);
  184. return dib0700_ctrl_wr(adap->dev, b, 4);
  185. }
  186. static int dib0700_probe(struct usb_interface *intf,
  187. const struct usb_device_id *id)
  188. {
  189. int i;
  190. for (i = 0; i < dib0700_device_count; i++)
  191. if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE, NULL) == 0)
  192. return 0;
  193. return -ENODEV;
  194. }
  195. static struct usb_driver dib0700_driver = {
  196. .name = "dvb_usb_dib0700",
  197. .probe = dib0700_probe,
  198. .disconnect = dvb_usb_device_exit,
  199. .id_table = dib0700_usb_id_table,
  200. };
  201. /* module stuff */
  202. static int __init dib0700_module_init(void)
  203. {
  204. int result;
  205. info("loaded with support for %d different device-types", dib0700_device_count);
  206. if ((result = usb_register(&dib0700_driver))) {
  207. err("usb_register failed. Error number %d",result);
  208. return result;
  209. }
  210. return 0;
  211. }
  212. static void __exit dib0700_module_exit(void)
  213. {
  214. /* deregister this driver from the USB subsystem */
  215. usb_deregister(&dib0700_driver);
  216. }
  217. module_init (dib0700_module_init);
  218. module_exit (dib0700_module_exit);
  219. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  220. MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
  221. MODULE_VERSION("1.0");
  222. MODULE_LICENSE("GPL");