1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ds2490.c USB to one wire bridge
5 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/usb.h>
12 #include <linux/slab.h>
17 /* USB Control request vendor type */
20 /* COMMAND TYPE CODES */
21 #define CONTROL_CMD 0x00
25 /* CONTROL COMMAND CODES */
26 #define CTL_RESET_DEVICE 0x0000
27 #define CTL_START_EXE 0x0001
28 #define CTL_RESUME_EXE 0x0002
29 #define CTL_HALT_EXE_IDLE 0x0003
30 #define CTL_HALT_EXE_DONE 0x0004
31 #define CTL_FLUSH_COMM_CMDS 0x0007
32 #define CTL_FLUSH_RCV_BUFFER 0x0008
33 #define CTL_FLUSH_XMT_BUFFER 0x0009
34 #define CTL_GET_COMM_CMDS 0x000A
36 /* MODE COMMAND CODES */
37 #define MOD_PULSE_EN 0x0000
38 #define MOD_SPEED_CHANGE_EN 0x0001
39 #define MOD_1WIRE_SPEED 0x0002
40 #define MOD_STRONG_PU_DURATION 0x0003
41 #define MOD_PULLDOWN_SLEWRATE 0x0004
42 #define MOD_PROG_PULSE_DURATION 0x0005
43 #define MOD_WRITE1_LOWTIME 0x0006
44 #define MOD_DSOW0_TREC 0x0007
46 /* COMMUNICATION COMMAND CODES */
47 #define COMM_ERROR_ESCAPE 0x0601
48 #define COMM_SET_DURATION 0x0012
49 #define COMM_BIT_IO 0x0020
50 #define COMM_PULSE 0x0030
51 #define COMM_1_WIRE_RESET 0x0042
52 #define COMM_BYTE_IO 0x0052
53 #define COMM_MATCH_ACCESS 0x0064
54 #define COMM_BLOCK_IO 0x0074
55 #define COMM_READ_STRAIGHT 0x0080
56 #define COMM_DO_RELEASE 0x6092
57 #define COMM_SET_PATH 0x00A2
58 #define COMM_WRITE_SRAM_PAGE 0x00B2
59 #define COMM_WRITE_EPROM 0x00C4
60 #define COMM_READ_CRC_PROT_PAGE 0x00D4
61 #define COMM_READ_REDIRECT_PAGE_CRC 0x21E4
62 #define COMM_SEARCH_ACCESS 0x00F4
64 /* Communication command bits */
65 #define COMM_TYPE 0x0008
66 #define COMM_SE 0x0008
69 #define COMM_CH 0x0008
70 #define COMM_SM 0x0008
72 #define COMM_IM 0x0001
74 #define COMM_PS 0x4000
75 #define COMM_PST 0x4000
76 #define COMM_CIB 0x4000
77 #define COMM_RTS 0x4000
78 #define COMM_DT 0x2000
79 #define COMM_SPU 0x1000
81 #define COMM_NTF 0x0400
82 #define COMM_ICP 0x0200
83 #define COMM_RST 0x0100
85 #define PULSE_PROG 0x01
86 #define PULSE_SPUE 0x02
88 #define BRANCH_MAIN 0xCC
89 #define BRANCH_AUX 0x33
92 #define ST_SPUA 0x01 /* Strong Pull-up is active */
93 #define ST_PRGA 0x02 /* 12V programming pulse is being generated */
94 #define ST_12VP 0x04 /* external 12V programming voltage is present */
95 #define ST_PMOD 0x08 /* DS2490 powered from USB and external sources */
96 #define ST_HALT 0x10 /* DS2490 is currently halted */
97 #define ST_IDLE 0x20 /* DS2490 is currently idle */
99 /* Status transfer size, 16 bytes status, 16 byte result flags */
101 /* 1-wire data i/o fifo size, 128 bytes */
102 #define FIFO_SIZE 0x80
104 /* Result Register flags */
105 #define RR_DETECT 0xA5 /* New device detected */
106 #define RR_NRS 0x01 /* Reset no presence or ... */
107 #define RR_SH 0x02 /* short on reset or set path */
108 #define RR_APP 0x04 /* alarming presence on reset */
109 #define RR_VPP 0x08 /* 12V expected not seen */
110 #define RR_CMP 0x10 /* compare error */
111 #define RR_CRC 0x20 /* CRC error detected */
112 #define RR_RDP 0x40 /* redirected page */
113 #define RR_EOS 0x80 /* end of search error */
115 #define SPEED_NORMAL 0x00
116 #define SPEED_FLEXIBLE 0x01
117 #define SPEED_OVERDRIVE 0x02
122 #define EP_DATA_OUT 2
126 struct list_head ds_entry
;
128 struct usb_device
*udev
;
129 struct usb_interface
*intf
;
134 * 0: pullup not active, else duration in milliseconds
137 /* spu_bit contains COMM_SPU or 0 depending on if the strong pullup
138 * should be active or not for writes.
145 struct w1_bus_master master
;
160 u8 command_buffer_status
;
161 u8 data_out_buffer_status
;
162 u8 data_in_buffer_status
;
167 static LIST_HEAD(ds_devices
);
168 static DEFINE_MUTEX(ds_mutex
);
170 static int ds_send_control_cmd(struct ds_device
*dev
, u16 value
, u16 index
)
174 err
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, dev
->ep
[EP_CONTROL
]),
175 CONTROL_CMD
, VENDOR
, value
, index
, NULL
, 0, 1000);
177 dev_err(&dev
->udev
->dev
,
178 "Failed to send command control message %x.%x: err=%d.\n",
186 static int ds_send_control_mode(struct ds_device
*dev
, u16 value
, u16 index
)
190 err
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, dev
->ep
[EP_CONTROL
]),
191 MODE_CMD
, VENDOR
, value
, index
, NULL
, 0, 1000);
193 dev_err(&dev
->udev
->dev
,
194 "Failed to send mode control message %x.%x: err=%d.\n",
202 static int ds_send_control(struct ds_device
*dev
, u16 value
, u16 index
)
206 err
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, dev
->ep
[EP_CONTROL
]),
207 COMM_CMD
, VENDOR
, value
, index
, NULL
, 0, 1000);
209 dev_err(&dev
->udev
->dev
,
210 "Failed to send control message %x.%x: err=%d.\n",
218 static void ds_dump_status(struct ds_device
*ds_dev
, unsigned char *buf
, int count
)
220 struct device
*dev
= &ds_dev
->udev
->dev
;
223 dev_info(dev
, "ep_status=0x%x, count=%d, status=%*phC",
224 ds_dev
->ep
[EP_STATUS
], count
, count
, buf
);
227 dev_dbg(dev
, "enable flag: 0x%02x", buf
[0]);
228 dev_dbg(dev
, "1-wire speed: 0x%02x", buf
[1]);
229 dev_dbg(dev
, "strong pullup duration: 0x%02x", buf
[2]);
230 dev_dbg(dev
, "programming pulse duration: 0x%02x", buf
[3]);
231 dev_dbg(dev
, "pulldown slew rate control: 0x%02x", buf
[4]);
232 dev_dbg(dev
, "write-1 low time: 0x%02x", buf
[5]);
233 dev_dbg(dev
, "data sample offset/write-0 recovery time: 0x%02x", buf
[6]);
234 dev_dbg(dev
, "reserved (test register): 0x%02x", buf
[7]);
235 dev_dbg(dev
, "device status flags: 0x%02x", buf
[8]);
236 dev_dbg(dev
, "communication command byte 1: 0x%02x", buf
[9]);
237 dev_dbg(dev
, "communication command byte 2: 0x%02x", buf
[10]);
238 dev_dbg(dev
, "communication command buffer status: 0x%02x", buf
[11]);
239 dev_dbg(dev
, "1-wire data output buffer status: 0x%02x", buf
[12]);
240 dev_dbg(dev
, "1-wire data input buffer status: 0x%02x", buf
[13]);
241 dev_dbg(dev
, "reserved: 0x%02x", buf
[14]);
242 dev_dbg(dev
, "reserved: 0x%02x", buf
[15]);
245 for (i
= 16; i
< count
; ++i
) {
246 if (buf
[i
] == RR_DETECT
) {
247 dev_dbg(dev
, "New device detect.\n");
250 dev_dbg(dev
, "Result Register Value: 0x%02x", buf
[i
]);
252 dev_dbg(dev
, "NRS: Reset no presence or ...\n");
254 dev_dbg(dev
, "SH: short on reset or set path\n");
256 dev_dbg(dev
, "APP: alarming presence on reset\n");
258 dev_dbg(dev
, "VPP: 12V expected not seen\n");
260 dev_dbg(dev
, "CMP: compare error\n");
262 dev_dbg(dev
, "CRC: CRC error detected\n");
264 dev_dbg(dev
, "RDP: redirected page\n");
266 dev_dbg(dev
, "EOS: end of search error\n");
270 static int ds_recv_status(struct ds_device
*dev
, struct ds_status
*st
)
275 memset(st
, 0, sizeof(*st
));
278 err
= usb_interrupt_msg(dev
->udev
,
279 usb_rcvintpipe(dev
->udev
,
281 dev
->st_buf
, sizeof(dev
->st_buf
),
284 dev_err(&dev
->udev
->dev
,
285 "Failed to read 1-wire data from 0x%x: err=%d.\n",
286 dev
->ep
[EP_STATUS
], err
);
290 if (st
&& count
>= sizeof(*st
))
291 memcpy(st
, dev
->st_buf
, sizeof(*st
));
296 static void ds_reset_device(struct ds_device
*dev
)
298 ds_send_control_cmd(dev
, CTL_RESET_DEVICE
, 0);
299 /* Always allow strong pullup which allow individual writes to use
302 if (ds_send_control_mode(dev
, MOD_PULSE_EN
, PULSE_SPUE
))
303 dev_err(&dev
->udev
->dev
,
304 "%s: Error allowing strong pullup\n", __func__
);
305 /* Chip strong pullup time was cleared. */
306 if (dev
->spu_sleep
) {
307 /* lower 4 bits are 0, see ds_set_pullup */
308 u8 del
= dev
->spu_sleep
>>4;
310 if (ds_send_control(dev
, COMM_SET_DURATION
| COMM_IM
, del
))
311 dev_err(&dev
->udev
->dev
,
312 "%s: Error setting duration\n", __func__
);
316 static int ds_recv_data(struct ds_device
*dev
, unsigned char *buf
, int size
)
320 /* Careful on size. If size is less than what is available in
321 * the input buffer, the device fails the bulk transfer and
322 * clears the input buffer. It could read the maximum size of
323 * the data buffer, but then do you return the first, last, or
324 * some set of the middle size bytes? As long as the rest of
325 * the code is correct there will be size bytes waiting. A
326 * call to ds_wait_status will wait until the device is idle
327 * and any data to be received would have been available.
330 err
= usb_bulk_msg(dev
->udev
, usb_rcvbulkpipe(dev
->udev
, dev
->ep
[EP_DATA_IN
]),
331 buf
, size
, &count
, 1000);
335 dev_info(&dev
->udev
->dev
, "Clearing ep0x%x.\n", dev
->ep
[EP_DATA_IN
]);
336 usb_clear_halt(dev
->udev
, usb_rcvbulkpipe(dev
->udev
, dev
->ep
[EP_DATA_IN
]));
338 /* status might tell us why endpoint is stuck? */
339 recv_len
= ds_recv_status(dev
, NULL
);
341 ds_dump_status(dev
, dev
->st_buf
, recv_len
);
350 printk("%s: count=%d: ", __func__
, count
);
351 for (i
= 0; i
< count
; ++i
)
352 printk("%02x ", buf
[i
]);
359 static int ds_send_data(struct ds_device
*dev
, unsigned char *buf
, int len
)
364 err
= usb_bulk_msg(dev
->udev
, usb_sndbulkpipe(dev
->udev
, dev
->ep
[EP_DATA_OUT
]), buf
, len
, &count
, 1000);
366 dev_err(&dev
->udev
->dev
, "Failed to write 1-wire data to ep0x%x: "
367 "err=%d.\n", dev
->ep
[EP_DATA_OUT
], err
);
376 int ds_stop_pulse(struct ds_device
*dev
, int limit
)
379 int count
= 0, err
= 0;
382 err
= ds_send_control(dev
, CTL_HALT_EXE_IDLE
, 0);
385 err
= ds_send_control(dev
, CTL_RESUME_EXE
, 0);
388 err
= ds_recv_status(dev
, &st
);
392 if ((st
.status
& ST_SPUA
) == 0) {
393 err
= ds_send_control_mode(dev
, MOD_PULSE_EN
, 0);
397 } while (++count
< limit
);
402 int ds_detect(struct ds_device
*dev
, struct ds_status
*st
)
406 err
= ds_send_control_cmd(dev
, CTL_RESET_DEVICE
, 0);
410 err
= ds_send_control(dev
, COMM_SET_DURATION
| COMM_IM
, 0);
414 err
= ds_send_control(dev
, COMM_SET_DURATION
| COMM_IM
| COMM_TYPE
, 0x40);
418 err
= ds_send_control_mode(dev
, MOD_PULSE_EN
, PULSE_PROG
);
422 err
= ds_dump_status(dev
, st
);
429 static int ds_wait_status(struct ds_device
*dev
, struct ds_status
*st
)
435 err
= ds_recv_status(dev
, st
);
439 printk("0x%x: count=%d, status: ", dev
->ep
[EP_STATUS
], err
);
440 for (i
= 0; i
< err
; ++i
)
441 printk("%02x ", dev
->st_buf
[i
]);
445 } while (!(st
->status
& ST_IDLE
) && !(err
< 0) && ++count
< 100);
447 if (err
>= 16 && st
->status
& ST_EPOF
) {
448 dev_info(&dev
->udev
->dev
, "Resetting device after ST_EPOF.\n");
449 ds_reset_device(dev
);
450 /* Always dump the device status. */
454 /* Dump the status for errors or if there is extended return data.
455 * The extended status includes new device detection (maybe someone
456 * can do something with it).
458 if (err
> 16 || count
>= 100 || err
< 0)
459 ds_dump_status(dev
, dev
->st_buf
, err
);
461 /* Extended data isn't an error. Well, a short is, but the dump
462 * would have already told the user that and we can't do anything
463 * about it in software anyway.
465 if (count
>= 100 || err
< 0)
471 static int ds_reset(struct ds_device
*dev
)
475 /* Other potentionally interesting flags for reset.
477 * COMM_NTF: Return result register feedback. This could be used to
478 * detect some conditions such as short, alarming presence, or
479 * detect if a new device was detected.
481 * COMM_SE which allows SPEED_NORMAL, SPEED_FLEXIBLE, SPEED_OVERDRIVE:
482 * Select the data transfer rate.
484 err
= ds_send_control(dev
, COMM_1_WIRE_RESET
| COMM_IM
, SPEED_NORMAL
);
492 static int ds_set_speed(struct ds_device
*dev
, int speed
)
496 if (speed
!= SPEED_NORMAL
&& speed
!= SPEED_FLEXIBLE
&& speed
!= SPEED_OVERDRIVE
)
499 if (speed
!= SPEED_OVERDRIVE
)
500 speed
= SPEED_FLEXIBLE
;
504 err
= ds_send_control_mode(dev
, MOD_1WIRE_SPEED
, speed
);
512 static int ds_set_pullup(struct ds_device
*dev
, int delay
)
515 u8 del
= 1 + (u8
)(delay
>> 4);
516 /* Just storing delay would not get the trunication and roundup. */
519 /* Enable spu_bit if a delay is set. */
520 dev
->spu_bit
= delay
? COMM_SPU
: 0;
521 /* If delay is zero, it has already been disabled, if the time is
522 * the same as the hardware was last programmed to, there is also
523 * nothing more to do. Compare with the recalculated value ms
524 * rather than del or delay which can have a different value.
526 if (delay
== 0 || ms
== dev
->spu_sleep
)
529 err
= ds_send_control(dev
, COMM_SET_DURATION
| COMM_IM
, del
);
538 static int ds_touch_bit(struct ds_device
*dev
, u8 bit
, u8
*tbit
)
543 err
= ds_send_control(dev
, COMM_BIT_IO
| COMM_IM
| (bit
? COMM_D
: 0),
548 ds_wait_status(dev
, &st
);
550 err
= ds_recv_data(dev
, tbit
, sizeof(*tbit
));
558 static int ds_write_bit(struct ds_device
*dev
, u8 bit
)
563 /* Set COMM_ICP to write without a readback. Note, this will
564 * produce one time slot, a down followed by an up with COMM_D
565 * only determing the timing.
567 err
= ds_send_control(dev
, COMM_BIT_IO
| COMM_IM
| COMM_ICP
|
568 (bit
? COMM_D
: 0), 0);
572 ds_wait_status(dev
, &st
);
578 static int ds_write_byte(struct ds_device
*dev
, u8 byte
)
583 err
= ds_send_control(dev
, COMM_BYTE_IO
| COMM_IM
| dev
->spu_bit
, byte
);
588 msleep(dev
->spu_sleep
);
590 err
= ds_wait_status(dev
, &st
);
594 err
= ds_recv_data(dev
, &dev
->byte_buf
, 1);
598 return !(byte
== dev
->byte_buf
);
601 static int ds_read_byte(struct ds_device
*dev
, u8
*byte
)
606 err
= ds_send_control(dev
, COMM_BYTE_IO
| COMM_IM
, 0xff);
610 ds_wait_status(dev
, &st
);
612 err
= ds_recv_data(dev
, byte
, sizeof(*byte
));
619 static int read_block_chunk(struct ds_device
*dev
, u8
*buf
, int len
)
624 memset(buf
, 0xFF, len
);
626 err
= ds_send_data(dev
, buf
, len
);
630 err
= ds_send_control(dev
, COMM_BLOCK_IO
| COMM_IM
, len
);
634 ds_wait_status(dev
, &st
);
636 memset(buf
, 0x00, len
);
637 err
= ds_recv_data(dev
, buf
, len
);
642 static int ds_read_block(struct ds_device
*dev
, u8
*buf
, int len
)
644 int err
, to_read
, rem
= len
;
650 to_read
= rem
<= FIFO_SIZE
? rem
: FIFO_SIZE
;
651 err
= read_block_chunk(dev
, &buf
[len
- rem
], to_read
);
660 static int ds_write_block(struct ds_device
*dev
, u8
*buf
, int len
)
665 err
= ds_send_data(dev
, buf
, len
);
669 err
= ds_send_control(dev
, COMM_BLOCK_IO
| COMM_IM
| dev
->spu_bit
, len
);
674 msleep(dev
->spu_sleep
);
676 ds_wait_status(dev
, &st
);
678 err
= ds_recv_data(dev
, buf
, len
);
682 return !(err
== len
);
685 static void ds9490r_search(void *data
, struct w1_master
*master
,
686 u8 search_type
, w1_slave_found_callback callback
)
688 /* When starting with an existing id, the first id returned will
689 * be that device (if it is still on the bus most likely).
691 * If the number of devices found is less than or equal to the
692 * search_limit, that number of IDs will be returned. If there are
693 * more, search_limit IDs will be returned followed by a non-zero
696 struct ds_device
*dev
= data
;
704 /* DS18b20 spec, 13.16 ms per device, 75 per second, sleep for
705 * discovering 8 devices (1 bulk transfer and 1/2 FIFO size) at a time.
707 const unsigned long jtime
= msecs_to_jiffies(1000*8/75);
708 /* FIFO 128 bytes, bulk packet size 64, read a multiple of the
711 const size_t bufsize
= 2 * 64;
712 u64
*buf
, *found_ids
;
714 buf
= kmalloc(bufsize
, GFP_KERNEL
);
719 * We are holding the bus mutex during the scan, but adding devices via the
720 * callback needs the bus to be unlocked. So we queue up found ids here.
722 found_ids
= kmalloc_array(master
->max_slave_count
, sizeof(u64
), GFP_KERNEL
);
728 mutex_lock(&master
->bus_mutex
);
730 /* address to start searching at */
731 if (ds_send_data(dev
, (u8
*)&master
->search_id
, 8) < 0)
733 master
->search_id
= 0;
735 value
= COMM_SEARCH_ACCESS
| COMM_IM
| COMM_RST
| COMM_SM
| COMM_F
|
737 search_limit
= master
->max_slave_count
;
738 if (search_limit
> 255)
740 index
= search_type
| (search_limit
<< 8);
741 if (ds_send_control(dev
, value
, index
) < 0)
745 schedule_timeout(jtime
);
747 err
= ds_recv_status(dev
, &st
);
748 if (err
< 0 || err
< sizeof(st
))
751 if (st
.data_in_buffer_status
) {
753 * Bulk in can receive partial ids, but when it does
754 * they fail crc and will be discarded anyway.
755 * That has only been seen when status in buffer
756 * is 0 and bulk is read anyway, so don't read
757 * bulk without first checking if status says there
760 err
= ds_recv_data(dev
, (u8
*)buf
, bufsize
);
763 for (i
= 0; i
< err
/8; ++i
) {
764 found_ids
[found
++] = buf
[i
];
766 * can't know if there will be a discrepancy
767 * value after until the next id
769 if (found
== search_limit
) {
770 master
->search_id
= buf
[i
];
776 if (test_bit(W1_ABORT_SEARCH
, &master
->flags
))
778 } while (!(st
.status
& (ST_IDLE
| ST_HALT
)));
780 /* only continue the search if some weren't found */
781 if (found
<= search_limit
) {
782 master
->search_id
= 0;
783 } else if (!test_bit(W1_WARN_MAX_COUNT
, &master
->flags
)) {
785 * Only max_slave_count will be scanned in a search,
786 * but it will start where it left off next search
787 * until all ids are identified and then it will start
788 * over. A continued search will report the previous
789 * last id as the first id (provided it is still on the
792 dev_info(&dev
->udev
->dev
, "%s: max_slave_count %d reached, "
793 "will continue next search.\n", __func__
,
794 master
->max_slave_count
);
795 set_bit(W1_WARN_MAX_COUNT
, &master
->flags
);
799 mutex_unlock(&master
->bus_mutex
);
802 for (i
= 0; i
< found
; i
++) /* run callback for all queued up IDs */
803 callback(master
, found_ids
[i
]);
809 * FIXME: if this disabled code is ever used in the future all ds_send_data()
810 * calls must be changed to use a DMAable buffer.
812 static int ds_match_access(struct ds_device
*dev
, u64 init
)
817 err
= ds_send_data(dev
, (unsigned char *)&init
, sizeof(init
));
821 ds_wait_status(dev
, &st
);
823 err
= ds_send_control(dev
, COMM_MATCH_ACCESS
| COMM_IM
| COMM_RST
, 0x0055);
827 ds_wait_status(dev
, &st
);
832 static int ds_set_path(struct ds_device
*dev
, u64 init
)
838 memcpy(buf
, &init
, 8);
839 buf
[8] = BRANCH_MAIN
;
841 err
= ds_send_data(dev
, buf
, sizeof(buf
));
845 ds_wait_status(dev
, &st
);
847 err
= ds_send_control(dev
, COMM_SET_PATH
| COMM_IM
| COMM_RST
, 0);
851 ds_wait_status(dev
, &st
);
858 static u8
ds9490r_touch_bit(void *data
, u8 bit
)
860 struct ds_device
*dev
= data
;
862 if (ds_touch_bit(dev
, bit
, &dev
->byte_buf
))
865 return dev
->byte_buf
;
869 static void ds9490r_write_bit(void *data
, u8 bit
)
871 struct ds_device
*dev
= data
;
873 ds_write_bit(dev
, bit
);
876 static u8
ds9490r_read_bit(void *data
)
878 struct ds_device
*dev
= data
;
881 err
= ds_touch_bit(dev
, 1, &dev
->byte_buf
);
885 return dev
->byte_buf
& 1;
889 static void ds9490r_write_byte(void *data
, u8 byte
)
891 struct ds_device
*dev
= data
;
893 ds_write_byte(dev
, byte
);
896 static u8
ds9490r_read_byte(void *data
)
898 struct ds_device
*dev
= data
;
901 err
= ds_read_byte(dev
, &dev
->byte_buf
);
905 return dev
->byte_buf
;
908 static void ds9490r_write_block(void *data
, const u8
*buf
, int len
)
910 struct ds_device
*dev
= data
;
916 tbuf
= kmemdup(buf
, len
, GFP_KERNEL
);
920 ds_write_block(dev
, tbuf
, len
);
925 static u8
ds9490r_read_block(void *data
, u8
*buf
, int len
)
927 struct ds_device
*dev
= data
;
934 tbuf
= kmalloc(len
, GFP_KERNEL
);
938 err
= ds_read_block(dev
, tbuf
, len
);
940 memcpy(buf
, tbuf
, len
);
944 return err
>= 0 ? len
: 0;
947 static u8
ds9490r_reset(void *data
)
949 struct ds_device
*dev
= data
;
959 static u8
ds9490r_set_pullup(void *data
, int delay
)
961 struct ds_device
*dev
= data
;
963 if (ds_set_pullup(dev
, delay
))
969 static int ds_w1_init(struct ds_device
*dev
)
971 memset(&dev
->master
, 0, sizeof(struct w1_bus_master
));
973 /* Reset the device as it can be in a bad state.
974 * This is necessary because a block write will wait for data
975 * to be placed in the output buffer and block any later
976 * commands which will keep accumulating and the device will
977 * not be idle. Another case is removing the ds2490 module
978 * while a bus search is in progress, somehow a few commands
979 * get through, but the input transfers fail leaving data in
980 * the input buffer. This will cause the next read to fail
981 * see the note in ds_recv_data.
983 ds_reset_device(dev
);
985 dev
->master
.data
= dev
;
986 dev
->master
.touch_bit
= &ds9490r_touch_bit
;
987 /* read_bit and write_bit in w1_bus_master are expected to set and
988 * sample the line level. For write_bit that means it is expected to
989 * set it to that value and leave it there. ds2490 only supports an
990 * individual time slot at the lowest level. The requirement from
991 * pulling the bus state down to reading the state is 15us, something
992 * that isn't realistic on the USB bus anyway.
993 dev->master.read_bit = &ds9490r_read_bit;
994 dev->master.write_bit = &ds9490r_write_bit;
996 dev
->master
.read_byte
= &ds9490r_read_byte
;
997 dev
->master
.write_byte
= &ds9490r_write_byte
;
998 dev
->master
.read_block
= &ds9490r_read_block
;
999 dev
->master
.write_block
= &ds9490r_write_block
;
1000 dev
->master
.reset_bus
= &ds9490r_reset
;
1001 dev
->master
.set_pullup
= &ds9490r_set_pullup
;
1002 dev
->master
.search
= &ds9490r_search
;
1004 return w1_add_master_device(&dev
->master
);
1007 static void ds_w1_fini(struct ds_device
*dev
)
1009 w1_remove_master_device(&dev
->master
);
1012 static int ds_probe(struct usb_interface
*intf
,
1013 const struct usb_device_id
*udev_id
)
1015 struct usb_device
*udev
= interface_to_usbdev(intf
);
1016 struct usb_endpoint_descriptor
*endpoint
;
1017 struct usb_host_interface
*iface_desc
;
1018 struct ds_device
*dev
;
1021 dev
= kzalloc(sizeof(struct ds_device
), GFP_KERNEL
);
1025 dev
->udev
= usb_get_dev(udev
);
1030 memset(dev
->ep
, 0, sizeof(dev
->ep
));
1032 usb_set_intfdata(intf
, dev
);
1034 err
= usb_reset_configuration(dev
->udev
);
1036 dev_err(&dev
->udev
->dev
,
1037 "Failed to reset configuration: err=%d.\n", err
);
1041 /* alternative 3, 1ms interrupt (greatly speeds search), 64 byte bulk */
1043 err
= usb_set_interface(dev
->udev
,
1044 intf
->cur_altsetting
->desc
.bInterfaceNumber
, alt
);
1046 dev_err(&dev
->udev
->dev
, "Failed to set alternative setting %d "
1047 "for %d interface: err=%d.\n", alt
,
1048 intf
->cur_altsetting
->desc
.bInterfaceNumber
, err
);
1052 iface_desc
= intf
->cur_altsetting
;
1053 if (iface_desc
->desc
.bNumEndpoints
!= NUM_EP
-1) {
1054 dev_err(&dev
->udev
->dev
, "Num endpoints=%d. It is not DS9490R.\n",
1055 iface_desc
->desc
.bNumEndpoints
);
1061 * This loop doesn'd show control 0 endpoint,
1062 * so we will fill only 1-3 endpoints entry.
1064 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1065 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1067 dev
->ep
[i
+1] = endpoint
->bEndpointAddress
;
1069 printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
1070 i
, endpoint
->bEndpointAddress
, le16_to_cpu(endpoint
->wMaxPacketSize
),
1071 (endpoint
->bEndpointAddress
& USB_DIR_IN
)?"IN":"OUT",
1072 endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
1076 err
= ds_w1_init(dev
);
1080 mutex_lock(&ds_mutex
);
1081 list_add_tail(&dev
->ds_entry
, &ds_devices
);
1082 mutex_unlock(&ds_mutex
);
1087 usb_set_intfdata(intf
, NULL
);
1088 usb_put_dev(dev
->udev
);
1094 static void ds_disconnect(struct usb_interface
*intf
)
1096 struct ds_device
*dev
;
1098 dev
= usb_get_intfdata(intf
);
1102 mutex_lock(&ds_mutex
);
1103 list_del(&dev
->ds_entry
);
1104 mutex_unlock(&ds_mutex
);
1108 usb_set_intfdata(intf
, NULL
);
1110 usb_put_dev(dev
->udev
);
1114 static const struct usb_device_id ds_id_table
[] = {
1115 { USB_DEVICE(0x04fa, 0x2490) },
1118 MODULE_DEVICE_TABLE(usb
, ds_id_table
);
1120 static struct usb_driver ds_driver
= {
1123 .disconnect
= ds_disconnect
,
1124 .id_table
= ds_id_table
,
1126 module_usb_driver(ds_driver
);
1128 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
1129 MODULE_DESCRIPTION("DS2490 USB <-> W1 bus master driver (DS9490*)");
1130 MODULE_LICENSE("GPL");