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