w1_smem.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_bin(struct kobject *, char *, loff_t, size_t);
  36. static struct w1_family_ops w1_smem_fops = {
  37. .rname = &w1_smem_read_name,
  38. .rbin = &w1_smem_read_bin,
  39. };
  40. static ssize_t w1_smem_read_name(struct device *dev, struct device_attribute *attr, char *buf)
  41. {
  42. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  43. return sprintf(buf, "%s\n", sl->name);
  44. }
  45. static ssize_t w1_smem_read_bin(struct kobject *kobj, char *buf, loff_t off, size_t count)
  46. {
  47. struct w1_slave *sl = container_of(container_of(kobj, struct device, kobj),
  48. struct w1_slave, dev);
  49. int i;
  50. atomic_inc(&sl->refcnt);
  51. if (down_interruptible(&sl->master->mutex)) {
  52. count = 0;
  53. goto out_dec;
  54. }
  55. if (off > W1_SLAVE_DATA_SIZE) {
  56. count = 0;
  57. goto out;
  58. }
  59. if (off + count > W1_SLAVE_DATA_SIZE) {
  60. count = 0;
  61. goto out;
  62. }
  63. for (i = 0; i < 8; ++i)
  64. count += sprintf(buf + count, "%02x ", ((u8 *)&sl->reg_num)[i]);
  65. count += sprintf(buf + count, "\n");
  66. out:
  67. up(&sl->master->mutex);
  68. out_dec:
  69. atomic_dec(&sl->refcnt);
  70. return count;
  71. }
  72. static struct w1_family w1_smem_family_01 = {
  73. .fid = W1_FAMILY_SMEM_01,
  74. .fops = &w1_smem_fops,
  75. };
  76. static struct w1_family w1_smem_family_81 = {
  77. .fid = W1_FAMILY_SMEM_81,
  78. .fops = &w1_smem_fops,
  79. };
  80. static int __init w1_smem_init(void)
  81. {
  82. int err;
  83. err = w1_register_family(&w1_smem_family_01);
  84. if (err)
  85. return err;
  86. err = w1_register_family(&w1_smem_family_81);
  87. if (err) {
  88. w1_unregister_family(&w1_smem_family_01);
  89. return err;
  90. }
  91. return 0;
  92. }
  93. static void __exit w1_smem_fini(void)
  94. {
  95. w1_unregister_family(&w1_smem_family_01);
  96. w1_unregister_family(&w1_smem_family_81);
  97. }
  98. module_init(w1_smem_init);
  99. module_exit(w1_smem_fini);