aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/w1
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/w1
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/w1')
-rw-r--r--drivers/w1/Kconfig57
-rw-r--r--drivers/w1/Makefile20
-rw-r--r--drivers/w1/ds_w1_bridge.c174
-rw-r--r--drivers/w1/dscore.c789
-rw-r--r--drivers/w1/dscore.h170
-rw-r--r--drivers/w1/matrox_w1.c247
-rw-r--r--drivers/w1/w1.c835
-rw-r--r--drivers/w1/w1.h145
-rw-r--r--drivers/w1/w1_family.c150
-rw-r--r--drivers/w1/w1_family.h65
-rw-r--r--drivers/w1/w1_int.c220
-rw-r--r--drivers/w1/w1_int.h36
-rw-r--r--drivers/w1/w1_io.c195
-rw-r--r--drivers/w1/w1_io.h39
-rw-r--r--drivers/w1/w1_log.h38
-rw-r--r--drivers/w1/w1_netlink.c66
-rw-r--r--drivers/w1/w1_netlink.h57
-rw-r--r--drivers/w1/w1_smem.c118
-rw-r--r--drivers/w1/w1_therm.c205
19 files changed, 3626 insertions, 0 deletions
diff --git a/drivers/w1/Kconfig b/drivers/w1/Kconfig
new file mode 100644
index 000000000000..2ab65c902fe5
--- /dev/null
+++ b/drivers/w1/Kconfig
@@ -0,0 +1,57 @@
1menu "Dallas's 1-wire bus"
2
3config W1
4 tristate "Dallas's 1-wire support"
5 ---help---
6 Dallas's 1-wire bus is usefull to connect slow 1-pin devices
7 such as iButtons and thermal sensors.
8
9 If you want W1 support, you should say Y here.
10
11 This W1 support can also be built as a module. If so, the module
12 will be called wire.ko.
13
14config W1_MATROX
15 tristate "Matrox G400 transport layer for 1-wire"
16 depends on W1 && PCI
17 help
18 Say Y here if you want to communicate with your 1-wire devices
19 using Matrox's G400 GPIO pins.
20
21 This support is also available as a module. If so, the module
22 will be called matrox_w1.ko.
23
24config W1_DS9490
25 tristate "DS9490R transport layer driver"
26 depends on W1 && USB
27 help
28 Say Y here if you want to have a driver for DS9490R UWB <-> W1 bridge.
29
30 This support is also available as a module. If so, the module
31 will be called ds9490r.ko.
32
33config W1_DS9490_BRIDGE
34 tristate "DS9490R USB <-> W1 transport layer for 1-wire"
35 depends on W1_DS9490
36 help
37 Say Y here if you want to communicate with your 1-wire devices
38 using DS9490R USB bridge.
39
40 This support is also available as a module. If so, the module
41 will be called ds_w1_bridge.ko.
42
43config W1_THERM
44 tristate "Thermal family implementation"
45 depends on W1
46 help
47 Say Y here if you want to connect 1-wire thermal sensors to you
48 wire.
49
50config W1_SMEM
51 tristate "Simple 64bit memory family implementation"
52 depends on W1
53 help
54 Say Y here if you want to connect 1-wire
55 simple 64bit memory rom(ds2401/ds2411/ds1990*) to you wire.
56
57endmenu
diff --git a/drivers/w1/Makefile b/drivers/w1/Makefile
new file mode 100644
index 000000000000..80725c348e70
--- /dev/null
+++ b/drivers/w1/Makefile
@@ -0,0 +1,20 @@
1#
2# Makefile for the Dallas's 1-wire bus.
3#
4
5ifneq ($(CONFIG_NET), y)
6EXTRA_CFLAGS += -DNETLINK_DISABLED
7endif
8
9obj-$(CONFIG_W1) += wire.o
10wire-objs := w1.o w1_int.o w1_family.o w1_netlink.o w1_io.o
11
12obj-$(CONFIG_W1_MATROX) += matrox_w1.o
13obj-$(CONFIG_W1_THERM) += w1_therm.o
14obj-$(CONFIG_W1_SMEM) += w1_smem.o
15
16obj-$(CONFIG_W1_DS9490) += ds9490r.o
17ds9490r-objs := dscore.o
18
19obj-$(CONFIG_W1_DS9490_BRIDGE) += ds_w1_bridge.o
20
diff --git a/drivers/w1/ds_w1_bridge.c b/drivers/w1/ds_w1_bridge.c
new file mode 100644
index 000000000000..0baaeb5fd630
--- /dev/null
+++ b/drivers/w1/ds_w1_bridge.c
@@ -0,0 +1,174 @@
1/*
2 * ds_w1_bridge.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 terms 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 <linux/module.h>
23#include <linux/types.h>
24
25#include "../w1/w1.h"
26#include "../w1/w1_int.h"
27#include "dscore.h"
28
29static struct ds_device *ds_dev;
30static struct w1_bus_master *ds_bus_master;
31
32static u8 ds9490r_touch_bit(unsigned long data, u8 bit)
33{
34 u8 ret;
35 struct ds_device *dev = (struct ds_device *)data;
36
37 if (ds_touch_bit(dev, bit, &ret))
38 return 0;
39
40 return ret;
41}
42
43static void ds9490r_write_bit(unsigned long data, u8 bit)
44{
45 struct ds_device *dev = (struct ds_device *)data;
46
47 ds_write_bit(dev, bit);
48}
49
50static void ds9490r_write_byte(unsigned long data, u8 byte)
51{
52 struct ds_device *dev = (struct ds_device *)data;
53
54 ds_write_byte(dev, byte);
55}
56
57static u8 ds9490r_read_bit(unsigned long data)
58{
59 struct ds_device *dev = (struct ds_device *)data;
60 int err;
61 u8 bit = 0;
62
63 err = ds_touch_bit(dev, 1, &bit);
64 if (err)
65 return 0;
66 //err = ds_read_bit(dev, &bit);
67 //if (err)
68 // return 0;
69
70 return bit & 1;
71}
72
73static u8 ds9490r_read_byte(unsigned long data)
74{
75 struct ds_device *dev = (struct ds_device *)data;
76 int err;
77 u8 byte = 0;
78
79 err = ds_read_byte(dev, &byte);
80 if (err)
81 return 0;
82
83 return byte;
84}
85
86static void ds9490r_write_block(unsigned long data, u8 *buf, int len)
87{
88 struct ds_device *dev = (struct ds_device *)data;
89
90 ds_write_block(dev, buf, len);
91}
92
93static u8 ds9490r_read_block(unsigned long data, u8 *buf, int len)
94{
95 struct ds_device *dev = (struct ds_device *)data;
96 int err;
97
98 err = ds_read_block(dev, buf, len);
99 if (err < 0)
100 return 0;
101
102 return len;
103}
104
105static u8 ds9490r_reset(unsigned long data)
106{
107 struct ds_device *dev = (struct ds_device *)data;
108 struct ds_status st;
109 int err;
110
111 memset(&st, 0, sizeof(st));
112
113 err = ds_reset(dev, &st);
114 if (err)
115 return 1;
116
117 return 0;
118}
119
120static int __devinit ds_w1_init(void)
121{
122 int err;
123
124 ds_bus_master = kmalloc(sizeof(*ds_bus_master), GFP_KERNEL);
125 if (!ds_bus_master) {
126 printk(KERN_ERR "Failed to allocate DS9490R USB<->W1 bus_master structure.\n");
127 return -ENOMEM;
128 }
129
130 ds_dev = ds_get_device();
131 if (!ds_dev) {
132 printk(KERN_ERR "DS9490R is not registered.\n");
133 err = -ENODEV;
134 goto err_out_free_bus_master;
135 }
136
137 memset(ds_bus_master, 0, sizeof(*ds_bus_master));
138
139 ds_bus_master->data = (unsigned long)ds_dev;
140 ds_bus_master->touch_bit = &ds9490r_touch_bit;
141 ds_bus_master->read_bit = &ds9490r_read_bit;
142 ds_bus_master->write_bit = &ds9490r_write_bit;
143 ds_bus_master->read_byte = &ds9490r_read_byte;
144 ds_bus_master->write_byte = &ds9490r_write_byte;
145 ds_bus_master->read_block = &ds9490r_read_block;
146 ds_bus_master->write_block = &ds9490r_write_block;
147 ds_bus_master->reset_bus = &ds9490r_reset;
148
149 err = w1_add_master_device(ds_bus_master);
150 if (err)
151 goto err_out_put_device;
152
153 return 0;
154
155err_out_put_device:
156 ds_put_device(ds_dev);
157err_out_free_bus_master:
158 kfree(ds_bus_master);
159
160 return err;
161}
162
163static void __devexit ds_w1_fini(void)
164{
165 w1_remove_master_device(ds_bus_master);
166 ds_put_device(ds_dev);
167 kfree(ds_bus_master);
168}
169
170module_init(ds_w1_init);
171module_exit(ds_w1_fini);
172
173MODULE_LICENSE("GPL");
174MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
diff --git a/drivers/w1/dscore.c b/drivers/w1/dscore.c
new file mode 100644
index 000000000000..eee6644d33d6
--- /dev/null
+++ b/drivers/w1/dscore.c
@@ -0,0 +1,789 @@
1/*
2 * dscore.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 terms 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 <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/mod_devicetable.h>
25#include <linux/usb.h>
26
27#include "dscore.h"
28
29static struct usb_device_id ds_id_table [] = {
30 { USB_DEVICE(0x04fa, 0x2490) },
31 { },
32};
33MODULE_DEVICE_TABLE(usb, ds_id_table);
34
35int ds_probe(struct usb_interface *, const struct usb_device_id *);
36void ds_disconnect(struct usb_interface *);
37
38int ds_touch_bit(struct ds_device *, u8, u8 *);
39int ds_read_byte(struct ds_device *, u8 *);
40int ds_read_bit(struct ds_device *, u8 *);
41int ds_write_byte(struct ds_device *, u8);
42int ds_write_bit(struct ds_device *, u8);
43int ds_start_pulse(struct ds_device *, int);
44int ds_set_speed(struct ds_device *, int);
45int ds_reset(struct ds_device *, struct ds_status *);
46int ds_detect(struct ds_device *, struct ds_status *);
47int ds_stop_pulse(struct ds_device *, int);
48struct ds_device * ds_get_device(void);
49void ds_put_device(struct ds_device *);
50
51static inline void ds_dump_status(unsigned char *, unsigned char *, int);
52static int ds_send_control(struct ds_device *, u16, u16);
53static int ds_send_control_mode(struct ds_device *, u16, u16);
54static int ds_send_control_cmd(struct ds_device *, u16, u16);
55
56
57static struct usb_driver ds_driver = {
58 .owner = THIS_MODULE,
59 .name = "DS9490R",
60 .probe = ds_probe,
61 .disconnect = ds_disconnect,
62 .id_table = ds_id_table,
63};
64
65static struct ds_device *ds_dev;
66
67struct ds_device * ds_get_device(void)
68{
69 if (ds_dev)
70 atomic_inc(&ds_dev->refcnt);
71 return ds_dev;
72}
73
74void ds_put_device(struct ds_device *dev)
75{
76 atomic_dec(&dev->refcnt);
77}
78
79static int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index)
80{
81 int err;
82
83 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
84 CONTROL_CMD, 0x40, value, index, NULL, 0, 1000);
85 if (err < 0) {
86 printk(KERN_ERR "Failed to send command control message %x.%x: err=%d.\n",
87 value, index, err);
88 return err;
89 }
90
91 return err;
92}
93
94static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
95{
96 int err;
97
98 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
99 MODE_CMD, 0x40, value, index, NULL, 0, 1000);
100 if (err < 0) {
101 printk(KERN_ERR "Failed to send mode control message %x.%x: err=%d.\n",
102 value, index, err);
103 return err;
104 }
105
106 return err;
107}
108
109static int ds_send_control(struct ds_device *dev, u16 value, u16 index)
110{
111 int err;
112
113 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
114 COMM_CMD, 0x40, value, index, NULL, 0, 1000);
115 if (err < 0) {
116 printk(KERN_ERR "Failed to send control message %x.%x: err=%d.\n",
117 value, index, err);
118 return err;
119 }
120
121 return err;
122}
123
124static inline void ds_dump_status(unsigned char *buf, unsigned char *str, int off)
125{
126 printk("%45s: %8x\n", str, buf[off]);
127}
128
129int ds_recv_status_nodump(struct ds_device *dev, struct ds_status *st, unsigned char *buf, int size)
130{
131 int count, err;
132
133 memset(st, 0, sizeof(st));
134
135 count = 0;
136 err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_STATUS]), buf, size, &count, 100);
137 if (err < 0) {
138 printk(KERN_ERR "Failed to read 1-wire data from 0x%x: err=%d.\n", dev->ep[EP_STATUS], err);
139 return err;
140 }
141
142 if (count >= sizeof(*st))
143 memcpy(st, buf, sizeof(*st));
144
145 return count;
146}
147
148static int ds_recv_status(struct ds_device *dev, struct ds_status *st)
149{
150 unsigned char buf[64];
151 int count, err = 0, i;
152
153 memcpy(st, buf, sizeof(*st));
154
155 count = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
156 if (count < 0)
157 return err;
158
159 printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], count);
160 for (i=0; i<count; ++i)
161 printk("%02x ", buf[i]);
162 printk("\n");
163
164 if (count >= 16) {
165 ds_dump_status(buf, "enable flag", 0);
166 ds_dump_status(buf, "1-wire speed", 1);
167 ds_dump_status(buf, "strong pullup duration", 2);
168 ds_dump_status(buf, "programming pulse duration", 3);
169 ds_dump_status(buf, "pulldown slew rate control", 4);
170 ds_dump_status(buf, "write-1 low time", 5);
171 ds_dump_status(buf, "data sample offset/write-0 recovery time", 6);
172 ds_dump_status(buf, "reserved (test register)", 7);
173 ds_dump_status(buf, "device status flags", 8);
174 ds_dump_status(buf, "communication command byte 1", 9);
175 ds_dump_status(buf, "communication command byte 2", 10);
176 ds_dump_status(buf, "communication command buffer status", 11);
177 ds_dump_status(buf, "1-wire data output buffer status", 12);
178 ds_dump_status(buf, "1-wire data input buffer status", 13);
179 ds_dump_status(buf, "reserved", 14);
180 ds_dump_status(buf, "reserved", 15);
181 }
182
183 memcpy(st, buf, sizeof(*st));
184
185 if (st->status & ST_EPOF) {
186 printk(KERN_INFO "Resetting device after ST_EPOF.\n");
187 err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
188 if (err)
189 return err;
190 count = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
191 if (count < 0)
192 return err;
193 }
194#if 0
195 if (st->status & ST_IDLE) {
196 printk(KERN_INFO "Resetting pulse after ST_IDLE.\n");
197 err = ds_start_pulse(dev, PULLUP_PULSE_DURATION);
198 if (err)
199 return err;
200 }
201#endif
202
203 return err;
204}
205
206static int ds_recv_data(struct ds_device *dev, unsigned char *buf, int size)
207{
208 int count, err;
209 struct ds_status st;
210
211 count = 0;
212 err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]),
213 buf, size, &count, 1000);
214 if (err < 0) {
215 printk(KERN_INFO "Clearing ep0x%x.\n", dev->ep[EP_DATA_IN]);
216 usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]));
217 ds_recv_status(dev, &st);
218 return err;
219 }
220
221#if 0
222 {
223 int i;
224
225 printk("%s: count=%d: ", __func__, count);
226 for (i=0; i<count; ++i)
227 printk("%02x ", buf[i]);
228 printk("\n");
229 }
230#endif
231 return count;
232}
233
234static int ds_send_data(struct ds_device *dev, unsigned char *buf, int len)
235{
236 int count, err;
237
238 count = 0;
239 err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000);
240 if (err < 0) {
241 printk(KERN_ERR "Failed to read 1-wire data from 0x02: err=%d.\n", err);
242 return err;
243 }
244
245 return err;
246}
247
248int ds_stop_pulse(struct ds_device *dev, int limit)
249{
250 struct ds_status st;
251 int count = 0, err = 0;
252 u8 buf[0x20];
253
254 do {
255 err = ds_send_control(dev, CTL_HALT_EXE_IDLE, 0);
256 if (err)
257 break;
258 err = ds_send_control(dev, CTL_RESUME_EXE, 0);
259 if (err)
260 break;
261 err = ds_recv_status_nodump(dev, &st, buf, sizeof(buf));
262 if (err)
263 break;
264
265 if ((st.status & ST_SPUA) == 0) {
266 err = ds_send_control_mode(dev, MOD_PULSE_EN, 0);
267 if (err)
268 break;
269 }
270 } while(++count < limit);
271
272 return err;
273}
274
275int ds_detect(struct ds_device *dev, struct ds_status *st)
276{
277 int err;
278
279 err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
280 if (err)
281 return err;
282
283 err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, 0);
284 if (err)
285 return err;
286
287 err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40);
288 if (err)
289 return err;
290
291 err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG);
292 if (err)
293 return err;
294
295 err = ds_recv_status(dev, st);
296
297 return err;
298}
299
300int ds_wait_status(struct ds_device *dev, struct ds_status *st)
301{
302 u8 buf[0x20];
303 int err, count = 0;
304
305 do {
306 err = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
307#if 0
308 if (err >= 0) {
309 int i;
310 printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], err);
311 for (i=0; i<err; ++i)
312 printk("%02x ", buf[i]);
313 printk("\n");
314 }
315#endif
316 } while(!(buf[0x08] & 0x20) && !(err < 0) && ++count < 100);
317
318
319 if (((err > 16) && (buf[0x10] & 0x01)) || count >= 100 || err < 0) {
320 ds_recv_status(dev, st);
321 return -1;
322 }
323 else {
324 return 0;
325 }
326}
327
328int ds_reset(struct ds_device *dev, struct ds_status *st)
329{
330 int err;
331
332 //err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_F | COMM_IM | COMM_SE, SPEED_FLEXIBLE);
333 err = ds_send_control(dev, 0x43, SPEED_NORMAL);
334 if (err)
335 return err;
336
337 ds_wait_status(dev, st);
338#if 0
339 if (st->command_buffer_status) {
340 printk(KERN_INFO "Short circuit.\n");
341 return -EIO;
342 }
343#endif
344
345 return 0;
346}
347
348int ds_set_speed(struct ds_device *dev, int speed)
349{
350 int err;
351
352 if (speed != SPEED_NORMAL && speed != SPEED_FLEXIBLE && speed != SPEED_OVERDRIVE)
353 return -EINVAL;
354
355 if (speed != SPEED_OVERDRIVE)
356 speed = SPEED_FLEXIBLE;
357
358 speed &= 0xff;
359
360 err = ds_send_control_mode(dev, MOD_1WIRE_SPEED, speed);
361 if (err)
362 return err;
363
364 return err;
365}
366
367int ds_start_pulse(struct ds_device *dev, int delay)
368{
369 int err;
370 u8 del = 1 + (u8)(delay >> 4);
371 struct ds_status st;
372
373#if 0
374 err = ds_stop_pulse(dev, 10);
375 if (err)
376 return err;
377
378 err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE);
379 if (err)
380 return err;
381#endif
382 err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del);
383 if (err)
384 return err;
385
386 err = ds_send_control(dev, COMM_PULSE | COMM_IM | COMM_F, 0);
387 if (err)
388 return err;
389
390 mdelay(delay);
391
392 ds_wait_status(dev, &st);
393
394 return err;
395}
396
397int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
398{
399 int err, count;
400 struct ds_status st;
401 u16 value = (COMM_BIT_IO | COMM_IM) | ((bit) ? COMM_D : 0);
402 u16 cmd;
403
404 err = ds_send_control(dev, value, 0);
405 if (err)
406 return err;
407
408 count = 0;
409 do {
410 err = ds_wait_status(dev, &st);
411 if (err)
412 return err;
413
414 cmd = st.command0 | (st.command1 << 8);
415 } while (cmd != value && ++count < 10);
416
417 if (err < 0 || count >= 10) {
418 printk(KERN_ERR "Failed to obtain status.\n");
419 return -EINVAL;
420 }
421
422 err = ds_recv_data(dev, tbit, sizeof(*tbit));
423 if (err < 0)
424 return err;
425
426 return 0;
427}
428
429int ds_write_bit(struct ds_device *dev, u8 bit)
430{
431 int err;
432 struct ds_status st;
433
434 err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit) ? COMM_D : 0, 0);
435 if (err)
436 return err;
437
438 ds_wait_status(dev, &st);
439
440 return 0;
441}
442
443int ds_write_byte(struct ds_device *dev, u8 byte)
444{
445 int err;
446 struct ds_status st;
447 u8 rbyte;
448
449 err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM | COMM_SPU, byte);
450 if (err)
451 return err;
452
453 err = ds_wait_status(dev, &st);
454 if (err)
455 return err;
456
457 err = ds_recv_data(dev, &rbyte, sizeof(rbyte));
458 if (err < 0)
459 return err;
460
461 ds_start_pulse(dev, PULLUP_PULSE_DURATION);
462
463 return !(byte == rbyte);
464}
465
466int ds_read_bit(struct ds_device *dev, u8 *bit)
467{
468 int err;
469
470 err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE);
471 if (err)
472 return err;
473
474 err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_SPU | COMM_D, 0);
475 if (err)
476 return err;
477
478 err = ds_recv_data(dev, bit, sizeof(*bit));
479 if (err < 0)
480 return err;
481
482 return 0;
483}
484
485int ds_read_byte(struct ds_device *dev, u8 *byte)
486{
487 int err;
488 struct ds_status st;
489
490 err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM , 0xff);
491 if (err)
492 return err;
493
494 ds_wait_status(dev, &st);
495
496 err = ds_recv_data(dev, byte, sizeof(*byte));
497 if (err < 0)
498 return err;
499
500 return 0;
501}
502
503int ds_read_block(struct ds_device *dev, u8 *buf, int len)
504{
505 struct ds_status st;
506 int err;
507
508 if (len > 64*1024)
509 return -E2BIG;
510
511 memset(buf, 0xFF, len);
512
513 err = ds_send_data(dev, buf, len);
514 if (err < 0)
515 return err;
516
517 err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | COMM_SPU, len);
518 if (err)
519 return err;
520
521 ds_wait_status(dev, &st);
522
523 memset(buf, 0x00, len);
524 err = ds_recv_data(dev, buf, len);
525
526 return err;
527}
528
529int ds_write_block(struct ds_device *dev, u8 *buf, int len)
530{
531 int err;
532 struct ds_status st;
533
534 err = ds_send_data(dev, buf, len);
535 if (err < 0)
536 return err;
537
538 ds_wait_status(dev, &st);
539
540 err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | COMM_SPU, len);
541 if (err)
542 return err;
543
544 ds_wait_status(dev, &st);
545
546 err = ds_recv_data(dev, buf, len);
547 if (err < 0)
548 return err;
549
550 ds_start_pulse(dev, PULLUP_PULSE_DURATION);
551
552 return !(err == len);
553}
554
555int ds_search(struct ds_device *dev, u64 init, u64 *buf, u8 id_number, int conditional_search)
556{
557 int err;
558 u16 value, index;
559 struct ds_status st;
560
561 memset(buf, 0, sizeof(buf));
562
563 err = ds_send_data(ds_dev, (unsigned char *)&init, 8);
564 if (err)
565 return err;
566
567 ds_wait_status(ds_dev, &st);
568
569 value = COMM_SEARCH_ACCESS | COMM_IM | COMM_SM | COMM_F | COMM_RTS;
570 index = (conditional_search ? 0xEC : 0xF0) | (id_number << 8);
571 err = ds_send_control(ds_dev, value, index);
572 if (err)
573 return err;
574
575 ds_wait_status(ds_dev, &st);
576
577 err = ds_recv_data(ds_dev, (unsigned char *)buf, 8*id_number);
578 if (err < 0)
579 return err;
580
581 return err/8;
582}
583
584int ds_match_access(struct ds_device *dev, u64 init)
585{
586 int err;
587 struct ds_status st;
588
589 err = ds_send_data(dev, (unsigned char *)&init, sizeof(init));
590 if (err)
591 return err;
592
593 ds_wait_status(dev, &st);
594
595 err = ds_send_control(dev, COMM_MATCH_ACCESS | COMM_IM | COMM_RST, 0x0055);
596 if (err)
597 return err;
598
599 ds_wait_status(dev, &st);
600
601 return 0;
602}
603
604int ds_set_path(struct ds_device *dev, u64 init)
605{
606 int err;
607 struct ds_status st;
608 u8 buf[9];
609
610 memcpy(buf, &init, 8);
611 buf[8] = BRANCH_MAIN;
612
613 err = ds_send_data(dev, buf, sizeof(buf));
614 if (err)
615 return err;
616
617 ds_wait_status(dev, &st);
618
619 err = ds_send_control(dev, COMM_SET_PATH | COMM_IM | COMM_RST, 0);
620 if (err)
621 return err;
622
623 ds_wait_status(dev, &st);
624
625 return 0;
626}
627
628int ds_probe(struct usb_interface *intf, const struct usb_device_id *udev_id)
629{
630 struct usb_device *udev = interface_to_usbdev(intf);
631 struct usb_endpoint_descriptor *endpoint;
632 struct usb_host_interface *iface_desc;
633 int i, err;
634
635 ds_dev = kmalloc(sizeof(struct ds_device), GFP_KERNEL);
636 if (!ds_dev) {
637 printk(KERN_INFO "Failed to allocate new DS9490R structure.\n");
638 return -ENOMEM;
639 }
640
641 ds_dev->udev = usb_get_dev(udev);
642 usb_set_intfdata(intf, ds_dev);
643
644 err = usb_set_interface(ds_dev->udev, intf->altsetting[0].desc.bInterfaceNumber, 3);
645 if (err) {
646 printk(KERN_ERR "Failed to set alternative setting 3 for %d interface: err=%d.\n",
647 intf->altsetting[0].desc.bInterfaceNumber, err);
648 return err;
649 }
650
651 err = usb_reset_configuration(ds_dev->udev);
652 if (err) {
653 printk(KERN_ERR "Failed to reset configuration: err=%d.\n", err);
654 return err;
655 }
656
657 iface_desc = &intf->altsetting[0];
658 if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
659 printk(KERN_INFO "Num endpoints=%d. It is not DS9490R.\n", iface_desc->desc.bNumEndpoints);
660 return -ENODEV;
661 }
662
663 atomic_set(&ds_dev->refcnt, 0);
664 memset(ds_dev->ep, 0, sizeof(ds_dev->ep));
665
666 /*
667 * This loop doesn'd show control 0 endpoint,
668 * so we will fill only 1-3 endpoints entry.
669 */
670 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
671 endpoint = &iface_desc->endpoint[i].desc;
672
673 ds_dev->ep[i+1] = endpoint->bEndpointAddress;
674
675 printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
676 i, endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize),
677 (endpoint->bEndpointAddress & USB_DIR_IN)?"IN":"OUT",
678 endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
679 }
680
681#if 0
682 {
683 int err, i;
684 u64 buf[3];
685 u64 init=0xb30000002078ee81ull;
686 struct ds_status st;
687
688 ds_reset(ds_dev, &st);
689 err = ds_search(ds_dev, init, buf, 3, 0);
690 if (err < 0)
691 return err;
692 for (i=0; i<err; ++i)
693 printk("%d: %llx\n", i, buf[i]);
694
695 printk("Resetting...\n");
696 ds_reset(ds_dev, &st);
697 printk("Setting path for %llx.\n", init);
698 err = ds_set_path(ds_dev, init);
699 if (err)
700 return err;
701 printk("Calling MATCH_ACCESS.\n");
702 err = ds_match_access(ds_dev, init);
703 if (err)
704 return err;
705
706 printk("Searching the bus...\n");
707 err = ds_search(ds_dev, init, buf, 3, 0);
708
709 printk("ds_search() returned %d\n", err);
710
711 if (err < 0)
712 return err;
713 for (i=0; i<err; ++i)
714 printk("%d: %llx\n", i, buf[i]);
715
716 return 0;
717 }
718#endif
719
720 return 0;
721}
722
723void ds_disconnect(struct usb_interface *intf)
724{
725 struct ds_device *dev;
726
727 dev = usb_get_intfdata(intf);
728 usb_set_intfdata(intf, NULL);
729
730 while (atomic_read(&dev->refcnt)) {
731 printk(KERN_INFO "Waiting for DS to become free: refcnt=%d.\n",
732 atomic_read(&dev->refcnt));
733
734 if (msleep_interruptible(1000))
735 flush_signals(current);
736 }
737
738 usb_put_dev(dev->udev);
739 kfree(dev);
740 ds_dev = NULL;
741}
742
743int ds_init(void)
744{
745 int err;
746
747 err = usb_register(&ds_driver);
748 if (err) {
749 printk(KERN_INFO "Failed to register DS9490R USB device: err=%d.\n", err);
750 return err;
751 }
752
753 return 0;
754}
755
756void ds_fini(void)
757{
758 usb_deregister(&ds_driver);
759}
760
761module_init(ds_init);
762module_exit(ds_fini);
763
764MODULE_LICENSE("GPL");
765MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
766
767EXPORT_SYMBOL(ds_touch_bit);
768EXPORT_SYMBOL(ds_read_byte);
769EXPORT_SYMBOL(ds_read_bit);
770EXPORT_SYMBOL(ds_read_block);
771EXPORT_SYMBOL(ds_write_byte);
772EXPORT_SYMBOL(ds_write_bit);
773EXPORT_SYMBOL(ds_write_block);
774EXPORT_SYMBOL(ds_reset);
775EXPORT_SYMBOL(ds_get_device);
776EXPORT_SYMBOL(ds_put_device);
777
778/*
779 * This functions can be used for EEPROM programming,
780 * when driver will be included into mainline this will
781 * require uncommenting.
782 */
783#if 0
784EXPORT_SYMBOL(ds_start_pulse);
785EXPORT_SYMBOL(ds_set_speed);
786EXPORT_SYMBOL(ds_detect);
787EXPORT_SYMBOL(ds_stop_pulse);
788EXPORT_SYMBOL(ds_search);
789#endif
diff --git a/drivers/w1/dscore.h b/drivers/w1/dscore.h
new file mode 100644
index 000000000000..9c767ef4ac24
--- /dev/null
+++ b/drivers/w1/dscore.h
@@ -0,0 +1,170 @@
1/*
2 * dscore.h
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 terms 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#ifndef __DSCORE_H
23#define __DSCORE_H
24
25#include <linux/usb.h>
26#include <asm/atomic.h>
27
28/* COMMAND TYPE CODES */
29#define CONTROL_CMD 0x00
30#define COMM_CMD 0x01
31#define MODE_CMD 0x02
32
33/* CONTROL COMMAND CODES */
34#define CTL_RESET_DEVICE 0x0000
35#define CTL_START_EXE 0x0001
36#define CTL_RESUME_EXE 0x0002
37#define CTL_HALT_EXE_IDLE 0x0003
38#define CTL_HALT_EXE_DONE 0x0004
39#define CTL_FLUSH_COMM_CMDS 0x0007
40#define CTL_FLUSH_RCV_BUFFER 0x0008
41#define CTL_FLUSH_XMT_BUFFER 0x0009
42#define CTL_GET_COMM_CMDS 0x000A
43
44/* MODE COMMAND CODES */
45#define MOD_PULSE_EN 0x0000
46#define MOD_SPEED_CHANGE_EN 0x0001
47#define MOD_1WIRE_SPEED 0x0002
48#define MOD_STRONG_PU_DURATION 0x0003
49#define MOD_PULLDOWN_SLEWRATE 0x0004
50#define MOD_PROG_PULSE_DURATION 0x0005
51#define MOD_WRITE1_LOWTIME 0x0006
52#define MOD_DSOW0_TREC 0x0007
53
54/* COMMUNICATION COMMAND CODES */
55#define COMM_ERROR_ESCAPE 0x0601
56#define COMM_SET_DURATION 0x0012
57#define COMM_BIT_IO 0x0020
58#define COMM_PULSE 0x0030
59#define COMM_1_WIRE_RESET 0x0042
60#define COMM_BYTE_IO 0x0052
61#define COMM_MATCH_ACCESS 0x0064
62#define COMM_BLOCK_IO 0x0074
63#define COMM_READ_STRAIGHT 0x0080
64#define COMM_DO_RELEASE 0x6092
65#define COMM_SET_PATH 0x00A2
66#define COMM_WRITE_SRAM_PAGE 0x00B2
67#define COMM_WRITE_EPROM 0x00C4
68#define COMM_READ_CRC_PROT_PAGE 0x00D4
69#define COMM_READ_REDIRECT_PAGE_CRC 0x21E4
70#define COMM_SEARCH_ACCESS 0x00F4
71
72/* Communication command bits */
73#define COMM_TYPE 0x0008
74#define COMM_SE 0x0008
75#define COMM_D 0x0008
76#define COMM_Z 0x0008
77#define COMM_CH 0x0008
78#define COMM_SM 0x0008
79#define COMM_R 0x0008
80#define COMM_IM 0x0001
81
82#define COMM_PS 0x4000
83#define COMM_PST 0x4000
84#define COMM_CIB 0x4000
85#define COMM_RTS 0x4000
86#define COMM_DT 0x2000
87#define COMM_SPU 0x1000
88#define COMM_F 0x0800
89#define COMM_NTP 0x0400
90#define COMM_ICP 0x0200
91#define COMM_RST 0x0100
92
93#define PULSE_PROG 0x01
94#define PULSE_SPUE 0x02
95
96#define BRANCH_MAIN 0xCC
97#define BRANCH_AUX 0x33
98
99/*
100 * Duration of the strong pull-up pulse in milliseconds.
101 */
102#define PULLUP_PULSE_DURATION 750
103
104/* Status flags */
105#define ST_SPUA 0x01 /* Strong Pull-up is active */
106#define ST_PRGA 0x02 /* 12V programming pulse is being generated */
107#define ST_12VP 0x04 /* external 12V programming voltage is present */
108#define ST_PMOD 0x08 /* DS2490 powered from USB and external sources */
109#define ST_HALT 0x10 /* DS2490 is currently halted */
110#define ST_IDLE 0x20 /* DS2490 is currently idle */
111#define ST_EPOF 0x80
112
113#define SPEED_NORMAL 0x00
114#define SPEED_FLEXIBLE 0x01
115#define SPEED_OVERDRIVE 0x02
116
117#define NUM_EP 4
118#define EP_CONTROL 0
119#define EP_STATUS 1
120#define EP_DATA_OUT 2
121#define EP_DATA_IN 3
122
123struct ds_device
124{
125 struct usb_device *udev;
126 struct usb_interface *intf;
127
128 int ep[NUM_EP];
129
130 atomic_t refcnt;
131};
132
133struct ds_status
134{
135 u8 enable;
136 u8 speed;
137 u8 pullup_dur;
138 u8 ppuls_dur;
139 u8 pulldown_slew;
140 u8 write1_time;
141 u8 write0_time;
142 u8 reserved0;
143 u8 status;
144 u8 command0;
145 u8 command1;
146 u8 command_buffer_status;
147 u8 data_out_buffer_status;
148 u8 data_in_buffer_status;
149 u8 reserved1;
150 u8 reserved2;
151
152};
153
154int ds_touch_bit(struct ds_device *, u8, u8 *);
155int ds_read_byte(struct ds_device *, u8 *);
156int ds_read_bit(struct ds_device *, u8 *);
157int ds_write_byte(struct ds_device *, u8);
158int ds_write_bit(struct ds_device *, u8);
159int ds_start_pulse(struct ds_device *, int);
160int ds_set_speed(struct ds_device *, int);
161int ds_reset(struct ds_device *, struct ds_status *);
162int ds_detect(struct ds_device *, struct ds_status *);
163int ds_stop_pulse(struct ds_device *, int);
164struct ds_device * ds_get_device(void);
165void ds_put_device(struct ds_device *);
166int ds_write_block(struct ds_device *, u8 *, int);
167int ds_read_block(struct ds_device *, u8 *, int);
168
169#endif /* __DSCORE_H */
170
diff --git a/drivers/w1/matrox_w1.c b/drivers/w1/matrox_w1.c
new file mode 100644
index 000000000000..e565416458ea
--- /dev/null
+++ b/drivers/w1/matrox_w1.c
@@ -0,0 +1,247 @@
1/*
2 * matrox_w1.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 terms 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/atomic.h>
23#include <asm/types.h>
24#include <asm/io.h>
25
26#include <linux/delay.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/list.h>
30#include <linux/interrupt.h>
31#include <linux/spinlock.h>
32#include <linux/timer.h>
33#include <linux/slab.h>
34#include <linux/pci_ids.h>
35#include <linux/pci.h>
36#include <linux/timer.h>
37
38#include "w1.h"
39#include "w1_int.h"
40#include "w1_log.h"
41
42MODULE_LICENSE("GPL");
43MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
44MODULE_DESCRIPTION("Driver for transport(Dallas 1-wire prtocol) over VGA DDC(matrox gpio).");
45
46static struct pci_device_id matrox_w1_tbl[] = {
47 { PCI_DEVICE(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400) },
48 { },
49};
50MODULE_DEVICE_TABLE(pci, matrox_w1_tbl);
51
52static int __devinit matrox_w1_probe(struct pci_dev *, const struct pci_device_id *);
53static void __devexit matrox_w1_remove(struct pci_dev *);
54
55static struct pci_driver matrox_w1_pci_driver = {
56 .name = "matrox_w1",
57 .id_table = matrox_w1_tbl,
58 .probe = matrox_w1_probe,
59 .remove = __devexit_p(matrox_w1_remove),
60};
61
62/*
63 * Matrox G400 DDC registers.
64 */
65
66#define MATROX_G400_DDC_CLK (1<<4)
67#define MATROX_G400_DDC_DATA (1<<1)
68
69#define MATROX_BASE 0x3C00
70#define MATROX_STATUS 0x1e14
71
72#define MATROX_PORT_INDEX_OFFSET 0x00
73#define MATROX_PORT_DATA_OFFSET 0x0A
74
75#define MATROX_GET_CONTROL 0x2A
76#define MATROX_GET_DATA 0x2B
77#define MATROX_CURSOR_CTL 0x06
78
79struct matrox_device
80{
81 void __iomem *base_addr;
82 void __iomem *port_index;
83 void __iomem *port_data;
84 u8 data_mask;
85
86 unsigned long phys_addr;
87 void __iomem *virt_addr;
88 unsigned long found;
89
90 struct w1_bus_master *bus_master;
91};
92
93static u8 matrox_w1_read_ddc_bit(unsigned long);
94static void matrox_w1_write_ddc_bit(unsigned long, u8);
95
96/*
97 * These functions read and write DDC Data bit.
98 *
99 * Using tristate pins, since i can't find any open-drain pin in whole motherboard.
100 * Unfortunately we can't connect to Intel's 82801xx IO controller
101 * since we don't know motherboard schema, wich has pretty unused(may be not) GPIO.
102 *
103 * I've heard that PIIX also has open drain pin.
104 *
105 * Port mapping.
106 */
107static __inline__ u8 matrox_w1_read_reg(struct matrox_device *dev, u8 reg)
108{
109 u8 ret;
110
111 writeb(reg, dev->port_index);
112 ret = readb(dev->port_data);
113 barrier();
114
115 return ret;
116}
117
118static __inline__ void matrox_w1_write_reg(struct matrox_device *dev, u8 reg, u8 val)
119{
120 writeb(reg, dev->port_index);
121 writeb(val, dev->port_data);
122 wmb();
123}
124
125static void matrox_w1_write_ddc_bit(unsigned long data, u8 bit)
126{
127 u8 ret;
128 struct matrox_device *dev = (struct matrox_device *) data;
129
130 if (bit)
131 bit = 0;
132 else
133 bit = dev->data_mask;
134
135 ret = matrox_w1_read_reg(dev, MATROX_GET_CONTROL);
136 matrox_w1_write_reg(dev, MATROX_GET_CONTROL, ((ret & ~dev->data_mask) | bit));
137 matrox_w1_write_reg(dev, MATROX_GET_DATA, 0x00);
138}
139
140static u8 matrox_w1_read_ddc_bit(unsigned long data)
141{
142 u8 ret;
143 struct matrox_device *dev = (struct matrox_device *) data;
144
145 ret = matrox_w1_read_reg(dev, MATROX_GET_DATA);
146
147 return ret;
148}
149
150static void matrox_w1_hw_init(struct matrox_device *dev)
151{
152 matrox_w1_write_reg(dev, MATROX_GET_DATA, 0xFF);
153 matrox_w1_write_reg(dev, MATROX_GET_CONTROL, 0x00);
154}
155
156static int __devinit matrox_w1_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
157{
158 struct matrox_device *dev;
159 int err;
160
161 assert(pdev != NULL);
162 assert(ent != NULL);
163
164 if (pdev->vendor != PCI_VENDOR_ID_MATROX || pdev->device != PCI_DEVICE_ID_MATROX_G400)
165 return -ENODEV;
166
167 dev = kmalloc(sizeof(struct matrox_device) +
168 sizeof(struct w1_bus_master), GFP_KERNEL);
169 if (!dev) {
170 dev_err(&pdev->dev,
171 "%s: Failed to create new matrox_device object.\n",
172 __func__);
173 return -ENOMEM;
174 }
175
176 memset(dev, 0, sizeof(struct matrox_device) + sizeof(struct w1_bus_master));
177
178 dev->bus_master = (struct w1_bus_master *)(dev + 1);
179
180 /*
181 * True for G400, for some other we need resource 0, see drivers/video/matrox/matroxfb_base.c
182 */
183
184 dev->phys_addr = pci_resource_start(pdev, 1);
185
186 dev->virt_addr = ioremap_nocache(dev->phys_addr, 16384);
187 if (!dev->virt_addr) {
188 dev_err(&pdev->dev, "%s: failed to ioremap(0x%lx, %d).\n",
189 __func__, dev->phys_addr, 16384);
190 err = -EIO;
191 goto err_out_free_device;
192 }
193
194 dev->base_addr = dev->virt_addr + MATROX_BASE;
195 dev->port_index = dev->base_addr + MATROX_PORT_INDEX_OFFSET;
196 dev->port_data = dev->base_addr + MATROX_PORT_DATA_OFFSET;
197 dev->data_mask = (MATROX_G400_DDC_DATA);
198
199 matrox_w1_hw_init(dev);
200
201 dev->bus_master->data = (unsigned long) dev;
202 dev->bus_master->read_bit = &matrox_w1_read_ddc_bit;
203 dev->bus_master->write_bit = &matrox_w1_write_ddc_bit;
204
205 err = w1_add_master_device(dev->bus_master);
206 if (err)
207 goto err_out_free_device;
208
209 pci_set_drvdata(pdev, dev);
210
211 dev->found = 1;
212
213 dev_info(&pdev->dev, "Matrox G400 GPIO transport layer for 1-wire.\n");
214
215 return 0;
216
217err_out_free_device:
218 kfree(dev);
219
220 return err;
221}
222
223static void __devexit matrox_w1_remove(struct pci_dev *pdev)
224{
225 struct matrox_device *dev = pci_get_drvdata(pdev);
226
227 assert(dev != NULL);
228
229 if (dev->found) {
230 w1_remove_master_device(dev->bus_master);
231 iounmap(dev->virt_addr);
232 }
233 kfree(dev);
234}
235
236static int __init matrox_w1_init(void)
237{
238 return pci_register_driver(&matrox_w1_pci_driver);
239}
240
241static void __exit matrox_w1_fini(void)
242{
243 pci_unregister_driver(&matrox_w1_pci_driver);
244}
245
246module_init(matrox_w1_init);
247module_exit(matrox_w1_fini);
diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
new file mode 100644
index 000000000000..fd630cec0e79
--- /dev/null
+++ b/drivers/w1/w1.c
@@ -0,0 +1,835 @@
1/*
2 * w1.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 terms 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 <linux/delay.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/list.h>
27#include <linux/interrupt.h>
28#include <linux/spinlock.h>
29#include <linux/timer.h>
30#include <linux/device.h>
31#include <linux/slab.h>
32#include <linux/sched.h>
33
34#include <asm/atomic.h>
35
36#include "w1.h"
37#include "w1_io.h"
38#include "w1_log.h"
39#include "w1_int.h"
40#include "w1_family.h"
41#include "w1_netlink.h"
42
43MODULE_LICENSE("GPL");
44MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
45MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
46
47static int w1_timeout = 10;
48int w1_max_slave_count = 10;
49int w1_max_slave_ttl = 10;
50
51module_param_named(timeout, w1_timeout, int, 0);
52module_param_named(max_slave_count, w1_max_slave_count, int, 0);
53module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
54
55DEFINE_SPINLOCK(w1_mlock);
56LIST_HEAD(w1_masters);
57
58static pid_t control_thread;
59static int control_needs_exit;
60static DECLARE_COMPLETION(w1_control_complete);
61
62static int w1_master_match(struct device *dev, struct device_driver *drv)
63{
64 return 1;
65}
66
67static int w1_master_probe(struct device *dev)
68{
69 return -ENODEV;
70}
71
72static int w1_master_remove(struct device *dev)
73{
74 return 0;
75}
76
77static void w1_master_release(struct device *dev)
78{
79 struct w1_master *md = container_of(dev, struct w1_master, dev);
80
81 complete(&md->dev_released);
82}
83
84static void w1_slave_release(struct device *dev)
85{
86 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
87
88 complete(&sl->dev_released);
89}
90
91static ssize_t w1_default_read_name(struct device *dev, char *buf)
92{
93 return sprintf(buf, "No family registered.\n");
94}
95
96static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off,
97 size_t count)
98{
99 return sprintf(buf, "No family registered.\n");
100}
101
102static struct bus_type w1_bus_type = {
103 .name = "w1",
104 .match = w1_master_match,
105};
106
107struct device_driver w1_driver = {
108 .name = "w1_driver",
109 .bus = &w1_bus_type,
110 .probe = w1_master_probe,
111 .remove = w1_master_remove,
112};
113
114struct device w1_device = {
115 .parent = NULL,
116 .bus = &w1_bus_type,
117 .bus_id = "w1 bus master",
118 .driver = &w1_driver,
119 .release = &w1_master_release
120};
121
122static struct device_attribute w1_slave_attribute = {
123 .attr = {
124 .name = "name",
125 .mode = S_IRUGO,
126 .owner = THIS_MODULE
127 },
128 .show = &w1_default_read_name,
129};
130
131static struct device_attribute w1_slave_attribute_val = {
132 .attr = {
133 .name = "value",
134 .mode = S_IRUGO,
135 .owner = THIS_MODULE
136 },
137 .show = &w1_default_read_name,
138};
139
140static ssize_t w1_master_attribute_show_name(struct device *dev, char *buf)
141{
142 struct w1_master *md = container_of (dev, struct w1_master, dev);
143 ssize_t count;
144
145 if (down_interruptible (&md->mutex))
146 return -EBUSY;
147
148 count = sprintf(buf, "%s\n", md->name);
149
150 up(&md->mutex);
151
152 return count;
153}
154
155static ssize_t w1_master_attribute_show_pointer(struct device *dev, char *buf)
156{
157 struct w1_master *md = container_of(dev, struct w1_master, dev);
158 ssize_t count;
159
160 if (down_interruptible(&md->mutex))
161 return -EBUSY;
162
163 count = sprintf(buf, "0x%p\n", md->bus_master);
164
165 up(&md->mutex);
166 return count;
167}
168
169static ssize_t w1_master_attribute_show_timeout(struct device *dev, char *buf)
170{
171 ssize_t count;
172 count = sprintf(buf, "%d\n", w1_timeout);
173 return count;
174}
175
176static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, char *buf)
177{
178 struct w1_master *md = container_of(dev, struct w1_master, dev);
179 ssize_t count;
180
181 if (down_interruptible(&md->mutex))
182 return -EBUSY;
183
184 count = sprintf(buf, "%d\n", md->max_slave_count);
185
186 up(&md->mutex);
187 return count;
188}
189
190static ssize_t w1_master_attribute_show_attempts(struct device *dev, char *buf)
191{
192 struct w1_master *md = container_of(dev, struct w1_master, dev);
193 ssize_t count;
194
195 if (down_interruptible(&md->mutex))
196 return -EBUSY;
197
198 count = sprintf(buf, "%lu\n", md->attempts);
199
200 up(&md->mutex);
201 return count;
202}
203
204static ssize_t w1_master_attribute_show_slave_count(struct device *dev, char *buf)
205{
206 struct w1_master *md = container_of(dev, struct w1_master, dev);
207 ssize_t count;
208
209 if (down_interruptible(&md->mutex))
210 return -EBUSY;
211
212 count = sprintf(buf, "%d\n", md->slave_count);
213
214 up(&md->mutex);
215 return count;
216}
217
218static ssize_t w1_master_attribute_show_slaves(struct device *dev, char *buf)
219
220{
221 struct w1_master *md = container_of(dev, struct w1_master, dev);
222 int c = PAGE_SIZE;
223
224 if (down_interruptible(&md->mutex))
225 return -EBUSY;
226
227 if (md->slave_count == 0)
228 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
229 else {
230 struct list_head *ent, *n;
231 struct w1_slave *sl;
232
233 list_for_each_safe(ent, n, &md->slist) {
234 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
235
236 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
237 }
238 }
239
240 up(&md->mutex);
241
242 return PAGE_SIZE - c;
243}
244
245static struct device_attribute w1_master_attribute_slaves = {
246 .attr = {
247 .name = "w1_master_slaves",
248 .mode = S_IRUGO,
249 .owner = THIS_MODULE,
250 },
251 .show = &w1_master_attribute_show_slaves,
252};
253static struct device_attribute w1_master_attribute_slave_count = {
254 .attr = {
255 .name = "w1_master_slave_count",
256 .mode = S_IRUGO,
257 .owner = THIS_MODULE
258 },
259 .show = &w1_master_attribute_show_slave_count,
260};
261static struct device_attribute w1_master_attribute_attempts = {
262 .attr = {
263 .name = "w1_master_attempts",
264 .mode = S_IRUGO,
265 .owner = THIS_MODULE
266 },
267 .show = &w1_master_attribute_show_attempts,
268};
269static struct device_attribute w1_master_attribute_max_slave_count = {
270 .attr = {
271 .name = "w1_master_max_slave_count",
272 .mode = S_IRUGO,
273 .owner = THIS_MODULE
274 },
275 .show = &w1_master_attribute_show_max_slave_count,
276};
277static struct device_attribute w1_master_attribute_timeout = {
278 .attr = {
279 .name = "w1_master_timeout",
280 .mode = S_IRUGO,
281 .owner = THIS_MODULE
282 },
283 .show = &w1_master_attribute_show_timeout,
284};
285static struct device_attribute w1_master_attribute_pointer = {
286 .attr = {
287 .name = "w1_master_pointer",
288 .mode = S_IRUGO,
289 .owner = THIS_MODULE
290 },
291 .show = &w1_master_attribute_show_pointer,
292};
293static struct device_attribute w1_master_attribute_name = {
294 .attr = {
295 .name = "w1_master_name",
296 .mode = S_IRUGO,
297 .owner = THIS_MODULE
298 },
299 .show = &w1_master_attribute_show_name,
300};
301
302static struct bin_attribute w1_slave_bin_attribute = {
303 .attr = {
304 .name = "w1_slave",
305 .mode = S_IRUGO,
306 .owner = THIS_MODULE,
307 },
308 .size = W1_SLAVE_DATA_SIZE,
309 .read = &w1_default_read_bin,
310};
311
312static int __w1_attach_slave_device(struct w1_slave *sl)
313{
314 int err;
315
316 sl->dev.parent = &sl->master->dev;
317 sl->dev.driver = sl->master->driver;
318 sl->dev.bus = &w1_bus_type;
319 sl->dev.release = &w1_slave_release;
320
321 snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
322 "%02x-%012llx",
323 (unsigned int) sl->reg_num.family,
324 (unsigned long long) sl->reg_num.id);
325 snprintf (&sl->name[0], sizeof(sl->name),
326 "%02x-%012llx",
327 (unsigned int) sl->reg_num.family,
328 (unsigned long long) sl->reg_num.id);
329
330 dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,
331 &sl->dev.bus_id[0]);
332
333 err = device_register(&sl->dev);
334 if (err < 0) {
335 dev_err(&sl->dev,
336 "Device registration [%s] failed. err=%d\n",
337 sl->dev.bus_id, err);
338 return err;
339 }
340
341 memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin));
342 memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name));
343 memcpy(&sl->attr_val, &w1_slave_attribute_val, sizeof(sl->attr_val));
344
345 sl->attr_bin.read = sl->family->fops->rbin;
346 sl->attr_name.show = sl->family->fops->rname;
347 sl->attr_val.show = sl->family->fops->rval;
348 sl->attr_val.attr.name = sl->family->fops->rvalname;
349
350 err = device_create_file(&sl->dev, &sl->attr_name);
351 if (err < 0) {
352 dev_err(&sl->dev,
353 "sysfs file creation for [%s] failed. err=%d\n",
354 sl->dev.bus_id, err);
355 device_unregister(&sl->dev);
356 return err;
357 }
358
359 err = device_create_file(&sl->dev, &sl->attr_val);
360 if (err < 0) {
361 dev_err(&sl->dev,
362 "sysfs file creation for [%s] failed. err=%d\n",
363 sl->dev.bus_id, err);
364 device_remove_file(&sl->dev, &sl->attr_name);
365 device_unregister(&sl->dev);
366 return err;
367 }
368
369 err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin);
370 if (err < 0) {
371 dev_err(&sl->dev,
372 "sysfs file creation for [%s] failed. err=%d\n",
373 sl->dev.bus_id, err);
374 device_remove_file(&sl->dev, &sl->attr_name);
375 device_remove_file(&sl->dev, &sl->attr_val);
376 device_unregister(&sl->dev);
377 return err;
378 }
379
380 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
381
382 return 0;
383}
384
385static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
386{
387 struct w1_slave *sl;
388 struct w1_family *f;
389 int err;
390 struct w1_netlink_msg msg;
391
392 sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
393 if (!sl) {
394 dev_err(&dev->dev,
395 "%s: failed to allocate new slave device.\n",
396 __func__);
397 return -ENOMEM;
398 }
399
400 memset(sl, 0, sizeof(*sl));
401
402 sl->owner = THIS_MODULE;
403 sl->master = dev;
404 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
405
406 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
407 atomic_set(&sl->refcnt, 0);
408 init_completion(&sl->dev_released);
409
410 spin_lock(&w1_flock);
411 f = w1_family_registered(rn->family);
412 if (!f) {
413 spin_unlock(&w1_flock);
414 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
415 rn->family, rn->family,
416 (unsigned long long)rn->id, rn->crc);
417 kfree(sl);
418 return -ENODEV;
419 }
420 __w1_family_get(f);
421 spin_unlock(&w1_flock);
422
423 sl->family = f;
424
425
426 err = __w1_attach_slave_device(sl);
427 if (err < 0) {
428 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
429 sl->name);
430 w1_family_put(sl->family);
431 kfree(sl);
432 return err;
433 }
434
435 sl->ttl = dev->slave_ttl;
436 dev->slave_count++;
437
438 memcpy(&msg.id.id, rn, sizeof(msg.id.id));
439 msg.type = W1_SLAVE_ADD;
440 w1_netlink_send(dev, &msg);
441
442 return 0;
443}
444
445static void w1_slave_detach(struct w1_slave *sl)
446{
447 struct w1_netlink_msg msg;
448
449 dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name);
450
451 while (atomic_read(&sl->refcnt)) {
452 printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
453 sl->name, atomic_read(&sl->refcnt));
454
455 if (msleep_interruptible(1000))
456 flush_signals(current);
457 }
458
459 sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin);
460 device_remove_file(&sl->dev, &sl->attr_name);
461 device_remove_file(&sl->dev, &sl->attr_val);
462 device_unregister(&sl->dev);
463 w1_family_put(sl->family);
464
465 memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id));
466 msg.type = W1_SLAVE_REMOVE;
467 w1_netlink_send(sl->master, &msg);
468}
469
470static struct w1_master *w1_search_master(unsigned long data)
471{
472 struct w1_master *dev;
473 int found = 0;
474
475 spin_lock_irq(&w1_mlock);
476 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
477 if (dev->bus_master->data == data) {
478 found = 1;
479 atomic_inc(&dev->refcnt);
480 break;
481 }
482 }
483 spin_unlock_irq(&w1_mlock);
484
485 return (found)?dev:NULL;
486}
487
488void w1_slave_found(unsigned long data, u64 rn)
489{
490 int slave_count;
491 struct w1_slave *sl;
492 struct list_head *ent;
493 struct w1_reg_num *tmp;
494 int family_found = 0;
495 struct w1_master *dev;
496
497 dev = w1_search_master(data);
498 if (!dev) {
499 printk(KERN_ERR "Failed to find w1 master device for data %08lx, it is impossible.\n",
500 data);
501 return;
502 }
503
504 tmp = (struct w1_reg_num *) &rn;
505
506 slave_count = 0;
507 list_for_each(ent, &dev->slist) {
508
509 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
510
511 if (sl->reg_num.family == tmp->family &&
512 sl->reg_num.id == tmp->id &&
513 sl->reg_num.crc == tmp->crc) {
514 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
515 break;
516 }
517 else if (sl->reg_num.family == tmp->family) {
518 family_found = 1;
519 break;
520 }
521
522 slave_count++;
523 }
524
525 if (slave_count == dev->slave_count && rn ) {
526 tmp = cpu_to_le64(rn);
527 if(((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&tmp, 7))
528 w1_attach_slave_device(dev, (struct w1_reg_num *) &rn);
529 }
530
531 atomic_dec(&dev->refcnt);
532}
533
534void w1_search(struct w1_master *dev)
535{
536 u64 last, rn, tmp;
537 int i, count = 0;
538 int last_family_desc, last_zero, last_device;
539 int search_bit, id_bit, comp_bit, desc_bit;
540
541 search_bit = id_bit = comp_bit = 0;
542 rn = tmp = last = 0;
543 last_device = last_zero = last_family_desc = 0;
544
545 desc_bit = 64;
546
547 while (!(id_bit && comp_bit) && !last_device
548 && count++ < dev->max_slave_count) {
549 last = rn;
550 rn = 0;
551
552 last_family_desc = 0;
553
554 /*
555 * Reset bus and all 1-wire device state machines
556 * so they can respond to our requests.
557 *
558 * Return 0 - device(s) present, 1 - no devices present.
559 */
560 if (w1_reset_bus(dev)) {
561 dev_info(&dev->dev, "No devices present on the wire.\n");
562 break;
563 }
564
565#if 1
566 w1_write_8(dev, W1_SEARCH);
567 for (i = 0; i < 64; ++i) {
568 /*
569 * Read 2 bits from bus.
570 * All who don't sleep must send ID bit and COMPLEMENT ID bit.
571 * They actually are ANDed between all senders.
572 */
573 id_bit = w1_touch_bit(dev, 1);
574 comp_bit = w1_touch_bit(dev, 1);
575
576 if (id_bit && comp_bit)
577 break;
578
579 if (id_bit == 0 && comp_bit == 0) {
580 if (i == desc_bit)
581 search_bit = 1;
582 else if (i > desc_bit)
583 search_bit = 0;
584 else
585 search_bit = ((last >> i) & 0x1);
586
587 if (search_bit == 0) {
588 last_zero = i;
589 if (last_zero < 9)
590 last_family_desc = last_zero;
591 }
592
593 }
594 else
595 search_bit = id_bit;
596
597 tmp = search_bit;
598 rn |= (tmp << i);
599
600 /*
601 * Write 1 bit to bus
602 * and make all who don't have "search_bit" in "i"'th position
603 * in it's registration number sleep.
604 */
605 if (dev->bus_master->touch_bit)
606 w1_touch_bit(dev, search_bit);
607 else
608 w1_write_bit(dev, search_bit);
609
610 }
611#endif
612
613 if (desc_bit == last_zero)
614 last_device = 1;
615
616 desc_bit = last_zero;
617
618 w1_slave_found(dev->bus_master->data, rn);
619 }
620}
621
622int w1_create_master_attributes(struct w1_master *dev)
623{
624 if ( device_create_file(&dev->dev, &w1_master_attribute_slaves) < 0 ||
625 device_create_file(&dev->dev, &w1_master_attribute_slave_count) < 0 ||
626 device_create_file(&dev->dev, &w1_master_attribute_attempts) < 0 ||
627 device_create_file(&dev->dev, &w1_master_attribute_max_slave_count) < 0 ||
628 device_create_file(&dev->dev, &w1_master_attribute_timeout) < 0||
629 device_create_file(&dev->dev, &w1_master_attribute_pointer) < 0||
630 device_create_file(&dev->dev, &w1_master_attribute_name) < 0)
631 return -EINVAL;
632
633 return 0;
634}
635
636void w1_destroy_master_attributes(struct w1_master *dev)
637{
638 device_remove_file(&dev->dev, &w1_master_attribute_slaves);
639 device_remove_file(&dev->dev, &w1_master_attribute_slave_count);
640 device_remove_file(&dev->dev, &w1_master_attribute_attempts);
641 device_remove_file(&dev->dev, &w1_master_attribute_max_slave_count);
642 device_remove_file(&dev->dev, &w1_master_attribute_timeout);
643 device_remove_file(&dev->dev, &w1_master_attribute_pointer);
644 device_remove_file(&dev->dev, &w1_master_attribute_name);
645}
646
647
648int w1_control(void *data)
649{
650 struct w1_slave *sl;
651 struct w1_master *dev;
652 struct list_head *ent, *ment, *n, *mn;
653 int err, have_to_wait = 0;
654
655 daemonize("w1_control");
656 allow_signal(SIGTERM);
657
658 while (!control_needs_exit || have_to_wait) {
659 have_to_wait = 0;
660
661 try_to_freeze(PF_FREEZE);
662 msleep_interruptible(w1_timeout * 1000);
663
664 if (signal_pending(current))
665 flush_signals(current);
666
667 list_for_each_safe(ment, mn, &w1_masters) {
668 dev = list_entry(ment, struct w1_master, w1_master_entry);
669
670 if (!control_needs_exit && !dev->need_exit)
671 continue;
672 /*
673 * Little race: we can create thread but not set the flag.
674 * Get a chance for external process to set flag up.
675 */
676 if (!dev->initialized) {
677 have_to_wait = 1;
678 continue;
679 }
680
681 spin_lock(&w1_mlock);
682 list_del(&dev->w1_master_entry);
683 spin_unlock(&w1_mlock);
684
685 if (control_needs_exit) {
686 dev->need_exit = 1;
687
688 err = kill_proc(dev->kpid, SIGTERM, 1);
689 if (err)
690 dev_err(&dev->dev,
691 "Failed to send signal to w1 kernel thread %d.\n",
692 dev->kpid);
693 }
694
695 wait_for_completion(&dev->dev_exited);
696
697 list_for_each_safe(ent, n, &dev->slist) {
698 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
699
700 if (!sl)
701 dev_warn(&dev->dev,
702 "%s: slave entry is NULL.\n",
703 __func__);
704 else {
705 list_del(&sl->w1_slave_entry);
706
707 w1_slave_detach(sl);
708 kfree(sl);
709 }
710 }
711 w1_destroy_master_attributes(dev);
712 atomic_dec(&dev->refcnt);
713 }
714 }
715
716 complete_and_exit(&w1_control_complete, 0);
717}
718
719int w1_process(void *data)
720{
721 struct w1_master *dev = (struct w1_master *) data;
722 struct list_head *ent, *n;
723 struct w1_slave *sl;
724
725 daemonize("%s", dev->name);
726 allow_signal(SIGTERM);
727
728 while (!dev->need_exit) {
729 try_to_freeze(PF_FREEZE);
730 msleep_interruptible(w1_timeout * 1000);
731
732 if (signal_pending(current))
733 flush_signals(current);
734
735 if (dev->need_exit)
736 break;
737
738 if (!dev->initialized)
739 continue;
740
741 if (down_interruptible(&dev->mutex))
742 continue;
743
744 list_for_each_safe(ent, n, &dev->slist) {
745 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
746
747 if (sl)
748 clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
749 }
750
751 w1_search_devices(dev, w1_slave_found);
752
753 list_for_each_safe(ent, n, &dev->slist) {
754 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
755
756 if (sl && !test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
757 list_del (&sl->w1_slave_entry);
758
759 w1_slave_detach (sl);
760 kfree (sl);
761
762 dev->slave_count--;
763 }
764 else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
765 sl->ttl = dev->slave_ttl;
766 }
767 up(&dev->mutex);
768 }
769
770 atomic_dec(&dev->refcnt);
771 complete_and_exit(&dev->dev_exited, 0);
772
773 return 0;
774}
775
776int w1_init(void)
777{
778 int retval;
779
780 printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
781
782 retval = bus_register(&w1_bus_type);
783 if (retval) {
784 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
785 goto err_out_exit_init;
786 }
787
788 retval = driver_register(&w1_driver);
789 if (retval) {
790 printk(KERN_ERR
791 "Failed to register master driver. err=%d.\n",
792 retval);
793 goto err_out_bus_unregister;
794 }
795
796 control_thread = kernel_thread(&w1_control, NULL, 0);
797 if (control_thread < 0) {
798 printk(KERN_ERR "Failed to create control thread. err=%d\n",
799 control_thread);
800 retval = control_thread;
801 goto err_out_driver_unregister;
802 }
803
804 return 0;
805
806err_out_driver_unregister:
807 driver_unregister(&w1_driver);
808
809err_out_bus_unregister:
810 bus_unregister(&w1_bus_type);
811
812err_out_exit_init:
813 return retval;
814}
815
816void w1_fini(void)
817{
818 struct w1_master *dev;
819 struct list_head *ent, *n;
820
821 list_for_each_safe(ent, n, &w1_masters) {
822 dev = list_entry(ent, struct w1_master, w1_master_entry);
823 __w1_remove_master_device(dev);
824 }
825
826 control_needs_exit = 1;
827
828 wait_for_completion(&w1_control_complete);
829
830 driver_unregister(&w1_driver);
831 bus_unregister(&w1_bus_type);
832}
833
834module_init(w1_init);
835module_exit(w1_fini);
diff --git a/drivers/w1/w1.h b/drivers/w1/w1.h
new file mode 100644
index 000000000000..abbddaf3f8e2
--- /dev/null
+++ b/drivers/w1/w1.h
@@ -0,0 +1,145 @@
1/*
2 * w1.h
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 terms 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#ifndef __W1_H
23#define __W1_H
24
25struct w1_reg_num
26{
27#if defined(__LITTLE_ENDIAN_BITFIELD)
28 __u64 family:8,
29 id:48,
30 crc:8;
31#elif defined(__BIG_ENDIAN_BITFIELD)
32 __u64 crc:8,
33 id:48,
34 family:8;
35#else
36#error "Please fix <asm/byteorder.h>"
37#endif
38};
39
40#ifdef __KERNEL__
41
42#include <linux/completion.h>
43#include <linux/device.h>
44
45#include <net/sock.h>
46
47#include <asm/semaphore.h>
48
49#include "w1_family.h"
50
51#define W1_MAXNAMELEN 32
52#define W1_SLAVE_DATA_SIZE 128
53
54#define W1_SEARCH 0xF0
55#define W1_CONDITIONAL_SEARCH 0xEC
56#define W1_CONVERT_TEMP 0x44
57#define W1_SKIP_ROM 0xCC
58#define W1_READ_SCRATCHPAD 0xBE
59#define W1_READ_ROM 0x33
60#define W1_READ_PSUPPLY 0xB4
61#define W1_MATCH_ROM 0x55
62
63#define W1_SLAVE_ACTIVE (1<<0)
64
65struct w1_slave
66{
67 struct module *owner;
68 unsigned char name[W1_MAXNAMELEN];
69 struct list_head w1_slave_entry;
70 struct w1_reg_num reg_num;
71 atomic_t refcnt;
72 u8 rom[9];
73 u32 flags;
74 int ttl;
75
76 struct w1_master *master;
77 struct w1_family *family;
78 struct device dev;
79 struct completion dev_released;
80
81 struct bin_attribute attr_bin;
82 struct device_attribute attr_name, attr_val;
83};
84
85typedef void (* w1_slave_found_callback)(unsigned long, u64);
86
87struct w1_bus_master
88{
89 unsigned long data;
90
91 u8 (*read_bit)(unsigned long);
92 void (*write_bit)(unsigned long, u8);
93
94 u8 (*read_byte)(unsigned long);
95 void (*write_byte)(unsigned long, u8);
96
97 u8 (*read_block)(unsigned long, u8 *, int);
98 void (*write_block)(unsigned long, u8 *, int);
99
100 u8 (*touch_bit)(unsigned long, u8);
101
102 u8 (*reset_bus)(unsigned long);
103
104 void (*search)(unsigned long, w1_slave_found_callback);
105};
106
107struct w1_master
108{
109 struct list_head w1_master_entry;
110 struct module *owner;
111 unsigned char name[W1_MAXNAMELEN];
112 struct list_head slist;
113 int max_slave_count, slave_count;
114 unsigned long attempts;
115 int slave_ttl;
116 int initialized;
117 u32 id;
118
119 atomic_t refcnt;
120
121 void *priv;
122 int priv_size;
123
124 int need_exit;
125 pid_t kpid;
126 struct semaphore mutex;
127
128 struct device_driver *driver;
129 struct device dev;
130 struct completion dev_released;
131 struct completion dev_exited;
132
133 struct w1_bus_master *bus_master;
134
135 u32 seq, groups;
136 struct sock *nls;
137};
138
139int w1_create_master_attributes(struct w1_master *);
140void w1_destroy_master_attributes(struct w1_master *);
141void w1_search(struct w1_master *dev);
142
143#endif /* __KERNEL__ */
144
145#endif /* __W1_H */
diff --git a/drivers/w1/w1_family.c b/drivers/w1/w1_family.c
new file mode 100644
index 000000000000..d1d56eca1061
--- /dev/null
+++ b/drivers/w1/w1_family.c
@@ -0,0 +1,150 @@
1/*
2 * w1_family.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 terms 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 <linux/spinlock.h>
23#include <linux/list.h>
24#include <linux/delay.h>
25
26#include "w1_family.h"
27
28DEFINE_SPINLOCK(w1_flock);
29static LIST_HEAD(w1_families);
30
31static int w1_check_family(struct w1_family *f)
32{
33 if (!f->fops->rname || !f->fops->rbin || !f->fops->rval || !f->fops->rvalname)
34 return -EINVAL;
35
36 return 0;
37}
38
39int w1_register_family(struct w1_family *newf)
40{
41 struct list_head *ent, *n;
42 struct w1_family *f;
43 int ret = 0;
44
45 if (w1_check_family(newf))
46 return -EINVAL;
47
48 spin_lock(&w1_flock);
49 list_for_each_safe(ent, n, &w1_families) {
50 f = list_entry(ent, struct w1_family, family_entry);
51
52 if (f->fid == newf->fid) {
53 ret = -EEXIST;
54 break;
55 }
56 }
57
58 if (!ret) {
59 atomic_set(&newf->refcnt, 0);
60 newf->need_exit = 0;
61 list_add_tail(&newf->family_entry, &w1_families);
62 }
63
64 spin_unlock(&w1_flock);
65
66 return ret;
67}
68
69void w1_unregister_family(struct w1_family *fent)
70{
71 struct list_head *ent, *n;
72 struct w1_family *f;
73
74 spin_lock(&w1_flock);
75 list_for_each_safe(ent, n, &w1_families) {
76 f = list_entry(ent, struct w1_family, family_entry);
77
78 if (f->fid == fent->fid) {
79 list_del(&fent->family_entry);
80 break;
81 }
82 }
83
84 fent->need_exit = 1;
85
86 spin_unlock(&w1_flock);
87
88 while (atomic_read(&fent->refcnt)) {
89 printk(KERN_INFO "Waiting for family %u to become free: refcnt=%d.\n",
90 fent->fid, atomic_read(&fent->refcnt));
91
92 if (msleep_interruptible(1000))
93 flush_signals(current);
94 }
95}
96
97/*
98 * Should be called under w1_flock held.
99 */
100struct w1_family * w1_family_registered(u8 fid)
101{
102 struct list_head *ent, *n;
103 struct w1_family *f = NULL;
104 int ret = 0;
105
106 list_for_each_safe(ent, n, &w1_families) {
107 f = list_entry(ent, struct w1_family, family_entry);
108
109 if (f->fid == fid) {
110 ret = 1;
111 break;
112 }
113 }
114
115 return (ret) ? f : NULL;
116}
117
118void w1_family_put(struct w1_family *f)
119{
120 spin_lock(&w1_flock);
121 __w1_family_put(f);
122 spin_unlock(&w1_flock);
123}
124
125void __w1_family_put(struct w1_family *f)
126{
127 if (atomic_dec_and_test(&f->refcnt))
128 f->need_exit = 1;
129}
130
131void w1_family_get(struct w1_family *f)
132{
133 spin_lock(&w1_flock);
134 __w1_family_get(f);
135 spin_unlock(&w1_flock);
136
137}
138
139void __w1_family_get(struct w1_family *f)
140{
141 smp_mb__before_atomic_inc();
142 atomic_inc(&f->refcnt);
143 smp_mb__after_atomic_inc();
144}
145
146EXPORT_SYMBOL(w1_family_get);
147EXPORT_SYMBOL(w1_family_put);
148EXPORT_SYMBOL(w1_family_registered);
149EXPORT_SYMBOL(w1_unregister_family);
150EXPORT_SYMBOL(w1_register_family);
diff --git a/drivers/w1/w1_family.h b/drivers/w1/w1_family.h
new file mode 100644
index 000000000000..03a2de7a601f
--- /dev/null
+++ b/drivers/w1/w1_family.h
@@ -0,0 +1,65 @@
1/*
2 * w1_family.h
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 terms 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#ifndef __W1_FAMILY_H
23#define __W1_FAMILY_H
24
25#include <linux/types.h>
26#include <linux/device.h>
27#include <asm/atomic.h>
28
29#define W1_FAMILY_DEFAULT 0
30#define W1_FAMILY_THERM 0x10
31#define W1_FAMILY_SMEM 0x01
32
33#define MAXNAMELEN 32
34
35struct w1_family_ops
36{
37 ssize_t (* rname)(struct device *, char *);
38 ssize_t (* rbin)(struct kobject *, char *, loff_t, size_t);
39
40 ssize_t (* rval)(struct device *, char *);
41 unsigned char rvalname[MAXNAMELEN];
42};
43
44struct w1_family
45{
46 struct list_head family_entry;
47 u8 fid;
48
49 struct w1_family_ops *fops;
50
51 atomic_t refcnt;
52 u8 need_exit;
53};
54
55extern spinlock_t w1_flock;
56
57void w1_family_get(struct w1_family *);
58void w1_family_put(struct w1_family *);
59void __w1_family_get(struct w1_family *);
60void __w1_family_put(struct w1_family *);
61struct w1_family * w1_family_registered(u8);
62void w1_unregister_family(struct w1_family *);
63int w1_register_family(struct w1_family *);
64
65#endif /* __W1_FAMILY_H */
diff --git a/drivers/w1/w1_int.c b/drivers/w1/w1_int.c
new file mode 100644
index 000000000000..5f0bafbbd575
--- /dev/null
+++ b/drivers/w1/w1_int.c
@@ -0,0 +1,220 @@
1/*
2 * w1_int.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 terms 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 <linux/kernel.h>
23#include <linux/list.h>
24#include <linux/delay.h>
25
26#include "w1.h"
27#include "w1_log.h"
28#include "w1_netlink.h"
29
30static u32 w1_ids = 1;
31
32extern struct device_driver w1_driver;
33extern struct bus_type w1_bus_type;
34extern struct device w1_device;
35extern int w1_max_slave_count;
36extern int w1_max_slave_ttl;
37extern struct list_head w1_masters;
38extern spinlock_t w1_mlock;
39
40extern int w1_process(void *);
41
42struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
43 struct device_driver *driver, struct device *device)
44{
45 struct w1_master *dev;
46 int err;
47
48 /*
49 * We are in process context(kernel thread), so can sleep.
50 */
51 dev = kmalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
52 if (!dev) {
53 printk(KERN_ERR
54 "Failed to allocate %zd bytes for new w1 device.\n",
55 sizeof(struct w1_master));
56 return NULL;
57 }
58
59 memset(dev, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
60
61 dev->bus_master = (struct w1_bus_master *)(dev + 1);
62
63 dev->owner = THIS_MODULE;
64 dev->max_slave_count = slave_count;
65 dev->slave_count = 0;
66 dev->attempts = 0;
67 dev->kpid = -1;
68 dev->initialized = 0;
69 dev->id = id;
70 dev->slave_ttl = slave_ttl;
71
72 atomic_set(&dev->refcnt, 2);
73
74 INIT_LIST_HEAD(&dev->slist);
75 init_MUTEX(&dev->mutex);
76
77 init_completion(&dev->dev_released);
78 init_completion(&dev->dev_exited);
79
80 memcpy(&dev->dev, device, sizeof(struct device));
81 snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
82 "w1_bus_master%u", dev->id);
83 snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
84
85 dev->driver = driver;
86
87 dev->groups = 23;
88 dev->seq = 1;
89 dev->nls = netlink_kernel_create(NETLINK_NFLOG, NULL);
90 if (!dev->nls) {
91 printk(KERN_ERR "Failed to create new netlink socket(%u) for w1 master %s.\n",
92 NETLINK_NFLOG, dev->dev.bus_id);
93 }
94
95 err = device_register(&dev->dev);
96 if (err) {
97 printk(KERN_ERR "Failed to register master device. err=%d\n", err);
98 if (dev->nls && dev->nls->sk_socket)
99 sock_release(dev->nls->sk_socket);
100 memset(dev, 0, sizeof(struct w1_master));
101 kfree(dev);
102 dev = NULL;
103 }
104
105 return dev;
106}
107
108void w1_free_dev(struct w1_master *dev)
109{
110 device_unregister(&dev->dev);
111 if (dev->nls && dev->nls->sk_socket)
112 sock_release(dev->nls->sk_socket);
113 memset(dev, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
114 kfree(dev);
115}
116
117int w1_add_master_device(struct w1_bus_master *master)
118{
119 struct w1_master *dev;
120 int retval = 0;
121 struct w1_netlink_msg msg;
122
123 dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_driver, &w1_device);
124 if (!dev)
125 return -ENOMEM;
126
127 dev->kpid = kernel_thread(&w1_process, dev, 0);
128 if (dev->kpid < 0) {
129 dev_err(&dev->dev,
130 "Failed to create new kernel thread. err=%d\n",
131 dev->kpid);
132 retval = dev->kpid;
133 goto err_out_free_dev;
134 }
135
136 retval = w1_create_master_attributes(dev);
137 if (retval)
138 goto err_out_kill_thread;
139
140 memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
141
142 dev->initialized = 1;
143
144 spin_lock(&w1_mlock);
145 list_add(&dev->w1_master_entry, &w1_masters);
146 spin_unlock(&w1_mlock);
147
148 msg.id.mst.id = dev->id;
149 msg.id.mst.pid = dev->kpid;
150 msg.type = W1_MASTER_ADD;
151 w1_netlink_send(dev, &msg);
152
153 return 0;
154
155err_out_kill_thread:
156 dev->need_exit = 1;
157 if (kill_proc(dev->kpid, SIGTERM, 1))
158 dev_err(&dev->dev,
159 "Failed to send signal to w1 kernel thread %d.\n",
160 dev->kpid);
161 wait_for_completion(&dev->dev_exited);
162
163err_out_free_dev:
164 w1_free_dev(dev);
165
166 return retval;
167}
168
169void __w1_remove_master_device(struct w1_master *dev)
170{
171 int err;
172 struct w1_netlink_msg msg;
173
174 dev->need_exit = 1;
175 err = kill_proc(dev->kpid, SIGTERM, 1);
176 if (err)
177 dev_err(&dev->dev,
178 "%s: Failed to send signal to w1 kernel thread %d.\n",
179 __func__, dev->kpid);
180
181 while (atomic_read(&dev->refcnt)) {
182 printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
183 dev->name, atomic_read(&dev->refcnt));
184
185 if (msleep_interruptible(1000))
186 flush_signals(current);
187 }
188
189 msg.id.mst.id = dev->id;
190 msg.id.mst.pid = dev->kpid;
191 msg.type = W1_MASTER_REMOVE;
192 w1_netlink_send(dev, &msg);
193
194 w1_free_dev(dev);
195}
196
197void w1_remove_master_device(struct w1_bus_master *bm)
198{
199 struct w1_master *dev = NULL;
200 struct list_head *ent, *n;
201
202 list_for_each_safe(ent, n, &w1_masters) {
203 dev = list_entry(ent, struct w1_master, w1_master_entry);
204 if (!dev->initialized)
205 continue;
206
207 if (dev->bus_master->data == bm->data)
208 break;
209 }
210
211 if (!dev) {
212 printk(KERN_ERR "Device doesn't exist.\n");
213 return;
214 }
215
216 __w1_remove_master_device(dev);
217}
218
219EXPORT_SYMBOL(w1_add_master_device);
220EXPORT_SYMBOL(w1_remove_master_device);
diff --git a/drivers/w1/w1_int.h b/drivers/w1/w1_int.h
new file mode 100644
index 000000000000..fdb531e87faa
--- /dev/null
+++ b/drivers/w1/w1_int.h
@@ -0,0 +1,36 @@
1/*
2 * w1_int.h
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 terms 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#ifndef __W1_INT_H
23#define __W1_INT_H
24
25#include <linux/kernel.h>
26#include <linux/device.h>
27
28#include "w1.h"
29
30struct w1_master * w1_alloc_dev(u32, int, int, struct device_driver *, struct device *);
31void w1_free_dev(struct w1_master *dev);
32int w1_add_master_device(struct w1_bus_master *);
33void w1_remove_master_device(struct w1_bus_master *);
34void __w1_remove_master_device(struct w1_master *);
35
36#endif /* __W1_INT_H */
diff --git a/drivers/w1/w1_io.c b/drivers/w1/w1_io.c
new file mode 100644
index 000000000000..02796b5a39f6
--- /dev/null
+++ b/drivers/w1/w1_io.c
@@ -0,0 +1,195 @@
1/*
2 * w1_io.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 terms 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/io.h>
23
24#include <linux/delay.h>
25#include <linux/moduleparam.h>
26
27#include "w1.h"
28#include "w1_log.h"
29#include "w1_io.h"
30
31int w1_delay_parm = 1;
32module_param_named(delay_coef, w1_delay_parm, int, 0);
33
34static u8 w1_crc8_table[] = {
35 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
36 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
37 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
38 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
39 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
40 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
41 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
42 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
43 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
44 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
45 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
46 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
47 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
48 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
49 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
50 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
51};
52
53void w1_delay(unsigned long tm)
54{
55 udelay(tm * w1_delay_parm);
56}
57
58u8 w1_touch_bit(struct w1_master *dev, int bit)
59{
60 if (dev->bus_master->touch_bit)
61 return dev->bus_master->touch_bit(dev->bus_master->data, bit);
62 else
63 return w1_read_bit(dev);
64}
65
66void w1_write_bit(struct w1_master *dev, int bit)
67{
68 if (bit) {
69 dev->bus_master->write_bit(dev->bus_master->data, 0);
70 w1_delay(6);
71 dev->bus_master->write_bit(dev->bus_master->data, 1);
72 w1_delay(64);
73 } else {
74 dev->bus_master->write_bit(dev->bus_master->data, 0);
75 w1_delay(60);
76 dev->bus_master->write_bit(dev->bus_master->data, 1);
77 w1_delay(10);
78 }
79}
80
81void w1_write_8(struct w1_master *dev, u8 byte)
82{
83 int i;
84
85 if (dev->bus_master->write_byte)
86 dev->bus_master->write_byte(dev->bus_master->data, byte);
87 else
88 for (i = 0; i < 8; ++i)
89 w1_write_bit(dev, (byte >> i) & 0x1);
90}
91
92u8 w1_read_bit(struct w1_master *dev)
93{
94 int result;
95
96 dev->bus_master->write_bit(dev->bus_master->data, 0);
97 w1_delay(6);
98 dev->bus_master->write_bit(dev->bus_master->data, 1);
99 w1_delay(9);
100
101 result = dev->bus_master->read_bit(dev->bus_master->data);
102 w1_delay(55);
103
104 return result & 0x1;
105}
106
107u8 w1_read_8(struct w1_master * dev)
108{
109 int i;
110 u8 res = 0;
111
112 if (dev->bus_master->read_byte)
113 res = dev->bus_master->read_byte(dev->bus_master->data);
114 else
115 for (i = 0; i < 8; ++i)
116 res |= (w1_read_bit(dev) << i);
117
118 return res;
119}
120
121void w1_write_block(struct w1_master *dev, u8 *buf, int len)
122{
123 int i;
124
125 if (dev->bus_master->write_block)
126 dev->bus_master->write_block(dev->bus_master->data, buf, len);
127 else
128 for (i = 0; i < len; ++i)
129 w1_write_8(dev, buf[i]);
130}
131
132u8 w1_read_block(struct w1_master *dev, u8 *buf, int len)
133{
134 int i;
135 u8 ret;
136
137 if (dev->bus_master->read_block)
138 ret = dev->bus_master->read_block(dev->bus_master->data, buf, len);
139 else {
140 for (i = 0; i < len; ++i)
141 buf[i] = w1_read_8(dev);
142 ret = len;
143 }
144
145 return ret;
146}
147
148int w1_reset_bus(struct w1_master *dev)
149{
150 int result = 0;
151
152 if (dev->bus_master->reset_bus)
153 result = dev->bus_master->reset_bus(dev->bus_master->data) & 0x1;
154 else {
155 dev->bus_master->write_bit(dev->bus_master->data, 0);
156 w1_delay(480);
157 dev->bus_master->write_bit(dev->bus_master->data, 1);
158 w1_delay(70);
159
160 result = dev->bus_master->read_bit(dev->bus_master->data) & 0x1;
161 w1_delay(410);
162 }
163
164 return result;
165}
166
167u8 w1_calc_crc8(u8 * data, int len)
168{
169 u8 crc = 0;
170
171 while (len--)
172 crc = w1_crc8_table[crc ^ *data++];
173
174 return crc;
175}
176
177void w1_search_devices(struct w1_master *dev, w1_slave_found_callback cb)
178{
179 dev->attempts++;
180 if (dev->bus_master->search)
181 dev->bus_master->search(dev->bus_master->data, cb);
182 else
183 w1_search(dev);
184}
185
186EXPORT_SYMBOL(w1_write_bit);
187EXPORT_SYMBOL(w1_write_8);
188EXPORT_SYMBOL(w1_read_bit);
189EXPORT_SYMBOL(w1_read_8);
190EXPORT_SYMBOL(w1_reset_bus);
191EXPORT_SYMBOL(w1_calc_crc8);
192EXPORT_SYMBOL(w1_delay);
193EXPORT_SYMBOL(w1_read_block);
194EXPORT_SYMBOL(w1_write_block);
195EXPORT_SYMBOL(w1_search_devices);
diff --git a/drivers/w1/w1_io.h b/drivers/w1/w1_io.h
new file mode 100644
index 000000000000..6c573005a712
--- /dev/null
+++ b/drivers/w1/w1_io.h
@@ -0,0 +1,39 @@
1/*
2 * w1_io.h
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 terms 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#ifndef __W1_IO_H
23#define __W1_IO_H
24
25#include "w1.h"
26
27void w1_delay(unsigned long);
28u8 w1_touch_bit(struct w1_master *, int);
29void w1_write_bit(struct w1_master *, int);
30void w1_write_8(struct w1_master *, u8);
31u8 w1_read_bit(struct w1_master *);
32u8 w1_read_8(struct w1_master *);
33int w1_reset_bus(struct w1_master *);
34u8 w1_calc_crc8(u8 *, int);
35void w1_write_block(struct w1_master *, u8 *, int);
36u8 w1_read_block(struct w1_master *, u8 *, int);
37void w1_search_devices(struct w1_master *dev, w1_slave_found_callback cb);
38
39#endif /* __W1_IO_H */
diff --git a/drivers/w1/w1_log.h b/drivers/w1/w1_log.h
new file mode 100644
index 000000000000..a6bf6f44dce2
--- /dev/null
+++ b/drivers/w1/w1_log.h
@@ -0,0 +1,38 @@
1/*
2 * w1_log.h
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 terms 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#ifndef __W1_LOG_H
23#define __W1_LOG_H
24
25#define DEBUG
26
27#ifdef W1_DEBUG
28# define assert(expr) do {} while (0)
29#else
30# define assert(expr) \
31 if(unlikely(!(expr))) { \
32 printk(KERN_ERR "Assertion failed! %s,%s,%s,line=%d\n", \
33 #expr,__FILE__,__FUNCTION__,__LINE__); \
34 }
35#endif
36
37#endif /* __W1_LOG_H */
38
diff --git a/drivers/w1/w1_netlink.c b/drivers/w1/w1_netlink.c
new file mode 100644
index 000000000000..2a82fb055c70
--- /dev/null
+++ b/drivers/w1/w1_netlink.c
@@ -0,0 +1,66 @@
1/*
2 * w1_netlink.c
3 *
4 * Copyright (c) 2003 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 terms 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 <linux/skbuff.h>
23#include <linux/netlink.h>
24
25#include "w1.h"
26#include "w1_log.h"
27#include "w1_netlink.h"
28
29#ifndef NETLINK_DISABLED
30void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
31{
32 unsigned int size;
33 struct sk_buff *skb;
34 struct w1_netlink_msg *data;
35 struct nlmsghdr *nlh;
36
37 if (!dev->nls)
38 return;
39
40 size = NLMSG_SPACE(sizeof(struct w1_netlink_msg));
41
42 skb = alloc_skb(size, GFP_ATOMIC);
43 if (!skb) {
44 dev_err(&dev->dev, "skb_alloc() failed.\n");
45 return;
46 }
47
48 nlh = NLMSG_PUT(skb, 0, dev->seq++, NLMSG_DONE, size - sizeof(*nlh));
49
50 data = (struct w1_netlink_msg *)NLMSG_DATA(nlh);
51
52 memcpy(data, msg, sizeof(struct w1_netlink_msg));
53
54 NETLINK_CB(skb).dst_groups = dev->groups;
55 netlink_broadcast(dev->nls, skb, 0, dev->groups, GFP_ATOMIC);
56
57nlmsg_failure:
58 return;
59}
60#else
61#warning Netlink support is disabled. Please compile with NET support enabled.
62
63void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
64{
65}
66#endif
diff --git a/drivers/w1/w1_netlink.h b/drivers/w1/w1_netlink.h
new file mode 100644
index 000000000000..ea1b530abad0
--- /dev/null
+++ b/drivers/w1/w1_netlink.h
@@ -0,0 +1,57 @@
1/*
2 * w1_netlink.h
3 *
4 * Copyright (c) 2003 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 terms 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#ifndef __W1_NETLINK_H
23#define __W1_NETLINK_H
24
25#include <asm/types.h>
26
27#include "w1.h"
28
29enum w1_netlink_message_types {
30 W1_SLAVE_ADD = 0,
31 W1_SLAVE_REMOVE,
32 W1_MASTER_ADD,
33 W1_MASTER_REMOVE,
34};
35
36struct w1_netlink_msg
37{
38 __u8 type;
39 __u8 reserved[3];
40 union
41 {
42 struct w1_reg_num id;
43 __u64 w1_id;
44 struct
45 {
46 __u32 id;
47 __u32 pid;
48 } mst;
49 } id;
50};
51
52#ifdef __KERNEL__
53
54void w1_netlink_send(struct w1_master *, struct w1_netlink_msg *);
55
56#endif /* __KERNEL__ */
57#endif /* __W1_NETLINK_H */
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
35MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
37MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, 64bit memory family.");
38
39static ssize_t w1_smem_read_name(struct device *, char *);
40static ssize_t w1_smem_read_val(struct device *, char *);
41static 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
50static 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
57static 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
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),
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
94out:
95 up(&sl->master->mutex);
96out_dec:
97 atomic_dec(&sl->refcnt);
98
99 return count;
100}
101
102static struct w1_family w1_smem_family = {
103 .fid = W1_FAMILY_SMEM,
104 .fops = &w1_smem_fops,
105};
106
107static int __init w1_smem_init(void)
108{
109 return w1_register_family(&w1_smem_family);
110}
111
112static void __exit w1_smem_fini(void)
113{
114 w1_unregister_family(&w1_smem_family);
115}
116
117module_init(w1_smem_init);
118module_exit(w1_smem_fini);
diff --git a/drivers/w1/w1_therm.c b/drivers/w1/w1_therm.c
new file mode 100644
index 000000000000..0b1817890503
--- /dev/null
+++ b/drivers/w1/w1_therm.c
@@ -0,0 +1,205 @@
1/*
2 * w1_therm.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 therms 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#include <linux/delay.h>
30
31#include "w1.h"
32#include "w1_io.h"
33#include "w1_int.h"
34#include "w1_family.h"
35
36MODULE_LICENSE("GPL");
37MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
38MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
39
40static u8 bad_roms[][9] = {
41 {0xaa, 0x00, 0x4b, 0x46, 0xff, 0xff, 0x0c, 0x10, 0x87},
42 {}
43 };
44
45static ssize_t w1_therm_read_name(struct device *, char *);
46static ssize_t w1_therm_read_temp(struct device *, char *);
47static ssize_t w1_therm_read_bin(struct kobject *, char *, loff_t, size_t);
48
49static struct w1_family_ops w1_therm_fops = {
50 .rname = &w1_therm_read_name,
51 .rbin = &w1_therm_read_bin,
52 .rval = &w1_therm_read_temp,
53 .rvalname = "temp1_input",
54};
55
56static ssize_t w1_therm_read_name(struct device *dev, char *buf)
57{
58 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
59
60 return sprintf(buf, "%s\n", sl->name);
61}
62
63static inline int w1_convert_temp(u8 rom[9])
64{
65 int t, h;
66
67 if (rom[1] == 0)
68 t = ((s32)rom[0] >> 1)*1000;
69 else
70 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
71
72 t -= 250;
73 h = 1000*((s32)rom[7] - (s32)rom[6]);
74 h /= (s32)rom[7];
75 t += h;
76
77 return t;
78}
79
80static ssize_t w1_therm_read_temp(struct device *dev, char *buf)
81{
82 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
83
84 return sprintf(buf, "%d\n", w1_convert_temp(sl->rom));
85}
86
87static int w1_therm_check_rom(u8 rom[9])
88{
89 int i;
90
91 for (i=0; i<sizeof(bad_roms)/9; ++i)
92 if (!memcmp(bad_roms[i], rom, 9))
93 return 1;
94
95 return 0;
96}
97
98static ssize_t w1_therm_read_bin(struct kobject *kobj, char *buf, loff_t off, size_t count)
99{
100 struct w1_slave *sl = container_of(container_of(kobj, struct device, kobj),
101 struct w1_slave, dev);
102 struct w1_master *dev = sl->master;
103 u8 rom[9], crc, verdict;
104 int i, max_trying = 10;
105
106 atomic_inc(&sl->refcnt);
107 smp_mb__after_atomic_inc();
108 if (down_interruptible(&sl->master->mutex)) {
109 count = 0;
110 goto out_dec;
111 }
112
113 if (off > W1_SLAVE_DATA_SIZE) {
114 count = 0;
115 goto out;
116 }
117 if (off + count > W1_SLAVE_DATA_SIZE) {
118 count = 0;
119 goto out;
120 }
121
122 memset(buf, 0, count);
123 memset(rom, 0, sizeof(rom));
124
125 count = 0;
126 verdict = 0;
127 crc = 0;
128
129 while (max_trying--) {
130 if (!w1_reset_bus (dev)) {
131 int count = 0;
132 u8 match[9] = {W1_MATCH_ROM, };
133 unsigned int tm = 750;
134
135 memcpy(&match[1], (u64 *) & sl->reg_num, 8);
136
137 w1_write_block(dev, match, 9);
138
139 w1_write_8(dev, W1_CONVERT_TEMP);
140
141 while (tm) {
142 tm = msleep_interruptible(tm);
143 if (signal_pending(current))
144 flush_signals(current);
145 }
146
147 if (!w1_reset_bus (dev)) {
148 w1_write_block(dev, match, 9);
149
150 w1_write_8(dev, W1_READ_SCRATCHPAD);
151 if ((count = w1_read_block(dev, rom, 9)) != 9) {
152 dev_warn(&dev->dev, "w1_read_block() returned %d instead of 9.\n", count);
153 }
154
155 crc = w1_calc_crc8(rom, 8);
156
157 if (rom[8] == crc && rom[0])
158 verdict = 1;
159
160 }
161 }
162
163 if (!w1_therm_check_rom(rom))
164 break;
165 }
166
167 for (i = 0; i < 9; ++i)
168 count += sprintf(buf + count, "%02x ", rom[i]);
169 count += sprintf(buf + count, ": crc=%02x %s\n",
170 crc, (verdict) ? "YES" : "NO");
171 if (verdict)
172 memcpy(sl->rom, rom, sizeof(sl->rom));
173 else
174 dev_warn(&dev->dev, "18S20 doesn't respond to CONVERT_TEMP.\n");
175
176 for (i = 0; i < 9; ++i)
177 count += sprintf(buf + count, "%02x ", sl->rom[i]);
178
179 count += sprintf(buf + count, "t=%d\n", w1_convert_temp(rom));
180out:
181 up(&dev->mutex);
182out_dec:
183 smp_mb__before_atomic_inc();
184 atomic_dec(&sl->refcnt);
185
186 return count;
187}
188
189static struct w1_family w1_therm_family = {
190 .fid = W1_FAMILY_THERM,
191 .fops = &w1_therm_fops,
192};
193
194static int __init w1_therm_init(void)
195{
196 return w1_register_family(&w1_therm_family);
197}
198
199static void __exit w1_therm_fini(void)
200{
201 w1_unregister_family(&w1_therm_family);
202}
203
204module_init(w1_therm_init);
205module_exit(w1_therm_fini);