w1_ds2433.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * w1_ds2433.c - w1 family 23 (DS2433) driver
  3. *
  4. * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the smems of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/device.h>
  14. #include <linux/types.h>
  15. #include <linux/delay.h>
  16. #include "w1.h"
  17. #include "w1_io.h"
  18. #include "w1_int.h"
  19. #include "w1_family.h"
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  22. MODULE_DESCRIPTION("w1 family 23 driver for DS2433, 4kb EEPROM");
  23. #define W1_EEPROM_SIZE 512
  24. #define W1_PAGE_SIZE 32
  25. #define W1_PAGE_BITS 5
  26. #define W1_PAGE_MASK 0x1F
  27. #define W1_F23_READ_EEPROM 0xF0
  28. #define W1_F23_WRITE_SCRATCH 0x0F
  29. #define W1_F23_READ_SCRATCH 0xAA
  30. #define W1_F23_COPY_SCRATCH 0x55
  31. /**
  32. * Check the file size bounds and adjusts count as needed.
  33. * This may not be needed if the sysfs layer checks bounds.
  34. */
  35. static inline size_t w1_f23_fix_count(loff_t off, size_t count, size_t size)
  36. {
  37. if (off > size)
  38. return 0;
  39. if ((off + count) > size)
  40. return (size - off);
  41. return count;
  42. }
  43. static ssize_t w1_f23_read_bin(struct kobject *kobj, char *buf, loff_t off, size_t count)
  44. {
  45. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  46. u8 wrbuf[3];
  47. if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
  48. return 0;
  49. atomic_inc(&sl->refcnt);
  50. if (down_interruptible(&sl->master->mutex)) {
  51. count = 0;
  52. goto out_dec;
  53. }
  54. /* read directly from the EEPROM */
  55. if (w1_reset_select_slave(sl)) {
  56. count = -EIO;
  57. goto out_up;
  58. }
  59. wrbuf[0] = W1_F23_READ_EEPROM;
  60. wrbuf[1] = off & 0xff;
  61. wrbuf[2] = off >> 8;
  62. w1_write_block(sl->master, wrbuf, 3);
  63. w1_read_block(sl->master, buf, count);
  64. out_up:
  65. up(&sl->master->mutex);
  66. out_dec:
  67. atomic_dec(&sl->refcnt);
  68. return count;
  69. }
  70. /**
  71. * Writes to the scratchpad and reads it back for verification.
  72. * The master must be locked.
  73. *
  74. * @param sl The slave structure
  75. * @param addr Address for the write
  76. * @param len length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK))
  77. * @param data The data to write
  78. * @return 0=Success -1=failure
  79. */
  80. static int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data)
  81. {
  82. u8 wrbuf[4];
  83. u8 rdbuf[W1_PAGE_SIZE + 3];
  84. u8 es = (addr + len - 1) & 0x1f;
  85. /* Write the data to the scratchpad */
  86. if (w1_reset_select_slave(sl))
  87. return -1;
  88. wrbuf[0] = W1_F23_WRITE_SCRATCH;
  89. wrbuf[1] = addr & 0xff;
  90. wrbuf[2] = addr >> 8;
  91. w1_write_block(sl->master, wrbuf, 3);
  92. w1_write_block(sl->master, data, len);
  93. /* Read the scratchpad and verify */
  94. if (w1_reset_select_slave(sl))
  95. return -1;
  96. w1_write_8(sl->master, W1_F23_READ_SCRATCH);
  97. w1_read_block(sl->master, rdbuf, len + 3);
  98. /* Compare what was read against the data written */
  99. if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
  100. (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0))
  101. return -1;
  102. /* Copy the scratchpad to EEPROM */
  103. if (w1_reset_select_slave(sl))
  104. return -1;
  105. wrbuf[0] = W1_F23_COPY_SCRATCH;
  106. wrbuf[3] = es;
  107. w1_write_block(sl->master, wrbuf, 4);
  108. /* Sleep for 5 ms to wait for the write to complete */
  109. msleep(5);
  110. /* Reset the bus to wake up the EEPROM (this may not be needed) */
  111. w1_reset_bus(sl->master);
  112. return 0;
  113. }
  114. static ssize_t w1_f23_write_bin(struct kobject *kobj, char *buf, loff_t off,
  115. size_t count)
  116. {
  117. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  118. int addr, len, idx;
  119. if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
  120. return 0;
  121. atomic_inc(&sl->refcnt);
  122. if (down_interruptible(&sl->master->mutex)) {
  123. count = 0;
  124. goto out_dec;
  125. }
  126. /* Can only write data to one page at a time */
  127. idx = 0;
  128. while (idx < count) {
  129. addr = off + idx;
  130. len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
  131. if (len > (count - idx))
  132. len = count - idx;
  133. if (w1_f23_write(sl, addr, len, &buf[idx]) < 0) {
  134. count = -EIO;
  135. goto out_up;
  136. }
  137. idx += len;
  138. }
  139. out_up:
  140. up(&sl->master->mutex);
  141. out_dec:
  142. atomic_dec(&sl->refcnt);
  143. return count;
  144. }
  145. static struct bin_attribute w1_f23_bin_attr = {
  146. .attr = {
  147. .name = "eeprom",
  148. .mode = S_IRUGO | S_IWUSR,
  149. .owner = THIS_MODULE,
  150. },
  151. .size = W1_EEPROM_SIZE,
  152. .read = w1_f23_read_bin,
  153. .write = w1_f23_write_bin,
  154. };
  155. static int w1_f23_add_slave(struct w1_slave *sl)
  156. {
  157. return sysfs_create_bin_file(&sl->dev.kobj, &w1_f23_bin_attr);
  158. }
  159. static void w1_f23_remove_slave(struct w1_slave *sl)
  160. {
  161. sysfs_remove_bin_file(&sl->dev.kobj, &w1_f23_bin_attr);
  162. }
  163. static struct w1_family_ops w1_f23_fops = {
  164. .add_slave = w1_f23_add_slave,
  165. .remove_slave = w1_f23_remove_slave,
  166. };
  167. static struct w1_family w1_family_23 = {
  168. .fid = W1_EEPROM_DS2433,
  169. .fops = &w1_f23_fops,
  170. };
  171. static int __init w1_f23_init(void)
  172. {
  173. return w1_register_family(&w1_family_23);
  174. }
  175. static void __exit w1_f23_fini(void)
  176. {
  177. w1_unregister_family(&w1_family_23);
  178. }
  179. module_init(w1_f23_init);
  180. module_exit(w1_f23_fini);