usbvision-i2c.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * I2C_ALGO_USB.C
  3. * i2c algorithm for USB-I2C Bridges
  4. *
  5. * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
  6. * Dwaine Garden <dwainegarden@rogers.com>
  7. *
  8. * This module is part of usbvision driver project.
  9. * Updates to driver completed by Dwaine P. Garden
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/delay.h>
  28. #include <linux/slab.h>
  29. #include <linux/version.h>
  30. #include <linux/utsname.h>
  31. #include <linux/init.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/ioport.h>
  34. #include <linux/errno.h>
  35. #include <linux/sched.h>
  36. #include <linux/usb.h>
  37. #include <linux/i2c.h>
  38. #include "usbvision-i2c.h"
  39. static int debug_i2c_usb = 0;
  40. #if defined(module_param) // Showing parameters under SYSFS
  41. module_param (debug_i2c_usb, int, 0444); // debug_i2c_usb mode of the device driver
  42. #else
  43. MODULE_PARM(debug_i2c_usb, "i"); // debug_i2c_usb mode of the device driver
  44. #endif
  45. static inline int try_write_address(struct i2c_adapter *i2c_adap,
  46. unsigned char addr, int retries)
  47. {
  48. struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
  49. void *data;
  50. int i, ret = -1;
  51. char buf[4];
  52. data = i2c_get_adapdata(i2c_adap);
  53. buf[0] = 0x00;
  54. for (i = 0; i <= retries; i++) {
  55. ret = (adap->outb(data, addr, buf, 1));
  56. if (ret == 1)
  57. break; /* success! */
  58. udelay(5 /*adap->udelay */ );
  59. if (i == retries) /* no success */
  60. break;
  61. udelay(adap->udelay);
  62. }
  63. if (debug_i2c_usb) {
  64. if (i) {
  65. info("%s: Needed %d retries for address %#2x", __FUNCTION__, i, addr);
  66. info("%s: Maybe there's no device at this address", __FUNCTION__);
  67. }
  68. }
  69. return ret;
  70. }
  71. static inline int try_read_address(struct i2c_adapter *i2c_adap,
  72. unsigned char addr, int retries)
  73. {
  74. struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
  75. void *data;
  76. int i, ret = -1;
  77. char buf[4];
  78. data = i2c_get_adapdata(i2c_adap);
  79. for (i = 0; i <= retries; i++) {
  80. ret = (adap->inb(data, addr, buf, 1));
  81. if (ret == 1)
  82. break; /* success! */
  83. udelay(5 /*adap->udelay */ );
  84. if (i == retries) /* no success */
  85. break;
  86. udelay(adap->udelay);
  87. }
  88. if (debug_i2c_usb) {
  89. if (i) {
  90. info("%s: Needed %d retries for address %#2x", __FUNCTION__, i, addr);
  91. info("%s: Maybe there's no device at this address", __FUNCTION__);
  92. }
  93. }
  94. return ret;
  95. }
  96. static inline int usb_find_address(struct i2c_adapter *i2c_adap,
  97. struct i2c_msg *msg, int retries,
  98. unsigned char *add)
  99. {
  100. unsigned short flags = msg->flags;
  101. unsigned char addr;
  102. int ret;
  103. if ((flags & I2C_M_TEN)) {
  104. /* a ten bit address */
  105. addr = 0xf0 | ((msg->addr >> 7) & 0x03);
  106. /* try extended address code... */
  107. ret = try_write_address(i2c_adap, addr, retries);
  108. if (ret != 1) {
  109. err("died at extended address code, while writing");
  110. return -EREMOTEIO;
  111. }
  112. add[0] = addr;
  113. if (flags & I2C_M_RD) {
  114. /* okay, now switch into reading mode */
  115. addr |= 0x01;
  116. ret = try_read_address(i2c_adap, addr, retries);
  117. if (ret != 1) {
  118. err("died at extended address code, while reading");
  119. return -EREMOTEIO;
  120. }
  121. }
  122. } else { /* normal 7bit address */
  123. addr = (msg->addr << 1);
  124. if (flags & I2C_M_RD)
  125. addr |= 1;
  126. if (flags & I2C_M_REV_DIR_ADDR)
  127. addr ^= 1;
  128. add[0] = addr;
  129. if (flags & I2C_M_RD)
  130. ret = try_read_address(i2c_adap, addr, retries);
  131. else
  132. ret = try_write_address(i2c_adap, addr, retries);
  133. if (ret != 1) {
  134. return -EREMOTEIO;
  135. }
  136. }
  137. return 0;
  138. }
  139. static int
  140. usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  141. {
  142. struct i2c_msg *pmsg;
  143. struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
  144. void *data;
  145. int i, ret;
  146. unsigned char addr;
  147. data = i2c_get_adapdata(i2c_adap);
  148. for (i = 0; i < num; i++) {
  149. pmsg = &msgs[i];
  150. ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
  151. if (ret != 0) {
  152. if (debug_i2c_usb) {
  153. info("%s: got NAK from device, message #%d\n", __FUNCTION__, i);
  154. }
  155. return (ret < 0) ? ret : -EREMOTEIO;
  156. }
  157. if (pmsg->flags & I2C_M_RD) {
  158. /* read bytes into buffer */
  159. ret = (adap->inb(data, addr, pmsg->buf, pmsg->len));
  160. if (ret < pmsg->len) {
  161. return (ret < 0) ? ret : -EREMOTEIO;
  162. }
  163. } else {
  164. /* write bytes from buffer */
  165. ret = (adap->outb(data, addr, pmsg->buf, pmsg->len));
  166. if (ret < pmsg->len) {
  167. return (ret < 0) ? ret : -EREMOTEIO;
  168. }
  169. }
  170. }
  171. return num;
  172. }
  173. static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg)
  174. {
  175. return 0;
  176. }
  177. static u32 usb_func(struct i2c_adapter *adap)
  178. {
  179. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  180. }
  181. /* -----exported algorithm data: ------------------------------------- */
  182. static struct i2c_algorithm i2c_usb_algo = {
  183. .master_xfer = usb_xfer,
  184. .smbus_xfer = NULL,
  185. .slave_send = NULL,
  186. .slave_recv = NULL,
  187. .algo_control = algo_control,
  188. .functionality = usb_func,
  189. };
  190. /*
  191. * registering functions to load algorithms at runtime
  192. */
  193. int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
  194. {
  195. /* register new adapter to i2c module... */
  196. adap->algo = &i2c_usb_algo;
  197. adap->timeout = 100; /* default values, should */
  198. adap->retries = 3; /* be replaced by defines */
  199. #ifdef MODULE
  200. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 21)
  201. MOD_INC_USE_COUNT;
  202. #endif
  203. #endif
  204. i2c_add_adapter(adap);
  205. if (debug_i2c_usb) {
  206. info("i2c bus for %s registered", adap->name);
  207. }
  208. return 0;
  209. }
  210. int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap)
  211. {
  212. i2c_del_adapter(adap);
  213. if (debug_i2c_usb) {
  214. info("i2c bus for %s unregistered", adap->name);
  215. }
  216. #ifdef MODULE
  217. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 21)
  218. MOD_DEC_USE_COUNT;
  219. #endif
  220. #endif
  221. return 0;
  222. }
  223. EXPORT_SYMBOL(usbvision_i2c_usb_add_bus);
  224. EXPORT_SYMBOL(usbvision_i2c_usb_del_bus);