[PATCH] w1: Cleans up usage of touch_bit/w1_read_bit/w1_write_bit.
[safe/jmp/linux-2.6] / drivers / w1 / w1_smem.c
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 *, struct device_attribute *attr, char *);
40 static ssize_t w1_smem_read_bin(struct kobject *, char *, loff_t, size_t);
41
42 static struct w1_family_ops w1_smem_fops = {
43         .rname = &w1_smem_read_name,
44         .rbin = &w1_smem_read_bin,
45 };
46
47 static ssize_t w1_smem_read_name(struct device *dev, struct device_attribute *attr, char *buf)
48 {
49         struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
50
51         return sprintf(buf, "%s\n", sl->name);
52 }
53
54 static ssize_t w1_smem_read_bin(struct kobject *kobj, char *buf, loff_t off, size_t count)
55 {
56         struct w1_slave *sl = container_of(container_of(kobj, struct device, kobj),
57                                            struct w1_slave, dev);
58         int i;
59
60         atomic_inc(&sl->refcnt);
61         if (down_interruptible(&sl->master->mutex)) {
62                 count = 0;
63                 goto out_dec;
64         }
65
66         if (off > W1_SLAVE_DATA_SIZE) {
67                 count = 0;
68                 goto out;
69         }
70         if (off + count > W1_SLAVE_DATA_SIZE) {
71                 count = 0;
72                 goto out;
73         }
74         for (i = 0; i < 8; ++i)
75                 count += sprintf(buf + count, "%02x ", ((u8 *)&sl->reg_num)[i]);
76         count += sprintf(buf + count, "\n");
77
78 out:
79         up(&sl->master->mutex);
80 out_dec:
81         atomic_dec(&sl->refcnt);
82
83         return count;
84 }
85
86 static struct w1_family w1_smem_family_01 = {
87         .fid = W1_FAMILY_SMEM_01,
88         .fops = &w1_smem_fops,
89 };
90
91 static struct w1_family w1_smem_family_81 = {
92         .fid = W1_FAMILY_SMEM_81,
93         .fops = &w1_smem_fops,
94 };
95
96 static int __init w1_smem_init(void)
97 {
98         int err;
99
100         err = w1_register_family(&w1_smem_family_01);
101         if (err)
102                 return err;
103         
104         err = w1_register_family(&w1_smem_family_81);
105         if (err) {
106                 w1_unregister_family(&w1_smem_family_01);
107                 return err;
108         }
109
110         return 0;
111 }
112
113 static void __exit w1_smem_fini(void)
114 {
115         w1_unregister_family(&w1_smem_family_01);
116         w1_unregister_family(&w1_smem_family_81);
117 }
118
119 module_init(w1_smem_init);
120 module_exit(w1_smem_fini);