w1.h 9.2 KB

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