2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2014-01-16 12:29:21 +08:00
|
|
|
* ds2490.c USB to one wire bridge
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2011-08-26 06:59:06 +08:00
|
|
|
* Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mod_devicetable.h>
|
|
|
|
#include <linux/usb.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-06-05 21:52:08 +08:00
|
|
|
#include <linux/w1.h>
|
2006-03-24 00:11:58 +08:00
|
|
|
|
2014-01-16 12:29:21 +08:00
|
|
|
/* USB Standard */
|
|
|
|
/* USB Control request vendor type */
|
|
|
|
#define VENDOR 0x40
|
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
/* COMMAND TYPE CODES */
|
|
|
|
#define CONTROL_CMD 0x00
|
|
|
|
#define COMM_CMD 0x01
|
|
|
|
#define MODE_CMD 0x02
|
|
|
|
|
|
|
|
/* CONTROL COMMAND CODES */
|
|
|
|
#define CTL_RESET_DEVICE 0x0000
|
|
|
|
#define CTL_START_EXE 0x0001
|
|
|
|
#define CTL_RESUME_EXE 0x0002
|
|
|
|
#define CTL_HALT_EXE_IDLE 0x0003
|
|
|
|
#define CTL_HALT_EXE_DONE 0x0004
|
|
|
|
#define CTL_FLUSH_COMM_CMDS 0x0007
|
|
|
|
#define CTL_FLUSH_RCV_BUFFER 0x0008
|
|
|
|
#define CTL_FLUSH_XMT_BUFFER 0x0009
|
|
|
|
#define CTL_GET_COMM_CMDS 0x000A
|
|
|
|
|
|
|
|
/* MODE COMMAND CODES */
|
|
|
|
#define MOD_PULSE_EN 0x0000
|
|
|
|
#define MOD_SPEED_CHANGE_EN 0x0001
|
|
|
|
#define MOD_1WIRE_SPEED 0x0002
|
|
|
|
#define MOD_STRONG_PU_DURATION 0x0003
|
|
|
|
#define MOD_PULLDOWN_SLEWRATE 0x0004
|
|
|
|
#define MOD_PROG_PULSE_DURATION 0x0005
|
|
|
|
#define MOD_WRITE1_LOWTIME 0x0006
|
|
|
|
#define MOD_DSOW0_TREC 0x0007
|
|
|
|
|
|
|
|
/* COMMUNICATION COMMAND CODES */
|
|
|
|
#define COMM_ERROR_ESCAPE 0x0601
|
|
|
|
#define COMM_SET_DURATION 0x0012
|
|
|
|
#define COMM_BIT_IO 0x0020
|
|
|
|
#define COMM_PULSE 0x0030
|
|
|
|
#define COMM_1_WIRE_RESET 0x0042
|
|
|
|
#define COMM_BYTE_IO 0x0052
|
|
|
|
#define COMM_MATCH_ACCESS 0x0064
|
|
|
|
#define COMM_BLOCK_IO 0x0074
|
|
|
|
#define COMM_READ_STRAIGHT 0x0080
|
|
|
|
#define COMM_DO_RELEASE 0x6092
|
|
|
|
#define COMM_SET_PATH 0x00A2
|
|
|
|
#define COMM_WRITE_SRAM_PAGE 0x00B2
|
|
|
|
#define COMM_WRITE_EPROM 0x00C4
|
|
|
|
#define COMM_READ_CRC_PROT_PAGE 0x00D4
|
|
|
|
#define COMM_READ_REDIRECT_PAGE_CRC 0x21E4
|
|
|
|
#define COMM_SEARCH_ACCESS 0x00F4
|
|
|
|
|
|
|
|
/* Communication command bits */
|
|
|
|
#define COMM_TYPE 0x0008
|
|
|
|
#define COMM_SE 0x0008
|
|
|
|
#define COMM_D 0x0008
|
|
|
|
#define COMM_Z 0x0008
|
|
|
|
#define COMM_CH 0x0008
|
|
|
|
#define COMM_SM 0x0008
|
|
|
|
#define COMM_R 0x0008
|
|
|
|
#define COMM_IM 0x0001
|
|
|
|
|
|
|
|
#define COMM_PS 0x4000
|
|
|
|
#define COMM_PST 0x4000
|
|
|
|
#define COMM_CIB 0x4000
|
|
|
|
#define COMM_RTS 0x4000
|
|
|
|
#define COMM_DT 0x2000
|
|
|
|
#define COMM_SPU 0x1000
|
|
|
|
#define COMM_F 0x0800
|
2008-10-16 13:05:08 +08:00
|
|
|
#define COMM_NTF 0x0400
|
2006-03-24 00:11:58 +08:00
|
|
|
#define COMM_ICP 0x0200
|
|
|
|
#define COMM_RST 0x0100
|
|
|
|
|
|
|
|
#define PULSE_PROG 0x01
|
|
|
|
#define PULSE_SPUE 0x02
|
|
|
|
|
|
|
|
#define BRANCH_MAIN 0xCC
|
|
|
|
#define BRANCH_AUX 0x33
|
|
|
|
|
|
|
|
/* Status flags */
|
|
|
|
#define ST_SPUA 0x01 /* Strong Pull-up is active */
|
|
|
|
#define ST_PRGA 0x02 /* 12V programming pulse is being generated */
|
|
|
|
#define ST_12VP 0x04 /* external 12V programming voltage is present */
|
|
|
|
#define ST_PMOD 0x08 /* DS2490 powered from USB and external sources */
|
|
|
|
#define ST_HALT 0x10 /* DS2490 is currently halted */
|
|
|
|
#define ST_IDLE 0x20 /* DS2490 is currently idle */
|
|
|
|
#define ST_EPOF 0x80
|
2014-01-16 12:29:21 +08:00
|
|
|
/* Status transfer size, 16 bytes status, 16 byte result flags */
|
|
|
|
#define ST_SIZE 0x20
|
2006-03-24 00:11:58 +08:00
|
|
|
|
2008-10-16 13:05:06 +08:00
|
|
|
/* Result Register flags */
|
|
|
|
#define RR_DETECT 0xA5 /* New device detected */
|
|
|
|
#define RR_NRS 0x01 /* Reset no presence or ... */
|
|
|
|
#define RR_SH 0x02 /* short on reset or set path */
|
|
|
|
#define RR_APP 0x04 /* alarming presence on reset */
|
|
|
|
#define RR_VPP 0x08 /* 12V expected not seen */
|
|
|
|
#define RR_CMP 0x10 /* compare error */
|
|
|
|
#define RR_CRC 0x20 /* CRC error detected */
|
|
|
|
#define RR_RDP 0x40 /* redirected page */
|
|
|
|
#define RR_EOS 0x80 /* end of search error */
|
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
#define SPEED_NORMAL 0x00
|
|
|
|
#define SPEED_FLEXIBLE 0x01
|
|
|
|
#define SPEED_OVERDRIVE 0x02
|
|
|
|
|
|
|
|
#define NUM_EP 4
|
|
|
|
#define EP_CONTROL 0
|
|
|
|
#define EP_STATUS 1
|
|
|
|
#define EP_DATA_OUT 2
|
|
|
|
#define EP_DATA_IN 3
|
|
|
|
|
2018-06-14 23:20:41 +08:00
|
|
|
struct ds_device {
|
2006-03-24 00:11:58 +08:00
|
|
|
struct list_head ds_entry;
|
|
|
|
|
|
|
|
struct usb_device *udev;
|
|
|
|
struct usb_interface *intf;
|
|
|
|
|
|
|
|
int ep[NUM_EP];
|
|
|
|
|
2008-10-16 13:05:03 +08:00
|
|
|
/* Strong PullUp
|
|
|
|
* 0: pullup not active, else duration in milliseconds
|
|
|
|
*/
|
|
|
|
int spu_sleep;
|
2008-10-16 13:05:10 +08:00
|
|
|
/* spu_bit contains COMM_SPU or 0 depending on if the strong pullup
|
|
|
|
* should be active or not for writes.
|
|
|
|
*/
|
|
|
|
u16 spu_bit;
|
2008-10-16 13:05:03 +08:00
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
u8 st_buf[ST_SIZE];
|
|
|
|
u8 byte_buf;
|
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
struct w1_bus_master master;
|
|
|
|
};
|
|
|
|
|
2018-06-14 23:20:41 +08:00
|
|
|
struct ds_status {
|
2006-03-24 00:11:58 +08:00
|
|
|
u8 enable;
|
|
|
|
u8 speed;
|
|
|
|
u8 pullup_dur;
|
|
|
|
u8 ppuls_dur;
|
|
|
|
u8 pulldown_slew;
|
|
|
|
u8 write1_time;
|
|
|
|
u8 write0_time;
|
|
|
|
u8 reserved0;
|
|
|
|
u8 status;
|
|
|
|
u8 command0;
|
|
|
|
u8 command1;
|
|
|
|
u8 command_buffer_status;
|
|
|
|
u8 data_out_buffer_status;
|
|
|
|
u8 data_in_buffer_status;
|
|
|
|
u8 reserved1;
|
|
|
|
u8 reserved2;
|
|
|
|
};
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
static LIST_HEAD(ds_devices);
|
2006-04-03 16:04:27 +08:00
|
|
|
static DEFINE_MUTEX(ds_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index)
|
|
|
|
{
|
|
|
|
int err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
|
|
|
err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
|
2014-01-16 12:29:21 +08:00
|
|
|
CONTROL_CMD, VENDOR, value, index, NULL, 0, 1000);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err < 0) {
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_err(&dev->udev->dev,
|
|
|
|
"Failed to send command control message %x.%x: err=%d.\n",
|
|
|
|
value, index, err);
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2008-10-16 13:05:03 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
|
|
|
|
{
|
|
|
|
int err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
|
|
|
err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
|
2014-01-16 12:29:21 +08:00
|
|
|
MODE_CMD, VENDOR, value, index, NULL, 0, 1000);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err < 0) {
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_err(&dev->udev->dev,
|
|
|
|
"Failed to send mode control message %x.%x: err=%d.\n",
|
|
|
|
value, index, err);
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2008-10-16 13:05:03 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int ds_send_control(struct ds_device *dev, u16 value, u16 index)
|
|
|
|
{
|
|
|
|
int err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
|
|
|
err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
|
2014-01-16 12:29:21 +08:00
|
|
|
COMM_CMD, VENDOR, value, index, NULL, 0, 1000);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err < 0) {
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_err(&dev->udev->dev,
|
|
|
|
"Failed to send control message %x.%x: err=%d.\n",
|
|
|
|
value, index, err);
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-03-25 03:32:46 +08:00
|
|
|
static void ds_dump_status(struct ds_device *ds_dev, unsigned char *buf, int count)
|
2008-10-16 13:05:06 +08:00
|
|
|
{
|
2022-03-25 03:32:46 +08:00
|
|
|
struct device *dev = &ds_dev->udev->dev;
|
2008-10-16 13:05:06 +08:00
|
|
|
int i;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_info(dev, "ep_status=0x%x, count=%d, status=%*phC",
|
|
|
|
ds_dev->ep[EP_STATUS], count, count, buf);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (count >= 16) {
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_dbg(dev, "enable flag: 0x%02x", buf[0]);
|
|
|
|
dev_dbg(dev, "1-wire speed: 0x%02x", buf[1]);
|
|
|
|
dev_dbg(dev, "strong pullup duration: 0x%02x", buf[2]);
|
|
|
|
dev_dbg(dev, "programming pulse duration: 0x%02x", buf[3]);
|
|
|
|
dev_dbg(dev, "pulldown slew rate control: 0x%02x", buf[4]);
|
|
|
|
dev_dbg(dev, "write-1 low time: 0x%02x", buf[5]);
|
|
|
|
dev_dbg(dev, "data sample offset/write-0 recovery time: 0x%02x", buf[6]);
|
|
|
|
dev_dbg(dev, "reserved (test register): 0x%02x", buf[7]);
|
|
|
|
dev_dbg(dev, "device status flags: 0x%02x", buf[8]);
|
|
|
|
dev_dbg(dev, "communication command byte 1: 0x%02x", buf[9]);
|
|
|
|
dev_dbg(dev, "communication command byte 2: 0x%02x", buf[10]);
|
|
|
|
dev_dbg(dev, "communication command buffer status: 0x%02x", buf[11]);
|
|
|
|
dev_dbg(dev, "1-wire data output buffer status: 0x%02x", buf[12]);
|
|
|
|
dev_dbg(dev, "1-wire data input buffer status: 0x%02x", buf[13]);
|
|
|
|
dev_dbg(dev, "reserved: 0x%02x", buf[14]);
|
|
|
|
dev_dbg(dev, "reserved: 0x%02x", buf[15]);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2022-03-25 03:32:46 +08:00
|
|
|
|
2008-10-16 13:05:06 +08:00
|
|
|
for (i = 16; i < count; ++i) {
|
|
|
|
if (buf[i] == RR_DETECT) {
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_dbg(dev, "New device detect.\n");
|
2008-10-16 13:05:06 +08:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_dbg(dev, "Result Register Value: 0x%02x", buf[i]);
|
2008-10-16 13:05:06 +08:00
|
|
|
if (buf[i] & RR_NRS)
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_dbg(dev, "NRS: Reset no presence or ...\n");
|
2008-10-16 13:05:06 +08:00
|
|
|
if (buf[i] & RR_SH)
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_dbg(dev, "SH: short on reset or set path\n");
|
2008-10-16 13:05:06 +08:00
|
|
|
if (buf[i] & RR_APP)
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_dbg(dev, "APP: alarming presence on reset\n");
|
2008-10-16 13:05:06 +08:00
|
|
|
if (buf[i] & RR_VPP)
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_dbg(dev, "VPP: 12V expected not seen\n");
|
2008-10-16 13:05:06 +08:00
|
|
|
if (buf[i] & RR_CMP)
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_dbg(dev, "CMP: compare error\n");
|
2008-10-16 13:05:06 +08:00
|
|
|
if (buf[i] & RR_CRC)
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_dbg(dev, "CRC: CRC error detected\n");
|
2008-10-16 13:05:06 +08:00
|
|
|
if (buf[i] & RR_RDP)
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_dbg(dev, "RDP: redirected page\n");
|
2008-10-16 13:05:06 +08:00
|
|
|
if (buf[i] & RR_EOS)
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_dbg(dev, "EOS: end of search error\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 03:32:46 +08:00
|
|
|
static int ds_recv_status(struct ds_device *dev, struct ds_status *st)
|
2017-01-19 04:31:11 +08:00
|
|
|
{
|
|
|
|
int count, err;
|
|
|
|
|
|
|
|
if (st)
|
|
|
|
memset(st, 0, sizeof(*st));
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
err = usb_interrupt_msg(dev->udev,
|
|
|
|
usb_rcvintpipe(dev->udev,
|
|
|
|
dev->ep[EP_STATUS]),
|
|
|
|
dev->st_buf, sizeof(dev->st_buf),
|
|
|
|
&count, 1000);
|
|
|
|
if (err < 0) {
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_err(&dev->udev->dev,
|
|
|
|
"Failed to read 1-wire data from 0x%x: err=%d.\n",
|
|
|
|
dev->ep[EP_STATUS], err);
|
2017-01-19 04:31:11 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st && count >= sizeof(*st))
|
|
|
|
memcpy(st, dev->st_buf, sizeof(*st));
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-10-16 13:05:10 +08:00
|
|
|
static void ds_reset_device(struct ds_device *dev)
|
|
|
|
{
|
|
|
|
ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
|
|
|
|
/* Always allow strong pullup which allow individual writes to use
|
|
|
|
* the strong pullup.
|
|
|
|
*/
|
|
|
|
if (ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE))
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_err(&dev->udev->dev,
|
|
|
|
"%s: Error allowing strong pullup\n", __func__);
|
2008-10-16 13:05:10 +08:00
|
|
|
/* Chip strong pullup time was cleared. */
|
|
|
|
if (dev->spu_sleep) {
|
|
|
|
/* lower 4 bits are 0, see ds_set_pullup */
|
|
|
|
u8 del = dev->spu_sleep>>4;
|
|
|
|
if (ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del))
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_err(&dev->udev->dev,
|
|
|
|
"%s: Error setting duration\n", __func__);
|
2008-10-16 13:05:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int ds_recv_data(struct ds_device *dev, unsigned char *buf, int size)
|
|
|
|
{
|
|
|
|
int count, err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2008-10-16 13:05:08 +08:00
|
|
|
/* Careful on size. If size is less than what is available in
|
|
|
|
* the input buffer, the device fails the bulk transfer and
|
|
|
|
* clears the input buffer. It could read the maximum size of
|
|
|
|
* the data buffer, but then do you return the first, last, or
|
|
|
|
* some set of the middle size bytes? As long as the rest of
|
|
|
|
* the code is correct there will be size bytes waiting. A
|
|
|
|
* call to ds_wait_status will wait until the device is idle
|
|
|
|
* and any data to be received would have been available.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
count = 0;
|
2005-08-03 19:14:50 +08:00
|
|
|
err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]),
|
2005-04-17 06:20:36 +08:00
|
|
|
buf, size, &count, 1000);
|
|
|
|
if (err < 0) {
|
2022-03-25 03:32:46 +08:00
|
|
|
int recv_len;
|
|
|
|
|
2022-03-12 03:28:32 +08:00
|
|
|
dev_info(&dev->udev->dev, "Clearing ep0x%x.\n", dev->ep[EP_DATA_IN]);
|
2005-04-17 06:20:36 +08:00
|
|
|
usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]));
|
2022-03-25 03:32:46 +08:00
|
|
|
|
|
|
|
/* status might tell us why endpoint is stuck? */
|
|
|
|
recv_len = ds_recv_status(dev, NULL);
|
|
|
|
if (recv_len >= 0)
|
|
|
|
ds_dump_status(dev, dev->st_buf, recv_len);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printk("%s: count=%d: ", __func__, count);
|
2018-06-14 23:20:39 +08:00
|
|
|
for (i = 0; i < count; ++i)
|
2005-04-17 06:20:36 +08:00
|
|
|
printk("%02x ", buf[i]);
|
|
|
|
printk("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ds_send_data(struct ds_device *dev, unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
int count, err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
count = 0;
|
|
|
|
err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000);
|
|
|
|
if (err < 0) {
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_err(&dev->udev->dev, "Failed to write 1-wire data to ep0x%x: "
|
2008-10-16 13:05:02 +08:00
|
|
|
"err=%d.\n", dev->ep[EP_DATA_OUT], err);
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2005-08-03 19:14:50 +08:00
|
|
|
#if 0
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
int ds_stop_pulse(struct ds_device *dev, int limit)
|
|
|
|
{
|
|
|
|
struct ds_status st;
|
|
|
|
int count = 0, err = 0;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
do {
|
|
|
|
err = ds_send_control(dev, CTL_HALT_EXE_IDLE, 0);
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
err = ds_send_control(dev, CTL_RESUME_EXE, 0);
|
|
|
|
if (err)
|
|
|
|
break;
|
2022-03-25 03:32:46 +08:00
|
|
|
err = ds_recv_status(dev, &st);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((st.status & ST_SPUA) == 0) {
|
|
|
|
err = ds_send_control_mode(dev, MOD_PULSE_EN, 0);
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
}
|
2018-06-14 23:20:39 +08:00
|
|
|
} while (++count < limit);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ds_detect(struct ds_device *dev, struct ds_status *st)
|
|
|
|
{
|
|
|
|
int err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, 0);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2008-10-16 13:05:06 +08:00
|
|
|
err = ds_dump_status(dev, st);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2005-08-03 19:14:50 +08:00
|
|
|
#endif /* 0 */
|
|
|
|
|
|
|
|
static int ds_wait_status(struct ds_device *dev, struct ds_status *st)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err, count = 0;
|
|
|
|
|
|
|
|
do {
|
2014-01-16 12:29:21 +08:00
|
|
|
st->status = 0;
|
2022-03-25 03:32:46 +08:00
|
|
|
err = ds_recv_status(dev, st);
|
2005-04-17 06:20:36 +08:00
|
|
|
#if 0
|
2005-08-03 19:14:50 +08:00
|
|
|
if (err >= 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
int i;
|
|
|
|
printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], err);
|
2018-06-14 23:20:39 +08:00
|
|
|
for (i = 0; i < err; ++i)
|
2017-01-19 04:31:11 +08:00
|
|
|
printk("%02x ", dev->st_buf[i]);
|
2005-04-17 06:20:36 +08:00
|
|
|
printk("\n");
|
|
|
|
}
|
|
|
|
#endif
|
2014-01-16 12:29:21 +08:00
|
|
|
} while (!(st->status & ST_IDLE) && !(err < 0) && ++count < 100);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-10-16 13:05:06 +08:00
|
|
|
if (err >= 16 && st->status & ST_EPOF) {
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_info(&dev->udev->dev, "Resetting device after ST_EPOF.\n");
|
2008-10-16 13:05:10 +08:00
|
|
|
ds_reset_device(dev);
|
2008-10-16 13:05:06 +08:00
|
|
|
/* Always dump the device status. */
|
|
|
|
count = 101;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-10-16 13:05:06 +08:00
|
|
|
/* Dump the status for errors or if there is extended return data.
|
|
|
|
* The extended status includes new device detection (maybe someone
|
|
|
|
* can do something with it).
|
|
|
|
*/
|
|
|
|
if (err > 16 || count >= 100 || err < 0)
|
2017-01-19 04:31:11 +08:00
|
|
|
ds_dump_status(dev, dev->st_buf, err);
|
2008-10-16 13:05:06 +08:00
|
|
|
|
|
|
|
/* Extended data isn't an error. Well, a short is, but the dump
|
|
|
|
* would have already told the user that and we can't do anything
|
|
|
|
* about it in software anyway.
|
|
|
|
*/
|
|
|
|
if (count >= 100 || err < 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -1;
|
2008-10-16 13:05:06 +08:00
|
|
|
else
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-16 13:05:07 +08:00
|
|
|
static int ds_reset(struct ds_device *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2008-10-16 13:05:08 +08:00
|
|
|
/* Other potentionally interesting flags for reset.
|
|
|
|
*
|
|
|
|
* COMM_NTF: Return result register feedback. This could be used to
|
|
|
|
* detect some conditions such as short, alarming presence, or
|
|
|
|
* detect if a new device was detected.
|
|
|
|
*
|
|
|
|
* COMM_SE which allows SPEED_NORMAL, SPEED_FLEXIBLE, SPEED_OVERDRIVE:
|
|
|
|
* Select the data transfer rate.
|
|
|
|
*/
|
|
|
|
err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_IM, SPEED_NORMAL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-03 19:14:50 +08:00
|
|
|
#if 0
|
2006-03-24 00:11:58 +08:00
|
|
|
static int ds_set_speed(struct ds_device *dev, int speed)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err;
|
2005-12-06 18:38:28 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (speed != SPEED_NORMAL && speed != SPEED_FLEXIBLE && speed != SPEED_OVERDRIVE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (speed != SPEED_OVERDRIVE)
|
|
|
|
speed = SPEED_FLEXIBLE;
|
|
|
|
|
|
|
|
speed &= 0xff;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
err = ds_send_control_mode(dev, MOD_1WIRE_SPEED, speed);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2005-08-03 19:14:50 +08:00
|
|
|
#endif /* 0 */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-10-16 13:05:03 +08:00
|
|
|
static int ds_set_pullup(struct ds_device *dev, int delay)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-10-16 13:05:10 +08:00
|
|
|
int err = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
u8 del = 1 + (u8)(delay >> 4);
|
2008-10-16 13:05:10 +08:00
|
|
|
/* Just storing delay would not get the trunication and roundup. */
|
|
|
|
int ms = del<<4;
|
|
|
|
|
|
|
|
/* Enable spu_bit if a delay is set. */
|
|
|
|
dev->spu_bit = delay ? COMM_SPU : 0;
|
|
|
|
/* If delay is zero, it has already been disabled, if the time is
|
|
|
|
* the same as the hardware was last programmed to, there is also
|
|
|
|
* nothing more to do. Compare with the recalculated value ms
|
|
|
|
* rather than del or delay which can have a different value.
|
|
|
|
*/
|
|
|
|
if (delay == 0 || ms == dev->spu_sleep)
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-10-16 13:05:10 +08:00
|
|
|
err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2008-10-16 13:05:10 +08:00
|
|
|
dev->spu_sleep = ms;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
static int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-10-16 13:05:05 +08:00
|
|
|
int err;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct ds_status st;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2008-10-16 13:05:05 +08:00
|
|
|
err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit ? COMM_D : 0),
|
|
|
|
0);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2008-10-16 13:05:05 +08:00
|
|
|
ds_wait_status(dev, &st);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
err = ds_recv_data(dev, tbit, sizeof(*tbit));
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-16 13:05:04 +08:00
|
|
|
#if 0
|
2006-03-24 00:11:58 +08:00
|
|
|
static int ds_write_bit(struct ds_device *dev, u8 bit)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ds_status st;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2008-10-16 13:05:04 +08:00
|
|
|
/* Set COMM_ICP to write without a readback. Note, this will
|
|
|
|
* produce one time slot, a down followed by an up with COMM_D
|
|
|
|
* only determing the timing.
|
|
|
|
*/
|
|
|
|
err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_ICP |
|
|
|
|
(bit ? COMM_D : 0), 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
ds_wait_status(dev, &st);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-10-16 13:05:04 +08:00
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
static int ds_write_byte(struct ds_device *dev, u8 byte)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ds_status st;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2008-10-16 13:05:10 +08:00
|
|
|
err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM | dev->spu_bit, byte);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2008-10-16 13:05:10 +08:00
|
|
|
if (dev->spu_bit)
|
2008-10-16 13:05:03 +08:00
|
|
|
msleep(dev->spu_sleep);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
err = ds_wait_status(dev, &st);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
err = ds_recv_data(dev, &dev->byte_buf, 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
return !(byte == dev->byte_buf);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
static int ds_read_byte(struct ds_device *dev, u8 *byte)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ds_status st;
|
|
|
|
|
2018-06-14 23:20:40 +08:00
|
|
|
err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM, 0xff);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
ds_wait_status(dev, &st);
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
err = ds_recv_data(dev, byte, sizeof(*byte));
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
static int ds_read_block(struct ds_device *dev, u8 *buf, int len)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct ds_status st;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (len > 64*1024)
|
|
|
|
return -E2BIG;
|
|
|
|
|
|
|
|
memset(buf, 0xFF, len);
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
err = ds_send_data(dev, buf, len);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2008-10-16 13:05:03 +08:00
|
|
|
err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM, len);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
ds_wait_status(dev, &st);
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
memset(buf, 0x00, len);
|
|
|
|
err = ds_recv_data(dev, buf, len);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
static int ds_write_block(struct ds_device *dev, u8 *buf, int len)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ds_status st;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
err = ds_send_data(dev, buf, len);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2008-10-16 13:05:10 +08:00
|
|
|
err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | dev->spu_bit, len);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2008-10-16 13:05:10 +08:00
|
|
|
if (dev->spu_bit)
|
2008-10-16 13:05:03 +08:00
|
|
|
msleep(dev->spu_sleep);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
ds_wait_status(dev, &st);
|
|
|
|
|
|
|
|
err = ds_recv_data(dev, buf, len);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return !(err == len);
|
|
|
|
}
|
|
|
|
|
2014-01-16 12:29:23 +08:00
|
|
|
static void ds9490r_search(void *data, struct w1_master *master,
|
|
|
|
u8 search_type, w1_slave_found_callback callback)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2014-01-16 12:29:23 +08:00
|
|
|
/* When starting with an existing id, the first id returned will
|
|
|
|
* be that device (if it is still on the bus most likely).
|
|
|
|
*
|
|
|
|
* If the number of devices found is less than or equal to the
|
|
|
|
* search_limit, that number of IDs will be returned. If there are
|
|
|
|
* more, search_limit IDs will be returned followed by a non-zero
|
|
|
|
* discrepency value.
|
|
|
|
*/
|
|
|
|
struct ds_device *dev = data;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err;
|
|
|
|
u16 value, index;
|
|
|
|
struct ds_status st;
|
2014-01-16 12:29:23 +08:00
|
|
|
int search_limit;
|
|
|
|
int found = 0;
|
|
|
|
int i;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-01-16 12:29:23 +08:00
|
|
|
/* DS18b20 spec, 13.16 ms per device, 75 per second, sleep for
|
|
|
|
* discovering 8 devices (1 bulk transfer and 1/2 FIFO size) at a time.
|
|
|
|
*/
|
|
|
|
const unsigned long jtime = msecs_to_jiffies(1000*8/75);
|
|
|
|
/* FIFO 128 bytes, bulk packet size 64, read a multiple of the
|
|
|
|
* packet size.
|
|
|
|
*/
|
2017-01-19 04:31:11 +08:00
|
|
|
const size_t bufsize = 2 * 64;
|
2021-01-14 03:50:18 +08:00
|
|
|
u64 *buf, *found_ids;
|
2017-01-19 04:31:11 +08:00
|
|
|
|
|
|
|
buf = kmalloc(bufsize, GFP_KERNEL);
|
|
|
|
if (!buf)
|
|
|
|
return;
|
2014-01-16 12:29:23 +08:00
|
|
|
|
2021-01-14 03:50:18 +08:00
|
|
|
/*
|
|
|
|
* We are holding the bus mutex during the scan, but adding devices via the
|
|
|
|
* callback needs the bus to be unlocked. So we queue up found ids here.
|
|
|
|
*/
|
|
|
|
found_ids = kmalloc_array(master->max_slave_count, sizeof(u64), GFP_KERNEL);
|
|
|
|
if (!found_ids) {
|
|
|
|
kfree(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-16 12:29:26 +08:00
|
|
|
mutex_lock(&master->bus_mutex);
|
|
|
|
|
2014-01-16 12:29:23 +08:00
|
|
|
/* address to start searching at */
|
|
|
|
if (ds_send_data(dev, (u8 *)&master->search_id, 8) < 0)
|
2014-01-16 12:29:26 +08:00
|
|
|
goto search_out;
|
2014-01-16 12:29:23 +08:00
|
|
|
master->search_id = 0;
|
|
|
|
|
|
|
|
value = COMM_SEARCH_ACCESS | COMM_IM | COMM_RST | COMM_SM | COMM_F |
|
|
|
|
COMM_RTS;
|
|
|
|
search_limit = master->max_slave_count;
|
|
|
|
if (search_limit > 255)
|
|
|
|
search_limit = 0;
|
|
|
|
index = search_type | (search_limit << 8);
|
|
|
|
if (ds_send_control(dev, value, index) < 0)
|
2014-01-16 12:29:26 +08:00
|
|
|
goto search_out;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-01-16 12:29:23 +08:00
|
|
|
do {
|
|
|
|
schedule_timeout(jtime);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2022-03-25 03:32:46 +08:00
|
|
|
err = ds_recv_status(dev, &st);
|
2017-01-19 04:31:11 +08:00
|
|
|
if (err < 0 || err < sizeof(st))
|
2014-01-16 12:29:23 +08:00
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-01-16 12:29:23 +08:00
|
|
|
if (st.data_in_buffer_status) {
|
|
|
|
/* Bulk in can receive partial ids, but when it does
|
|
|
|
* they fail crc and will be discarded anyway.
|
|
|
|
* That has only been seen when status in buffer
|
|
|
|
* is 0 and bulk is read anyway, so don't read
|
|
|
|
* bulk without first checking if status says there
|
|
|
|
* is data to read.
|
|
|
|
*/
|
2017-01-19 04:31:11 +08:00
|
|
|
err = ds_recv_data(dev, (u8 *)buf, bufsize);
|
2014-01-16 12:29:23 +08:00
|
|
|
if (err < 0)
|
|
|
|
break;
|
|
|
|
for (i = 0; i < err/8; ++i) {
|
2021-01-14 03:50:18 +08:00
|
|
|
found_ids[found++] = buf[i];
|
2014-01-16 12:29:23 +08:00
|
|
|
/* can't know if there will be a discrepancy
|
|
|
|
* value after until the next id */
|
2021-01-14 03:50:18 +08:00
|
|
|
if (found == search_limit) {
|
2014-01-16 12:29:23 +08:00
|
|
|
master->search_id = buf[i];
|
2021-01-14 03:50:18 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-01-16 12:29:23 +08:00
|
|
|
}
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-01-16 12:29:23 +08:00
|
|
|
if (test_bit(W1_ABORT_SEARCH, &master->flags))
|
|
|
|
break;
|
|
|
|
} while (!(st.status & (ST_IDLE | ST_HALT)));
|
|
|
|
|
|
|
|
/* only continue the search if some weren't found */
|
|
|
|
if (found <= search_limit) {
|
|
|
|
master->search_id = 0;
|
|
|
|
} else if (!test_bit(W1_WARN_MAX_COUNT, &master->flags)) {
|
|
|
|
/* Only max_slave_count will be scanned in a search,
|
|
|
|
* but it will start where it left off next search
|
|
|
|
* until all ids are identified and then it will start
|
|
|
|
* over. A continued search will report the previous
|
|
|
|
* last id as the first id (provided it is still on the
|
|
|
|
* bus).
|
|
|
|
*/
|
|
|
|
dev_info(&dev->udev->dev, "%s: max_slave_count %d reached, "
|
|
|
|
"will continue next search.\n", __func__,
|
|
|
|
master->max_slave_count);
|
|
|
|
set_bit(W1_WARN_MAX_COUNT, &master->flags);
|
|
|
|
}
|
2021-01-14 03:50:18 +08:00
|
|
|
|
2014-01-16 12:29:26 +08:00
|
|
|
search_out:
|
|
|
|
mutex_unlock(&master->bus_mutex);
|
2017-01-19 04:31:11 +08:00
|
|
|
kfree(buf);
|
2021-01-14 03:50:18 +08:00
|
|
|
|
|
|
|
for (i = 0; i < found; i++) /* run callback for all queued up IDs */
|
|
|
|
callback(master, found_ids[i]);
|
|
|
|
kfree(found_ids);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2014-01-16 12:29:23 +08:00
|
|
|
#if 0
|
2017-01-19 04:31:11 +08:00
|
|
|
/*
|
|
|
|
* FIXME: if this disabled code is ever used in the future all ds_send_data()
|
|
|
|
* calls must be changed to use a DMAable buffer.
|
|
|
|
*/
|
2006-03-24 00:11:58 +08:00
|
|
|
static int ds_match_access(struct ds_device *dev, u64 init)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ds_status st;
|
|
|
|
|
|
|
|
err = ds_send_data(dev, (unsigned char *)&init, sizeof(init));
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
ds_wait_status(dev, &st);
|
|
|
|
|
|
|
|
err = ds_send_control(dev, COMM_MATCH_ACCESS | COMM_IM | COMM_RST, 0x0055);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
ds_wait_status(dev, &st);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
static int ds_set_path(struct ds_device *dev, u64 init)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ds_status st;
|
|
|
|
u8 buf[9];
|
|
|
|
|
|
|
|
memcpy(buf, &init, 8);
|
|
|
|
buf[8] = BRANCH_MAIN;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
err = ds_send_data(dev, buf, sizeof(buf));
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
ds_wait_status(dev, &st);
|
|
|
|
|
|
|
|
err = ds_send_control(dev, COMM_SET_PATH | COMM_IM | COMM_RST, 0);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
ds_wait_status(dev, &st);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-03 19:14:50 +08:00
|
|
|
#endif /* 0 */
|
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
static u8 ds9490r_touch_bit(void *data, u8 bit)
|
|
|
|
{
|
|
|
|
struct ds_device *dev = data;
|
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
if (ds_touch_bit(dev, bit, &dev->byte_buf))
|
2006-03-24 00:11:58 +08:00
|
|
|
return 0;
|
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
return dev->byte_buf;
|
2006-03-24 00:11:58 +08:00
|
|
|
}
|
|
|
|
|
2008-10-16 13:05:04 +08:00
|
|
|
#if 0
|
2006-03-24 00:11:58 +08:00
|
|
|
static void ds9490r_write_bit(void *data, u8 bit)
|
|
|
|
{
|
|
|
|
struct ds_device *dev = data;
|
|
|
|
|
|
|
|
ds_write_bit(dev, bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u8 ds9490r_read_bit(void *data)
|
|
|
|
{
|
|
|
|
struct ds_device *dev = data;
|
|
|
|
int err;
|
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
err = ds_touch_bit(dev, 1, &dev->byte_buf);
|
2006-03-24 00:11:58 +08:00
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
return dev->byte_buf & 1;
|
2006-03-24 00:11:58 +08:00
|
|
|
}
|
2008-10-16 13:05:04 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void ds9490r_write_byte(void *data, u8 byte)
|
|
|
|
{
|
|
|
|
struct ds_device *dev = data;
|
|
|
|
|
|
|
|
ds_write_byte(dev, byte);
|
|
|
|
}
|
2006-03-24 00:11:58 +08:00
|
|
|
|
|
|
|
static u8 ds9490r_read_byte(void *data)
|
|
|
|
{
|
|
|
|
struct ds_device *dev = data;
|
|
|
|
int err;
|
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
err = ds_read_byte(dev, &dev->byte_buf);
|
2006-03-24 00:11:58 +08:00
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
return dev->byte_buf;
|
2006-03-24 00:11:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ds9490r_write_block(void *data, const u8 *buf, int len)
|
|
|
|
{
|
|
|
|
struct ds_device *dev = data;
|
2017-01-19 04:31:11 +08:00
|
|
|
u8 *tbuf;
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
return;
|
|
|
|
|
2017-02-09 23:45:42 +08:00
|
|
|
tbuf = kmemdup(buf, len, GFP_KERNEL);
|
2017-01-19 04:31:11 +08:00
|
|
|
if (!tbuf)
|
|
|
|
return;
|
2006-03-24 00:11:58 +08:00
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
ds_write_block(dev, tbuf, len);
|
|
|
|
|
|
|
|
kfree(tbuf);
|
2006-03-24 00:11:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static u8 ds9490r_read_block(void *data, u8 *buf, int len)
|
|
|
|
{
|
|
|
|
struct ds_device *dev = data;
|
|
|
|
int err;
|
2017-01-19 04:31:11 +08:00
|
|
|
u8 *tbuf;
|
2006-03-24 00:11:58 +08:00
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
if (len <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
tbuf = kmalloc(len, GFP_KERNEL);
|
|
|
|
if (!tbuf)
|
2006-03-24 00:11:58 +08:00
|
|
|
return 0;
|
|
|
|
|
2017-01-19 04:31:11 +08:00
|
|
|
err = ds_read_block(dev, tbuf, len);
|
|
|
|
if (err >= 0)
|
|
|
|
memcpy(buf, tbuf, len);
|
|
|
|
|
|
|
|
kfree(tbuf);
|
|
|
|
|
|
|
|
return err >= 0 ? len : 0;
|
2006-03-24 00:11:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static u8 ds9490r_reset(void *data)
|
|
|
|
{
|
|
|
|
struct ds_device *dev = data;
|
|
|
|
int err;
|
|
|
|
|
2008-10-16 13:05:07 +08:00
|
|
|
err = ds_reset(dev);
|
2006-03-24 00:11:58 +08:00
|
|
|
if (err)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-16 13:05:03 +08:00
|
|
|
static u8 ds9490r_set_pullup(void *data, int delay)
|
|
|
|
{
|
|
|
|
struct ds_device *dev = data;
|
|
|
|
|
|
|
|
if (ds_set_pullup(dev, delay))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
static int ds_w1_init(struct ds_device *dev)
|
|
|
|
{
|
|
|
|
memset(&dev->master, 0, sizeof(struct w1_bus_master));
|
|
|
|
|
2008-10-16 13:05:08 +08:00
|
|
|
/* Reset the device as it can be in a bad state.
|
|
|
|
* This is necessary because a block write will wait for data
|
|
|
|
* to be placed in the output buffer and block any later
|
|
|
|
* commands which will keep accumulating and the device will
|
|
|
|
* not be idle. Another case is removing the ds2490 module
|
|
|
|
* while a bus search is in progress, somehow a few commands
|
|
|
|
* get through, but the input transfers fail leaving data in
|
|
|
|
* the input buffer. This will cause the next read to fail
|
|
|
|
* see the note in ds_recv_data.
|
|
|
|
*/
|
2008-10-16 13:05:10 +08:00
|
|
|
ds_reset_device(dev);
|
2008-10-16 13:05:08 +08:00
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
dev->master.data = dev;
|
|
|
|
dev->master.touch_bit = &ds9490r_touch_bit;
|
2008-10-16 13:05:04 +08:00
|
|
|
/* read_bit and write_bit in w1_bus_master are expected to set and
|
|
|
|
* sample the line level. For write_bit that means it is expected to
|
|
|
|
* set it to that value and leave it there. ds2490 only supports an
|
|
|
|
* individual time slot at the lowest level. The requirement from
|
|
|
|
* pulling the bus state down to reading the state is 15us, something
|
|
|
|
* that isn't realistic on the USB bus anyway.
|
2006-03-24 00:11:58 +08:00
|
|
|
dev->master.read_bit = &ds9490r_read_bit;
|
|
|
|
dev->master.write_bit = &ds9490r_write_bit;
|
2008-10-16 13:05:04 +08:00
|
|
|
*/
|
2006-03-24 00:11:58 +08:00
|
|
|
dev->master.read_byte = &ds9490r_read_byte;
|
|
|
|
dev->master.write_byte = &ds9490r_write_byte;
|
|
|
|
dev->master.read_block = &ds9490r_read_block;
|
|
|
|
dev->master.write_block = &ds9490r_write_block;
|
|
|
|
dev->master.reset_bus = &ds9490r_reset;
|
2008-10-16 13:05:03 +08:00
|
|
|
dev->master.set_pullup = &ds9490r_set_pullup;
|
2014-01-16 12:29:23 +08:00
|
|
|
dev->master.search = &ds9490r_search;
|
2006-03-24 00:11:58 +08:00
|
|
|
|
|
|
|
return w1_add_master_device(&dev->master);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ds_w1_fini(struct ds_device *dev)
|
|
|
|
{
|
|
|
|
w1_remove_master_device(&dev->master);
|
|
|
|
}
|
|
|
|
|
2005-08-03 19:14:50 +08:00
|
|
|
static int ds_probe(struct usb_interface *intf,
|
|
|
|
const struct usb_device_id *udev_id)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct usb_device *udev = interface_to_usbdev(intf);
|
|
|
|
struct usb_endpoint_descriptor *endpoint;
|
|
|
|
struct usb_host_interface *iface_desc;
|
2006-03-24 00:11:58 +08:00
|
|
|
struct ds_device *dev;
|
2014-01-16 12:29:22 +08:00
|
|
|
int i, err, alt;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-01-16 12:29:23 +08:00
|
|
|
dev = kzalloc(sizeof(struct ds_device), GFP_KERNEL);
|
2022-03-25 03:32:46 +08:00
|
|
|
if (!dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
2022-03-25 03:32:46 +08:00
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
dev->udev = usb_get_dev(udev);
|
|
|
|
if (!dev->udev) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err_out_free;
|
|
|
|
}
|
|
|
|
memset(dev->ep, 0, sizeof(dev->ep));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
usb_set_intfdata(intf, dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-01-16 12:29:22 +08:00
|
|
|
err = usb_reset_configuration(dev->udev);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err) {
|
2014-01-16 12:29:22 +08:00
|
|
|
dev_err(&dev->udev->dev,
|
|
|
|
"Failed to reset configuration: err=%d.\n", err);
|
2006-03-24 00:11:58 +08:00
|
|
|
goto err_out_clear;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2014-01-16 12:29:22 +08:00
|
|
|
/* alternative 3, 1ms interrupt (greatly speeds search), 64 byte bulk */
|
|
|
|
alt = 3;
|
|
|
|
err = usb_set_interface(dev->udev,
|
2019-04-22 23:16:04 +08:00
|
|
|
intf->cur_altsetting->desc.bInterfaceNumber, alt);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err) {
|
2014-01-16 12:29:22 +08:00
|
|
|
dev_err(&dev->udev->dev, "Failed to set alternative setting %d "
|
|
|
|
"for %d interface: err=%d.\n", alt,
|
2019-04-22 23:16:04 +08:00
|
|
|
intf->cur_altsetting->desc.bInterfaceNumber, err);
|
2006-03-24 00:11:58 +08:00
|
|
|
goto err_out_clear;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2019-04-22 23:16:04 +08:00
|
|
|
iface_desc = intf->cur_altsetting;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
|
2022-03-25 03:32:46 +08:00
|
|
|
dev_err(&dev->udev->dev, "Num endpoints=%d. It is not DS9490R.\n",
|
2014-06-19 08:52:01 +08:00
|
|
|
iface_desc->desc.bNumEndpoints);
|
2006-03-24 00:11:58 +08:00
|
|
|
err = -EINVAL;
|
|
|
|
goto err_out_clear;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-08-03 19:14:50 +08:00
|
|
|
* This loop doesn'd show control 0 endpoint,
|
2005-04-17 06:20:36 +08:00
|
|
|
* so we will fill only 1-3 endpoints entry.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
|
|
|
|
endpoint = &iface_desc->endpoint[i].desc;
|
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
dev->ep[i+1] = endpoint->bEndpointAddress;
|
|
|
|
#if 0
|
2005-04-17 06:20:36 +08:00
|
|
|
printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
|
|
|
|
i, endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize),
|
|
|
|
(endpoint->bEndpointAddress & USB_DIR_IN)?"IN":"OUT",
|
|
|
|
endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
|
2006-03-24 00:11:58 +08:00
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
err = ds_w1_init(dev);
|
|
|
|
if (err)
|
|
|
|
goto err_out_clear;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2006-04-03 16:04:27 +08:00
|
|
|
mutex_lock(&ds_mutex);
|
2006-03-24 00:11:58 +08:00
|
|
|
list_add_tail(&dev->ds_entry, &ds_devices);
|
2006-04-03 16:04:27 +08:00
|
|
|
mutex_unlock(&ds_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return 0;
|
2006-03-24 00:11:58 +08:00
|
|
|
|
|
|
|
err_out_clear:
|
|
|
|
usb_set_intfdata(intf, NULL);
|
|
|
|
usb_put_dev(dev->udev);
|
|
|
|
err_out_free:
|
|
|
|
kfree(dev);
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-08-03 19:14:50 +08:00
|
|
|
static void ds_disconnect(struct usb_interface *intf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct ds_device *dev;
|
2005-08-03 19:14:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
dev = usb_get_intfdata(intf);
|
2006-03-24 00:11:58 +08:00
|
|
|
if (!dev)
|
|
|
|
return;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-04-03 16:04:27 +08:00
|
|
|
mutex_lock(&ds_mutex);
|
2006-03-24 00:11:58 +08:00
|
|
|
list_del(&dev->ds_entry);
|
2006-04-03 16:04:27 +08:00
|
|
|
mutex_unlock(&ds_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-03-24 00:11:58 +08:00
|
|
|
ds_w1_fini(dev);
|
|
|
|
|
|
|
|
usb_set_intfdata(intf, NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
usb_put_dev(dev->udev);
|
|
|
|
kfree(dev);
|
|
|
|
}
|
|
|
|
|
2017-08-12 16:38:02 +08:00
|
|
|
static const struct usb_device_id ds_id_table[] = {
|
2017-05-17 04:02:12 +08:00
|
|
|
{ USB_DEVICE(0x04fa, 0x2490) },
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(usb, ds_id_table);
|
|
|
|
|
|
|
|
static struct usb_driver ds_driver = {
|
|
|
|
.name = "DS9490R",
|
|
|
|
.probe = ds_probe,
|
|
|
|
.disconnect = ds_disconnect,
|
|
|
|
.id_table = ds_id_table,
|
|
|
|
};
|
2011-11-19 01:52:10 +08:00
|
|
|
module_usb_driver(ds_driver);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-08-26 06:59:06 +08:00
|
|
|
MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
|
2006-03-24 00:11:58 +08:00
|
|
|
MODULE_DESCRIPTION("DS2490 USB <-> W1 bus master driver (DS9490*)");
|
2017-05-17 04:02:12 +08:00
|
|
|
MODULE_LICENSE("GPL");
|