i2c-robotfuzz-osif.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Driver for RobotFuzz OSIF
  3. *
  4. * Copyright (c) 2013 Andrew Lunn <andrew@lunn.ch>
  5. * Copyright (c) 2007 Barry Carter <Barry.Carter@robotfuzz.com>
  6. *
  7. * Based on the i2c-tiny-usb by
  8. *
  9. * Copyright (C) 2006 Til Harbaum (Till@Harbaum.org)
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation, version 2.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/errno.h>
  18. #include <linux/i2c.h>
  19. #include <linux/slab.h>
  20. #include <linux/usb.h>
  21. #define OSIFI2C_READ 20
  22. #define OSIFI2C_WRITE 21
  23. #define OSIFI2C_STOP 22
  24. #define OSIFI2C_STATUS 23
  25. #define OSIFI2C_SET_BIT_RATE 24
  26. #define STATUS_ADDRESS_ACK 0
  27. #define STATUS_ADDRESS_NAK 2
  28. struct osif_priv {
  29. struct usb_device *usb_dev;
  30. struct usb_interface *interface;
  31. struct i2c_adapter adapter;
  32. unsigned char status;
  33. };
  34. static int osif_usb_read(struct i2c_adapter *adapter, int cmd,
  35. int value, int index, void *data, int len)
  36. {
  37. struct osif_priv *priv = adapter->algo_data;
  38. return usb_control_msg(priv->usb_dev, usb_rcvctrlpipe(priv->usb_dev, 0),
  39. cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
  40. USB_DIR_IN, value, index, data, len, 2000);
  41. }
  42. static int osif_usb_write(struct i2c_adapter *adapter, int cmd,
  43. int value, int index, void *data, int len)
  44. {
  45. struct osif_priv *priv = adapter->algo_data;
  46. return usb_control_msg(priv->usb_dev, usb_sndctrlpipe(priv->usb_dev, 0),
  47. cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  48. value, index, data, len, 2000);
  49. }
  50. static int osif_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
  51. int num)
  52. {
  53. struct osif_priv *priv = adapter->algo_data;
  54. struct i2c_msg *pmsg;
  55. int ret = 0;
  56. int i, cmd;
  57. for (i = 0; ret >= 0 && i < num; i++) {
  58. pmsg = &msgs[i];
  59. if (pmsg->flags & I2C_M_RD) {
  60. cmd = OSIFI2C_READ;
  61. ret = osif_usb_read(adapter, cmd, pmsg->flags,
  62. pmsg->addr, pmsg->buf,
  63. pmsg->len);
  64. if (ret != pmsg->len) {
  65. dev_err(&adapter->dev, "failure reading data\n");
  66. return -EREMOTEIO;
  67. }
  68. } else {
  69. cmd = OSIFI2C_WRITE;
  70. ret = osif_usb_write(adapter, cmd, pmsg->flags,
  71. pmsg->addr, pmsg->buf, pmsg->len);
  72. if (ret != pmsg->len) {
  73. dev_err(&adapter->dev, "failure writing data\n");
  74. return -EREMOTEIO;
  75. }
  76. }
  77. ret = osif_usb_read(adapter, OSIFI2C_STOP, 0, 0, NULL, 0);
  78. if (ret) {
  79. dev_err(&adapter->dev, "failure sending STOP\n");
  80. return -EREMOTEIO;
  81. }
  82. /* read status */
  83. ret = osif_usb_read(adapter, OSIFI2C_STATUS, 0, 0,
  84. &priv->status, 1);
  85. if (ret != 1) {
  86. dev_err(&adapter->dev, "failure reading status\n");
  87. return -EREMOTEIO;
  88. }
  89. if (priv->status != STATUS_ADDRESS_ACK) {
  90. dev_dbg(&adapter->dev, "status = %d\n", priv->status);
  91. return -EREMOTEIO;
  92. }
  93. }
  94. return i;
  95. }
  96. static u32 osif_func(struct i2c_adapter *adapter)
  97. {
  98. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  99. }
  100. static struct i2c_algorithm osif_algorithm = {
  101. .master_xfer = osif_xfer,
  102. .functionality = osif_func,
  103. };
  104. #define USB_OSIF_VENDOR_ID 0x1964
  105. #define USB_OSIF_PRODUCT_ID 0x0001
  106. static struct usb_device_id osif_table[] = {
  107. { USB_DEVICE(USB_OSIF_VENDOR_ID, USB_OSIF_PRODUCT_ID) },
  108. { }
  109. };
  110. MODULE_DEVICE_TABLE(usb, osif_table);
  111. static int osif_probe(struct usb_interface *interface,
  112. const struct usb_device_id *id)
  113. {
  114. int ret;
  115. struct osif_priv *priv;
  116. u16 version;
  117. priv = devm_kzalloc(&interface->dev, sizeof(*priv), GFP_KERNEL);
  118. if (!priv)
  119. return -ENOMEM;
  120. priv->usb_dev = usb_get_dev(interface_to_usbdev(interface));
  121. priv->interface = interface;
  122. usb_set_intfdata(interface, priv);
  123. priv->adapter.owner = THIS_MODULE;
  124. priv->adapter.class = I2C_CLASS_HWMON;
  125. priv->adapter.algo = &osif_algorithm;
  126. priv->adapter.algo_data = priv;
  127. snprintf(priv->adapter.name, sizeof(priv->adapter.name),
  128. "OSIF at bus %03d device %03d",
  129. priv->usb_dev->bus->busnum, priv->usb_dev->devnum);
  130. /*
  131. * Set bus frequency. The frequency is:
  132. * 120,000,000 / ( 16 + 2 * div * 4^prescale).
  133. * Using dev = 52, prescale = 0 give 100KHz */
  134. ret = osif_usb_read(&priv->adapter, OSIFI2C_SET_BIT_RATE, 52, 0,
  135. NULL, 0);
  136. if (ret) {
  137. dev_err(&interface->dev, "failure sending bit rate");
  138. usb_put_dev(priv->usb_dev);
  139. return ret;
  140. }
  141. i2c_add_adapter(&(priv->adapter));
  142. version = le16_to_cpu(priv->usb_dev->descriptor.bcdDevice);
  143. dev_info(&interface->dev,
  144. "version %x.%02x found at bus %03d address %03d",
  145. version >> 8, version & 0xff,
  146. priv->usb_dev->bus->busnum, priv->usb_dev->devnum);
  147. return 0;
  148. }
  149. static void osif_disconnect(struct usb_interface *interface)
  150. {
  151. struct osif_priv *priv = usb_get_intfdata(interface);
  152. i2c_del_adapter(&(priv->adapter));
  153. usb_set_intfdata(interface, NULL);
  154. usb_put_dev(priv->usb_dev);
  155. }
  156. static struct usb_driver osif_driver = {
  157. .name = "RobotFuzz Open Source InterFace, OSIF",
  158. .probe = osif_probe,
  159. .disconnect = osif_disconnect,
  160. .id_table = osif_table,
  161. };
  162. module_usb_driver(osif_driver);
  163. MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
  164. MODULE_AUTHOR("Barry Carter <barry.carter@robotfuzz.com>");
  165. MODULE_DESCRIPTION("RobotFuzz OSIF driver");
  166. MODULE_LICENSE("GPL v2");