i2c-tiny-usb.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. void *dmadata = kmalloc(len, GFP_KERNEL);
  149. int ret;
  150. if (!dmadata)
  151. return -ENOMEM;
  152. /* do control transfer */
  153. ret = usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
  154. cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
  155. USB_DIR_IN, value, index, dmadata, len, 2000);
  156. memcpy(data, dmadata, len);
  157. kfree(dmadata);
  158. return ret;
  159. }
  160. static int usb_write(struct i2c_adapter *adapter, int cmd,
  161. int value, int index, void *data, int len)
  162. {
  163. struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
  164. void *dmadata = kmemdup(data, len, GFP_KERNEL);
  165. int ret;
  166. if (!dmadata)
  167. return -ENOMEM;
  168. /* do control transfer */
  169. ret = usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
  170. cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  171. value, index, dmadata, len, 2000);
  172. kfree(dmadata);
  173. return ret;
  174. }
  175. static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
  176. {
  177. usb_put_dev(dev->usb_dev);
  178. kfree(dev);
  179. }
  180. static int i2c_tiny_usb_probe(struct usb_interface *interface,
  181. const struct usb_device_id *id)
  182. {
  183. struct i2c_tiny_usb *dev;
  184. int retval = -ENOMEM;
  185. u16 version;
  186. dev_dbg(&interface->dev, "probing usb device\n");
  187. /* allocate memory for our device state and initialize it */
  188. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  189. if (dev == NULL) {
  190. dev_err(&interface->dev, "Out of memory\n");
  191. goto error;
  192. }
  193. dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
  194. dev->interface = interface;
  195. /* save our data pointer in this interface device */
  196. usb_set_intfdata(interface, dev);
  197. version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
  198. dev_info(&interface->dev,
  199. "version %x.%02x found at bus %03d address %03d\n",
  200. version >> 8, version & 0xff,
  201. dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
  202. /* setup i2c adapter description */
  203. dev->adapter.owner = THIS_MODULE;
  204. dev->adapter.class = I2C_CLASS_HWMON;
  205. dev->adapter.algo = &usb_algorithm;
  206. dev->adapter.algo_data = dev;
  207. snprintf(dev->adapter.name, sizeof(dev->adapter.name),
  208. "i2c-tiny-usb at bus %03d device %03d",
  209. dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
  210. if (usb_write(&dev->adapter, CMD_SET_DELAY, delay, 0, NULL, 0) != 0) {
  211. dev_err(&dev->adapter.dev,
  212. "failure setting delay to %dus\n", delay);
  213. retval = -EIO;
  214. goto error;
  215. }
  216. dev->adapter.dev.parent = &dev->interface->dev;
  217. /* and finally attach to i2c layer */
  218. i2c_add_adapter(&dev->adapter);
  219. /* inform user about successful attachment to i2c layer */
  220. dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
  221. return 0;
  222. error:
  223. if (dev)
  224. i2c_tiny_usb_free(dev);
  225. return retval;
  226. }
  227. static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
  228. {
  229. struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
  230. i2c_del_adapter(&dev->adapter);
  231. usb_set_intfdata(interface, NULL);
  232. i2c_tiny_usb_free(dev);
  233. dev_dbg(&interface->dev, "disconnected\n");
  234. }
  235. static struct usb_driver i2c_tiny_usb_driver = {
  236. .name = "i2c-tiny-usb",
  237. .probe = i2c_tiny_usb_probe,
  238. .disconnect = i2c_tiny_usb_disconnect,
  239. .id_table = i2c_tiny_usb_table,
  240. };
  241. module_usb_driver(i2c_tiny_usb_driver);
  242. /* ----- end of usb layer ------------------------------------------------ */
  243. MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
  244. MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
  245. MODULE_LICENSE("GPL");