i2c-tiny-usb.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * driver for the i2c-tiny-usb adapter - 1.0
  3. * http://www.harbaum.org/till/i2c_tiny_usb
  4. *
  5. * Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, version 2.
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. /* include interfaces to usb layer */
  17. #include <linux/usb.h>
  18. /* include interface to i2c layer */
  19. #include <linux/i2c.h>
  20. /* commands via USB, must match command ids in the firmware */
  21. #define CMD_ECHO 0
  22. #define CMD_GET_FUNC 1
  23. #define CMD_SET_DELAY 2
  24. #define CMD_GET_STATUS 3
  25. #define CMD_I2C_IO 4
  26. #define CMD_I2C_IO_BEGIN (1<<0)
  27. #define CMD_I2C_IO_END (1<<1)
  28. /* i2c bit delay, default is 10us -> 100kHz */
  29. static unsigned short delay = 10;
  30. module_param(delay, ushort, 0);
  31. MODULE_PARM_DESC(delay, "bit delay in microseconds, "
  32. "e.g. 10 for 100kHz (default is 100kHz)");
  33. static int usb_read(struct i2c_adapter *adapter, int cmd,
  34. int value, int index, void *data, int len);
  35. static int usb_write(struct i2c_adapter *adapter, int cmd,
  36. int value, int index, void *data, int len);
  37. /* ----- begin of i2c layer ---------------------------------------------- */
  38. #define STATUS_IDLE 0
  39. #define STATUS_ADDRESS_ACK 1
  40. #define STATUS_ADDRESS_NAK 2
  41. static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
  42. {
  43. unsigned char status;
  44. struct i2c_msg *pmsg;
  45. int i;
  46. dev_dbg(&adapter->dev, "master xfer %d messages:\n", num);
  47. for (i = 0 ; i < num ; i++) {
  48. int cmd = CMD_I2C_IO;
  49. if (i == 0)
  50. cmd |= CMD_I2C_IO_BEGIN;
  51. if (i == num-1)
  52. cmd |= CMD_I2C_IO_END;
  53. pmsg = &msgs[i];
  54. dev_dbg(&adapter->dev,
  55. " %d: %s (flags %d) %d bytes to 0x%02x\n",
  56. i, pmsg->flags & I2C_M_RD ? "read" : "write",
  57. pmsg->flags, pmsg->len, pmsg->addr);
  58. /* and directly send the message */
  59. if (pmsg->flags & I2C_M_RD) {
  60. /* read data */
  61. if (usb_read(adapter, cmd,
  62. pmsg->flags, pmsg->addr,
  63. pmsg->buf, pmsg->len) != pmsg->len) {
  64. dev_err(&adapter->dev,
  65. "failure reading data\n");
  66. return -EREMOTEIO;
  67. }
  68. } else {
  69. /* write data */
  70. if (usb_write(adapter, cmd,
  71. pmsg->flags, pmsg->addr,
  72. pmsg->buf, pmsg->len) != pmsg->len) {
  73. dev_err(&adapter->dev,
  74. "failure writing data\n");
  75. return -EREMOTEIO;
  76. }
  77. }
  78. /* read status */
  79. if (usb_read(adapter, CMD_GET_STATUS, 0, 0, &status, 1) != 1) {
  80. dev_err(&adapter->dev, "failure reading status\n");
  81. return -EREMOTEIO;
  82. }
  83. dev_dbg(&adapter->dev, " status = %d\n", status);
  84. if (status == STATUS_ADDRESS_NAK)
  85. return -EREMOTEIO;
  86. }
  87. return i;
  88. }
  89. static u32 usb_func(struct i2c_adapter *adapter)
  90. {
  91. __le32 func;
  92. /* get functionality from adapter */
  93. if (usb_read(adapter, CMD_GET_FUNC, 0, 0, &func, sizeof(func)) !=
  94. sizeof(func)) {
  95. dev_err(&adapter->dev, "failure reading functionality\n");
  96. return 0;
  97. }
  98. return le32_to_cpu(func);
  99. }
  100. /* This is the actual algorithm we define */
  101. static const struct i2c_algorithm usb_algorithm = {
  102. .master_xfer = usb_xfer,
  103. .functionality = usb_func,
  104. };
  105. /* ----- end of i2c layer ------------------------------------------------ */
  106. /* ----- begin of usb layer ---------------------------------------------- */
  107. /*
  108. * Initially the usb i2c interface uses a vid/pid pair donated by
  109. * Future Technology Devices International Ltd., later a pair was
  110. * bought from EZPrototypes
  111. */
  112. static struct usb_device_id i2c_tiny_usb_table [] = {
  113. { USB_DEVICE(0x0403, 0xc631) }, /* FTDI */
  114. { USB_DEVICE(0x1c40, 0x0534) }, /* EZPrototypes */
  115. { } /* Terminating entry */
  116. };
  117. MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
  118. /* Structure to hold all of our device specific stuff */
  119. struct i2c_tiny_usb {
  120. struct usb_device *usb_dev; /* the usb device for this device */
  121. struct usb_interface *interface; /* the interface for this device */
  122. struct i2c_adapter adapter; /* i2c related things */
  123. };
  124. static int usb_read(struct i2c_adapter *adapter, int cmd,
  125. int value, int index, void *data, int len)
  126. {
  127. struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
  128. /* do control transfer */
  129. return usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
  130. cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
  131. USB_DIR_IN, value, index, data, len, 2000);
  132. }
  133. static int usb_write(struct i2c_adapter *adapter, int cmd,
  134. int value, int index, void *data, int len)
  135. {
  136. struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
  137. /* do control transfer */
  138. return usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
  139. cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  140. value, index, data, len, 2000);
  141. }
  142. static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
  143. {
  144. usb_put_dev(dev->usb_dev);
  145. kfree(dev);
  146. }
  147. static int i2c_tiny_usb_probe(struct usb_interface *interface,
  148. const struct usb_device_id *id)
  149. {
  150. struct i2c_tiny_usb *dev;
  151. int retval = -ENOMEM;
  152. u16 version;
  153. dev_dbg(&interface->dev, "probing usb device\n");
  154. /* allocate memory for our device state and initialize it */
  155. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  156. if (dev == NULL) {
  157. dev_err(&interface->dev, "Out of memory\n");
  158. goto error;
  159. }
  160. dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
  161. dev->interface = interface;
  162. /* save our data pointer in this interface device */
  163. usb_set_intfdata(interface, dev);
  164. version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
  165. dev_info(&interface->dev,
  166. "version %x.%02x found at bus %03d address %03d\n",
  167. version >> 8, version & 0xff,
  168. dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
  169. /* setup i2c adapter description */
  170. dev->adapter.owner = THIS_MODULE;
  171. dev->adapter.class = I2C_CLASS_HWMON;
  172. dev->adapter.algo = &usb_algorithm;
  173. dev->adapter.algo_data = dev;
  174. snprintf(dev->adapter.name, sizeof(dev->adapter.name),
  175. "i2c-tiny-usb at bus %03d device %03d",
  176. dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
  177. if (usb_write(&dev->adapter, CMD_SET_DELAY, delay, 0, NULL, 0) != 0) {
  178. dev_err(&dev->adapter.dev,
  179. "failure setting delay to %dus\n", delay);
  180. retval = -EIO;
  181. goto error;
  182. }
  183. dev->adapter.dev.parent = &dev->interface->dev;
  184. /* and finally attach to i2c layer */
  185. i2c_add_adapter(&dev->adapter);
  186. /* inform user about successful attachment to i2c layer */
  187. dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
  188. return 0;
  189. error:
  190. if (dev)
  191. i2c_tiny_usb_free(dev);
  192. return retval;
  193. }
  194. static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
  195. {
  196. struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
  197. i2c_del_adapter(&dev->adapter);
  198. usb_set_intfdata(interface, NULL);
  199. i2c_tiny_usb_free(dev);
  200. dev_dbg(&interface->dev, "disconnected\n");
  201. }
  202. static struct usb_driver i2c_tiny_usb_driver = {
  203. .name = "i2c-tiny-usb",
  204. .probe = i2c_tiny_usb_probe,
  205. .disconnect = i2c_tiny_usb_disconnect,
  206. .id_table = i2c_tiny_usb_table,
  207. };
  208. static int __init usb_i2c_tiny_usb_init(void)
  209. {
  210. /* register this driver with the USB subsystem */
  211. return usb_register(&i2c_tiny_usb_driver);
  212. }
  213. static void __exit usb_i2c_tiny_usb_exit(void)
  214. {
  215. /* deregister this driver with the USB subsystem */
  216. usb_deregister(&i2c_tiny_usb_driver);
  217. }
  218. module_init(usb_i2c_tiny_usb_init);
  219. module_exit(usb_i2c_tiny_usb_exit);
  220. /* ----- end of usb layer ------------------------------------------------ */
  221. MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
  222. MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
  223. MODULE_LICENSE("GPL");