usbvision-i2c.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 = 0;
  40. #if defined(module_param) // Showing parameters under SYSFS
  41. module_param (debug, int, 0444); // debug mode of the device driver
  42. #else
  43. MODULE_PARM(debug, "i"); // debug mode of the device driver
  44. #endif
  45. MODULE_AUTHOR("Joerg Heckenbach");
  46. MODULE_DESCRIPTION("I2C algorithm for USB-I2C-bridges");
  47. MODULE_LICENSE("GPL");
  48. static inline int try_write_address(struct i2c_adapter *i2c_adap,
  49. unsigned char addr, int retries)
  50. {
  51. struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
  52. void *data;
  53. int i, ret = -1;
  54. char buf[4];
  55. data = i2c_get_adapdata(i2c_adap);
  56. buf[0] = 0x00;
  57. for (i = 0; i <= retries; i++) {
  58. ret = (adap->outb(data, addr, buf, 1));
  59. if (ret == 1)
  60. break; /* success! */
  61. udelay(5 /*adap->udelay */ );
  62. if (i == retries) /* no success */
  63. break;
  64. udelay(adap->udelay);
  65. }
  66. if (debug) {
  67. if (i) {
  68. info("%s: Needed %d retries for address %#2x", __FUNCTION__, i, addr);
  69. info("%s: Maybe there's no device at this address", __FUNCTION__);
  70. }
  71. }
  72. return ret;
  73. }
  74. static inline int try_read_address(struct i2c_adapter *i2c_adap,
  75. unsigned char addr, int retries)
  76. {
  77. struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
  78. void *data;
  79. int i, ret = -1;
  80. char buf[4];
  81. data = i2c_get_adapdata(i2c_adap);
  82. for (i = 0; i <= retries; i++) {
  83. ret = (adap->inb(data, addr, buf, 1));
  84. if (ret == 1)
  85. break; /* success! */
  86. udelay(5 /*adap->udelay */ );
  87. if (i == retries) /* no success */
  88. break;
  89. udelay(adap->udelay);
  90. }
  91. if (debug) {
  92. if (i) {
  93. info("%s: Needed %d retries for address %#2x", __FUNCTION__, i, addr);
  94. info("%s: Maybe there's no device at this address", __FUNCTION__);
  95. }
  96. }
  97. return ret;
  98. }
  99. static inline int usb_find_address(struct i2c_adapter *i2c_adap,
  100. struct i2c_msg *msg, int retries,
  101. unsigned char *add)
  102. {
  103. unsigned short flags = msg->flags;
  104. unsigned char addr;
  105. int ret;
  106. if ((flags & I2C_M_TEN)) {
  107. /* a ten bit address */
  108. addr = 0xf0 | ((msg->addr >> 7) & 0x03);
  109. /* try extended address code... */
  110. ret = try_write_address(i2c_adap, addr, retries);
  111. if (ret != 1) {
  112. err("died at extended address code, while writing");
  113. return -EREMOTEIO;
  114. }
  115. add[0] = addr;
  116. if (flags & I2C_M_RD) {
  117. /* okay, now switch into reading mode */
  118. addr |= 0x01;
  119. ret = try_read_address(i2c_adap, addr, retries);
  120. if (ret != 1) {
  121. err("died at extended address code, while reading");
  122. return -EREMOTEIO;
  123. }
  124. }
  125. } else { /* normal 7bit address */
  126. addr = (msg->addr << 1);
  127. if (flags & I2C_M_RD)
  128. addr |= 1;
  129. if (flags & I2C_M_REV_DIR_ADDR)
  130. addr ^= 1;
  131. add[0] = addr;
  132. if (flags & I2C_M_RD)
  133. ret = try_read_address(i2c_adap, addr, retries);
  134. else
  135. ret = try_write_address(i2c_adap, addr, retries);
  136. if (ret != 1) {
  137. return -EREMOTEIO;
  138. }
  139. }
  140. return 0;
  141. }
  142. static int
  143. usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  144. {
  145. struct i2c_msg *pmsg;
  146. struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
  147. void *data;
  148. int i, ret;
  149. unsigned char addr;
  150. data = i2c_get_adapdata(i2c_adap);
  151. for (i = 0; i < num; i++) {
  152. pmsg = &msgs[i];
  153. ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
  154. if (ret != 0) {
  155. if (debug) {
  156. info("%s: got NAK from device, message #%d\n", __FUNCTION__, i);
  157. }
  158. return (ret < 0) ? ret : -EREMOTEIO;
  159. }
  160. if (pmsg->flags & I2C_M_RD) {
  161. /* read bytes into buffer */
  162. ret = (adap->inb(data, addr, pmsg->buf, pmsg->len));
  163. if (ret < pmsg->len) {
  164. return (ret < 0) ? ret : -EREMOTEIO;
  165. }
  166. } else {
  167. /* write bytes from buffer */
  168. ret = (adap->outb(data, addr, pmsg->buf, pmsg->len));
  169. if (ret < pmsg->len) {
  170. return (ret < 0) ? ret : -EREMOTEIO;
  171. }
  172. }
  173. }
  174. return num;
  175. }
  176. static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg)
  177. {
  178. return 0;
  179. }
  180. static u32 usb_func(struct i2c_adapter *adap)
  181. {
  182. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  183. }
  184. /* -----exported algorithm data: ------------------------------------- */
  185. static struct i2c_algorithm i2c_usb_algo = {
  186. .master_xfer = usb_xfer,
  187. .smbus_xfer = NULL,
  188. .slave_send = NULL,
  189. .slave_recv = NULL,
  190. .algo_control = algo_control,
  191. .functionality = usb_func,
  192. };
  193. /*
  194. * registering functions to load algorithms at runtime
  195. */
  196. int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
  197. {
  198. /* register new adapter to i2c module... */
  199. adap->algo = &i2c_usb_algo;
  200. adap->timeout = 100; /* default values, should */
  201. adap->retries = 3; /* be replaced by defines */
  202. #ifdef MODULE
  203. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 21)
  204. MOD_INC_USE_COUNT;
  205. #endif
  206. #endif
  207. i2c_add_adapter(adap);
  208. if (debug) {
  209. info("i2c bus for %s registered", adap->name);
  210. }
  211. return 0;
  212. }
  213. int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap)
  214. {
  215. i2c_del_adapter(adap);
  216. if (debug) {
  217. info("i2c bus for %s unregistered", adap->name);
  218. }
  219. #ifdef MODULE
  220. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 21)
  221. MOD_DEC_USE_COUNT;
  222. #endif
  223. #endif
  224. return 0;
  225. }
  226. EXPORT_SYMBOL(usbvision_i2c_usb_add_bus);
  227. EXPORT_SYMBOL(usbvision_i2c_usb_del_bus);