i2c-tiny-usb.c 7.6 KB

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