w1.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
  4. */
  5. #ifndef __LINUX_W1_H
  6. #define __LINUX_W1_H
  7. #include <linux/device.h>
  8. /**
  9. * struct w1_reg_num - broken out slave device id
  10. *
  11. * @family: identifies the type of device
  12. * @id: along with family is the unique device id
  13. * @crc: checksum of the other bytes
  14. */
  15. struct w1_reg_num {
  16. #if defined(__LITTLE_ENDIAN_BITFIELD)
  17. __u64 family:8,
  18. id:48,
  19. crc:8;
  20. #elif defined(__BIG_ENDIAN_BITFIELD)
  21. __u64 crc:8,
  22. id:48,
  23. family:8;
  24. #else
  25. #error "Please fix <asm/byteorder.h>"
  26. #endif
  27. };
  28. #ifdef __KERNEL__
  29. #define W1_MAXNAMELEN 32
  30. #define W1_SEARCH 0xF0
  31. #define W1_ALARM_SEARCH 0xEC
  32. #define W1_CONVERT_TEMP 0x44
  33. #define W1_SKIP_ROM 0xCC
  34. #define W1_COPY_SCRATCHPAD 0x48
  35. #define W1_WRITE_SCRATCHPAD 0x4E
  36. #define W1_READ_SCRATCHPAD 0xBE
  37. #define W1_READ_ROM 0x33
  38. #define W1_READ_PSUPPLY 0xB4
  39. #define W1_MATCH_ROM 0x55
  40. #define W1_RESUME_CMD 0xA5
  41. /**
  42. * struct w1_slave - holds a single slave device on the bus
  43. *
  44. * @owner: Points to the one wire "wire" kernel module.
  45. * @name: Device id is ascii.
  46. * @w1_slave_entry: data for the linked list
  47. * @reg_num: the slave id in binary
  48. * @refcnt: reference count, delete when 0
  49. * @flags: bit flags for W1_SLAVE_ACTIVE W1_SLAVE_DETACH
  50. * @ttl: decrement per search this slave isn't found, deatch at 0
  51. * @master: bus which this slave is on
  52. * @family: module for device family type
  53. * @family_data: pointer for use by the family module
  54. * @dev: kernel device identifier
  55. * @hwmon: pointer to hwmon device
  56. *
  57. */
  58. struct w1_slave {
  59. struct module *owner;
  60. unsigned char name[W1_MAXNAMELEN];
  61. struct list_head w1_slave_entry;
  62. struct w1_reg_num reg_num;
  63. atomic_t refcnt;
  64. int ttl;
  65. unsigned long flags;
  66. struct w1_master *master;
  67. struct w1_family *family;
  68. void *family_data;
  69. struct device dev;
  70. struct device *hwmon;
  71. };
  72. typedef void (*w1_slave_found_callback)(struct w1_master *, u64);
  73. /**
  74. * struct w1_bus_master - operations available on a bus master
  75. *
  76. * @data: the first parameter in all the functions below
  77. *
  78. * @read_bit: Sample the line level @return the level read (0 or 1)
  79. *
  80. * @write_bit: Sets the line level
  81. *
  82. * @touch_bit: the lowest-level function for devices that really support the
  83. * 1-wire protocol.
  84. * touch_bit(0) = write-0 cycle
  85. * touch_bit(1) = write-1 / read cycle
  86. * @return the bit read (0 or 1)
  87. *
  88. * @read_byte: Reads a bytes. Same as 8 touch_bit(1) calls.
  89. * @return the byte read
  90. *
  91. * @write_byte: Writes a byte. Same as 8 touch_bit(x) calls.
  92. *
  93. * @read_block: Same as a series of read_byte() calls
  94. * @return the number of bytes read
  95. *
  96. * @write_block: Same as a series of write_byte() calls
  97. *
  98. * @triplet: Combines two reads and a smart write for ROM searches
  99. * @return bit0=Id bit1=comp_id bit2=dir_taken
  100. *
  101. * @reset_bus: long write-0 with a read for the presence pulse detection
  102. * @return -1=Error, 0=Device present, 1=No device present
  103. *
  104. * @set_pullup: Put out a strong pull-up pulse of the specified duration.
  105. * @return -1=Error, 0=completed
  106. *
  107. * @search: Really nice hardware can handles the different types of ROM search
  108. * w1_master* is passed to the slave found callback.
  109. * u8 is search_type, W1_SEARCH or W1_ALARM_SEARCH
  110. *
  111. * @dev_id: Optional device id string, which w1 slaves could use for
  112. * creating names, which then give a connection to the w1 master
  113. *
  114. * Note: read_bit and write_bit are very low level functions and should only
  115. * be used with hardware that doesn't really support 1-wire operations,
  116. * like a parallel/serial port.
  117. * Either define read_bit and write_bit OR define, at minimum, touch_bit and
  118. * reset_bus.
  119. *
  120. */
  121. struct w1_bus_master {
  122. void *data;
  123. u8 (*read_bit)(void *);
  124. void (*write_bit)(void *, u8);
  125. u8 (*touch_bit)(void *, u8);
  126. u8 (*read_byte)(void *);
  127. void (*write_byte)(void *, u8);
  128. u8 (*read_block)(void *, u8 *, int);
  129. void (*write_block)(void *, const u8 *, int);
  130. u8 (*triplet)(void *, u8);
  131. u8 (*reset_bus)(void *);
  132. u8 (*set_pullup)(void *, int);
  133. void (*search)(void *, struct w1_master *,
  134. u8, w1_slave_found_callback);
  135. char *dev_id;
  136. };
  137. /**
  138. * enum w1_master_flags - bitfields used in w1_master.flags
  139. * @W1_ABORT_SEARCH: abort searching early on shutdown
  140. * @W1_WARN_MAX_COUNT: limit warning when the maximum count is reached
  141. */
  142. enum w1_master_flags {
  143. W1_ABORT_SEARCH = 0,
  144. W1_WARN_MAX_COUNT = 1,
  145. };
  146. /**
  147. * struct w1_master - one per bus master
  148. * @w1_master_entry: master linked list
  149. * @owner: module owner
  150. * @name: dynamically allocate bus name
  151. * @list_mutex: protect slist and async_list
  152. * @slist: linked list of slaves
  153. * @async_list: linked list of netlink commands to execute
  154. * @max_slave_count: maximum number of slaves to search for at a time
  155. * @slave_count: current number of slaves known
  156. * @attempts: number of searches ran
  157. * @slave_ttl: number of searches before a slave is timed out
  158. * @initialized: prevent init/removal race conditions
  159. * @id: w1 bus number
  160. * @search_count: number of automatic searches to run, -1 unlimited
  161. * @search_id: allows continuing a search
  162. * @refcnt: reference count
  163. * @priv: private data storage
  164. * @enable_pullup: allows a strong pullup
  165. * @pullup_duration: time for the next strong pullup
  166. * @flags: one of w1_master_flags
  167. * @thread: thread for bus search and netlink commands
  168. * @mutex: protect most of w1_master
  169. * @bus_mutex: pretect concurrent bus access
  170. * @driver: sysfs driver
  171. * @dev: sysfs device
  172. * @bus_master: io operations available
  173. * @seq: sequence number used for netlink broadcasts
  174. */
  175. struct w1_master {
  176. struct list_head w1_master_entry;
  177. struct module *owner;
  178. unsigned char name[W1_MAXNAMELEN];
  179. /* list_mutex protects just slist and async_list so slaves can be
  180. * searched for and async commands added while the master has
  181. * w1_master.mutex locked and is operating on the bus.
  182. * lock order w1_mlock, w1_master.mutex, w1_master.list_mutex
  183. */
  184. struct mutex list_mutex;
  185. struct list_head slist;
  186. struct list_head async_list;
  187. int max_slave_count, slave_count;
  188. unsigned long attempts;
  189. int slave_ttl;
  190. int initialized;
  191. u32 id;
  192. int search_count;
  193. /* id to start searching on, to continue a search or 0 to restart */
  194. u64 search_id;
  195. atomic_t refcnt;
  196. void *priv;
  197. /** 5V strong pullup enabled flag, 1 enabled, zero disabled. */
  198. int enable_pullup;
  199. /** 5V strong pullup duration in milliseconds, zero disabled. */
  200. int pullup_duration;
  201. long flags;
  202. struct task_struct *thread;
  203. struct mutex mutex;
  204. struct mutex bus_mutex;
  205. struct device_driver *driver;
  206. struct device dev;
  207. struct w1_bus_master *bus_master;
  208. u32 seq;
  209. };
  210. int w1_add_master_device(struct w1_bus_master *master);
  211. void w1_remove_master_device(struct w1_bus_master *master);
  212. /**
  213. * struct w1_family_ops - operations for a family type
  214. * @add_slave: add_slave
  215. * @remove_slave: remove_slave
  216. * @groups: sysfs group
  217. * @chip_info: pointer to struct hwmon_chip_info
  218. */
  219. struct w1_family_ops {
  220. int (*add_slave)(struct w1_slave *sl);
  221. void (*remove_slave)(struct w1_slave *sl);
  222. const struct attribute_group **groups;
  223. const struct hwmon_chip_info *chip_info;
  224. };
  225. /**
  226. * struct w1_family - reference counted family structure.
  227. * @family_entry: family linked list
  228. * @fid: 8 bit family identifier
  229. * @fops: operations for this family
  230. * @of_match_table: open firmware match table
  231. * @refcnt: reference counter
  232. */
  233. struct w1_family {
  234. struct list_head family_entry;
  235. u8 fid;
  236. const struct w1_family_ops *fops;
  237. const struct of_device_id *of_match_table;
  238. atomic_t refcnt;
  239. };
  240. int w1_register_family(struct w1_family *family);
  241. void w1_unregister_family(struct w1_family *family);
  242. /**
  243. * module_w1_driver() - Helper macro for registering a 1-Wire families
  244. * @__w1_family: w1_family struct
  245. *
  246. * Helper macro for 1-Wire families which do not do anything special in module
  247. * init/exit. This eliminates a lot of boilerplate. Each module may only
  248. * use this macro once, and calling it replaces module_init() and module_exit()
  249. */
  250. #define module_w1_family(__w1_family) \
  251. module_driver(__w1_family, w1_register_family, \
  252. w1_unregister_family)
  253. u8 w1_triplet(struct w1_master *dev, int bdir);
  254. u8 w1_touch_bit(struct w1_master *dev, int bit);
  255. void w1_write_8(struct w1_master *, u8);
  256. u8 w1_read_8(struct w1_master *);
  257. int w1_reset_bus(struct w1_master *);
  258. u8 w1_calc_crc8(u8 *, int);
  259. void w1_write_block(struct w1_master *, const u8 *, int);
  260. void w1_touch_block(struct w1_master *, u8 *, int);
  261. u8 w1_read_block(struct w1_master *, u8 *, int);
  262. int w1_reset_select_slave(struct w1_slave *sl);
  263. int w1_reset_resume_command(struct w1_master *);
  264. void w1_next_pullup(struct w1_master *, int);
  265. static inline struct w1_slave* dev_to_w1_slave(struct device *dev)
  266. {
  267. return container_of(dev, struct w1_slave, dev);
  268. }
  269. static inline struct w1_slave* kobj_to_w1_slave(struct kobject *kobj)
  270. {
  271. return dev_to_w1_slave(container_of(kobj, struct device, kobj));
  272. }
  273. static inline struct w1_master* dev_to_w1_master(struct device *dev)
  274. {
  275. return container_of(dev, struct w1_master, dev);
  276. }
  277. #endif /* __KERNEL__ */
  278. #endif /* __LINUX_W1_H */