Commit bd529cfb40c427d5b5aae0d315afb9f0a1da5e76

Authored by Evgeniy Polyakov
Committed by Greg Kroah-Hartman
1 parent ccd6994000

[PATCH] W1: Move w1 bus master code into 'w1/masters' and move w1 slave code into 'w1/slaves'

Signed-off-by: Ben Gardner <bgardner@wabtec.com>
Signed-off-by: Evgeniy Polyakov <johnpol@2ka.mipt.ru>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

Showing 20 changed files with 2152 additions and 2117 deletions Side-by-side Diff

... ... @@ -11,64 +11,8 @@
11 11 This W1 support can also be built as a module. If so, the module
12 12 will be called wire.ko.
13 13  
14   -config 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   -
24   -config 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   -
33   -config 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   -
43   -config 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   -
50   -config 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   -
57   -config W1_DS2433
58   - tristate "4kb EEPROM family support (DS2433)"
59   - depends on W1
60   - help
61   - Say Y here if you want to use a 1-wire
62   - 4kb EEPROM family device (DS2433).
63   -
64   -config W1_DS2433_CRC
65   - bool "Protect DS2433 data with a CRC16"
66   - depends on W1_DS2433
67   - select CRC16
68   - help
69   - Say Y here to protect DS2433 data with a CRC16.
70   - Each block has 30 bytes of data and a two byte CRC16.
71   - Full block writes are only allowed if the CRC is valid.
  14 +source drivers/w1/masters/Kconfig
  15 +source drivers/w1/slaves/Kconfig
72 16  
73 17 endmenu
... ... @@ -13,14 +13,5 @@
13 13 obj-$(CONFIG_W1) += wire.o
14 14 wire-objs := w1.o w1_int.o w1_family.o w1_netlink.o w1_io.o
15 15  
16   -obj-$(CONFIG_W1_MATROX) += matrox_w1.o
17   -obj-$(CONFIG_W1_THERM) += w1_therm.o
18   -obj-$(CONFIG_W1_SMEM) += w1_smem.o
19   -
20   -obj-$(CONFIG_W1_DS9490) += ds9490r.o
21   -ds9490r-objs := dscore.o
22   -
23   -obj-$(CONFIG_W1_DS9490_BRIDGE) += ds_w1_bridge.o
24   -
25   -obj-$(CONFIG_W1_DS2433) += w1_ds2433.o
  16 +obj-y += masters/ slaves/
drivers/w1/ds_w1_bridge.c
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   -
29   -static struct ds_device *ds_dev;
30   -static struct w1_bus_master *ds_bus_master;
31   -
32   -static u8 ds9490r_touch_bit(void *data, u8 bit)
33   -{
34   - u8 ret;
35   - struct ds_device *dev = data;
36   -
37   - if (ds_touch_bit(dev, bit, &ret))
38   - return 0;
39   -
40   - return ret;
41   -}
42   -
43   -static void ds9490r_write_bit(void *data, u8 bit)
44   -{
45   - struct ds_device *dev = data;
46   -
47   - ds_write_bit(dev, bit);
48   -}
49   -
50   -static void ds9490r_write_byte(void *data, u8 byte)
51   -{
52   - struct ds_device *dev = data;
53   -
54   - ds_write_byte(dev, byte);
55   -}
56   -
57   -static u8 ds9490r_read_bit(void *data)
58   -{
59   - struct ds_device *dev = 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   -
73   -static u8 ds9490r_read_byte(void *data)
74   -{
75   - struct ds_device *dev = 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   -
86   -static void ds9490r_write_block(void *data, const u8 *buf, int len)
87   -{
88   - struct ds_device *dev = data;
89   -
90   - ds_write_block(dev, (u8 *)buf, len);
91   -}
92   -
93   -static u8 ds9490r_read_block(void *data, u8 *buf, int len)
94   -{
95   - struct ds_device *dev = 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   -
105   -static u8 ds9490r_reset(void *data)
106   -{
107   - struct ds_device *dev = 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   -
120   -static 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 = 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   -
155   -err_out_put_device:
156   - ds_put_device(ds_dev);
157   -err_out_free_bus_master:
158   - kfree(ds_bus_master);
159   -
160   - return err;
161   -}
162   -
163   -static 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   -
170   -module_init(ds_w1_init);
171   -module_exit(ds_w1_fini);
172   -
173   -MODULE_LICENSE("GPL");
174   -MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
drivers/w1/dscore.c
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   -
29   -static struct usb_device_id ds_id_table [] = {
30   - { USB_DEVICE(0x04fa, 0x2490) },
31   - { },
32   -};
33   -MODULE_DEVICE_TABLE(usb, ds_id_table);
34   -
35   -static int ds_probe(struct usb_interface *, const struct usb_device_id *);
36   -static void ds_disconnect(struct usb_interface *);
37   -
38   -int ds_touch_bit(struct ds_device *, u8, u8 *);
39   -int ds_read_byte(struct ds_device *, u8 *);
40   -int ds_read_bit(struct ds_device *, u8 *);
41   -int ds_write_byte(struct ds_device *, u8);
42   -int ds_write_bit(struct ds_device *, u8);
43   -static int ds_start_pulse(struct ds_device *, int);
44   -int ds_reset(struct ds_device *, struct ds_status *);
45   -struct ds_device * ds_get_device(void);
46   -void ds_put_device(struct ds_device *);
47   -
48   -static inline void ds_dump_status(unsigned char *, unsigned char *, int);
49   -static int ds_send_control(struct ds_device *, u16, u16);
50   -static int ds_send_control_mode(struct ds_device *, u16, u16);
51   -static int ds_send_control_cmd(struct ds_device *, u16, u16);
52   -
53   -
54   -static struct usb_driver ds_driver = {
55   - .name = "DS9490R",
56   - .probe = ds_probe,
57   - .disconnect = ds_disconnect,
58   - .id_table = ds_id_table,
59   -};
60   -
61   -static struct ds_device *ds_dev;
62   -
63   -struct ds_device * ds_get_device(void)
64   -{
65   - if (ds_dev)
66   - atomic_inc(&ds_dev->refcnt);
67   - return ds_dev;
68   -}
69   -
70   -void ds_put_device(struct ds_device *dev)
71   -{
72   - atomic_dec(&dev->refcnt);
73   -}
74   -
75   -static int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index)
76   -{
77   - int err;
78   -
79   - err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
80   - CONTROL_CMD, 0x40, value, index, NULL, 0, 1000);
81   - if (err < 0) {
82   - printk(KERN_ERR "Failed to send command control message %x.%x: err=%d.\n",
83   - value, index, err);
84   - return err;
85   - }
86   -
87   - return err;
88   -}
89   -
90   -static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
91   -{
92   - int err;
93   -
94   - err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
95   - MODE_CMD, 0x40, value, index, NULL, 0, 1000);
96   - if (err < 0) {
97   - printk(KERN_ERR "Failed to send mode control message %x.%x: err=%d.\n",
98   - value, index, err);
99   - return err;
100   - }
101   -
102   - return err;
103   -}
104   -
105   -static int ds_send_control(struct ds_device *dev, u16 value, u16 index)
106   -{
107   - int err;
108   -
109   - err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
110   - COMM_CMD, 0x40, value, index, NULL, 0, 1000);
111   - if (err < 0) {
112   - printk(KERN_ERR "Failed to send control message %x.%x: err=%d.\n",
113   - value, index, err);
114   - return err;
115   - }
116   -
117   - return err;
118   -}
119   -
120   -static inline void ds_dump_status(unsigned char *buf, unsigned char *str, int off)
121   -{
122   - printk("%45s: %8x\n", str, buf[off]);
123   -}
124   -
125   -static int ds_recv_status_nodump(struct ds_device *dev, struct ds_status *st,
126   - unsigned char *buf, int size)
127   -{
128   - int count, err;
129   -
130   - memset(st, 0, sizeof(st));
131   -
132   - count = 0;
133   - err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_STATUS]), buf, size, &count, 100);
134   - if (err < 0) {
135   - printk(KERN_ERR "Failed to read 1-wire data from 0x%x: err=%d.\n", dev->ep[EP_STATUS], err);
136   - return err;
137   - }
138   -
139   - if (count >= sizeof(*st))
140   - memcpy(st, buf, sizeof(*st));
141   -
142   - return count;
143   -}
144   -
145   -static int ds_recv_status(struct ds_device *dev, struct ds_status *st)
146   -{
147   - unsigned char buf[64];
148   - int count, err = 0, i;
149   -
150   - memcpy(st, buf, sizeof(*st));
151   -
152   - count = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
153   - if (count < 0)
154   - return err;
155   -
156   - printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], count);
157   - for (i=0; i<count; ++i)
158   - printk("%02x ", buf[i]);
159   - printk("\n");
160   -
161   - if (count >= 16) {
162   - ds_dump_status(buf, "enable flag", 0);
163   - ds_dump_status(buf, "1-wire speed", 1);
164   - ds_dump_status(buf, "strong pullup duration", 2);
165   - ds_dump_status(buf, "programming pulse duration", 3);
166   - ds_dump_status(buf, "pulldown slew rate control", 4);
167   - ds_dump_status(buf, "write-1 low time", 5);
168   - ds_dump_status(buf, "data sample offset/write-0 recovery time", 6);
169   - ds_dump_status(buf, "reserved (test register)", 7);
170   - ds_dump_status(buf, "device status flags", 8);
171   - ds_dump_status(buf, "communication command byte 1", 9);
172   - ds_dump_status(buf, "communication command byte 2", 10);
173   - ds_dump_status(buf, "communication command buffer status", 11);
174   - ds_dump_status(buf, "1-wire data output buffer status", 12);
175   - ds_dump_status(buf, "1-wire data input buffer status", 13);
176   - ds_dump_status(buf, "reserved", 14);
177   - ds_dump_status(buf, "reserved", 15);
178   - }
179   -
180   - memcpy(st, buf, sizeof(*st));
181   -
182   - if (st->status & ST_EPOF) {
183   - printk(KERN_INFO "Resetting device after ST_EPOF.\n");
184   - err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
185   - if (err)
186   - return err;
187   - count = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
188   - if (count < 0)
189   - return err;
190   - }
191   -#if 0
192   - if (st->status & ST_IDLE) {
193   - printk(KERN_INFO "Resetting pulse after ST_IDLE.\n");
194   - err = ds_start_pulse(dev, PULLUP_PULSE_DURATION);
195   - if (err)
196   - return err;
197   - }
198   -#endif
199   -
200   - return err;
201   -}
202   -
203   -static int ds_recv_data(struct ds_device *dev, unsigned char *buf, int size)
204   -{
205   - int count, err;
206   - struct ds_status st;
207   -
208   - count = 0;
209   - err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]),
210   - buf, size, &count, 1000);
211   - if (err < 0) {
212   - printk(KERN_INFO "Clearing ep0x%x.\n", dev->ep[EP_DATA_IN]);
213   - usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]));
214   - ds_recv_status(dev, &st);
215   - return err;
216   - }
217   -
218   -#if 0
219   - {
220   - int i;
221   -
222   - printk("%s: count=%d: ", __func__, count);
223   - for (i=0; i<count; ++i)
224   - printk("%02x ", buf[i]);
225   - printk("\n");
226   - }
227   -#endif
228   - return count;
229   -}
230   -
231   -static int ds_send_data(struct ds_device *dev, unsigned char *buf, int len)
232   -{
233   - int count, err;
234   -
235   - count = 0;
236   - err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000);
237   - if (err < 0) {
238   - printk(KERN_ERR "Failed to read 1-wire data from 0x02: err=%d.\n", err);
239   - return err;
240   - }
241   -
242   - return err;
243   -}
244   -
245   -#if 0
246   -
247   -int ds_stop_pulse(struct ds_device *dev, int limit)
248   -{
249   - struct ds_status st;
250   - int count = 0, err = 0;
251   - u8 buf[0x20];
252   -
253   - do {
254   - err = ds_send_control(dev, CTL_HALT_EXE_IDLE, 0);
255   - if (err)
256   - break;
257   - err = ds_send_control(dev, CTL_RESUME_EXE, 0);
258   - if (err)
259   - break;
260   - err = ds_recv_status_nodump(dev, &st, buf, sizeof(buf));
261   - if (err)
262   - break;
263   -
264   - if ((st.status & ST_SPUA) == 0) {
265   - err = ds_send_control_mode(dev, MOD_PULSE_EN, 0);
266   - if (err)
267   - break;
268   - }
269   - } while(++count < limit);
270   -
271   - return err;
272   -}
273   -
274   -int ds_detect(struct ds_device *dev, struct ds_status *st)
275   -{
276   - int err;
277   -
278   - err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
279   - if (err)
280   - return err;
281   -
282   - err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, 0);
283   - if (err)
284   - return err;
285   -
286   - err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40);
287   - if (err)
288   - return err;
289   -
290   - err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG);
291   - if (err)
292   - return err;
293   -
294   - err = ds_recv_status(dev, st);
295   -
296   - return err;
297   -}
298   -
299   -#endif /* 0 */
300   -
301   -static int ds_wait_status(struct ds_device *dev, struct ds_status *st)
302   -{
303   - u8 buf[0x20];
304   - int err, count = 0;
305   -
306   - do {
307   - err = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
308   -#if 0
309   - if (err >= 0) {
310   - int i;
311   - printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], err);
312   - for (i=0; i<err; ++i)
313   - printk("%02x ", buf[i]);
314   - printk("\n");
315   - }
316   -#endif
317   - } while(!(buf[0x08] & 0x20) && !(err < 0) && ++count < 100);
318   -
319   -
320   - if (((err > 16) && (buf[0x10] & 0x01)) || count >= 100 || err < 0) {
321   - ds_recv_status(dev, st);
322   - return -1;
323   - } else
324   - return 0;
325   -}
326   -
327   -int ds_reset(struct ds_device *dev, struct ds_status *st)
328   -{
329   - int err;
330   -
331   - //err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_F | COMM_IM | COMM_SE, SPEED_FLEXIBLE);
332   - err = ds_send_control(dev, 0x43, SPEED_NORMAL);
333   - if (err)
334   - return err;
335   -
336   - ds_wait_status(dev, st);
337   -#if 0
338   - if (st->command_buffer_status) {
339   - printk(KERN_INFO "Short circuit.\n");
340   - return -EIO;
341   - }
342   -#endif
343   -
344   - return 0;
345   -}
346   -
347   -#if 0
348   -int 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   -#endif /* 0 */
367   -
368   -static int ds_start_pulse(struct ds_device *dev, int delay)
369   -{
370   - int err;
371   - u8 del = 1 + (u8)(delay >> 4);
372   - struct ds_status st;
373   -
374   -#if 0
375   - err = ds_stop_pulse(dev, 10);
376   - if (err)
377   - return err;
378   -
379   - err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE);
380   - if (err)
381   - return err;
382   -#endif
383   - err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del);
384   - if (err)
385   - return err;
386   -
387   - err = ds_send_control(dev, COMM_PULSE | COMM_IM | COMM_F, 0);
388   - if (err)
389   - return err;
390   -
391   - mdelay(delay);
392   -
393   - ds_wait_status(dev, &st);
394   -
395   - return err;
396   -}
397   -
398   -int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
399   -{
400   - int err, count;
401   - struct ds_status st;
402   - u16 value = (COMM_BIT_IO | COMM_IM) | ((bit) ? COMM_D : 0);
403   - u16 cmd;
404   -
405   - err = ds_send_control(dev, value, 0);
406   - if (err)
407   - return err;
408   -
409   - count = 0;
410   - do {
411   - err = ds_wait_status(dev, &st);
412   - if (err)
413   - return err;
414   -
415   - cmd = st.command0 | (st.command1 << 8);
416   - } while (cmd != value && ++count < 10);
417   -
418   - if (err < 0 || count >= 10) {
419   - printk(KERN_ERR "Failed to obtain status.\n");
420   - return -EINVAL;
421   - }
422   -
423   - err = ds_recv_data(dev, tbit, sizeof(*tbit));
424   - if (err < 0)
425   - return err;
426   -
427   - return 0;
428   -}
429   -
430   -int ds_write_bit(struct ds_device *dev, u8 bit)
431   -{
432   - int err;
433   - struct ds_status st;
434   -
435   - err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit) ? COMM_D : 0, 0);
436   - if (err)
437   - return err;
438   -
439   - ds_wait_status(dev, &st);
440   -
441   - return 0;
442   -}
443   -
444   -int ds_write_byte(struct ds_device *dev, u8 byte)
445   -{
446   - int err;
447   - struct ds_status st;
448   - u8 rbyte;
449   -
450   - err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM | COMM_SPU, byte);
451   - if (err)
452   - return err;
453   -
454   - err = ds_wait_status(dev, &st);
455   - if (err)
456   - return err;
457   -
458   - err = ds_recv_data(dev, &rbyte, sizeof(rbyte));
459   - if (err < 0)
460   - return err;
461   -
462   - ds_start_pulse(dev, PULLUP_PULSE_DURATION);
463   -
464   - return !(byte == rbyte);
465   -}
466   -
467   -int ds_read_bit(struct ds_device *dev, u8 *bit)
468   -{
469   - int err;
470   -
471   - err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE);
472   - if (err)
473   - return err;
474   -
475   - err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_SPU | COMM_D, 0);
476   - if (err)
477   - return err;
478   -
479   - err = ds_recv_data(dev, bit, sizeof(*bit));
480   - if (err < 0)
481   - return err;
482   -
483   - return 0;
484   -}
485   -
486   -int ds_read_byte(struct ds_device *dev, u8 *byte)
487   -{
488   - int err;
489   - struct ds_status st;
490   -
491   - err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM , 0xff);
492   - if (err)
493   - return err;
494   -
495   - ds_wait_status(dev, &st);
496   -
497   - err = ds_recv_data(dev, byte, sizeof(*byte));
498   - if (err < 0)
499   - return err;
500   -
501   - return 0;
502   -}
503   -
504   -int ds_read_block(struct ds_device *dev, u8 *buf, int len)
505   -{
506   - struct ds_status st;
507   - int err;
508   -
509   - if (len > 64*1024)
510   - return -E2BIG;
511   -
512   - memset(buf, 0xFF, len);
513   -
514   - err = ds_send_data(dev, buf, len);
515   - if (err < 0)
516   - return err;
517   -
518   - err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | COMM_SPU, len);
519   - if (err)
520   - return err;
521   -
522   - ds_wait_status(dev, &st);
523   -
524   - memset(buf, 0x00, len);
525   - err = ds_recv_data(dev, buf, len);
526   -
527   - return err;
528   -}
529   -
530   -int ds_write_block(struct ds_device *dev, u8 *buf, int len)
531   -{
532   - int err;
533   - struct ds_status st;
534   -
535   - err = ds_send_data(dev, buf, len);
536   - if (err < 0)
537   - return err;
538   -
539   - ds_wait_status(dev, &st);
540   -
541   - err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | COMM_SPU, len);
542   - if (err)
543   - return err;
544   -
545   - ds_wait_status(dev, &st);
546   -
547   - err = ds_recv_data(dev, buf, len);
548   - if (err < 0)
549   - return err;
550   -
551   - ds_start_pulse(dev, PULLUP_PULSE_DURATION);
552   -
553   - return !(err == len);
554   -}
555   -
556   -#if 0
557   -
558   -int ds_search(struct ds_device *dev, u64 init, u64 *buf, u8 id_number, int conditional_search)
559   -{
560   - int err;
561   - u16 value, index;
562   - struct ds_status st;
563   -
564   - memset(buf, 0, sizeof(buf));
565   -
566   - err = ds_send_data(ds_dev, (unsigned char *)&init, 8);
567   - if (err)
568   - return err;
569   -
570   - ds_wait_status(ds_dev, &st);
571   -
572   - value = COMM_SEARCH_ACCESS | COMM_IM | COMM_SM | COMM_F | COMM_RTS;
573   - index = (conditional_search ? 0xEC : 0xF0) | (id_number << 8);
574   - err = ds_send_control(ds_dev, value, index);
575   - if (err)
576   - return err;
577   -
578   - ds_wait_status(ds_dev, &st);
579   -
580   - err = ds_recv_data(ds_dev, (unsigned char *)buf, 8*id_number);
581   - if (err < 0)
582   - return err;
583   -
584   - return err/8;
585   -}
586   -
587   -int ds_match_access(struct ds_device *dev, u64 init)
588   -{
589   - int err;
590   - struct ds_status st;
591   -
592   - err = ds_send_data(dev, (unsigned char *)&init, sizeof(init));
593   - if (err)
594   - return err;
595   -
596   - ds_wait_status(dev, &st);
597   -
598   - err = ds_send_control(dev, COMM_MATCH_ACCESS | COMM_IM | COMM_RST, 0x0055);
599   - if (err)
600   - return err;
601   -
602   - ds_wait_status(dev, &st);
603   -
604   - return 0;
605   -}
606   -
607   -int ds_set_path(struct ds_device *dev, u64 init)
608   -{
609   - int err;
610   - struct ds_status st;
611   - u8 buf[9];
612   -
613   - memcpy(buf, &init, 8);
614   - buf[8] = BRANCH_MAIN;
615   -
616   - err = ds_send_data(dev, buf, sizeof(buf));
617   - if (err)
618   - return err;
619   -
620   - ds_wait_status(dev, &st);
621   -
622   - err = ds_send_control(dev, COMM_SET_PATH | COMM_IM | COMM_RST, 0);
623   - if (err)
624   - return err;
625   -
626   - ds_wait_status(dev, &st);
627   -
628   - return 0;
629   -}
630   -
631   -#endif /* 0 */
632   -
633   -static int ds_probe(struct usb_interface *intf,
634   - const struct usb_device_id *udev_id)
635   -{
636   - struct usb_device *udev = interface_to_usbdev(intf);
637   - struct usb_endpoint_descriptor *endpoint;
638   - struct usb_host_interface *iface_desc;
639   - int i, err;
640   -
641   - ds_dev = kmalloc(sizeof(struct ds_device), GFP_KERNEL);
642   - if (!ds_dev) {
643   - printk(KERN_INFO "Failed to allocate new DS9490R structure.\n");
644   - return -ENOMEM;
645   - }
646   -
647   - ds_dev->udev = usb_get_dev(udev);
648   - usb_set_intfdata(intf, ds_dev);
649   -
650   - err = usb_set_interface(ds_dev->udev, intf->altsetting[0].desc.bInterfaceNumber, 3);
651   - if (err) {
652   - printk(KERN_ERR "Failed to set alternative setting 3 for %d interface: err=%d.\n",
653   - intf->altsetting[0].desc.bInterfaceNumber, err);
654   - return err;
655   - }
656   -
657   - err = usb_reset_configuration(ds_dev->udev);
658   - if (err) {
659   - printk(KERN_ERR "Failed to reset configuration: err=%d.\n", err);
660   - return err;
661   - }
662   -
663   - iface_desc = &intf->altsetting[0];
664   - if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
665   - printk(KERN_INFO "Num endpoints=%d. It is not DS9490R.\n", iface_desc->desc.bNumEndpoints);
666   - return -ENODEV;
667   - }
668   -
669   - atomic_set(&ds_dev->refcnt, 0);
670   - memset(ds_dev->ep, 0, sizeof(ds_dev->ep));
671   -
672   - /*
673   - * This loop doesn'd show control 0 endpoint,
674   - * so we will fill only 1-3 endpoints entry.
675   - */
676   - for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
677   - endpoint = &iface_desc->endpoint[i].desc;
678   -
679   - ds_dev->ep[i+1] = endpoint->bEndpointAddress;
680   -
681   - printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
682   - i, endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize),
683   - (endpoint->bEndpointAddress & USB_DIR_IN)?"IN":"OUT",
684   - endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
685   - }
686   -
687   -#if 0
688   - {
689   - int err, i;
690   - u64 buf[3];
691   - u64 init=0xb30000002078ee81ull;
692   - struct ds_status st;
693   -
694   - ds_reset(ds_dev, &st);
695   - err = ds_search(ds_dev, init, buf, 3, 0);
696   - if (err < 0)
697   - return err;
698   - for (i=0; i<err; ++i)
699   - printk("%d: %llx\n", i, buf[i]);
700   -
701   - printk("Resetting...\n");
702   - ds_reset(ds_dev, &st);
703   - printk("Setting path for %llx.\n", init);
704   - err = ds_set_path(ds_dev, init);
705   - if (err)
706   - return err;
707   - printk("Calling MATCH_ACCESS.\n");
708   - err = ds_match_access(ds_dev, init);
709   - if (err)
710   - return err;
711   -
712   - printk("Searching the bus...\n");
713   - err = ds_search(ds_dev, init, buf, 3, 0);
714   -
715   - printk("ds_search() returned %d\n", err);
716   -
717   - if (err < 0)
718   - return err;
719   - for (i=0; i<err; ++i)
720   - printk("%d: %llx\n", i, buf[i]);
721   -
722   - return 0;
723   - }
724   -#endif
725   -
726   - return 0;
727   -}
728   -
729   -static void ds_disconnect(struct usb_interface *intf)
730   -{
731   - struct ds_device *dev;
732   -
733   - dev = usb_get_intfdata(intf);
734   - usb_set_intfdata(intf, NULL);
735   -
736   - while (atomic_read(&dev->refcnt)) {
737   - printk(KERN_INFO "Waiting for DS to become free: refcnt=%d.\n",
738   - atomic_read(&dev->refcnt));
739   -
740   - if (msleep_interruptible(1000))
741   - flush_signals(current);
742   - }
743   -
744   - usb_put_dev(dev->udev);
745   - kfree(dev);
746   - ds_dev = NULL;
747   -}
748   -
749   -static int ds_init(void)
750   -{
751   - int err;
752   -
753   - err = usb_register(&ds_driver);
754   - if (err) {
755   - printk(KERN_INFO "Failed to register DS9490R USB device: err=%d.\n", err);
756   - return err;
757   - }
758   -
759   - return 0;
760   -}
761   -
762   -static void ds_fini(void)
763   -{
764   - usb_deregister(&ds_driver);
765   -}
766   -
767   -module_init(ds_init);
768   -module_exit(ds_fini);
769   -
770   -MODULE_LICENSE("GPL");
771   -MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
772   -
773   -EXPORT_SYMBOL(ds_touch_bit);
774   -EXPORT_SYMBOL(ds_read_byte);
775   -EXPORT_SYMBOL(ds_read_bit);
776   -EXPORT_SYMBOL(ds_read_block);
777   -EXPORT_SYMBOL(ds_write_byte);
778   -EXPORT_SYMBOL(ds_write_bit);
779   -EXPORT_SYMBOL(ds_write_block);
780   -EXPORT_SYMBOL(ds_reset);
781   -EXPORT_SYMBOL(ds_get_device);
782   -EXPORT_SYMBOL(ds_put_device);
783   -
784   -/*
785   - * This functions can be used for EEPROM programming,
786   - * when driver will be included into mainline this will
787   - * require uncommenting.
788   - */
789   -#if 0
790   -EXPORT_SYMBOL(ds_start_pulse);
791   -EXPORT_SYMBOL(ds_set_speed);
792   -EXPORT_SYMBOL(ds_detect);
793   -EXPORT_SYMBOL(ds_stop_pulse);
794   -EXPORT_SYMBOL(ds_search);
795   -#endif
drivers/w1/dscore.h
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   -
123   -struct 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   -
133   -struct 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   -
154   -int ds_touch_bit(struct ds_device *, u8, u8 *);
155   -int ds_read_byte(struct ds_device *, u8 *);
156   -int ds_read_bit(struct ds_device *, u8 *);
157   -int ds_write_byte(struct ds_device *, u8);
158   -int ds_write_bit(struct ds_device *, u8);
159   -int ds_reset(struct ds_device *, struct ds_status *);
160   -struct ds_device * ds_get_device(void);
161   -void ds_put_device(struct ds_device *);
162   -int ds_write_block(struct ds_device *, u8 *, int);
163   -int ds_read_block(struct ds_device *, u8 *, int);
164   -
165   -#endif /* __DSCORE_H */
drivers/w1/masters/Kconfig
  1 +#
  2 +# 1-wire bus master configuration
  3 +#
  4 +
  5 +menu "1-wire Bus Masters"
  6 + depends on W1
  7 +
  8 +config W1_MASTER_MATROX
  9 + tristate "Matrox G400 transport layer for 1-wire"
  10 + depends on W1 && PCI
  11 + help
  12 + Say Y here if you want to communicate with your 1-wire devices
  13 + using Matrox's G400 GPIO pins.
  14 +
  15 + This support is also available as a module. If so, the module
  16 + will be called matrox_w1.ko.
  17 +
  18 +config W1_MASTER_DS9490
  19 + tristate "DS9490R transport layer driver"
  20 + depends on W1 && USB
  21 + help
  22 + Say Y here if you want to have a driver for DS9490R UWB <-> W1 bridge.
  23 +
  24 + This support is also available as a module. If so, the module
  25 + will be called ds9490r.ko.
  26 +
  27 +config W1_MASTER_DS9490_BRIDGE
  28 + tristate "DS9490R USB <-> W1 transport layer for 1-wire"
  29 + depends on W1_DS9490
  30 + help
  31 + Say Y here if you want to communicate with your 1-wire devices
  32 + using DS9490R USB bridge.
  33 +
  34 + This support is also available as a module. If so, the module
  35 + will be called ds_w1_bridge.ko.
  36 +
  37 +endmenu
drivers/w1/masters/Makefile
  1 +#
  2 +# Makefile for 1-wire bus master drivers.
  3 +#
  4 +
  5 +obj-$(CONFIG_W1_MASTER_MATROX) += matrox_w1.o
  6 +
  7 +obj-$(CONFIG_W1_MASTER_DS9490) += ds9490r.o
  8 +ds9490r-objs := dscore.o
  9 +
  10 +obj-$(CONFIG_W1_MASTER_DS9490_BRIDGE) += ds_w1_bridge.o
drivers/w1/masters/ds_w1_bridge.c
  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.h"
  26 +#include "../w1_int.h"
  27 +#include "dscore.h"
  28 +
  29 +static struct ds_device *ds_dev;
  30 +static struct w1_bus_master *ds_bus_master;
  31 +
  32 +static u8 ds9490r_touch_bit(void *data, u8 bit)
  33 +{
  34 + u8 ret;
  35 + struct ds_device *dev = data;
  36 +
  37 + if (ds_touch_bit(dev, bit, &ret))
  38 + return 0;
  39 +
  40 + return ret;
  41 +}
  42 +
  43 +static void ds9490r_write_bit(void *data, u8 bit)
  44 +{
  45 + struct ds_device *dev = data;
  46 +
  47 + ds_write_bit(dev, bit);
  48 +}
  49 +
  50 +static void ds9490r_write_byte(void *data, u8 byte)
  51 +{
  52 + struct ds_device *dev = data;
  53 +
  54 + ds_write_byte(dev, byte);
  55 +}
  56 +
  57 +static u8 ds9490r_read_bit(void *data)
  58 +{
  59 + struct ds_device *dev = 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 +
  73 +static u8 ds9490r_read_byte(void *data)
  74 +{
  75 + struct ds_device *dev = 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 +
  86 +static void ds9490r_write_block(void *data, const u8 *buf, int len)
  87 +{
  88 + struct ds_device *dev = data;
  89 +
  90 + ds_write_block(dev, (u8 *)buf, len);
  91 +}
  92 +
  93 +static u8 ds9490r_read_block(void *data, u8 *buf, int len)
  94 +{
  95 + struct ds_device *dev = 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 +
  105 +static u8 ds9490r_reset(void *data)
  106 +{
  107 + struct ds_device *dev = 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 +
  120 +static 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 = 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 +
  155 +err_out_put_device:
  156 + ds_put_device(ds_dev);
  157 +err_out_free_bus_master:
  158 + kfree(ds_bus_master);
  159 +
  160 + return err;
  161 +}
  162 +
  163 +static 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 +
  170 +module_init(ds_w1_init);
  171 +module_exit(ds_w1_fini);
  172 +
  173 +MODULE_LICENSE("GPL");
  174 +MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
drivers/w1/masters/dscore.c
  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 +
  29 +static struct usb_device_id ds_id_table [] = {
  30 + { USB_DEVICE(0x04fa, 0x2490) },
  31 + { },
  32 +};
  33 +MODULE_DEVICE_TABLE(usb, ds_id_table);
  34 +
  35 +static int ds_probe(struct usb_interface *, const struct usb_device_id *);
  36 +static void ds_disconnect(struct usb_interface *);
  37 +
  38 +int ds_touch_bit(struct ds_device *, u8, u8 *);
  39 +int ds_read_byte(struct ds_device *, u8 *);
  40 +int ds_read_bit(struct ds_device *, u8 *);
  41 +int ds_write_byte(struct ds_device *, u8);
  42 +int ds_write_bit(struct ds_device *, u8);
  43 +static int ds_start_pulse(struct ds_device *, int);
  44 +int ds_reset(struct ds_device *, struct ds_status *);
  45 +struct ds_device * ds_get_device(void);
  46 +void ds_put_device(struct ds_device *);
  47 +
  48 +static inline void ds_dump_status(unsigned char *, unsigned char *, int);
  49 +static int ds_send_control(struct ds_device *, u16, u16);
  50 +static int ds_send_control_mode(struct ds_device *, u16, u16);
  51 +static int ds_send_control_cmd(struct ds_device *, u16, u16);
  52 +
  53 +
  54 +static struct usb_driver ds_driver = {
  55 + .name = "DS9490R",
  56 + .probe = ds_probe,
  57 + .disconnect = ds_disconnect,
  58 + .id_table = ds_id_table,
  59 +};
  60 +
  61 +static struct ds_device *ds_dev;
  62 +
  63 +struct ds_device * ds_get_device(void)
  64 +{
  65 + if (ds_dev)
  66 + atomic_inc(&ds_dev->refcnt);
  67 + return ds_dev;
  68 +}
  69 +
  70 +void ds_put_device(struct ds_device *dev)
  71 +{
  72 + atomic_dec(&dev->refcnt);
  73 +}
  74 +
  75 +static int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index)
  76 +{
  77 + int err;
  78 +
  79 + err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
  80 + CONTROL_CMD, 0x40, value, index, NULL, 0, 1000);
  81 + if (err < 0) {
  82 + printk(KERN_ERR "Failed to send command control message %x.%x: err=%d.\n",
  83 + value, index, err);
  84 + return err;
  85 + }
  86 +
  87 + return err;
  88 +}
  89 +
  90 +static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
  91 +{
  92 + int err;
  93 +
  94 + err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
  95 + MODE_CMD, 0x40, value, index, NULL, 0, 1000);
  96 + if (err < 0) {
  97 + printk(KERN_ERR "Failed to send mode control message %x.%x: err=%d.\n",
  98 + value, index, err);
  99 + return err;
  100 + }
  101 +
  102 + return err;
  103 +}
  104 +
  105 +static int ds_send_control(struct ds_device *dev, u16 value, u16 index)
  106 +{
  107 + int err;
  108 +
  109 + err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
  110 + COMM_CMD, 0x40, value, index, NULL, 0, 1000);
  111 + if (err < 0) {
  112 + printk(KERN_ERR "Failed to send control message %x.%x: err=%d.\n",
  113 + value, index, err);
  114 + return err;
  115 + }
  116 +
  117 + return err;
  118 +}
  119 +
  120 +static inline void ds_dump_status(unsigned char *buf, unsigned char *str, int off)
  121 +{
  122 + printk("%45s: %8x\n", str, buf[off]);
  123 +}
  124 +
  125 +static int ds_recv_status_nodump(struct ds_device *dev, struct ds_status *st,
  126 + unsigned char *buf, int size)
  127 +{
  128 + int count, err;
  129 +
  130 + memset(st, 0, sizeof(st));
  131 +
  132 + count = 0;
  133 + err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_STATUS]), buf, size, &count, 100);
  134 + if (err < 0) {
  135 + printk(KERN_ERR "Failed to read 1-wire data from 0x%x: err=%d.\n", dev->ep[EP_STATUS], err);
  136 + return err;
  137 + }
  138 +
  139 + if (count >= sizeof(*st))
  140 + memcpy(st, buf, sizeof(*st));
  141 +
  142 + return count;
  143 +}
  144 +
  145 +static int ds_recv_status(struct ds_device *dev, struct ds_status *st)
  146 +{
  147 + unsigned char buf[64];
  148 + int count, err = 0, i;
  149 +
  150 + memcpy(st, buf, sizeof(*st));
  151 +
  152 + count = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
  153 + if (count < 0)
  154 + return err;
  155 +
  156 + printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], count);
  157 + for (i=0; i<count; ++i)
  158 + printk("%02x ", buf[i]);
  159 + printk("\n");
  160 +
  161 + if (count >= 16) {
  162 + ds_dump_status(buf, "enable flag", 0);
  163 + ds_dump_status(buf, "1-wire speed", 1);
  164 + ds_dump_status(buf, "strong pullup duration", 2);
  165 + ds_dump_status(buf, "programming pulse duration", 3);
  166 + ds_dump_status(buf, "pulldown slew rate control", 4);
  167 + ds_dump_status(buf, "write-1 low time", 5);
  168 + ds_dump_status(buf, "data sample offset/write-0 recovery time", 6);
  169 + ds_dump_status(buf, "reserved (test register)", 7);
  170 + ds_dump_status(buf, "device status flags", 8);
  171 + ds_dump_status(buf, "communication command byte 1", 9);
  172 + ds_dump_status(buf, "communication command byte 2", 10);
  173 + ds_dump_status(buf, "communication command buffer status", 11);
  174 + ds_dump_status(buf, "1-wire data output buffer status", 12);
  175 + ds_dump_status(buf, "1-wire data input buffer status", 13);
  176 + ds_dump_status(buf, "reserved", 14);
  177 + ds_dump_status(buf, "reserved", 15);
  178 + }
  179 +
  180 + memcpy(st, buf, sizeof(*st));
  181 +
  182 + if (st->status & ST_EPOF) {
  183 + printk(KERN_INFO "Resetting device after ST_EPOF.\n");
  184 + err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
  185 + if (err)
  186 + return err;
  187 + count = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
  188 + if (count < 0)
  189 + return err;
  190 + }
  191 +#if 0
  192 + if (st->status & ST_IDLE) {
  193 + printk(KERN_INFO "Resetting pulse after ST_IDLE.\n");
  194 + err = ds_start_pulse(dev, PULLUP_PULSE_DURATION);
  195 + if (err)
  196 + return err;
  197 + }
  198 +#endif
  199 +
  200 + return err;
  201 +}
  202 +
  203 +static int ds_recv_data(struct ds_device *dev, unsigned char *buf, int size)
  204 +{
  205 + int count, err;
  206 + struct ds_status st;
  207 +
  208 + count = 0;
  209 + err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]),
  210 + buf, size, &count, 1000);
  211 + if (err < 0) {
  212 + printk(KERN_INFO "Clearing ep0x%x.\n", dev->ep[EP_DATA_IN]);
  213 + usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]));
  214 + ds_recv_status(dev, &st);
  215 + return err;
  216 + }
  217 +
  218 +#if 0
  219 + {
  220 + int i;
  221 +
  222 + printk("%s: count=%d: ", __func__, count);
  223 + for (i=0; i<count; ++i)
  224 + printk("%02x ", buf[i]);
  225 + printk("\n");
  226 + }
  227 +#endif
  228 + return count;
  229 +}
  230 +
  231 +static int ds_send_data(struct ds_device *dev, unsigned char *buf, int len)
  232 +{
  233 + int count, err;
  234 +
  235 + count = 0;
  236 + err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000);
  237 + if (err < 0) {
  238 + printk(KERN_ERR "Failed to read 1-wire data from 0x02: err=%d.\n", err);
  239 + return err;
  240 + }
  241 +
  242 + return err;
  243 +}
  244 +
  245 +#if 0
  246 +
  247 +int ds_stop_pulse(struct ds_device *dev, int limit)
  248 +{
  249 + struct ds_status st;
  250 + int count = 0, err = 0;
  251 + u8 buf[0x20];
  252 +
  253 + do {
  254 + err = ds_send_control(dev, CTL_HALT_EXE_IDLE, 0);
  255 + if (err)
  256 + break;
  257 + err = ds_send_control(dev, CTL_RESUME_EXE, 0);
  258 + if (err)
  259 + break;
  260 + err = ds_recv_status_nodump(dev, &st, buf, sizeof(buf));
  261 + if (err)
  262 + break;
  263 +
  264 + if ((st.status & ST_SPUA) == 0) {
  265 + err = ds_send_control_mode(dev, MOD_PULSE_EN, 0);
  266 + if (err)
  267 + break;
  268 + }
  269 + } while(++count < limit);
  270 +
  271 + return err;
  272 +}
  273 +
  274 +int ds_detect(struct ds_device *dev, struct ds_status *st)
  275 +{
  276 + int err;
  277 +
  278 + err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
  279 + if (err)
  280 + return err;
  281 +
  282 + err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, 0);
  283 + if (err)
  284 + return err;
  285 +
  286 + err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40);
  287 + if (err)
  288 + return err;
  289 +
  290 + err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG);
  291 + if (err)
  292 + return err;
  293 +
  294 + err = ds_recv_status(dev, st);
  295 +
  296 + return err;
  297 +}
  298 +
  299 +#endif /* 0 */
  300 +
  301 +static int ds_wait_status(struct ds_device *dev, struct ds_status *st)
  302 +{
  303 + u8 buf[0x20];
  304 + int err, count = 0;
  305 +
  306 + do {
  307 + err = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
  308 +#if 0
  309 + if (err >= 0) {
  310 + int i;
  311 + printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], err);
  312 + for (i=0; i<err; ++i)
  313 + printk("%02x ", buf[i]);
  314 + printk("\n");
  315 + }
  316 +#endif
  317 + } while(!(buf[0x08] & 0x20) && !(err < 0) && ++count < 100);
  318 +
  319 +
  320 + if (((err > 16) && (buf[0x10] & 0x01)) || count >= 100 || err < 0) {
  321 + ds_recv_status(dev, st);
  322 + return -1;
  323 + } else
  324 + return 0;
  325 +}
  326 +
  327 +int ds_reset(struct ds_device *dev, struct ds_status *st)
  328 +{
  329 + int err;
  330 +
  331 + //err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_F | COMM_IM | COMM_SE, SPEED_FLEXIBLE);
  332 + err = ds_send_control(dev, 0x43, SPEED_NORMAL);
  333 + if (err)
  334 + return err;
  335 +
  336 + ds_wait_status(dev, st);
  337 +#if 0
  338 + if (st->command_buffer_status) {
  339 + printk(KERN_INFO "Short circuit.\n");
  340 + return -EIO;
  341 + }
  342 +#endif
  343 +
  344 + return 0;
  345 +}
  346 +
  347 +#if 0
  348 +int 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 +#endif /* 0 */
  367 +
  368 +static int ds_start_pulse(struct ds_device *dev, int delay)
  369 +{
  370 + int err;
  371 + u8 del = 1 + (u8)(delay >> 4);
  372 + struct ds_status st;
  373 +
  374 +#if 0
  375 + err = ds_stop_pulse(dev, 10);
  376 + if (err)
  377 + return err;
  378 +
  379 + err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE);
  380 + if (err)
  381 + return err;
  382 +#endif
  383 + err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del);
  384 + if (err)
  385 + return err;
  386 +
  387 + err = ds_send_control(dev, COMM_PULSE | COMM_IM | COMM_F, 0);
  388 + if (err)
  389 + return err;
  390 +
  391 + mdelay(delay);
  392 +
  393 + ds_wait_status(dev, &st);
  394 +
  395 + return err;
  396 +}
  397 +
  398 +int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
  399 +{
  400 + int err, count;
  401 + struct ds_status st;
  402 + u16 value = (COMM_BIT_IO | COMM_IM) | ((bit) ? COMM_D : 0);
  403 + u16 cmd;
  404 +
  405 + err = ds_send_control(dev, value, 0);
  406 + if (err)
  407 + return err;
  408 +
  409 + count = 0;
  410 + do {
  411 + err = ds_wait_status(dev, &st);
  412 + if (err)
  413 + return err;
  414 +
  415 + cmd = st.command0 | (st.command1 << 8);
  416 + } while (cmd != value && ++count < 10);
  417 +
  418 + if (err < 0 || count >= 10) {
  419 + printk(KERN_ERR "Failed to obtain status.\n");
  420 + return -EINVAL;
  421 + }
  422 +
  423 + err = ds_recv_data(dev, tbit, sizeof(*tbit));
  424 + if (err < 0)
  425 + return err;
  426 +
  427 + return 0;
  428 +}
  429 +
  430 +int ds_write_bit(struct ds_device *dev, u8 bit)
  431 +{
  432 + int err;
  433 + struct ds_status st;
  434 +
  435 + err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit) ? COMM_D : 0, 0);
  436 + if (err)
  437 + return err;
  438 +
  439 + ds_wait_status(dev, &st);
  440 +
  441 + return 0;
  442 +}
  443 +
  444 +int ds_write_byte(struct ds_device *dev, u8 byte)
  445 +{
  446 + int err;
  447 + struct ds_status st;
  448 + u8 rbyte;
  449 +
  450 + err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM | COMM_SPU, byte);
  451 + if (err)
  452 + return err;
  453 +
  454 + err = ds_wait_status(dev, &st);
  455 + if (err)
  456 + return err;
  457 +
  458 + err = ds_recv_data(dev, &rbyte, sizeof(rbyte));
  459 + if (err < 0)
  460 + return err;
  461 +
  462 + ds_start_pulse(dev, PULLUP_PULSE_DURATION);
  463 +
  464 + return !(byte == rbyte);
  465 +}
  466 +
  467 +int ds_read_bit(struct ds_device *dev, u8 *bit)
  468 +{
  469 + int err;
  470 +
  471 + err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE);
  472 + if (err)
  473 + return err;
  474 +
  475 + err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_SPU | COMM_D, 0);
  476 + if (err)
  477 + return err;
  478 +
  479 + err = ds_recv_data(dev, bit, sizeof(*bit));
  480 + if (err < 0)
  481 + return err;
  482 +
  483 + return 0;
  484 +}
  485 +
  486 +int ds_read_byte(struct ds_device *dev, u8 *byte)
  487 +{
  488 + int err;
  489 + struct ds_status st;
  490 +
  491 + err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM , 0xff);
  492 + if (err)
  493 + return err;
  494 +
  495 + ds_wait_status(dev, &st);
  496 +
  497 + err = ds_recv_data(dev, byte, sizeof(*byte));
  498 + if (err < 0)
  499 + return err;
  500 +
  501 + return 0;
  502 +}
  503 +
  504 +int ds_read_block(struct ds_device *dev, u8 *buf, int len)
  505 +{
  506 + struct ds_status st;
  507 + int err;
  508 +
  509 + if (len > 64*1024)
  510 + return -E2BIG;
  511 +
  512 + memset(buf, 0xFF, len);
  513 +
  514 + err = ds_send_data(dev, buf, len);
  515 + if (err < 0)
  516 + return err;
  517 +
  518 + err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | COMM_SPU, len);
  519 + if (err)
  520 + return err;
  521 +
  522 + ds_wait_status(dev, &st);
  523 +
  524 + memset(buf, 0x00, len);
  525 + err = ds_recv_data(dev, buf, len);
  526 +
  527 + return err;
  528 +}
  529 +
  530 +int ds_write_block(struct ds_device *dev, u8 *buf, int len)
  531 +{
  532 + int err;
  533 + struct ds_status st;
  534 +
  535 + err = ds_send_data(dev, buf, len);
  536 + if (err < 0)
  537 + return err;
  538 +
  539 + ds_wait_status(dev, &st);
  540 +
  541 + err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | COMM_SPU, len);
  542 + if (err)
  543 + return err;
  544 +
  545 + ds_wait_status(dev, &st);
  546 +
  547 + err = ds_recv_data(dev, buf, len);
  548 + if (err < 0)
  549 + return err;
  550 +
  551 + ds_start_pulse(dev, PULLUP_PULSE_DURATION);
  552 +
  553 + return !(err == len);
  554 +}
  555 +
  556 +#if 0
  557 +
  558 +int ds_search(struct ds_device *dev, u64 init, u64 *buf, u8 id_number, int conditional_search)
  559 +{
  560 + int err;
  561 + u16 value, index;
  562 + struct ds_status st;
  563 +
  564 + memset(buf, 0, sizeof(buf));
  565 +
  566 + err = ds_send_data(ds_dev, (unsigned char *)&init, 8);
  567 + if (err)
  568 + return err;
  569 +
  570 + ds_wait_status(ds_dev, &st);
  571 +
  572 + value = COMM_SEARCH_ACCESS | COMM_IM | COMM_SM | COMM_F | COMM_RTS;
  573 + index = (conditional_search ? 0xEC : 0xF0) | (id_number << 8);
  574 + err = ds_send_control(ds_dev, value, index);
  575 + if (err)
  576 + return err;
  577 +
  578 + ds_wait_status(ds_dev, &st);
  579 +
  580 + err = ds_recv_data(ds_dev, (unsigned char *)buf, 8*id_number);
  581 + if (err < 0)
  582 + return err;
  583 +
  584 + return err/8;
  585 +}
  586 +
  587 +int ds_match_access(struct ds_device *dev, u64 init)
  588 +{
  589 + int err;
  590 + struct ds_status st;
  591 +
  592 + err = ds_send_data(dev, (unsigned char *)&init, sizeof(init));
  593 + if (err)
  594 + return err;
  595 +
  596 + ds_wait_status(dev, &st);
  597 +
  598 + err = ds_send_control(dev, COMM_MATCH_ACCESS | COMM_IM | COMM_RST, 0x0055);
  599 + if (err)
  600 + return err;
  601 +
  602 + ds_wait_status(dev, &st);
  603 +
  604 + return 0;
  605 +}
  606 +
  607 +int ds_set_path(struct ds_device *dev, u64 init)
  608 +{
  609 + int err;
  610 + struct ds_status st;
  611 + u8 buf[9];
  612 +
  613 + memcpy(buf, &init, 8);
  614 + buf[8] = BRANCH_MAIN;
  615 +
  616 + err = ds_send_data(dev, buf, sizeof(buf));
  617 + if (err)
  618 + return err;
  619 +
  620 + ds_wait_status(dev, &st);
  621 +
  622 + err = ds_send_control(dev, COMM_SET_PATH | COMM_IM | COMM_RST, 0);
  623 + if (err)
  624 + return err;
  625 +
  626 + ds_wait_status(dev, &st);
  627 +
  628 + return 0;
  629 +}
  630 +
  631 +#endif /* 0 */
  632 +
  633 +static int ds_probe(struct usb_interface *intf,
  634 + const struct usb_device_id *udev_id)
  635 +{
  636 + struct usb_device *udev = interface_to_usbdev(intf);
  637 + struct usb_endpoint_descriptor *endpoint;
  638 + struct usb_host_interface *iface_desc;
  639 + int i, err;
  640 +
  641 + ds_dev = kmalloc(sizeof(struct ds_device), GFP_KERNEL);
  642 + if (!ds_dev) {
  643 + printk(KERN_INFO "Failed to allocate new DS9490R structure.\n");
  644 + return -ENOMEM;
  645 + }
  646 +
  647 + ds_dev->udev = usb_get_dev(udev);
  648 + usb_set_intfdata(intf, ds_dev);
  649 +
  650 + err = usb_set_interface(ds_dev->udev, intf->altsetting[0].desc.bInterfaceNumber, 3);
  651 + if (err) {
  652 + printk(KERN_ERR "Failed to set alternative setting 3 for %d interface: err=%d.\n",
  653 + intf->altsetting[0].desc.bInterfaceNumber, err);
  654 + return err;
  655 + }
  656 +
  657 + err = usb_reset_configuration(ds_dev->udev);
  658 + if (err) {
  659 + printk(KERN_ERR "Failed to reset configuration: err=%d.\n", err);
  660 + return err;
  661 + }
  662 +
  663 + iface_desc = &intf->altsetting[0];
  664 + if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
  665 + printk(KERN_INFO "Num endpoints=%d. It is not DS9490R.\n", iface_desc->desc.bNumEndpoints);
  666 + return -ENODEV;
  667 + }
  668 +
  669 + atomic_set(&ds_dev->refcnt, 0);
  670 + memset(ds_dev->ep, 0, sizeof(ds_dev->ep));
  671 +
  672 + /*
  673 + * This loop doesn'd show control 0 endpoint,
  674 + * so we will fill only 1-3 endpoints entry.
  675 + */
  676 + for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  677 + endpoint = &iface_desc->endpoint[i].desc;
  678 +
  679 + ds_dev->ep[i+1] = endpoint->bEndpointAddress;
  680 +
  681 + printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
  682 + i, endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize),
  683 + (endpoint->bEndpointAddress & USB_DIR_IN)?"IN":"OUT",
  684 + endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
  685 + }
  686 +
  687 +#if 0
  688 + {
  689 + int err, i;
  690 + u64 buf[3];
  691 + u64 init=0xb30000002078ee81ull;
  692 + struct ds_status st;
  693 +
  694 + ds_reset(ds_dev, &st);
  695 + err = ds_search(ds_dev, init, buf, 3, 0);
  696 + if (err < 0)
  697 + return err;
  698 + for (i=0; i<err; ++i)
  699 + printk("%d: %llx\n", i, buf[i]);
  700 +
  701 + printk("Resetting...\n");
  702 + ds_reset(ds_dev, &st);
  703 + printk("Setting path for %llx.\n", init);
  704 + err = ds_set_path(ds_dev, init);
  705 + if (err)
  706 + return err;
  707 + printk("Calling MATCH_ACCESS.\n");
  708 + err = ds_match_access(ds_dev, init);
  709 + if (err)
  710 + return err;
  711 +
  712 + printk("Searching the bus...\n");
  713 + err = ds_search(ds_dev, init, buf, 3, 0);
  714 +
  715 + printk("ds_search() returned %d\n", err);
  716 +
  717 + if (err < 0)
  718 + return err;
  719 + for (i=0; i<err; ++i)
  720 + printk("%d: %llx\n", i, buf[i]);
  721 +
  722 + return 0;
  723 + }
  724 +#endif
  725 +
  726 + return 0;
  727 +}
  728 +
  729 +static void ds_disconnect(struct usb_interface *intf)
  730 +{
  731 + struct ds_device *dev;
  732 +
  733 + dev = usb_get_intfdata(intf);
  734 + usb_set_intfdata(intf, NULL);
  735 +
  736 + while (atomic_read(&dev->refcnt)) {
  737 + printk(KERN_INFO "Waiting for DS to become free: refcnt=%d.\n",
  738 + atomic_read(&dev->refcnt));
  739 +
  740 + if (msleep_interruptible(1000))
  741 + flush_signals(current);
  742 + }
  743 +
  744 + usb_put_dev(dev->udev);
  745 + kfree(dev);
  746 + ds_dev = NULL;
  747 +}
  748 +
  749 +static int ds_init(void)
  750 +{
  751 + int err;
  752 +
  753 + err = usb_register(&ds_driver);
  754 + if (err) {
  755 + printk(KERN_INFO "Failed to register DS9490R USB device: err=%d.\n", err);
  756 + return err;
  757 + }
  758 +
  759 + return 0;
  760 +}
  761 +
  762 +static void ds_fini(void)
  763 +{
  764 + usb_deregister(&ds_driver);
  765 +}
  766 +
  767 +module_init(ds_init);
  768 +module_exit(ds_fini);
  769 +
  770 +MODULE_LICENSE("GPL");
  771 +MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  772 +
  773 +EXPORT_SYMBOL(ds_touch_bit);
  774 +EXPORT_SYMBOL(ds_read_byte);
  775 +EXPORT_SYMBOL(ds_read_bit);
  776 +EXPORT_SYMBOL(ds_read_block);
  777 +EXPORT_SYMBOL(ds_write_byte);
  778 +EXPORT_SYMBOL(ds_write_bit);
  779 +EXPORT_SYMBOL(ds_write_block);
  780 +EXPORT_SYMBOL(ds_reset);
  781 +EXPORT_SYMBOL(ds_get_device);
  782 +EXPORT_SYMBOL(ds_put_device);
  783 +
  784 +/*
  785 + * This functions can be used for EEPROM programming,
  786 + * when driver will be included into mainline this will
  787 + * require uncommenting.
  788 + */
  789 +#if 0
  790 +EXPORT_SYMBOL(ds_start_pulse);
  791 +EXPORT_SYMBOL(ds_set_speed);
  792 +EXPORT_SYMBOL(ds_detect);
  793 +EXPORT_SYMBOL(ds_stop_pulse);
  794 +EXPORT_SYMBOL(ds_search);
  795 +#endif
drivers/w1/masters/dscore.h
  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 +
  123 +struct 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 +
  133 +struct 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 +
  154 +int ds_touch_bit(struct ds_device *, u8, u8 *);
  155 +int ds_read_byte(struct ds_device *, u8 *);
  156 +int ds_read_bit(struct ds_device *, u8 *);
  157 +int ds_write_byte(struct ds_device *, u8);
  158 +int ds_write_bit(struct ds_device *, u8);
  159 +int ds_reset(struct ds_device *, struct ds_status *);
  160 +struct ds_device * ds_get_device(void);
  161 +void ds_put_device(struct ds_device *);
  162 +int ds_write_block(struct ds_device *, u8 *, int);
  163 +int ds_read_block(struct ds_device *, u8 *, int);
  164 +
  165 +#endif /* __DSCORE_H */
drivers/w1/masters/matrox_w1.c
  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/types.h>
  23 +#include <asm/atomic.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 +
  42 +MODULE_LICENSE("GPL");
  43 +MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  44 +MODULE_DESCRIPTION("Driver for transport(Dallas 1-wire prtocol) over VGA DDC(matrox gpio).");
  45 +
  46 +static struct pci_device_id matrox_w1_tbl[] = {
  47 + { PCI_DEVICE(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400) },
  48 + { },
  49 +};
  50 +MODULE_DEVICE_TABLE(pci, matrox_w1_tbl);
  51 +
  52 +static int __devinit matrox_w1_probe(struct pci_dev *, const struct pci_device_id *);
  53 +static void __devexit matrox_w1_remove(struct pci_dev *);
  54 +
  55 +static 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 +
  79 +struct 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 +
  93 +static u8 matrox_w1_read_ddc_bit(void *);
  94 +static void matrox_w1_write_ddc_bit(void *, 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 + */
  107 +static __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 +
  118 +static __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 +
  125 +static void matrox_w1_write_ddc_bit(void *data, u8 bit)
  126 +{
  127 + u8 ret;
  128 + struct matrox_device *dev = 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 +
  140 +static u8 matrox_w1_read_ddc_bit(void *data)
  141 +{
  142 + u8 ret;
  143 + struct matrox_device *dev = data;
  144 +
  145 + ret = matrox_w1_read_reg(dev, MATROX_GET_DATA);
  146 +
  147 + return ret;
  148 +}
  149 +
  150 +static 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 +
  156 +static 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 = 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 +
  217 +err_out_free_device:
  218 + kfree(dev);
  219 +
  220 + return err;
  221 +}
  222 +
  223 +static 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 +
  236 +static int __init matrox_w1_init(void)
  237 +{
  238 + return pci_register_driver(&matrox_w1_pci_driver);
  239 +}
  240 +
  241 +static void __exit matrox_w1_fini(void)
  242 +{
  243 + pci_unregister_driver(&matrox_w1_pci_driver);
  244 +}
  245 +
  246 +module_init(matrox_w1_init);
  247 +module_exit(matrox_w1_fini);
drivers/w1/matrox_w1.c
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   -
42   -MODULE_LICENSE("GPL");
43   -MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
44   -MODULE_DESCRIPTION("Driver for transport(Dallas 1-wire prtocol) over VGA DDC(matrox gpio).");
45   -
46   -static struct pci_device_id matrox_w1_tbl[] = {
47   - { PCI_DEVICE(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400) },
48   - { },
49   -};
50   -MODULE_DEVICE_TABLE(pci, matrox_w1_tbl);
51   -
52   -static int __devinit matrox_w1_probe(struct pci_dev *, const struct pci_device_id *);
53   -static void __devexit matrox_w1_remove(struct pci_dev *);
54   -
55   -static 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   -
79   -struct 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   -
93   -static u8 matrox_w1_read_ddc_bit(void *);
94   -static void matrox_w1_write_ddc_bit(void *, 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   - */
107   -static __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   -
118   -static __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   -
125   -static void matrox_w1_write_ddc_bit(void *data, u8 bit)
126   -{
127   - u8 ret;
128   - struct matrox_device *dev = 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   -
140   -static u8 matrox_w1_read_ddc_bit(void *data)
141   -{
142   - u8 ret;
143   - struct matrox_device *dev = data;
144   -
145   - ret = matrox_w1_read_reg(dev, MATROX_GET_DATA);
146   -
147   - return ret;
148   -}
149   -
150   -static 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   -
156   -static 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 = 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   -
217   -err_out_free_device:
218   - kfree(dev);
219   -
220   - return err;
221   -}
222   -
223   -static 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   -
236   -static int __init matrox_w1_init(void)
237   -{
238   - return pci_register_driver(&matrox_w1_pci_driver);
239   -}
240   -
241   -static void __exit matrox_w1_fini(void)
242   -{
243   - pci_unregister_driver(&matrox_w1_pci_driver);
244   -}
245   -
246   -module_init(matrox_w1_init);
247   -module_exit(matrox_w1_fini);
drivers/w1/slaves/Kconfig
  1 +#
  2 +# 1-wire slaves configuration
  3 +#
  4 +
  5 +menu "1-wire Slaves"
  6 + depends on W1
  7 +
  8 +config W1_SLAVE_THERM
  9 + tristate "Thermal family implementation"
  10 + depends on W1
  11 + help
  12 + Say Y here if you want to connect 1-wire thermal sensors to you
  13 + wire.
  14 +
  15 +config W1_SLAVE_SMEM
  16 + tristate "Simple 64bit memory family implementation"
  17 + depends on W1
  18 + help
  19 + Say Y here if you want to connect 1-wire
  20 + simple 64bit memory rom(ds2401/ds2411/ds1990*) to you wire.
  21 +
  22 +config W1_SLAVE_DS2433
  23 + tristate "4kb EEPROM family support (DS2433)"
  24 + depends on W1
  25 + help
  26 + Say Y here if you want to use a 1-wire
  27 + 4kb EEPROM family device (DS2433).
  28 +
  29 +config W1_SLAVE_DS2433_CRC
  30 + bool "Protect DS2433 data with a CRC16"
  31 + depends on W1_DS2433
  32 + select CRC16
  33 + help
  34 + Say Y here to protect DS2433 data with a CRC16.
  35 + Each block has 30 bytes of data and a two byte CRC16.
  36 + Full block writes are only allowed if the CRC is valid.
  37 +
  38 +endmenu
drivers/w1/slaves/Makefile
  1 +#
  2 +# Makefile for the Dallas's 1-wire slaves.
  3 +#
  4 +
  5 +ifeq ($(CONFIG_W1_SLAVE_DS2433_CRC), y)
  6 +EXTRA_CFLAGS += -DCONFIG_W1_F23_CRC
  7 +endif
  8 +
  9 +obj-$(CONFIG_W1_SLAVE_THERM) += w1_therm.o
  10 +obj-$(CONFIG_W1_SLAVE_SMEM) += w1_smem.o
  11 +obj-$(CONFIG_W1_SLAVE_DS2433) += w1_ds2433.o
drivers/w1/slaves/w1_ds2433.c
  1 +/*
  2 + * w1_ds2433.c - w1 family 23 (DS2433) driver
  3 + *
  4 + * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com>
  5 + *
  6 + * This source code is licensed under the GNU General Public License,
  7 + * Version 2. See the file COPYING for more details.
  8 + */
  9 +
  10 +#include <linux/kernel.h>
  11 +#include <linux/module.h>
  12 +#include <linux/moduleparam.h>
  13 +#include <linux/device.h>
  14 +#include <linux/types.h>
  15 +#include <linux/delay.h>
  16 +#ifdef CONFIG_W1_F23_CRC
  17 +#include <linux/crc16.h>
  18 +
  19 +#define CRC16_INIT 0
  20 +#define CRC16_VALID 0xb001
  21 +
  22 +#endif
  23 +
  24 +#include "../w1.h"
  25 +#include "../w1_io.h"
  26 +#include "../w1_int.h"
  27 +#include "../w1_family.h"
  28 +
  29 +MODULE_LICENSE("GPL");
  30 +MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  31 +MODULE_DESCRIPTION("w1 family 23 driver for DS2433, 4kb EEPROM");
  32 +
  33 +#define W1_EEPROM_SIZE 512
  34 +#define W1_PAGE_COUNT 16
  35 +#define W1_PAGE_SIZE 32
  36 +#define W1_PAGE_BITS 5
  37 +#define W1_PAGE_MASK 0x1F
  38 +
  39 +#define W1_F23_TIME 300
  40 +
  41 +#define W1_F23_READ_EEPROM 0xF0
  42 +#define W1_F23_WRITE_SCRATCH 0x0F
  43 +#define W1_F23_READ_SCRATCH 0xAA
  44 +#define W1_F23_COPY_SCRATCH 0x55
  45 +
  46 +struct w1_f23_data {
  47 + u8 memory[W1_EEPROM_SIZE];
  48 + u32 validcrc;
  49 +};
  50 +
  51 +/**
  52 + * Check the file size bounds and adjusts count as needed.
  53 + * This would not be needed if the file size didn't reset to 0 after a write.
  54 + */
  55 +static inline size_t w1_f23_fix_count(loff_t off, size_t count, size_t size)
  56 +{
  57 + if (off > size)
  58 + return 0;
  59 +
  60 + if ((off + count) > size)
  61 + return (size - off);
  62 +
  63 + return count;
  64 +}
  65 +
  66 +#ifdef CONFIG_W1_F23_CRC
  67 +static int w1_f23_refresh_block(struct w1_slave *sl, struct w1_f23_data *data,
  68 + int block)
  69 +{
  70 + u8 wrbuf[3];
  71 + int off = block * W1_PAGE_SIZE;
  72 +
  73 + if (data->validcrc & (1 << block))
  74 + return 0;
  75 +
  76 + if (w1_reset_select_slave(sl)) {
  77 + data->validcrc = 0;
  78 + return -EIO;
  79 + }
  80 +
  81 + wrbuf[0] = W1_F23_READ_EEPROM;
  82 + wrbuf[1] = off & 0xff;
  83 + wrbuf[2] = off >> 8;
  84 + w1_write_block(sl->master, wrbuf, 3);
  85 + w1_read_block(sl->master, &data->memory[off], W1_PAGE_SIZE);
  86 +
  87 + /* cache the block if the CRC is valid */
  88 + if (crc16(CRC16_INIT, &data->memory[off], W1_PAGE_SIZE) == CRC16_VALID)
  89 + data->validcrc |= (1 << block);
  90 +
  91 + return 0;
  92 +}
  93 +#endif /* CONFIG_W1_F23_CRC */
  94 +
  95 +static ssize_t w1_f23_read_bin(struct kobject *kobj, char *buf, loff_t off,
  96 + size_t count)
  97 +{
  98 + struct w1_slave *sl = kobj_to_w1_slave(kobj);
  99 +#ifdef CONFIG_W1_F23_CRC
  100 + struct w1_f23_data *data = sl->family_data;
  101 + int i, min_page, max_page;
  102 +#else
  103 + u8 wrbuf[3];
  104 +#endif
  105 +
  106 + if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
  107 + return 0;
  108 +
  109 + atomic_inc(&sl->refcnt);
  110 + if (down_interruptible(&sl->master->mutex)) {
  111 + count = 0;
  112 + goto out_dec;
  113 + }
  114 +
  115 +#ifdef CONFIG_W1_F23_CRC
  116 +
  117 + min_page = (off >> W1_PAGE_BITS);
  118 + max_page = (off + count - 1) >> W1_PAGE_BITS;
  119 + for (i = min_page; i <= max_page; i++) {
  120 + if (w1_f23_refresh_block(sl, data, i)) {
  121 + count = -EIO;
  122 + goto out_up;
  123 + }
  124 + }
  125 + memcpy(buf, &data->memory[off], count);
  126 +
  127 +#else /* CONFIG_W1_F23_CRC */
  128 +
  129 + /* read directly from the EEPROM */
  130 + if (w1_reset_select_slave(sl)) {
  131 + count = -EIO;
  132 + goto out_up;
  133 + }
  134 +
  135 + wrbuf[0] = W1_F23_READ_EEPROM;
  136 + wrbuf[1] = off & 0xff;
  137 + wrbuf[2] = off >> 8;
  138 + w1_write_block(sl->master, wrbuf, 3);
  139 + w1_read_block(sl->master, buf, count);
  140 +
  141 +#endif /* CONFIG_W1_F23_CRC */
  142 +
  143 +out_up:
  144 + up(&sl->master->mutex);
  145 +out_dec:
  146 + atomic_dec(&sl->refcnt);
  147 +
  148 + return count;
  149 +}
  150 +
  151 +/**
  152 + * Writes to the scratchpad and reads it back for verification.
  153 + * Then copies the scratchpad to EEPROM.
  154 + * The data must be on one page.
  155 + * The master must be locked.
  156 + *
  157 + * @param sl The slave structure
  158 + * @param addr Address for the write
  159 + * @param len length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK))
  160 + * @param data The data to write
  161 + * @return 0=Success -1=failure
  162 + */
  163 +static int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data)
  164 +{
  165 + u8 wrbuf[4];
  166 + u8 rdbuf[W1_PAGE_SIZE + 3];
  167 + u8 es = (addr + len - 1) & 0x1f;
  168 +
  169 + /* Write the data to the scratchpad */
  170 + if (w1_reset_select_slave(sl))
  171 + return -1;
  172 +
  173 + wrbuf[0] = W1_F23_WRITE_SCRATCH;
  174 + wrbuf[1] = addr & 0xff;
  175 + wrbuf[2] = addr >> 8;
  176 +
  177 + w1_write_block(sl->master, wrbuf, 3);
  178 + w1_write_block(sl->master, data, len);
  179 +
  180 + /* Read the scratchpad and verify */
  181 + if (w1_reset_select_slave(sl))
  182 + return -1;
  183 +
  184 + w1_write_8(sl->master, W1_F23_READ_SCRATCH);
  185 + w1_read_block(sl->master, rdbuf, len + 3);
  186 +
  187 + /* Compare what was read against the data written */
  188 + if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
  189 + (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0))
  190 + return -1;
  191 +
  192 + /* Copy the scratchpad to EEPROM */
  193 + if (w1_reset_select_slave(sl))
  194 + return -1;
  195 +
  196 + wrbuf[0] = W1_F23_COPY_SCRATCH;
  197 + wrbuf[3] = es;
  198 + w1_write_block(sl->master, wrbuf, 4);
  199 +
  200 + /* Sleep for 5 ms to wait for the write to complete */
  201 + msleep(5);
  202 +
  203 + /* Reset the bus to wake up the EEPROM (this may not be needed) */
  204 + w1_reset_bus(sl->master);
  205 +
  206 + return 0;
  207 +}
  208 +
  209 +static ssize_t w1_f23_write_bin(struct kobject *kobj, char *buf, loff_t off,
  210 + size_t count)
  211 +{
  212 + struct w1_slave *sl = kobj_to_w1_slave(kobj);
  213 + int addr, len, idx;
  214 +
  215 + if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
  216 + return 0;
  217 +
  218 +#ifdef CONFIG_W1_F23_CRC
  219 + /* can only write full blocks in cached mode */
  220 + if ((off & W1_PAGE_MASK) || (count & W1_PAGE_MASK)) {
  221 + dev_err(&sl->dev, "invalid offset/count off=%d cnt=%zd\n",
  222 + (int)off, count);
  223 + return -EINVAL;
  224 + }
  225 +
  226 + /* make sure the block CRCs are valid */
  227 + for (idx = 0; idx < count; idx += W1_PAGE_SIZE) {
  228 + if (crc16(CRC16_INIT, &buf[idx], W1_PAGE_SIZE) != CRC16_VALID) {
  229 + dev_err(&sl->dev, "bad CRC at offset %d\n", (int)off);
  230 + return -EINVAL;
  231 + }
  232 + }
  233 +#endif /* CONFIG_W1_F23_CRC */
  234 +
  235 + atomic_inc(&sl->refcnt);
  236 + if (down_interruptible(&sl->master->mutex)) {
  237 + count = 0;
  238 + goto out_dec;
  239 + }
  240 +
  241 + /* Can only write data to one page at a time */
  242 + idx = 0;
  243 + while (idx < count) {
  244 + addr = off + idx;
  245 + len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
  246 + if (len > (count - idx))
  247 + len = count - idx;
  248 +
  249 + if (w1_f23_write(sl, addr, len, &buf[idx]) < 0) {
  250 + count = -EIO;
  251 + goto out_up;
  252 + }
  253 + idx += len;
  254 + }
  255 +
  256 +out_up:
  257 + up(&sl->master->mutex);
  258 +out_dec:
  259 + atomic_dec(&sl->refcnt);
  260 +
  261 + return count;
  262 +}
  263 +
  264 +static struct bin_attribute w1_f23_bin_attr = {
  265 + .attr = {
  266 + .name = "eeprom",
  267 + .mode = S_IRUGO | S_IWUSR,
  268 + .owner = THIS_MODULE,
  269 + },
  270 + .size = W1_EEPROM_SIZE,
  271 + .read = w1_f23_read_bin,
  272 + .write = w1_f23_write_bin,
  273 +};
  274 +
  275 +static int w1_f23_add_slave(struct w1_slave *sl)
  276 +{
  277 + int err;
  278 +#ifdef CONFIG_W1_F23_CRC
  279 + struct w1_f23_data *data;
  280 +
  281 + data = kmalloc(sizeof(struct w1_f23_data), GFP_KERNEL);
  282 + if (!data)
  283 + return -ENOMEM;
  284 + memset(data, 0, sizeof(struct w1_f23_data));
  285 + sl->family_data = data;
  286 +
  287 +#endif /* CONFIG_W1_F23_CRC */
  288 +
  289 + err = sysfs_create_bin_file(&sl->dev.kobj, &w1_f23_bin_attr);
  290 +
  291 +#ifdef CONFIG_W1_F23_CRC
  292 + if (err)
  293 + kfree(data);
  294 +#endif /* CONFIG_W1_F23_CRC */
  295 +
  296 + return err;
  297 +}
  298 +
  299 +static void w1_f23_remove_slave(struct w1_slave *sl)
  300 +{
  301 +#ifdef CONFIG_W1_F23_CRC
  302 + kfree(sl->family_data);
  303 + sl->family_data = NULL;
  304 +#endif /* CONFIG_W1_F23_CRC */
  305 + sysfs_remove_bin_file(&sl->dev.kobj, &w1_f23_bin_attr);
  306 +}
  307 +
  308 +static struct w1_family_ops w1_f23_fops = {
  309 + .add_slave = w1_f23_add_slave,
  310 + .remove_slave = w1_f23_remove_slave,
  311 +};
  312 +
  313 +static struct w1_family w1_family_23 = {
  314 + .fid = W1_EEPROM_DS2433,
  315 + .fops = &w1_f23_fops,
  316 +};
  317 +
  318 +static int __init w1_f23_init(void)
  319 +{
  320 + return w1_register_family(&w1_family_23);
  321 +}
  322 +
  323 +static void __exit w1_f23_fini(void)
  324 +{
  325 + w1_unregister_family(&w1_family_23);
  326 +}
  327 +
  328 +module_init(w1_f23_init);
  329 +module_exit(w1_f23_fini);
drivers/w1/slaves/w1_smem.c
  1 +/*
  2 + * w1_smem.c
  3 + *
  4 + * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
  5 + *
  6 + *
  7 + * This program is free software; you can redistribute it and/or modify
  8 + * it under the smems of the GNU General Public License as published by
  9 + * the Free Software Foundation; either version 2 of the License, or
  10 + * (at your option) any later version.
  11 + *
  12 + * This program is distributed in the hope that it will be useful,
  13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 + * GNU General Public License for more details.
  16 + *
  17 + * You should have received a copy of the GNU General Public License
  18 + * along with this program; if not, write to the Free Software
  19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20 + */
  21 +
  22 +#include <asm/types.h>
  23 +
  24 +#include <linux/kernel.h>
  25 +#include <linux/module.h>
  26 +#include <linux/moduleparam.h>
  27 +#include <linux/device.h>
  28 +#include <linux/types.h>
  29 +
  30 +#include "../w1.h"
  31 +#include "../w1_io.h"
  32 +#include "../w1_int.h"
  33 +#include "../w1_family.h"
  34 +
  35 +MODULE_LICENSE("GPL");
  36 +MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  37 +MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, 64bit memory family.");
  38 +
  39 +static struct w1_family w1_smem_family_01 = {
  40 + .fid = W1_FAMILY_SMEM_01,
  41 +};
  42 +
  43 +static struct w1_family w1_smem_family_81 = {
  44 + .fid = W1_FAMILY_SMEM_81,
  45 +};
  46 +
  47 +static int __init w1_smem_init(void)
  48 +{
  49 + int err;
  50 +
  51 + err = w1_register_family(&w1_smem_family_01);
  52 + if (err)
  53 + return err;
  54 +
  55 + err = w1_register_family(&w1_smem_family_81);
  56 + if (err) {
  57 + w1_unregister_family(&w1_smem_family_01);
  58 + return err;
  59 + }
  60 +
  61 + return 0;
  62 +}
  63 +
  64 +static void __exit w1_smem_fini(void)
  65 +{
  66 + w1_unregister_family(&w1_smem_family_01);
  67 + w1_unregister_family(&w1_smem_family_81);
  68 +}
  69 +
  70 +module_init(w1_smem_init);
  71 +module_exit(w1_smem_fini);
drivers/w1/slaves/w1_therm.c
  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 +
  36 +MODULE_LICENSE("GPL");
  37 +MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  38 +MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
  39 +
  40 +static u8 bad_roms[][9] = {
  41 + {0xaa, 0x00, 0x4b, 0x46, 0xff, 0xff, 0x0c, 0x10, 0x87},
  42 + {}
  43 + };
  44 +
  45 +static ssize_t w1_therm_read_bin(struct kobject *, char *, loff_t, size_t);
  46 +
  47 +static struct bin_attribute w1_therm_bin_attr = {
  48 + .attr = {
  49 + .name = "w1_slave",
  50 + .mode = S_IRUGO,
  51 + .owner = THIS_MODULE,
  52 + },
  53 + .size = W1_SLAVE_DATA_SIZE,
  54 + .read = w1_therm_read_bin,
  55 +};
  56 +
  57 +static int w1_therm_add_slave(struct w1_slave *sl)
  58 +{
  59 + return sysfs_create_bin_file(&sl->dev.kobj, &w1_therm_bin_attr);
  60 +}
  61 +
  62 +static void w1_therm_remove_slave(struct w1_slave *sl)
  63 +{
  64 + sysfs_remove_bin_file(&sl->dev.kobj, &w1_therm_bin_attr);
  65 +}
  66 +
  67 +static struct w1_family_ops w1_therm_fops = {
  68 + .add_slave = w1_therm_add_slave,
  69 + .remove_slave = w1_therm_remove_slave,
  70 +};
  71 +
  72 +static struct w1_family w1_therm_family_DS18S20 = {
  73 + .fid = W1_THERM_DS18S20,
  74 + .fops = &w1_therm_fops,
  75 +};
  76 +
  77 +static struct w1_family w1_therm_family_DS18B20 = {
  78 + .fid = W1_THERM_DS18B20,
  79 + .fops = &w1_therm_fops,
  80 +};
  81 +
  82 +static struct w1_family w1_therm_family_DS1822 = {
  83 + .fid = W1_THERM_DS1822,
  84 + .fops = &w1_therm_fops,
  85 +};
  86 +
  87 +struct w1_therm_family_converter
  88 +{
  89 + u8 broken;
  90 + u16 reserved;
  91 + struct w1_family *f;
  92 + int (*convert)(u8 rom[9]);
  93 +};
  94 +
  95 +static inline int w1_DS18B20_convert_temp(u8 rom[9]);
  96 +static inline int w1_DS18S20_convert_temp(u8 rom[9]);
  97 +
  98 +static struct w1_therm_family_converter w1_therm_families[] = {
  99 + {
  100 + .f = &w1_therm_family_DS18S20,
  101 + .convert = w1_DS18S20_convert_temp
  102 + },
  103 + {
  104 + .f = &w1_therm_family_DS1822,
  105 + .convert = w1_DS18B20_convert_temp
  106 + },
  107 + {
  108 + .f = &w1_therm_family_DS18B20,
  109 + .convert = w1_DS18B20_convert_temp
  110 + },
  111 +};
  112 +
  113 +static inline int w1_DS18B20_convert_temp(u8 rom[9])
  114 +{
  115 + int t = (rom[1] << 8) | rom[0];
  116 + t /= 16;
  117 + return t;
  118 +}
  119 +
  120 +static inline int w1_DS18S20_convert_temp(u8 rom[9])
  121 +{
  122 + int t, h;
  123 +
  124 + if (!rom[7])
  125 + return 0;
  126 +
  127 + if (rom[1] == 0)
  128 + t = ((s32)rom[0] >> 1)*1000;
  129 + else
  130 + t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
  131 +
  132 + t -= 250;
  133 + h = 1000*((s32)rom[7] - (s32)rom[6]);
  134 + h /= (s32)rom[7];
  135 + t += h;
  136 +
  137 + return t;
  138 +}
  139 +
  140 +static inline int w1_convert_temp(u8 rom[9], u8 fid)
  141 +{
  142 + int i;
  143 +
  144 + for (i=0; i<sizeof(w1_therm_families)/sizeof(w1_therm_families[0]); ++i)
  145 + if (w1_therm_families[i].f->fid == fid)
  146 + return w1_therm_families[i].convert(rom);
  147 +
  148 + return 0;
  149 +}
  150 +
  151 +static int w1_therm_check_rom(u8 rom[9])
  152 +{
  153 + int i;
  154 +
  155 + for (i=0; i<sizeof(bad_roms)/9; ++i)
  156 + if (!memcmp(bad_roms[i], rom, 9))
  157 + return 1;
  158 +
  159 + return 0;
  160 +}
  161 +
  162 +static ssize_t w1_therm_read_bin(struct kobject *kobj, char *buf, loff_t off, size_t count)
  163 +{
  164 + struct w1_slave *sl = kobj_to_w1_slave(kobj);
  165 + struct w1_master *dev = sl->master;
  166 + u8 rom[9], crc, verdict;
  167 + int i, max_trying = 10;
  168 +
  169 + atomic_inc(&sl->refcnt);
  170 + smp_mb__after_atomic_inc();
  171 + if (down_interruptible(&sl->master->mutex)) {
  172 + count = 0;
  173 + goto out_dec;
  174 + }
  175 +
  176 + if (off > W1_SLAVE_DATA_SIZE) {
  177 + count = 0;
  178 + goto out;
  179 + }
  180 + if (off + count > W1_SLAVE_DATA_SIZE) {
  181 + count = 0;
  182 + goto out;
  183 + }
  184 +
  185 + memset(buf, 0, count);
  186 + memset(rom, 0, sizeof(rom));
  187 +
  188 + count = 0;
  189 + verdict = 0;
  190 + crc = 0;
  191 +
  192 + while (max_trying--) {
  193 + if (!w1_reset_select_slave(sl)) {
  194 + int count = 0;
  195 + unsigned int tm = 750;
  196 +
  197 + w1_write_8(dev, W1_CONVERT_TEMP);
  198 +
  199 + while (tm) {
  200 + tm = msleep_interruptible(tm);
  201 + if (signal_pending(current))
  202 + flush_signals(current);
  203 + }
  204 +
  205 + if (!w1_reset_select_slave(sl)) {
  206 +
  207 + w1_write_8(dev, W1_READ_SCRATCHPAD);
  208 + if ((count = w1_read_block(dev, rom, 9)) != 9) {
  209 + dev_warn(&dev->dev, "w1_read_block() returned %d instead of 9.\n", count);
  210 + }
  211 +
  212 + crc = w1_calc_crc8(rom, 8);
  213 +
  214 + if (rom[8] == crc && rom[0])
  215 + verdict = 1;
  216 + }
  217 + }
  218 +
  219 + if (!w1_therm_check_rom(rom))
  220 + break;
  221 + }
  222 +
  223 + for (i = 0; i < 9; ++i)
  224 + count += sprintf(buf + count, "%02x ", rom[i]);
  225 + count += sprintf(buf + count, ": crc=%02x %s\n",
  226 + crc, (verdict) ? "YES" : "NO");
  227 + if (verdict)
  228 + memcpy(sl->rom, rom, sizeof(sl->rom));
  229 + else
  230 + dev_warn(&dev->dev, "18S20 doesn't respond to CONVERT_TEMP.\n");
  231 +
  232 + for (i = 0; i < 9; ++i)
  233 + count += sprintf(buf + count, "%02x ", sl->rom[i]);
  234 +
  235 + count += sprintf(buf + count, "t=%d\n", w1_convert_temp(rom, sl->family->fid));
  236 +out:
  237 + up(&dev->mutex);
  238 +out_dec:
  239 + smp_mb__before_atomic_inc();
  240 + atomic_dec(&sl->refcnt);
  241 +
  242 + return count;
  243 +}
  244 +
  245 +static int __init w1_therm_init(void)
  246 +{
  247 + int err, i;
  248 +
  249 + for (i=0; i<sizeof(w1_therm_families)/sizeof(w1_therm_families[0]); ++i) {
  250 + err = w1_register_family(w1_therm_families[i].f);
  251 + if (err)
  252 + w1_therm_families[i].broken = 1;
  253 + }
  254 +
  255 + return 0;
  256 +}
  257 +
  258 +static void __exit w1_therm_fini(void)
  259 +{
  260 + int i;
  261 +
  262 + for (i=0; i<sizeof(w1_therm_families)/sizeof(w1_therm_families[0]); ++i)
  263 + if (!w1_therm_families[i].broken)
  264 + w1_unregister_family(w1_therm_families[i].f);
  265 +}
  266 +
  267 +module_init(w1_therm_init);
  268 +module_exit(w1_therm_fini);
drivers/w1/w1_ds2433.c
1   -/*
2   - * w1_ds2433.c - w1 family 23 (DS2433) driver
3   - *
4   - * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com>
5   - *
6   - * This source code is licensed under the GNU General Public License,
7   - * Version 2. See the file COPYING for more details.
8   - */
9   -
10   -#include <linux/kernel.h>
11   -#include <linux/module.h>
12   -#include <linux/moduleparam.h>
13   -#include <linux/device.h>
14   -#include <linux/types.h>
15   -#include <linux/delay.h>
16   -#ifdef CONFIG_W1_F23_CRC
17   -#include <linux/crc16.h>
18   -
19   -#define CRC16_INIT 0
20   -#define CRC16_VALID 0xb001
21   -
22   -#endif
23   -
24   -#include "w1.h"
25   -#include "w1_io.h"
26   -#include "w1_int.h"
27   -#include "w1_family.h"
28   -
29   -MODULE_LICENSE("GPL");
30   -MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
31   -MODULE_DESCRIPTION("w1 family 23 driver for DS2433, 4kb EEPROM");
32   -
33   -#define W1_EEPROM_SIZE 512
34   -#define W1_PAGE_COUNT 16
35   -#define W1_PAGE_SIZE 32
36   -#define W1_PAGE_BITS 5
37   -#define W1_PAGE_MASK 0x1F
38   -
39   -#define W1_F23_TIME 300
40   -
41   -#define W1_F23_READ_EEPROM 0xF0
42   -#define W1_F23_WRITE_SCRATCH 0x0F
43   -#define W1_F23_READ_SCRATCH 0xAA
44   -#define W1_F23_COPY_SCRATCH 0x55
45   -
46   -struct w1_f23_data {
47   - u8 memory[W1_EEPROM_SIZE];
48   - u32 validcrc;
49   -};
50   -
51   -/**
52   - * Check the file size bounds and adjusts count as needed.
53   - * This would not be needed if the file size didn't reset to 0 after a write.
54   - */
55   -static inline size_t w1_f23_fix_count(loff_t off, size_t count, size_t size)
56   -{
57   - if (off > size)
58   - return 0;
59   -
60   - if ((off + count) > size)
61   - return (size - off);
62   -
63   - return count;
64   -}
65   -
66   -#ifdef CONFIG_W1_F23_CRC
67   -static int w1_f23_refresh_block(struct w1_slave *sl, struct w1_f23_data *data,
68   - int block)
69   -{
70   - u8 wrbuf[3];
71   - int off = block * W1_PAGE_SIZE;
72   -
73   - if (data->validcrc & (1 << block))
74   - return 0;
75   -
76   - if (w1_reset_select_slave(sl)) {
77   - data->validcrc = 0;
78   - return -EIO;
79   - }
80   -
81   - wrbuf[0] = W1_F23_READ_EEPROM;
82   - wrbuf[1] = off & 0xff;
83   - wrbuf[2] = off >> 8;
84   - w1_write_block(sl->master, wrbuf, 3);
85   - w1_read_block(sl->master, &data->memory[off], W1_PAGE_SIZE);
86   -
87   - /* cache the block if the CRC is valid */
88   - if (crc16(CRC16_INIT, &data->memory[off], W1_PAGE_SIZE) == CRC16_VALID)
89   - data->validcrc |= (1 << block);
90   -
91   - return 0;
92   -}
93   -#endif /* CONFIG_W1_F23_CRC */
94   -
95   -static ssize_t w1_f23_read_bin(struct kobject *kobj, char *buf, loff_t off,
96   - size_t count)
97   -{
98   - struct w1_slave *sl = kobj_to_w1_slave(kobj);
99   -#ifdef CONFIG_W1_F23_CRC
100   - struct w1_f23_data *data = sl->family_data;
101   - int i, min_page, max_page;
102   -#else
103   - u8 wrbuf[3];
104   -#endif
105   -
106   - if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
107   - return 0;
108   -
109   - atomic_inc(&sl->refcnt);
110   - if (down_interruptible(&sl->master->mutex)) {
111   - count = 0;
112   - goto out_dec;
113   - }
114   -
115   -#ifdef CONFIG_W1_F23_CRC
116   -
117   - min_page = (off >> W1_PAGE_BITS);
118   - max_page = (off + count - 1) >> W1_PAGE_BITS;
119   - for (i = min_page; i <= max_page; i++) {
120   - if (w1_f23_refresh_block(sl, data, i)) {
121   - count = -EIO;
122   - goto out_up;
123   - }
124   - }
125   - memcpy(buf, &data->memory[off], count);
126   -
127   -#else /* CONFIG_W1_F23_CRC */
128   -
129   - /* read directly from the EEPROM */
130   - if (w1_reset_select_slave(sl)) {
131   - count = -EIO;
132   - goto out_up;
133   - }
134   -
135   - wrbuf[0] = W1_F23_READ_EEPROM;
136   - wrbuf[1] = off & 0xff;
137   - wrbuf[2] = off >> 8;
138   - w1_write_block(sl->master, wrbuf, 3);
139   - w1_read_block(sl->master, buf, count);
140   -
141   -#endif /* CONFIG_W1_F23_CRC */
142   -
143   -out_up:
144   - up(&sl->master->mutex);
145   -out_dec:
146   - atomic_dec(&sl->refcnt);
147   -
148   - return count;
149   -}
150   -
151   -/**
152   - * Writes to the scratchpad and reads it back for verification.
153   - * Then copies the scratchpad to EEPROM.
154   - * The data must be on one page.
155   - * The master must be locked.
156   - *
157   - * @param sl The slave structure
158   - * @param addr Address for the write
159   - * @param len length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK))
160   - * @param data The data to write
161   - * @return 0=Success -1=failure
162   - */
163   -static int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data)
164   -{
165   - u8 wrbuf[4];
166   - u8 rdbuf[W1_PAGE_SIZE + 3];
167   - u8 es = (addr + len - 1) & 0x1f;
168   -
169   - /* Write the data to the scratchpad */
170   - if (w1_reset_select_slave(sl))
171   - return -1;
172   -
173   - wrbuf[0] = W1_F23_WRITE_SCRATCH;
174   - wrbuf[1] = addr & 0xff;
175   - wrbuf[2] = addr >> 8;
176   -
177   - w1_write_block(sl->master, wrbuf, 3);
178   - w1_write_block(sl->master, data, len);
179   -
180   - /* Read the scratchpad and verify */
181   - if (w1_reset_select_slave(sl))
182   - return -1;
183   -
184   - w1_write_8(sl->master, W1_F23_READ_SCRATCH);
185   - w1_read_block(sl->master, rdbuf, len + 3);
186   -
187   - /* Compare what was read against the data written */
188   - if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
189   - (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0))
190   - return -1;
191   -
192   - /* Copy the scratchpad to EEPROM */
193   - if (w1_reset_select_slave(sl))
194   - return -1;
195   -
196   - wrbuf[0] = W1_F23_COPY_SCRATCH;
197   - wrbuf[3] = es;
198   - w1_write_block(sl->master, wrbuf, 4);
199   -
200   - /* Sleep for 5 ms to wait for the write to complete */
201   - msleep(5);
202   -
203   - /* Reset the bus to wake up the EEPROM (this may not be needed) */
204   - w1_reset_bus(sl->master);
205   -
206   - return 0;
207   -}
208   -
209   -static ssize_t w1_f23_write_bin(struct kobject *kobj, char *buf, loff_t off,
210   - size_t count)
211   -{
212   - struct w1_slave *sl = kobj_to_w1_slave(kobj);
213   - int addr, len, idx;
214   -
215   - if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
216   - return 0;
217   -
218   -#ifdef CONFIG_W1_F23_CRC
219   - /* can only write full blocks in cached mode */
220   - if ((off & W1_PAGE_MASK) || (count & W1_PAGE_MASK)) {
221   - dev_err(&sl->dev, "invalid offset/count off=%d cnt=%zd\n",
222   - (int)off, count);
223   - return -EINVAL;
224   - }
225   -
226   - /* make sure the block CRCs are valid */
227   - for (idx = 0; idx < count; idx += W1_PAGE_SIZE) {
228   - if (crc16(CRC16_INIT, &buf[idx], W1_PAGE_SIZE) != CRC16_VALID) {
229   - dev_err(&sl->dev, "bad CRC at offset %d\n", (int)off);
230   - return -EINVAL;
231   - }
232   - }
233   -#endif /* CONFIG_W1_F23_CRC */
234   -
235   - atomic_inc(&sl->refcnt);
236   - if (down_interruptible(&sl->master->mutex)) {
237   - count = 0;
238   - goto out_dec;
239   - }
240   -
241   - /* Can only write data to one page at a time */
242   - idx = 0;
243   - while (idx < count) {
244   - addr = off + idx;
245   - len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
246   - if (len > (count - idx))
247   - len = count - idx;
248   -
249   - if (w1_f23_write(sl, addr, len, &buf[idx]) < 0) {
250   - count = -EIO;
251   - goto out_up;
252   - }
253   - idx += len;
254   - }
255   -
256   -out_up:
257   - up(&sl->master->mutex);
258   -out_dec:
259   - atomic_dec(&sl->refcnt);
260   -
261   - return count;
262   -}
263   -
264   -static struct bin_attribute w1_f23_bin_attr = {
265   - .attr = {
266   - .name = "eeprom",
267   - .mode = S_IRUGO | S_IWUSR,
268   - .owner = THIS_MODULE,
269   - },
270   - .size = W1_EEPROM_SIZE,
271   - .read = w1_f23_read_bin,
272   - .write = w1_f23_write_bin,
273   -};
274   -
275   -static int w1_f23_add_slave(struct w1_slave *sl)
276   -{
277   - int err;
278   -#ifdef CONFIG_W1_F23_CRC
279   - struct w1_f23_data *data;
280   -
281   - data = kmalloc(sizeof(struct w1_f23_data), GFP_KERNEL);
282   - if (!data)
283   - return -ENOMEM;
284   - memset(data, 0, sizeof(struct w1_f23_data));
285   - sl->family_data = data;
286   -
287   -#endif /* CONFIG_W1_F23_CRC */
288   -
289   - err = sysfs_create_bin_file(&sl->dev.kobj, &w1_f23_bin_attr);
290   -
291   -#ifdef CONFIG_W1_F23_CRC
292   - if (err)
293   - kfree(data);
294   -#endif /* CONFIG_W1_F23_CRC */
295   -
296   - return err;
297   -}
298   -
299   -static void w1_f23_remove_slave(struct w1_slave *sl)
300   -{
301   -#ifdef CONFIG_W1_F23_CRC
302   - kfree(sl->family_data);
303   - sl->family_data = NULL;
304   -#endif /* CONFIG_W1_F23_CRC */
305   - sysfs_remove_bin_file(&sl->dev.kobj, &w1_f23_bin_attr);
306   -}
307   -
308   -static struct w1_family_ops w1_f23_fops = {
309   - .add_slave = w1_f23_add_slave,
310   - .remove_slave = w1_f23_remove_slave,
311   -};
312   -
313   -static struct w1_family w1_family_23 = {
314   - .fid = W1_EEPROM_DS2433,
315   - .fops = &w1_f23_fops,
316   -};
317   -
318   -static int __init w1_f23_init(void)
319   -{
320   - return w1_register_family(&w1_family_23);
321   -}
322   -
323   -static void __exit w1_f23_fini(void)
324   -{
325   - w1_unregister_family(&w1_family_23);
326   -}
327   -
328   -module_init(w1_f23_init);
329   -module_exit(w1_f23_fini);
drivers/w1/w1_smem.c
1   -/*
2   - * w1_smem.c
3   - *
4   - * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5   - *
6   - *
7   - * This program is free software; you can redistribute it and/or modify
8   - * it under the smems of the GNU General Public License as published by
9   - * the Free Software Foundation; either version 2 of the License, or
10   - * (at your option) any later version.
11   - *
12   - * This program is distributed in the hope that it will be useful,
13   - * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   - * GNU General Public License for more details.
16   - *
17   - * You should have received a copy of the GNU General Public License
18   - * along with this program; if not, write to the Free Software
19   - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20   - */
21   -
22   -#include <asm/types.h>
23   -
24   -#include <linux/kernel.h>
25   -#include <linux/module.h>
26   -#include <linux/moduleparam.h>
27   -#include <linux/device.h>
28   -#include <linux/types.h>
29   -
30   -#include "w1.h"
31   -#include "w1_io.h"
32   -#include "w1_int.h"
33   -#include "w1_family.h"
34   -
35   -MODULE_LICENSE("GPL");
36   -MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
37   -MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, 64bit memory family.");
38   -
39   -static struct w1_family w1_smem_family_01 = {
40   - .fid = W1_FAMILY_SMEM_01,
41   -};
42   -
43   -static struct w1_family w1_smem_family_81 = {
44   - .fid = W1_FAMILY_SMEM_81,
45   -};
46   -
47   -static int __init w1_smem_init(void)
48   -{
49   - int err;
50   -
51   - err = w1_register_family(&w1_smem_family_01);
52   - if (err)
53   - return err;
54   -
55   - err = w1_register_family(&w1_smem_family_81);
56   - if (err) {
57   - w1_unregister_family(&w1_smem_family_01);
58   - return err;
59   - }
60   -
61   - return 0;
62   -}
63   -
64   -static void __exit w1_smem_fini(void)
65   -{
66   - w1_unregister_family(&w1_smem_family_01);
67   - w1_unregister_family(&w1_smem_family_81);
68   -}
69   -
70   -module_init(w1_smem_init);
71   -module_exit(w1_smem_fini);
drivers/w1/w1_therm.c
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   -
36   -MODULE_LICENSE("GPL");
37   -MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
38   -MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
39   -
40   -static u8 bad_roms[][9] = {
41   - {0xaa, 0x00, 0x4b, 0x46, 0xff, 0xff, 0x0c, 0x10, 0x87},
42   - {}
43   - };
44   -
45   -static ssize_t w1_therm_read_bin(struct kobject *, char *, loff_t, size_t);
46   -
47   -static struct bin_attribute w1_therm_bin_attr = {
48   - .attr = {
49   - .name = "w1_slave",
50   - .mode = S_IRUGO,
51   - .owner = THIS_MODULE,
52   - },
53   - .size = W1_SLAVE_DATA_SIZE,
54   - .read = w1_therm_read_bin,
55   -};
56   -
57   -static int w1_therm_add_slave(struct w1_slave *sl)
58   -{
59   - return sysfs_create_bin_file(&sl->dev.kobj, &w1_therm_bin_attr);
60   -}
61   -
62   -static void w1_therm_remove_slave(struct w1_slave *sl)
63   -{
64   - sysfs_remove_bin_file(&sl->dev.kobj, &w1_therm_bin_attr);
65   -}
66   -
67   -static struct w1_family_ops w1_therm_fops = {
68   - .add_slave = w1_therm_add_slave,
69   - .remove_slave = w1_therm_remove_slave,
70   -};
71   -
72   -static struct w1_family w1_therm_family_DS18S20 = {
73   - .fid = W1_THERM_DS18S20,
74   - .fops = &w1_therm_fops,
75   -};
76   -
77   -static struct w1_family w1_therm_family_DS18B20 = {
78   - .fid = W1_THERM_DS18B20,
79   - .fops = &w1_therm_fops,
80   -};
81   -
82   -static struct w1_family w1_therm_family_DS1822 = {
83   - .fid = W1_THERM_DS1822,
84   - .fops = &w1_therm_fops,
85   -};
86   -
87   -struct w1_therm_family_converter
88   -{
89   - u8 broken;
90   - u16 reserved;
91   - struct w1_family *f;
92   - int (*convert)(u8 rom[9]);
93   -};
94   -
95   -static inline int w1_DS18B20_convert_temp(u8 rom[9]);
96   -static inline int w1_DS18S20_convert_temp(u8 rom[9]);
97   -
98   -static struct w1_therm_family_converter w1_therm_families[] = {
99   - {
100   - .f = &w1_therm_family_DS18S20,
101   - .convert = w1_DS18S20_convert_temp
102   - },
103   - {
104   - .f = &w1_therm_family_DS1822,
105   - .convert = w1_DS18B20_convert_temp
106   - },
107   - {
108   - .f = &w1_therm_family_DS18B20,
109   - .convert = w1_DS18B20_convert_temp
110   - },
111   -};
112   -
113   -static inline int w1_DS18B20_convert_temp(u8 rom[9])
114   -{
115   - int t = (rom[1] << 8) | rom[0];
116   - t /= 16;
117   - return t;
118   -}
119   -
120   -static inline int w1_DS18S20_convert_temp(u8 rom[9])
121   -{
122   - int t, h;
123   -
124   - if (!rom[7])
125   - return 0;
126   -
127   - if (rom[1] == 0)
128   - t = ((s32)rom[0] >> 1)*1000;
129   - else
130   - t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
131   -
132   - t -= 250;
133   - h = 1000*((s32)rom[7] - (s32)rom[6]);
134   - h /= (s32)rom[7];
135   - t += h;
136   -
137   - return t;
138   -}
139   -
140   -static inline int w1_convert_temp(u8 rom[9], u8 fid)
141   -{
142   - int i;
143   -
144   - for (i=0; i<sizeof(w1_therm_families)/sizeof(w1_therm_families[0]); ++i)
145   - if (w1_therm_families[i].f->fid == fid)
146   - return w1_therm_families[i].convert(rom);
147   -
148   - return 0;
149   -}
150   -
151   -static int w1_therm_check_rom(u8 rom[9])
152   -{
153   - int i;
154   -
155   - for (i=0; i<sizeof(bad_roms)/9; ++i)
156   - if (!memcmp(bad_roms[i], rom, 9))
157   - return 1;
158   -
159   - return 0;
160   -}
161   -
162   -static ssize_t w1_therm_read_bin(struct kobject *kobj, char *buf, loff_t off, size_t count)
163   -{
164   - struct w1_slave *sl = kobj_to_w1_slave(kobj);
165   - struct w1_master *dev = sl->master;
166   - u8 rom[9], crc, verdict;
167   - int i, max_trying = 10;
168   -
169   - atomic_inc(&sl->refcnt);
170   - smp_mb__after_atomic_inc();
171   - if (down_interruptible(&sl->master->mutex)) {
172   - count = 0;
173   - goto out_dec;
174   - }
175   -
176   - if (off > W1_SLAVE_DATA_SIZE) {
177   - count = 0;
178   - goto out;
179   - }
180   - if (off + count > W1_SLAVE_DATA_SIZE) {
181   - count = 0;
182   - goto out;
183   - }
184   -
185   - memset(buf, 0, count);
186   - memset(rom, 0, sizeof(rom));
187   -
188   - count = 0;
189   - verdict = 0;
190   - crc = 0;
191   -
192   - while (max_trying--) {
193   - if (!w1_reset_select_slave(sl)) {
194   - int count = 0;
195   - unsigned int tm = 750;
196   -
197   - w1_write_8(dev, W1_CONVERT_TEMP);
198   -
199   - while (tm) {
200   - tm = msleep_interruptible(tm);
201   - if (signal_pending(current))
202   - flush_signals(current);
203   - }
204   -
205   - if (!w1_reset_select_slave(sl)) {
206   -
207   - w1_write_8(dev, W1_READ_SCRATCHPAD);
208   - if ((count = w1_read_block(dev, rom, 9)) != 9) {
209   - dev_warn(&dev->dev, "w1_read_block() returned %d instead of 9.\n", count);
210   - }
211   -
212   - crc = w1_calc_crc8(rom, 8);
213   -
214   - if (rom[8] == crc && rom[0])
215   - verdict = 1;
216   - }
217   - }
218   -
219   - if (!w1_therm_check_rom(rom))
220   - break;
221   - }
222   -
223   - for (i = 0; i < 9; ++i)
224   - count += sprintf(buf + count, "%02x ", rom[i]);
225   - count += sprintf(buf + count, ": crc=%02x %s\n",
226   - crc, (verdict) ? "YES" : "NO");
227   - if (verdict)
228   - memcpy(sl->rom, rom, sizeof(sl->rom));
229   - else
230   - dev_warn(&dev->dev, "18S20 doesn't respond to CONVERT_TEMP.\n");
231   -
232   - for (i = 0; i < 9; ++i)
233   - count += sprintf(buf + count, "%02x ", sl->rom[i]);
234   -
235   - count += sprintf(buf + count, "t=%d\n", w1_convert_temp(rom, sl->family->fid));
236   -out:
237   - up(&dev->mutex);
238   -out_dec:
239   - smp_mb__before_atomic_inc();
240   - atomic_dec(&sl->refcnt);
241   -
242   - return count;
243   -}
244   -
245   -static int __init w1_therm_init(void)
246   -{
247   - int err, i;
248   -
249   - for (i=0; i<sizeof(w1_therm_families)/sizeof(w1_therm_families[0]); ++i) {
250   - err = w1_register_family(w1_therm_families[i].f);
251   - if (err)
252   - w1_therm_families[i].broken = 1;
253   - }
254   -
255   - return 0;
256   -}
257   -
258   -static void __exit w1_therm_fini(void)
259   -{
260   - int i;
261   -
262   - for (i=0; i<sizeof(w1_therm_families)/sizeof(w1_therm_families[0]); ++i)
263   - if (!w1_therm_families[i].broken)
264   - w1_unregister_family(w1_therm_families[i].f);
265   -}
266   -
267   -module_init(w1_therm_init);
268   -module_exit(w1_therm_fini);