4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
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.
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.
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
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/mod_devicetable.h>
25 #include <linux/usb.h>
29 static struct usb_device_id ds_id_table
[] = {
30 { USB_DEVICE(0x04fa, 0x2490) },
33 MODULE_DEVICE_TABLE(usb
, ds_id_table
);
35 int ds_probe(struct usb_interface
*, const struct usb_device_id
*);
36 void ds_disconnect(struct usb_interface
*);
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 int ds_start_pulse(struct ds_device
*, int);
44 int ds_set_speed(struct ds_device
*, int);
45 int ds_reset(struct ds_device
*, struct ds_status
*);
46 int ds_detect(struct ds_device
*, struct ds_status
*);
47 int ds_stop_pulse(struct ds_device
*, int);
48 struct ds_device
* ds_get_device(void);
49 void ds_put_device(struct ds_device
*);
51 static inline void ds_dump_status(unsigned char *, unsigned char *, int);
52 static int ds_send_control(struct ds_device
*, u16
, u16
);
53 static int ds_send_control_mode(struct ds_device
*, u16
, u16
);
54 static int ds_send_control_cmd(struct ds_device
*, u16
, u16
);
57 static struct usb_driver ds_driver
= {
61 .disconnect
= ds_disconnect
,
62 .id_table
= ds_id_table
,
65 static struct ds_device
*ds_dev
;
67 struct ds_device
* ds_get_device(void)
70 atomic_inc(&ds_dev
->refcnt
);
74 void ds_put_device(struct ds_device
*dev
)
76 atomic_dec(&dev
->refcnt
);
79 static int ds_send_control_cmd(struct ds_device
*dev
, u16 value
, u16 index
)
83 err
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, dev
->ep
[EP_CONTROL
]),
84 CONTROL_CMD
, 0x40, value
, index
, NULL
, 0, 1000);
86 printk(KERN_ERR
"Failed to send command control message %x.%x: err=%d.\n",
94 static int ds_send_control_mode(struct ds_device
*dev
, u16 value
, u16 index
)
98 err
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, dev
->ep
[EP_CONTROL
]),
99 MODE_CMD
, 0x40, value
, index
, NULL
, 0, 1000);
101 printk(KERN_ERR
"Failed to send mode control message %x.%x: err=%d.\n",
109 static int ds_send_control(struct ds_device
*dev
, u16 value
, u16 index
)
113 err
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, dev
->ep
[EP_CONTROL
]),
114 COMM_CMD
, 0x40, value
, index
, NULL
, 0, 1000);
116 printk(KERN_ERR
"Failed to send control message %x.%x: err=%d.\n",
124 static inline void ds_dump_status(unsigned char *buf
, unsigned char *str
, int off
)
126 printk("%45s: %8x\n", str
, buf
[off
]);
129 int ds_recv_status_nodump(struct ds_device
*dev
, struct ds_status
*st
, unsigned char *buf
, int size
)
133 memset(st
, 0, sizeof(st
));
136 err
= usb_bulk_msg(dev
->udev
, usb_rcvbulkpipe(dev
->udev
, dev
->ep
[EP_STATUS
]), buf
, size
, &count
, 100);
138 printk(KERN_ERR
"Failed to read 1-wire data from 0x%x: err=%d.\n", dev
->ep
[EP_STATUS
], err
);
142 if (count
>= sizeof(*st
))
143 memcpy(st
, buf
, sizeof(*st
));
148 static int ds_recv_status(struct ds_device
*dev
, struct ds_status
*st
)
150 unsigned char buf
[64];
151 int count
, err
= 0, i
;
153 memcpy(st
, buf
, sizeof(*st
));
155 count
= ds_recv_status_nodump(dev
, st
, buf
, sizeof(buf
));
159 printk("0x%x: count=%d, status: ", dev
->ep
[EP_STATUS
], count
);
160 for (i
=0; i
<count
; ++i
)
161 printk("%02x ", buf
[i
]);
165 ds_dump_status(buf
, "enable flag", 0);
166 ds_dump_status(buf
, "1-wire speed", 1);
167 ds_dump_status(buf
, "strong pullup duration", 2);
168 ds_dump_status(buf
, "programming pulse duration", 3);
169 ds_dump_status(buf
, "pulldown slew rate control", 4);
170 ds_dump_status(buf
, "write-1 low time", 5);
171 ds_dump_status(buf
, "data sample offset/write-0 recovery time", 6);
172 ds_dump_status(buf
, "reserved (test register)", 7);
173 ds_dump_status(buf
, "device status flags", 8);
174 ds_dump_status(buf
, "communication command byte 1", 9);
175 ds_dump_status(buf
, "communication command byte 2", 10);
176 ds_dump_status(buf
, "communication command buffer status", 11);
177 ds_dump_status(buf
, "1-wire data output buffer status", 12);
178 ds_dump_status(buf
, "1-wire data input buffer status", 13);
179 ds_dump_status(buf
, "reserved", 14);
180 ds_dump_status(buf
, "reserved", 15);
183 memcpy(st
, buf
, sizeof(*st
));
185 if (st
->status
& ST_EPOF
) {
186 printk(KERN_INFO
"Resetting device after ST_EPOF.\n");
187 err
= ds_send_control_cmd(dev
, CTL_RESET_DEVICE
, 0);
190 count
= ds_recv_status_nodump(dev
, st
, buf
, sizeof(buf
));
195 if (st
->status
& ST_IDLE
) {
196 printk(KERN_INFO
"Resetting pulse after ST_IDLE.\n");
197 err
= ds_start_pulse(dev
, PULLUP_PULSE_DURATION
);
206 static int ds_recv_data(struct ds_device
*dev
, unsigned char *buf
, int size
)
212 err
= usb_bulk_msg(dev
->udev
, usb_rcvbulkpipe(dev
->udev
, dev
->ep
[EP_DATA_IN
]),
213 buf
, size
, &count
, 1000);
215 printk(KERN_INFO
"Clearing ep0x%x.\n", dev
->ep
[EP_DATA_IN
]);
216 usb_clear_halt(dev
->udev
, usb_rcvbulkpipe(dev
->udev
, dev
->ep
[EP_DATA_IN
]));
217 ds_recv_status(dev
, &st
);
225 printk("%s: count=%d: ", __func__
, count
);
226 for (i
=0; i
<count
; ++i
)
227 printk("%02x ", buf
[i
]);
234 static int ds_send_data(struct ds_device
*dev
, unsigned char *buf
, int len
)
239 err
= usb_bulk_msg(dev
->udev
, usb_sndbulkpipe(dev
->udev
, dev
->ep
[EP_DATA_OUT
]), buf
, len
, &count
, 1000);
241 printk(KERN_ERR
"Failed to read 1-wire data from 0x02: err=%d.\n", err
);
248 int ds_stop_pulse(struct ds_device
*dev
, int limit
)
251 int count
= 0, err
= 0;
255 err
= ds_send_control(dev
, CTL_HALT_EXE_IDLE
, 0);
258 err
= ds_send_control(dev
, CTL_RESUME_EXE
, 0);
261 err
= ds_recv_status_nodump(dev
, &st
, buf
, sizeof(buf
));
265 if ((st
.status
& ST_SPUA
) == 0) {
266 err
= ds_send_control_mode(dev
, MOD_PULSE_EN
, 0);
270 } while(++count
< limit
);
275 int ds_detect(struct ds_device
*dev
, struct ds_status
*st
)
279 err
= ds_send_control_cmd(dev
, CTL_RESET_DEVICE
, 0);
283 err
= ds_send_control(dev
, COMM_SET_DURATION
| COMM_IM
, 0);
287 err
= ds_send_control(dev
, COMM_SET_DURATION
| COMM_IM
| COMM_TYPE
, 0x40);
291 err
= ds_send_control_mode(dev
, MOD_PULSE_EN
, PULSE_PROG
);
295 err
= ds_recv_status(dev
, st
);
300 int ds_wait_status(struct ds_device
*dev
, struct ds_status
*st
)
306 err
= ds_recv_status_nodump(dev
, st
, buf
, sizeof(buf
));
310 printk("0x%x: count=%d, status: ", dev
->ep
[EP_STATUS
], err
);
311 for (i
=0; i
<err
; ++i
)
312 printk("%02x ", buf
[i
]);
316 } while(!(buf
[0x08] & 0x20) && !(err
< 0) && ++count
< 100);
319 if (((err
> 16) && (buf
[0x10] & 0x01)) || count
>= 100 || err
< 0) {
320 ds_recv_status(dev
, st
);
328 int ds_reset(struct ds_device
*dev
, struct ds_status
*st
)
332 //err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_F | COMM_IM | COMM_SE, SPEED_FLEXIBLE);
333 err
= ds_send_control(dev
, 0x43, SPEED_NORMAL
);
337 ds_wait_status(dev
, st
);
339 if (st
->command_buffer_status
) {
340 printk(KERN_INFO
"Short circuit.\n");
348 int ds_set_speed(struct ds_device
*dev
, int speed
)
352 if (speed
!= SPEED_NORMAL
&& speed
!= SPEED_FLEXIBLE
&& speed
!= SPEED_OVERDRIVE
)
355 if (speed
!= SPEED_OVERDRIVE
)
356 speed
= SPEED_FLEXIBLE
;
360 err
= ds_send_control_mode(dev
, MOD_1WIRE_SPEED
, speed
);
367 int ds_start_pulse(struct ds_device
*dev
, int delay
)
370 u8 del
= 1 + (u8
)(delay
>> 4);
374 err
= ds_stop_pulse(dev
, 10);
378 err
= ds_send_control_mode(dev
, MOD_PULSE_EN
, PULSE_SPUE
);
382 err
= ds_send_control(dev
, COMM_SET_DURATION
| COMM_IM
, del
);
386 err
= ds_send_control(dev
, COMM_PULSE
| COMM_IM
| COMM_F
, 0);
392 ds_wait_status(dev
, &st
);
397 int ds_touch_bit(struct ds_device
*dev
, u8 bit
, u8
*tbit
)
401 u16 value
= (COMM_BIT_IO
| COMM_IM
) | ((bit
) ? COMM_D
: 0);
404 err
= ds_send_control(dev
, value
, 0);
410 err
= ds_wait_status(dev
, &st
);
414 cmd
= st
.command0
| (st
.command1
<< 8);
415 } while (cmd
!= value
&& ++count
< 10);
417 if (err
< 0 || count
>= 10) {
418 printk(KERN_ERR
"Failed to obtain status.\n");
422 err
= ds_recv_data(dev
, tbit
, sizeof(*tbit
));
429 int ds_write_bit(struct ds_device
*dev
, u8 bit
)
434 err
= ds_send_control(dev
, COMM_BIT_IO
| COMM_IM
| (bit
) ? COMM_D
: 0, 0);
438 ds_wait_status(dev
, &st
);
443 int ds_write_byte(struct ds_device
*dev
, u8 byte
)
449 err
= ds_send_control(dev
, COMM_BYTE_IO
| COMM_IM
| COMM_SPU
, byte
);
453 err
= ds_wait_status(dev
, &st
);
457 err
= ds_recv_data(dev
, &rbyte
, sizeof(rbyte
));
461 ds_start_pulse(dev
, PULLUP_PULSE_DURATION
);
463 return !(byte
== rbyte
);
466 int ds_read_bit(struct ds_device
*dev
, u8
*bit
)
470 err
= ds_send_control_mode(dev
, MOD_PULSE_EN
, PULSE_SPUE
);
474 err
= ds_send_control(dev
, COMM_BIT_IO
| COMM_IM
| COMM_SPU
| COMM_D
, 0);
478 err
= ds_recv_data(dev
, bit
, sizeof(*bit
));
485 int ds_read_byte(struct ds_device
*dev
, u8
*byte
)
490 err
= ds_send_control(dev
, COMM_BYTE_IO
| COMM_IM
, 0xff);
494 ds_wait_status(dev
, &st
);
496 err
= ds_recv_data(dev
, byte
, sizeof(*byte
));
503 int ds_read_block(struct ds_device
*dev
, u8
*buf
, int len
)
511 memset(buf
, 0xFF, len
);
513 err
= ds_send_data(dev
, buf
, len
);
517 err
= ds_send_control(dev
, COMM_BLOCK_IO
| COMM_IM
| COMM_SPU
, len
);
521 ds_wait_status(dev
, &st
);
523 memset(buf
, 0x00, len
);
524 err
= ds_recv_data(dev
, buf
, len
);
529 int ds_write_block(struct ds_device
*dev
, u8
*buf
, int len
)
534 err
= ds_send_data(dev
, buf
, len
);
538 ds_wait_status(dev
, &st
);
540 err
= ds_send_control(dev
, COMM_BLOCK_IO
| COMM_IM
| COMM_SPU
, len
);
544 ds_wait_status(dev
, &st
);
546 err
= ds_recv_data(dev
, buf
, len
);
550 ds_start_pulse(dev
, PULLUP_PULSE_DURATION
);
552 return !(err
== len
);
555 int ds_search(struct ds_device
*dev
, u64 init
, u64
*buf
, u8 id_number
, int conditional_search
)
561 memset(buf
, 0, sizeof(buf
));
563 err
= ds_send_data(ds_dev
, (unsigned char *)&init
, 8);
567 ds_wait_status(ds_dev
, &st
);
569 value
= COMM_SEARCH_ACCESS
| COMM_IM
| COMM_SM
| COMM_F
| COMM_RTS
;
570 index
= (conditional_search
? 0xEC : 0xF0) | (id_number
<< 8);
571 err
= ds_send_control(ds_dev
, value
, index
);
575 ds_wait_status(ds_dev
, &st
);
577 err
= ds_recv_data(ds_dev
, (unsigned char *)buf
, 8*id_number
);
584 int ds_match_access(struct ds_device
*dev
, u64 init
)
589 err
= ds_send_data(dev
, (unsigned char *)&init
, sizeof(init
));
593 ds_wait_status(dev
, &st
);
595 err
= ds_send_control(dev
, COMM_MATCH_ACCESS
| COMM_IM
| COMM_RST
, 0x0055);
599 ds_wait_status(dev
, &st
);
604 int ds_set_path(struct ds_device
*dev
, u64 init
)
610 memcpy(buf
, &init
, 8);
611 buf
[8] = BRANCH_MAIN
;
613 err
= ds_send_data(dev
, buf
, sizeof(buf
));
617 ds_wait_status(dev
, &st
);
619 err
= ds_send_control(dev
, COMM_SET_PATH
| COMM_IM
| COMM_RST
, 0);
623 ds_wait_status(dev
, &st
);
628 int ds_probe(struct usb_interface
*intf
, const struct usb_device_id
*udev_id
)
630 struct usb_device
*udev
= interface_to_usbdev(intf
);
631 struct usb_endpoint_descriptor
*endpoint
;
632 struct usb_host_interface
*iface_desc
;
635 ds_dev
= kmalloc(sizeof(struct ds_device
), GFP_KERNEL
);
637 printk(KERN_INFO
"Failed to allocate new DS9490R structure.\n");
641 ds_dev
->udev
= usb_get_dev(udev
);
642 usb_set_intfdata(intf
, ds_dev
);
644 err
= usb_set_interface(ds_dev
->udev
, intf
->altsetting
[0].desc
.bInterfaceNumber
, 3);
646 printk(KERN_ERR
"Failed to set alternative setting 3 for %d interface: err=%d.\n",
647 intf
->altsetting
[0].desc
.bInterfaceNumber
, err
);
651 err
= usb_reset_configuration(ds_dev
->udev
);
653 printk(KERN_ERR
"Failed to reset configuration: err=%d.\n", err
);
657 iface_desc
= &intf
->altsetting
[0];
658 if (iface_desc
->desc
.bNumEndpoints
!= NUM_EP
-1) {
659 printk(KERN_INFO
"Num endpoints=%d. It is not DS9490R.\n", iface_desc
->desc
.bNumEndpoints
);
663 atomic_set(&ds_dev
->refcnt
, 0);
664 memset(ds_dev
->ep
, 0, sizeof(ds_dev
->ep
));
667 * This loop doesn'd show control 0 endpoint,
668 * so we will fill only 1-3 endpoints entry.
670 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
671 endpoint
= &iface_desc
->endpoint
[i
].desc
;
673 ds_dev
->ep
[i
+1] = endpoint
->bEndpointAddress
;
675 printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
676 i
, endpoint
->bEndpointAddress
, le16_to_cpu(endpoint
->wMaxPacketSize
),
677 (endpoint
->bEndpointAddress
& USB_DIR_IN
)?"IN":"OUT",
678 endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
685 u64 init
=0xb30000002078ee81ull
;
688 ds_reset(ds_dev
, &st
);
689 err
= ds_search(ds_dev
, init
, buf
, 3, 0);
692 for (i
=0; i
<err
; ++i
)
693 printk("%d: %llx\n", i
, buf
[i
]);
695 printk("Resetting...\n");
696 ds_reset(ds_dev
, &st
);
697 printk("Setting path for %llx.\n", init
);
698 err
= ds_set_path(ds_dev
, init
);
701 printk("Calling MATCH_ACCESS.\n");
702 err
= ds_match_access(ds_dev
, init
);
706 printk("Searching the bus...\n");
707 err
= ds_search(ds_dev
, init
, buf
, 3, 0);
709 printk("ds_search() returned %d\n", err
);
713 for (i
=0; i
<err
; ++i
)
714 printk("%d: %llx\n", i
, buf
[i
]);
723 void ds_disconnect(struct usb_interface
*intf
)
725 struct ds_device
*dev
;
727 dev
= usb_get_intfdata(intf
);
728 usb_set_intfdata(intf
, NULL
);
730 while (atomic_read(&dev
->refcnt
)) {
731 printk(KERN_INFO
"Waiting for DS to become free: refcnt=%d.\n",
732 atomic_read(&dev
->refcnt
));
734 if (msleep_interruptible(1000))
735 flush_signals(current
);
738 usb_put_dev(dev
->udev
);
747 err
= usb_register(&ds_driver
);
749 printk(KERN_INFO
"Failed to register DS9490R USB device: err=%d.\n", err
);
758 usb_deregister(&ds_driver
);
761 module_init(ds_init
);
762 module_exit(ds_fini
);
764 MODULE_LICENSE("GPL");
765 MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
767 EXPORT_SYMBOL(ds_touch_bit
);
768 EXPORT_SYMBOL(ds_read_byte
);
769 EXPORT_SYMBOL(ds_read_bit
);
770 EXPORT_SYMBOL(ds_read_block
);
771 EXPORT_SYMBOL(ds_write_byte
);
772 EXPORT_SYMBOL(ds_write_bit
);
773 EXPORT_SYMBOL(ds_write_block
);
774 EXPORT_SYMBOL(ds_reset
);
775 EXPORT_SYMBOL(ds_get_device
);
776 EXPORT_SYMBOL(ds_put_device
);
779 * This functions can be used for EEPROM programming,
780 * when driver will be included into mainline this will
781 * require uncommenting.
784 EXPORT_SYMBOL(ds_start_pulse
);
785 EXPORT_SYMBOL(ds_set_speed
);
786 EXPORT_SYMBOL(ds_detect
);
787 EXPORT_SYMBOL(ds_stop_pulse
);
788 EXPORT_SYMBOL(ds_search
);