i2c-tiny-usb.c 7.4 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 interfaces to usb layer */
  16. #include <linux/usb.h>
  17. /* include interface to i2c layer */
  18. #include <linux/i2c.h>
  19. /* commands via USB, must match command ids in the firmware */
  20. #define CMD_ECHO 0
  21. #define CMD_GET_FUNC 1
  22. #define CMD_SET_DELAY 2
  23. #define CMD_GET_STATUS 3
  24. #define CMD_I2C_IO 4
  25. #define CMD_I2C_IO_BEGIN (1<<0)
  26. #define CMD_I2C_IO_END (1<<1)
  27. /* i2c bit delay, default is 10us -> 100kHz */
  28. static int delay = 10;
  29. module_param(delay, int, 0);
  30. MODULE_PARM_DESC(delay, "bit delay in microseconds, "
  31. "e.g. 10 for 100kHz (default is 100kHz)");
  32. static int usb_read(struct i2c_adapter *adapter, int cmd,
  33. int value, int index, void *data, int len);
  34. static int usb_write(struct i2c_adapter *adapter, int cmd,
  35. int value, int index, void *data, int len);
  36. /* ----- begin of i2c layer ---------------------------------------------- */
  37. #define STATUS_IDLE 0
  38. #define STATUS_ADDRESS_ACK 1
  39. #define STATUS_ADDRESS_NAK 2
  40. static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
  41. {
  42. unsigned char status;
  43. struct i2c_msg *pmsg;
  44. int i;
  45. dev_dbg(&adapter->dev, "master xfer %d messages:\n", num);
  46. for (i = 0 ; i < num ; i++) {
  47. int cmd = CMD_I2C_IO;
  48. if (i == 0)
  49. cmd |= CMD_I2C_IO_BEGIN;
  50. if (i == num-1)
  51. cmd |= CMD_I2C_IO_END;
  52. pmsg = &msgs[i];
  53. dev_dbg(&adapter->dev,
  54. " %d: %s (flags %d) %d bytes to 0x%02x\n",
  55. i, pmsg->flags & I2C_M_RD ? "read" : "write",
  56. pmsg->flags, pmsg->len, pmsg->addr);
  57. /* and directly send the message */
  58. if (pmsg->flags & I2C_M_RD) {
  59. /* read data */
  60. if (usb_read(adapter, cmd,
  61. pmsg->flags, pmsg->addr,
  62. pmsg->buf, pmsg->len) != pmsg->len) {
  63. dev_err(&adapter->dev,
  64. "failure reading data\n");
  65. return -EREMOTEIO;
  66. }
  67. } else {
  68. /* write data */
  69. if (usb_write(adapter, cmd,
  70. pmsg->flags, pmsg->addr,
  71. pmsg->buf, pmsg->len) != pmsg->len) {
  72. dev_err(&adapter->dev,
  73. "failure writing data\n");
  74. return -EREMOTEIO;
  75. }
  76. }
  77. /* read status */
  78. if (usb_read(adapter, CMD_GET_STATUS, 0, 0, &status, 1) != 1) {
  79. dev_err(&adapter->dev, "failure reading status\n");
  80. return -EREMOTEIO;
  81. }
  82. dev_dbg(&adapter->dev, " status = %d\n", status);
  83. if (status == STATUS_ADDRESS_NAK)
  84. return -EREMOTEIO;
  85. }
  86. return i;
  87. }
  88. static u32 usb_func(struct i2c_adapter *adapter)
  89. {
  90. u32 func;
  91. /* get functionality from adapter */
  92. if (usb_read(adapter, CMD_GET_FUNC, 0, 0, &func, sizeof(func)) !=
  93. sizeof(func)) {
  94. dev_err(&adapter->dev, "failure reading functionality\n");
  95. return 0;
  96. }
  97. return func;
  98. }
  99. /* This is the actual algorithm we define */
  100. static const struct i2c_algorithm usb_algorithm = {
  101. .master_xfer = usb_xfer,
  102. .functionality = usb_func,
  103. };
  104. /* ----- end of i2c layer ------------------------------------------------ */
  105. /* ----- begin of usb layer ---------------------------------------------- */
  106. /*
  107. * Initially the usb i2c interface uses a vid/pid pair donated by
  108. * Future Technology Devices International Ltd., later a pair was
  109. * bought from EZPrototypes
  110. */
  111. static struct usb_device_id i2c_tiny_usb_table [] = {
  112. { USB_DEVICE(0x0403, 0xc631) }, /* FTDI */
  113. { USB_DEVICE(0x1c40, 0x0534) }, /* EZPrototypes */
  114. { } /* Terminating entry */
  115. };
  116. MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
  117. /* Structure to hold all of our device specific stuff */
  118. struct i2c_tiny_usb {
  119. struct usb_device *usb_dev; /* the usb device for this device */
  120. struct usb_interface *interface; /* the interface for this device */
  121. struct i2c_adapter adapter; /* i2c related things */
  122. };
  123. static int usb_read(struct i2c_adapter *adapter, int cmd,
  124. int value, int index, void *data, int len)
  125. {
  126. struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
  127. /* do control transfer */
  128. return usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
  129. cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
  130. USB_DIR_IN, value, index, data, len, 2000);
  131. }
  132. static int usb_write(struct i2c_adapter *adapter, int cmd,
  133. int value, int index, void *data, int len)
  134. {
  135. struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
  136. /* do control transfer */
  137. return usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
  138. cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  139. value, index, data, len, 2000);
  140. }
  141. static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
  142. {
  143. usb_put_dev(dev->usb_dev);
  144. kfree(dev);
  145. }
  146. static int i2c_tiny_usb_probe(struct usb_interface *interface,
  147. const struct usb_device_id *id)
  148. {
  149. struct i2c_tiny_usb *dev;
  150. int retval = -ENOMEM;
  151. u16 version;
  152. dev_dbg(&interface->dev, "probing usb device\n");
  153. /* allocate memory for our device state and initialize it */
  154. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  155. if (dev == NULL) {
  156. dev_err(&interface->dev, "Out of memory\n");
  157. goto error;
  158. }
  159. dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
  160. dev->interface = interface;
  161. /* save our data pointer in this interface device */
  162. usb_set_intfdata(interface, dev);
  163. version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
  164. dev_info(&interface->dev,
  165. "version %x.%02x found at bus %03d address %03d\n",
  166. version >> 8, version & 0xff,
  167. dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
  168. /* setup i2c adapter description */
  169. dev->adapter.owner = THIS_MODULE;
  170. dev->adapter.class = I2C_CLASS_HWMON;
  171. dev->adapter.algo = &usb_algorithm;
  172. dev->adapter.algo_data = dev;
  173. snprintf(dev->adapter.name, sizeof(dev->adapter.name),
  174. "i2c-tiny-usb at bus %03d device %03d",
  175. dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
  176. if (usb_write(&dev->adapter, CMD_SET_DELAY,
  177. cpu_to_le16(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");