blob: 285eb8f845747d16fabe5a3e205bafe516e8288b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Evgeniy Polyakov77859252005-05-20 22:33:25 +04002 * w1_smem.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
Evgeniy Polyakov77859252005-05-20 22:33:25 +04005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
35MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
37MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, 64bit memory family.");
38
Yani Ioannou060b8842005-05-17 06:44:04 -040039static ssize_t w1_smem_read_name(struct device *, struct device_attribute *attr, char *);
40static ssize_t w1_smem_read_val(struct device *, struct device_attribute *attr, char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static ssize_t w1_smem_read_bin(struct kobject *, char *, loff_t, size_t);
42
43static 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
Yani Ioannou060b8842005-05-17 06:44:04 -040050static ssize_t w1_smem_read_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
53
54 return sprintf(buf, "%s\n", sl->name);
55}
56
Yani Ioannou060b8842005-05-17 06:44:04 -040057static ssize_t w1_smem_read_val(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
60 int i;
61 ssize_t count = 0;
62
johnpol@2ka.mipt.ru05985712005-04-18 21:16:57 -070063 for (i = 0; i < 8; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 count += sprintf(buf + count, "%02x ", ((u8 *)&sl->reg_num)[i]);
65 count += sprintf(buf + count, "\n");
66
67 return count;
68}
69
70static 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),
Evgeniy Polyakov77859252005-05-20 22:33:25 +040073 struct w1_slave, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 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 }
johnpol@2ka.mipt.ru05985712005-04-18 21:16:57 -070090 for (i = 0; i < 8; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 count += sprintf(buf + count, "%02x ", ((u8 *)&sl->reg_num)[i]);
92 count += sprintf(buf + count, "\n");
Evgeniy Polyakov77859252005-05-20 22:33:25 +040093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094out:
95 up(&sl->master->mutex);
96out_dec:
97 atomic_dec(&sl->refcnt);
98
99 return count;
100}
101
Evgeniy Polyakov85e941c2005-05-02 14:26:42 +0400102static struct w1_family w1_smem_family_01 = {
103 .fid = W1_FAMILY_SMEM_01,
104 .fops = &w1_smem_fops,
105};
106
107static struct w1_family w1_smem_family_81 = {
108 .fid = W1_FAMILY_SMEM_81,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .fops = &w1_smem_fops,
110};
111
112static int __init w1_smem_init(void)
113{
Evgeniy Polyakov85e941c2005-05-02 14:26:42 +0400114 int err;
115
116 err = w1_register_family(&w1_smem_family_01);
117 if (err)
118 return err;
119
120 err = w1_register_family(&w1_smem_family_81);
121 if (err) {
122 w1_unregister_family(&w1_smem_family_01);
123 return err;
124 }
125
126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static void __exit w1_smem_fini(void)
130{
Evgeniy Polyakov85e941c2005-05-02 14:26:42 +0400131 w1_unregister_family(&w1_smem_family_01);
132 w1_unregister_family(&w1_smem_family_81);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135module_init(w1_smem_init);
136module_exit(w1_smem_fini);