w1_smem.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * w1_smem.c
  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 smems 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. #include <asm/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/device.h>
  26. #include <linux/types.h>
  27. #include "w1.h"
  28. #include "w1_io.h"
  29. #include "w1_int.h"
  30. #include "w1_family.h"
  31. MODULE_LICENSE("GPL");
  32. MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  33. MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, 64bit memory family.");
  34. static ssize_t w1_smem_read_name(struct device *, struct device_attribute *attr, char *);
  35. static ssize_t w1_smem_read_val(struct device *, struct device_attribute *attr, char *);
  36. static ssize_t w1_smem_read_bin(struct kobject *, char *, loff_t, size_t);
  37. static struct w1_family_ops w1_smem_fops = {
  38. .rname = &w1_smem_read_name,
  39. .rbin = &w1_smem_read_bin,
  40. .rval = &w1_smem_read_val,
  41. .rvalname = "id",
  42. };
  43. static ssize_t w1_smem_read_name(struct device *dev, struct device_attribute *attr, char *buf)
  44. {
  45. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  46. return sprintf(buf, "%s\n", sl->name);
  47. }
  48. static ssize_t w1_smem_read_val(struct device *dev, struct device_attribute *attr, char *buf)
  49. {
  50. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  51. int i;
  52. ssize_t count = 0;
  53. for (i = 0; i < 8; ++i)
  54. count += sprintf(buf + count, "%02x ", ((u8 *)&sl->reg_num)[i]);
  55. count += sprintf(buf + count, "\n");
  56. return count;
  57. }
  58. static ssize_t w1_smem_read_bin(struct kobject *kobj, char *buf, loff_t off, size_t count)
  59. {
  60. struct w1_slave *sl = container_of(container_of(kobj, struct device, kobj),
  61. struct w1_slave, dev);
  62. int i;
  63. atomic_inc(&sl->refcnt);
  64. if (down_interruptible(&sl->master->mutex)) {
  65. count = 0;
  66. goto out_dec;
  67. }
  68. if (off > W1_SLAVE_DATA_SIZE) {
  69. count = 0;
  70. goto out;
  71. }
  72. if (off + count > W1_SLAVE_DATA_SIZE) {
  73. count = 0;
  74. goto out;
  75. }
  76. for (i = 0; i < 8; ++i)
  77. count += sprintf(buf + count, "%02x ", ((u8 *)&sl->reg_num)[i]);
  78. count += sprintf(buf + count, "\n");
  79. out:
  80. up(&sl->master->mutex);
  81. out_dec:
  82. atomic_dec(&sl->refcnt);
  83. return count;
  84. }
  85. static struct w1_family w1_smem_family_01 = {
  86. .fid = W1_FAMILY_SMEM_01,
  87. .fops = &w1_smem_fops,
  88. };
  89. static struct w1_family w1_smem_family_81 = {
  90. .fid = W1_FAMILY_SMEM_81,
  91. .fops = &w1_smem_fops,
  92. };
  93. static int __init w1_smem_init(void)
  94. {
  95. int err;
  96. err = w1_register_family(&w1_smem_family_01);
  97. if (err)
  98. return err;
  99. err = w1_register_family(&w1_smem_family_81);
  100. if (err) {
  101. w1_unregister_family(&w1_smem_family_01);
  102. return err;
  103. }
  104. return 0;
  105. }
  106. static void __exit w1_smem_fini(void)
  107. {
  108. w1_unregister_family(&w1_smem_family_01);
  109. w1_unregister_family(&w1_smem_family_81);
  110. }
  111. module_init(w1_smem_init);
  112. module_exit(w1_smem_fini);