i2c-tiny-usb.c 7.3 KB

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