Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 2 | * w1_smem.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 39 | static ssize_t w1_smem_read_name(struct device *, struct device_attribute *attr, char *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 47 | static ssize_t w1_smem_read_name(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | struct w1_slave *sl = container_of(dev, struct w1_slave, dev); |
| 50 | |
| 51 | return sprintf(buf, "%s\n", sl->name); |
| 52 | } |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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), |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 57 | struct w1_slave, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
johnpol@2ka.mipt.ru | 0598571 | 2005-04-18 21:16:57 -0700 | [diff] [blame] | 74 | for (i = 0; i < 8; ++i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | count += sprintf(buf + count, "%02x ", ((u8 *)&sl->reg_num)[i]); |
| 76 | count += sprintf(buf + count, "\n"); |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | out: |
| 79 | up(&sl->master->mutex); |
| 80 | out_dec: |
| 81 | atomic_dec(&sl->refcnt); |
| 82 | |
| 83 | return count; |
| 84 | } |
| 85 | |
Evgeniy Polyakov | 85e941c | 2005-05-02 14:26:42 +0400 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | .fops = &w1_smem_fops, |
| 94 | }; |
| 95 | |
| 96 | static int __init w1_smem_init(void) |
| 97 | { |
Evgeniy Polyakov | 85e941c | 2005-05-02 14:26:42 +0400 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void __exit w1_smem_fini(void) |
| 114 | { |
Evgeniy Polyakov | 85e941c | 2005-05-02 14:26:42 +0400 | [diff] [blame] | 115 | w1_unregister_family(&w1_smem_family_01); |
| 116 | w1_unregister_family(&w1_smem_family_81); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | module_init(w1_smem_init); |
| 120 | module_exit(w1_smem_fini); |