w1.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * w1.h
  3. *
  4. * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #ifndef __W1_H
  22. #define __W1_H
  23. struct w1_reg_num
  24. {
  25. #if defined(__LITTLE_ENDIAN_BITFIELD)
  26. __u64 family:8,
  27. id:48,
  28. crc:8;
  29. #elif defined(__BIG_ENDIAN_BITFIELD)
  30. __u64 crc:8,
  31. id:48,
  32. family:8;
  33. #else
  34. #error "Please fix <asm/byteorder.h>"
  35. #endif
  36. };
  37. #ifdef __KERNEL__
  38. #include <linux/completion.h>
  39. #include <linux/device.h>
  40. #include <net/sock.h>
  41. #include <asm/semaphore.h>
  42. #include "w1_family.h"
  43. #define W1_MAXNAMELEN 32
  44. #define W1_SLAVE_DATA_SIZE 128
  45. #define W1_SEARCH 0xF0
  46. #define W1_CONDITIONAL_SEARCH 0xEC
  47. #define W1_CONVERT_TEMP 0x44
  48. #define W1_SKIP_ROM 0xCC
  49. #define W1_READ_SCRATCHPAD 0xBE
  50. #define W1_READ_ROM 0x33
  51. #define W1_READ_PSUPPLY 0xB4
  52. #define W1_MATCH_ROM 0x55
  53. #define W1_SLAVE_ACTIVE (1<<0)
  54. struct w1_slave
  55. {
  56. struct module *owner;
  57. unsigned char name[W1_MAXNAMELEN];
  58. struct list_head w1_slave_entry;
  59. struct w1_reg_num reg_num;
  60. atomic_t refcnt;
  61. u8 rom[9];
  62. u32 flags;
  63. int ttl;
  64. struct w1_master *master;
  65. struct w1_family *family;
  66. void *family_data;
  67. struct device dev;
  68. struct completion released;
  69. };
  70. typedef void (* w1_slave_found_callback)(void *, u64);
  71. /**
  72. * Note: read_bit and write_bit are very low level functions and should only
  73. * be used with hardware that doesn't really support 1-wire operations,
  74. * like a parallel/serial port.
  75. * Either define read_bit and write_bit OR define, at minimum, touch_bit and
  76. * reset_bus.
  77. */
  78. struct w1_bus_master
  79. {
  80. /** the first parameter in all the functions below */
  81. void *data;
  82. /**
  83. * Sample the line level
  84. * @return the level read (0 or 1)
  85. */
  86. u8 (*read_bit)(void *);
  87. /** Sets the line level */
  88. void (*write_bit)(void *, u8);
  89. /**
  90. * touch_bit is the lowest-level function for devices that really
  91. * support the 1-wire protocol.
  92. * touch_bit(0) = write-0 cycle
  93. * touch_bit(1) = write-1 / read cycle
  94. * @return the bit read (0 or 1)
  95. */
  96. u8 (*touch_bit)(void *, u8);
  97. /**
  98. * Reads a bytes. Same as 8 touch_bit(1) calls.
  99. * @return the byte read
  100. */
  101. u8 (*read_byte)(void *);
  102. /**
  103. * Writes a byte. Same as 8 touch_bit(x) calls.
  104. */
  105. void (*write_byte)(void *, u8);
  106. /**
  107. * Same as a series of read_byte() calls
  108. * @return the number of bytes read
  109. */
  110. u8 (*read_block)(void *, u8 *, int);
  111. /** Same as a series of write_byte() calls */
  112. void (*write_block)(void *, const u8 *, int);
  113. /**
  114. * Combines two reads and a smart write for ROM searches
  115. * @return bit0=Id bit1=comp_id bit2=dir_taken
  116. */
  117. u8 (*triplet)(void *, u8);
  118. /**
  119. * long write-0 with a read for the presence pulse detection
  120. * @return -1=Error, 0=Device present, 1=No device present
  121. */
  122. u8 (*reset_bus)(void *);
  123. /** Really nice hardware can handles the ROM searches */
  124. void (*search)(void *, w1_slave_found_callback);
  125. };
  126. #define W1_MASTER_NEED_EXIT 0
  127. #define W1_MASTER_NEED_RECONNECT 1
  128. struct w1_master
  129. {
  130. struct list_head w1_master_entry;
  131. struct module *owner;
  132. unsigned char name[W1_MAXNAMELEN];
  133. struct list_head slist;
  134. int max_slave_count, slave_count;
  135. unsigned long attempts;
  136. int slave_ttl;
  137. int initialized;
  138. u32 id;
  139. int search_count;
  140. atomic_t refcnt;
  141. void *priv;
  142. int priv_size;
  143. long flags;
  144. struct task_struct *thread;
  145. struct semaphore mutex;
  146. struct device_driver *driver;
  147. struct device dev;
  148. struct w1_bus_master *bus_master;
  149. u32 seq, groups;
  150. struct sock *nls;
  151. };
  152. int w1_create_master_attributes(struct w1_master *);
  153. void w1_search(struct w1_master *dev, w1_slave_found_callback cb);
  154. static inline struct w1_slave* dev_to_w1_slave(struct device *dev)
  155. {
  156. return container_of(dev, struct w1_slave, dev);
  157. }
  158. static inline struct w1_slave* kobj_to_w1_slave(struct kobject *kobj)
  159. {
  160. return dev_to_w1_slave(container_of(kobj, struct device, kobj));
  161. }
  162. static inline struct w1_master* dev_to_w1_master(struct device *dev)
  163. {
  164. return container_of(dev, struct w1_master, dev);
  165. }
  166. extern int w1_max_slave_count;
  167. extern int w1_max_slave_ttl;
  168. extern spinlock_t w1_mlock;
  169. extern struct list_head w1_masters;
  170. extern struct device_driver w1_master_driver;
  171. extern struct device w1_master_device;
  172. int w1_process(void *data);
  173. void w1_reconnect_slaves(struct w1_family *f);
  174. #endif /* __KERNEL__ */
  175. #endif /* __W1_H */