blob: 70d2d469963cfe8e57be5b242c31ae1bec525a69 [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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static ssize_t w1_smem_read_bin(struct kobject *, char *, loff_t, size_t);
41
42static struct w1_family_ops w1_smem_fops = {
43 .rname = &w1_smem_read_name,
44 .rbin = &w1_smem_read_bin,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
Yani Ioannou060b8842005-05-17 06:44:04 -040047static ssize_t w1_smem_read_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
50
51 return sprintf(buf, "%s\n", sl->name);
52}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static 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),
Evgeniy Polyakov77859252005-05-20 22:33:25 +040057 struct w1_slave, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 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 }
johnpol@2ka.mipt.ru05985712005-04-18 21:16:57 -070074 for (i = 0; i < 8; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 count += sprintf(buf + count, "%02x ", ((u8 *)&sl->reg_num)[i]);
76 count += sprintf(buf + count, "\n");
Evgeniy Polyakov77859252005-05-20 22:33:25 +040077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078out:
79 up(&sl->master->mutex);
80out_dec:
81 atomic_dec(&sl->refcnt);
82
83 return count;
84}
85
Evgeniy Polyakov85e941c2005-05-02 14:26:42 +040086static struct w1_family w1_smem_family_01 = {
87 .fid = W1_FAMILY_SMEM_01,
88 .fops = &w1_smem_fops,
89};
90
91static struct w1_family w1_smem_family_81 = {
92 .fid = W1_FAMILY_SMEM_81,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .fops = &w1_smem_fops,
94};
95
96static int __init w1_smem_init(void)
97{
Evgeniy Polyakov85e941c2005-05-02 14:26:42 +040098 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static void __exit w1_smem_fini(void)
114{
Evgeniy Polyakov85e941c2005-05-02 14:26:42 +0400115 w1_unregister_family(&w1_smem_family_01);
116 w1_unregister_family(&w1_smem_family_81);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119module_init(w1_smem_init);
120module_exit(w1_smem_fini);