i2c-tiny-usb.c 7.6 KB

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