w1.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. *
  65. */
  66. struct w1_slave {
  67. struct module *owner;
  68. unsigned char name[W1_MAXNAMELEN];
  69. struct list_head w1_slave_entry;
  70. struct w1_reg_num reg_num;
  71. atomic_t refcnt;
  72. int ttl;
  73. unsigned long flags;
  74. struct w1_master *master;
  75. struct w1_family *family;
  76. void *family_data;
  77. struct device dev;
  78. };
  79. typedef void (*w1_slave_found_callback)(struct w1_master *, u64);
  80. /**
  81. * struct w1_bus_master - operations available on a bus master
  82. *
  83. * @data: the first parameter in all the functions below
  84. *
  85. * @read_bit: Sample the line level @return the level read (0 or 1)
  86. *
  87. * @write_bit: Sets the line level
  88. *
  89. * @touch_bit: the lowest-level function for devices that really support the
  90. * 1-wire protocol.
  91. * touch_bit(0) = write-0 cycle
  92. * touch_bit(1) = write-1 / read cycle
  93. * @return the bit read (0 or 1)
  94. *
  95. * @read_byte: Reads a bytes. Same as 8 touch_bit(1) calls.
  96. * @return the byte read
  97. *
  98. * @write_byte: Writes a byte. Same as 8 touch_bit(x) calls.
  99. *
  100. * @read_block: Same as a series of read_byte() calls
  101. * @return the number of bytes read
  102. *
  103. * @write_block: Same as a series of write_byte() calls
  104. *
  105. * @triplet: Combines two reads and a smart write for ROM searches
  106. * @return bit0=Id bit1=comp_id bit2=dir_taken
  107. *
  108. * @reset_bus: long write-0 with a read for the presence pulse detection
  109. * @return -1=Error, 0=Device present, 1=No device present
  110. *
  111. * @set_pullup: Put out a strong pull-up pulse of the specified duration.
  112. * @return -1=Error, 0=completed
  113. *
  114. * @search: Really nice hardware can handles the different types of ROM search
  115. * w1_master* is passed to the slave found callback.
  116. * u8 is search_type, W1_SEARCH or W1_ALARM_SEARCH
  117. *
  118. * Note: read_bit and write_bit are very low level functions and should only
  119. * be used with hardware that doesn't really support 1-wire operations,
  120. * like a parallel/serial port.
  121. * Either define read_bit and write_bit OR define, at minimum, touch_bit and
  122. * reset_bus.
  123. *
  124. */
  125. struct w1_bus_master {
  126. void *data;
  127. u8 (*read_bit)(void *);
  128. void (*write_bit)(void *, u8);
  129. u8 (*touch_bit)(void *, u8);
  130. u8 (*read_byte)(void *);
  131. void (*write_byte)(void *, u8);
  132. u8 (*read_block)(void *, u8 *, int);
  133. void (*write_block)(void *, const u8 *, int);
  134. u8 (*triplet)(void *, u8);
  135. u8 (*reset_bus)(void *);
  136. u8 (*set_pullup)(void *, int);
  137. void (*search)(void *, struct w1_master *,
  138. u8, w1_slave_found_callback);
  139. };
  140. /**
  141. * enum w1_master_flags - bitfields used in w1_master.flags
  142. * @W1_ABORT_SEARCH: abort searching early on shutdown
  143. * @W1_WARN_MAX_COUNT: limit warning when the maximum count is reached
  144. */
  145. enum w1_master_flags {
  146. W1_ABORT_SEARCH = 0,
  147. W1_WARN_MAX_COUNT = 1,
  148. };
  149. /**
  150. * struct w1_master - one per bus master
  151. * @w1_master_entry: master linked list
  152. * @owner: module owner
  153. * @name: dynamically allocate bus name
  154. * @list_mutex: protect slist and async_list
  155. * @slist: linked list of slaves
  156. * @async_list: linked list of netlink commands to execute
  157. * @max_slave_count: maximum number of slaves to search for at a time
  158. * @slave_count: current number of slaves known
  159. * @attempts: number of searches ran
  160. * @slave_ttl: number of searches before a slave is timed out
  161. * @initialized: prevent init/removal race conditions
  162. * @id: w1 bus number
  163. * @search_count: number of automatic searches to run, -1 unlimited
  164. * @search_id: allows continuing a search
  165. * @refcnt: reference count
  166. * @priv: private data storage
  167. * @enable_pullup: allows a strong pullup
  168. * @pullup_duration: time for the next strong pullup
  169. * @flags: one of w1_master_flags
  170. * @thread: thread for bus search and netlink commands
  171. * @mutex: protect most of w1_master
  172. * @bus_mutex: pretect concurrent bus access
  173. * @driver: sysfs driver
  174. * @dev: sysfs device
  175. * @bus_master: io operations available
  176. * @seq: sequence number used for netlink broadcasts
  177. */
  178. struct w1_master {
  179. struct list_head w1_master_entry;
  180. struct module *owner;
  181. unsigned char name[W1_MAXNAMELEN];
  182. /* list_mutex protects just slist and async_list so slaves can be
  183. * searched for and async commands added while the master has
  184. * w1_master.mutex locked and is operating on the bus.
  185. * lock order w1_mlock, w1_master.mutex, w1_master.list_mutex
  186. */
  187. struct mutex list_mutex;
  188. struct list_head slist;
  189. struct list_head async_list;
  190. int max_slave_count, slave_count;
  191. unsigned long attempts;
  192. int slave_ttl;
  193. int initialized;
  194. u32 id;
  195. int search_count;
  196. /* id to start searching on, to continue a search or 0 to restart */
  197. u64 search_id;
  198. atomic_t refcnt;
  199. void *priv;
  200. /** 5V strong pullup enabled flag, 1 enabled, zero disabled. */
  201. int enable_pullup;
  202. /** 5V strong pullup duration in milliseconds, zero disabled. */
  203. int pullup_duration;
  204. long flags;
  205. struct task_struct *thread;
  206. struct mutex mutex;
  207. struct mutex bus_mutex;
  208. struct device_driver *driver;
  209. struct device dev;
  210. struct w1_bus_master *bus_master;
  211. u32 seq;
  212. };
  213. int w1_add_master_device(struct w1_bus_master *master);
  214. void w1_remove_master_device(struct w1_bus_master *master);
  215. /**
  216. * struct w1_family_ops - operations for a family type
  217. * @add_slave: add_slave
  218. * @remove_slave: remove_slave
  219. * @groups: sysfs group
  220. */
  221. struct w1_family_ops {
  222. int (*add_slave)(struct w1_slave *sl);
  223. void (*remove_slave)(struct w1_slave *sl);
  224. const struct attribute_group **groups;
  225. };
  226. /**
  227. * struct w1_family - reference counted family structure.
  228. * @family_entry: family linked list
  229. * @fid: 8 bit family identifier
  230. * @fops: operations for this family
  231. * @refcnt: reference counter
  232. */
  233. struct w1_family {
  234. struct list_head family_entry;
  235. u8 fid;
  236. struct w1_family_ops *fops;
  237. atomic_t refcnt;
  238. };
  239. int w1_register_family(struct w1_family *family);
  240. void w1_unregister_family(struct w1_family *family);
  241. /**
  242. * module_w1_driver() - Helper macro for registering a 1-Wire families
  243. * @__w1_family: w1_family struct
  244. *
  245. * Helper macro for 1-Wire families which do not do anything special in module
  246. * init/exit. This eliminates a lot of boilerplate. Each module may only
  247. * use this macro once, and calling it replaces module_init() and module_exit()
  248. */
  249. #define module_w1_family(__w1_family) \
  250. module_driver(__w1_family, w1_register_family, \
  251. w1_unregister_family)
  252. u8 w1_triplet(struct w1_master *dev, int bdir);
  253. void w1_write_8(struct w1_master *, u8);
  254. u8 w1_read_8(struct w1_master *);
  255. int w1_reset_bus(struct w1_master *);
  256. u8 w1_calc_crc8(u8 *, int);
  257. void w1_write_block(struct w1_master *, const u8 *, int);
  258. void w1_touch_block(struct w1_master *, u8 *, int);
  259. u8 w1_read_block(struct w1_master *, u8 *, int);
  260. int w1_reset_select_slave(struct w1_slave *sl);
  261. int w1_reset_resume_command(struct w1_master *);
  262. void w1_next_pullup(struct w1_master *, int);
  263. static inline struct w1_slave* dev_to_w1_slave(struct device *dev)
  264. {
  265. return container_of(dev, struct w1_slave, dev);
  266. }
  267. static inline struct w1_slave* kobj_to_w1_slave(struct kobject *kobj)
  268. {
  269. return dev_to_w1_slave(container_of(kobj, struct device, kobj));
  270. }
  271. static inline struct w1_master* dev_to_w1_master(struct device *dev)
  272. {
  273. return container_of(dev, struct w1_master, dev);
  274. }
  275. #endif /* __KERNEL__ */
  276. #endif /* __LINUX_W1_H */