18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	ds2490.c  USB to one wire bridge
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
118c2ecf20Sopenharmony_ci#include <linux/usb.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/w1.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/* USB Standard */
178c2ecf20Sopenharmony_ci/* USB Control request vendor type */
188c2ecf20Sopenharmony_ci#define VENDOR				0x40
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* COMMAND TYPE CODES */
218c2ecf20Sopenharmony_ci#define CONTROL_CMD			0x00
228c2ecf20Sopenharmony_ci#define COMM_CMD			0x01
238c2ecf20Sopenharmony_ci#define MODE_CMD			0x02
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* CONTROL COMMAND CODES */
268c2ecf20Sopenharmony_ci#define CTL_RESET_DEVICE		0x0000
278c2ecf20Sopenharmony_ci#define CTL_START_EXE			0x0001
288c2ecf20Sopenharmony_ci#define CTL_RESUME_EXE			0x0002
298c2ecf20Sopenharmony_ci#define CTL_HALT_EXE_IDLE		0x0003
308c2ecf20Sopenharmony_ci#define CTL_HALT_EXE_DONE		0x0004
318c2ecf20Sopenharmony_ci#define CTL_FLUSH_COMM_CMDS		0x0007
328c2ecf20Sopenharmony_ci#define CTL_FLUSH_RCV_BUFFER		0x0008
338c2ecf20Sopenharmony_ci#define CTL_FLUSH_XMT_BUFFER		0x0009
348c2ecf20Sopenharmony_ci#define CTL_GET_COMM_CMDS		0x000A
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* MODE COMMAND CODES */
378c2ecf20Sopenharmony_ci#define MOD_PULSE_EN			0x0000
388c2ecf20Sopenharmony_ci#define MOD_SPEED_CHANGE_EN		0x0001
398c2ecf20Sopenharmony_ci#define MOD_1WIRE_SPEED			0x0002
408c2ecf20Sopenharmony_ci#define MOD_STRONG_PU_DURATION		0x0003
418c2ecf20Sopenharmony_ci#define MOD_PULLDOWN_SLEWRATE		0x0004
428c2ecf20Sopenharmony_ci#define MOD_PROG_PULSE_DURATION		0x0005
438c2ecf20Sopenharmony_ci#define MOD_WRITE1_LOWTIME		0x0006
448c2ecf20Sopenharmony_ci#define MOD_DSOW0_TREC			0x0007
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* COMMUNICATION COMMAND CODES */
478c2ecf20Sopenharmony_ci#define COMM_ERROR_ESCAPE		0x0601
488c2ecf20Sopenharmony_ci#define COMM_SET_DURATION		0x0012
498c2ecf20Sopenharmony_ci#define COMM_BIT_IO			0x0020
508c2ecf20Sopenharmony_ci#define COMM_PULSE			0x0030
518c2ecf20Sopenharmony_ci#define COMM_1_WIRE_RESET		0x0042
528c2ecf20Sopenharmony_ci#define COMM_BYTE_IO			0x0052
538c2ecf20Sopenharmony_ci#define COMM_MATCH_ACCESS		0x0064
548c2ecf20Sopenharmony_ci#define COMM_BLOCK_IO			0x0074
558c2ecf20Sopenharmony_ci#define COMM_READ_STRAIGHT		0x0080
568c2ecf20Sopenharmony_ci#define COMM_DO_RELEASE			0x6092
578c2ecf20Sopenharmony_ci#define COMM_SET_PATH			0x00A2
588c2ecf20Sopenharmony_ci#define COMM_WRITE_SRAM_PAGE		0x00B2
598c2ecf20Sopenharmony_ci#define COMM_WRITE_EPROM		0x00C4
608c2ecf20Sopenharmony_ci#define COMM_READ_CRC_PROT_PAGE		0x00D4
618c2ecf20Sopenharmony_ci#define COMM_READ_REDIRECT_PAGE_CRC	0x21E4
628c2ecf20Sopenharmony_ci#define COMM_SEARCH_ACCESS		0x00F4
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/* Communication command bits */
658c2ecf20Sopenharmony_ci#define COMM_TYPE			0x0008
668c2ecf20Sopenharmony_ci#define COMM_SE				0x0008
678c2ecf20Sopenharmony_ci#define COMM_D				0x0008
688c2ecf20Sopenharmony_ci#define COMM_Z				0x0008
698c2ecf20Sopenharmony_ci#define COMM_CH				0x0008
708c2ecf20Sopenharmony_ci#define COMM_SM				0x0008
718c2ecf20Sopenharmony_ci#define COMM_R				0x0008
728c2ecf20Sopenharmony_ci#define COMM_IM				0x0001
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define COMM_PS				0x4000
758c2ecf20Sopenharmony_ci#define COMM_PST			0x4000
768c2ecf20Sopenharmony_ci#define COMM_CIB			0x4000
778c2ecf20Sopenharmony_ci#define COMM_RTS			0x4000
788c2ecf20Sopenharmony_ci#define COMM_DT				0x2000
798c2ecf20Sopenharmony_ci#define COMM_SPU			0x1000
808c2ecf20Sopenharmony_ci#define COMM_F				0x0800
818c2ecf20Sopenharmony_ci#define COMM_NTF			0x0400
828c2ecf20Sopenharmony_ci#define COMM_ICP			0x0200
838c2ecf20Sopenharmony_ci#define COMM_RST			0x0100
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define PULSE_PROG			0x01
868c2ecf20Sopenharmony_ci#define PULSE_SPUE			0x02
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define BRANCH_MAIN			0xCC
898c2ecf20Sopenharmony_ci#define BRANCH_AUX			0x33
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/* Status flags */
928c2ecf20Sopenharmony_ci#define ST_SPUA				0x01  /* Strong Pull-up is active */
938c2ecf20Sopenharmony_ci#define ST_PRGA				0x02  /* 12V programming pulse is being generated */
948c2ecf20Sopenharmony_ci#define ST_12VP				0x04  /* external 12V programming voltage is present */
958c2ecf20Sopenharmony_ci#define ST_PMOD				0x08  /* DS2490 powered from USB and external sources */
968c2ecf20Sopenharmony_ci#define ST_HALT				0x10  /* DS2490 is currently halted */
978c2ecf20Sopenharmony_ci#define ST_IDLE				0x20  /* DS2490 is currently idle */
988c2ecf20Sopenharmony_ci#define ST_EPOF				0x80
998c2ecf20Sopenharmony_ci/* Status transfer size, 16 bytes status, 16 byte result flags */
1008c2ecf20Sopenharmony_ci#define ST_SIZE				0x20
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/* Result Register flags */
1038c2ecf20Sopenharmony_ci#define RR_DETECT			0xA5 /* New device detected */
1048c2ecf20Sopenharmony_ci#define RR_NRS				0x01 /* Reset no presence or ... */
1058c2ecf20Sopenharmony_ci#define RR_SH				0x02 /* short on reset or set path */
1068c2ecf20Sopenharmony_ci#define RR_APP				0x04 /* alarming presence on reset */
1078c2ecf20Sopenharmony_ci#define RR_VPP				0x08 /* 12V expected not seen */
1088c2ecf20Sopenharmony_ci#define RR_CMP				0x10 /* compare error */
1098c2ecf20Sopenharmony_ci#define RR_CRC				0x20 /* CRC error detected */
1108c2ecf20Sopenharmony_ci#define RR_RDP				0x40 /* redirected page */
1118c2ecf20Sopenharmony_ci#define RR_EOS				0x80 /* end of search error */
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#define SPEED_NORMAL			0x00
1148c2ecf20Sopenharmony_ci#define SPEED_FLEXIBLE			0x01
1158c2ecf20Sopenharmony_ci#define SPEED_OVERDRIVE			0x02
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define NUM_EP				4
1188c2ecf20Sopenharmony_ci#define EP_CONTROL			0
1198c2ecf20Sopenharmony_ci#define EP_STATUS			1
1208c2ecf20Sopenharmony_ci#define EP_DATA_OUT			2
1218c2ecf20Sopenharmony_ci#define EP_DATA_IN			3
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistruct ds_device {
1248c2ecf20Sopenharmony_ci	struct list_head	ds_entry;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	struct usb_device	*udev;
1278c2ecf20Sopenharmony_ci	struct usb_interface	*intf;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	int			ep[NUM_EP];
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	/* Strong PullUp
1328c2ecf20Sopenharmony_ci	 * 0: pullup not active, else duration in milliseconds
1338c2ecf20Sopenharmony_ci	 */
1348c2ecf20Sopenharmony_ci	int			spu_sleep;
1358c2ecf20Sopenharmony_ci	/* spu_bit contains COMM_SPU or 0 depending on if the strong pullup
1368c2ecf20Sopenharmony_ci	 * should be active or not for writes.
1378c2ecf20Sopenharmony_ci	 */
1388c2ecf20Sopenharmony_ci	u16			spu_bit;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	u8			st_buf[ST_SIZE];
1418c2ecf20Sopenharmony_ci	u8			byte_buf;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	struct w1_bus_master	master;
1448c2ecf20Sopenharmony_ci};
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistruct ds_status {
1478c2ecf20Sopenharmony_ci	u8			enable;
1488c2ecf20Sopenharmony_ci	u8			speed;
1498c2ecf20Sopenharmony_ci	u8			pullup_dur;
1508c2ecf20Sopenharmony_ci	u8			ppuls_dur;
1518c2ecf20Sopenharmony_ci	u8			pulldown_slew;
1528c2ecf20Sopenharmony_ci	u8			write1_time;
1538c2ecf20Sopenharmony_ci	u8			write0_time;
1548c2ecf20Sopenharmony_ci	u8			reserved0;
1558c2ecf20Sopenharmony_ci	u8			status;
1568c2ecf20Sopenharmony_ci	u8			command0;
1578c2ecf20Sopenharmony_ci	u8			command1;
1588c2ecf20Sopenharmony_ci	u8			command_buffer_status;
1598c2ecf20Sopenharmony_ci	u8			data_out_buffer_status;
1608c2ecf20Sopenharmony_ci	u8			data_in_buffer_status;
1618c2ecf20Sopenharmony_ci	u8			reserved1;
1628c2ecf20Sopenharmony_ci	u8			reserved2;
1638c2ecf20Sopenharmony_ci};
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic LIST_HEAD(ds_devices);
1668c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(ds_mutex);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	int err;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
1738c2ecf20Sopenharmony_ci			CONTROL_CMD, VENDOR, value, index, NULL, 0, 1000);
1748c2ecf20Sopenharmony_ci	if (err < 0) {
1758c2ecf20Sopenharmony_ci		pr_err("Failed to send command control message %x.%x: err=%d.\n",
1768c2ecf20Sopenharmony_ci				value, index, err);
1778c2ecf20Sopenharmony_ci		return err;
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	return err;
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	int err;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
1888c2ecf20Sopenharmony_ci			MODE_CMD, VENDOR, value, index, NULL, 0, 1000);
1898c2ecf20Sopenharmony_ci	if (err < 0) {
1908c2ecf20Sopenharmony_ci		pr_err("Failed to send mode control message %x.%x: err=%d.\n",
1918c2ecf20Sopenharmony_ci				value, index, err);
1928c2ecf20Sopenharmony_ci		return err;
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	return err;
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic int ds_send_control(struct ds_device *dev, u16 value, u16 index)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	int err;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
2038c2ecf20Sopenharmony_ci			COMM_CMD, VENDOR, value, index, NULL, 0, 1000);
2048c2ecf20Sopenharmony_ci	if (err < 0) {
2058c2ecf20Sopenharmony_ci		pr_err("Failed to send control message %x.%x: err=%d.\n",
2068c2ecf20Sopenharmony_ci				value, index, err);
2078c2ecf20Sopenharmony_ci		return err;
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	return err;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic inline void ds_print_msg(unsigned char *buf, unsigned char *str, int off)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	pr_info("%45s: %8x\n", str, buf[off]);
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic void ds_dump_status(struct ds_device *dev, unsigned char *buf, int count)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	int i;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	pr_info("0x%x: count=%d, status: ", dev->ep[EP_STATUS], count);
2238c2ecf20Sopenharmony_ci	for (i = 0; i < count; ++i)
2248c2ecf20Sopenharmony_ci		pr_info("%02x ", buf[i]);
2258c2ecf20Sopenharmony_ci	pr_info("\n");
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	if (count >= 16) {
2288c2ecf20Sopenharmony_ci		ds_print_msg(buf, "enable flag", 0);
2298c2ecf20Sopenharmony_ci		ds_print_msg(buf, "1-wire speed", 1);
2308c2ecf20Sopenharmony_ci		ds_print_msg(buf, "strong pullup duration", 2);
2318c2ecf20Sopenharmony_ci		ds_print_msg(buf, "programming pulse duration", 3);
2328c2ecf20Sopenharmony_ci		ds_print_msg(buf, "pulldown slew rate control", 4);
2338c2ecf20Sopenharmony_ci		ds_print_msg(buf, "write-1 low time", 5);
2348c2ecf20Sopenharmony_ci		ds_print_msg(buf, "data sample offset/write-0 recovery time",
2358c2ecf20Sopenharmony_ci			6);
2368c2ecf20Sopenharmony_ci		ds_print_msg(buf, "reserved (test register)", 7);
2378c2ecf20Sopenharmony_ci		ds_print_msg(buf, "device status flags", 8);
2388c2ecf20Sopenharmony_ci		ds_print_msg(buf, "communication command byte 1", 9);
2398c2ecf20Sopenharmony_ci		ds_print_msg(buf, "communication command byte 2", 10);
2408c2ecf20Sopenharmony_ci		ds_print_msg(buf, "communication command buffer status", 11);
2418c2ecf20Sopenharmony_ci		ds_print_msg(buf, "1-wire data output buffer status", 12);
2428c2ecf20Sopenharmony_ci		ds_print_msg(buf, "1-wire data input buffer status", 13);
2438c2ecf20Sopenharmony_ci		ds_print_msg(buf, "reserved", 14);
2448c2ecf20Sopenharmony_ci		ds_print_msg(buf, "reserved", 15);
2458c2ecf20Sopenharmony_ci	}
2468c2ecf20Sopenharmony_ci	for (i = 16; i < count; ++i) {
2478c2ecf20Sopenharmony_ci		if (buf[i] == RR_DETECT) {
2488c2ecf20Sopenharmony_ci			ds_print_msg(buf, "new device detect", i);
2498c2ecf20Sopenharmony_ci			continue;
2508c2ecf20Sopenharmony_ci		}
2518c2ecf20Sopenharmony_ci		ds_print_msg(buf, "Result Register Value: ", i);
2528c2ecf20Sopenharmony_ci		if (buf[i] & RR_NRS)
2538c2ecf20Sopenharmony_ci			pr_info("NRS: Reset no presence or ...\n");
2548c2ecf20Sopenharmony_ci		if (buf[i] & RR_SH)
2558c2ecf20Sopenharmony_ci			pr_info("SH: short on reset or set path\n");
2568c2ecf20Sopenharmony_ci		if (buf[i] & RR_APP)
2578c2ecf20Sopenharmony_ci			pr_info("APP: alarming presence on reset\n");
2588c2ecf20Sopenharmony_ci		if (buf[i] & RR_VPP)
2598c2ecf20Sopenharmony_ci			pr_info("VPP: 12V expected not seen\n");
2608c2ecf20Sopenharmony_ci		if (buf[i] & RR_CMP)
2618c2ecf20Sopenharmony_ci			pr_info("CMP: compare error\n");
2628c2ecf20Sopenharmony_ci		if (buf[i] & RR_CRC)
2638c2ecf20Sopenharmony_ci			pr_info("CRC: CRC error detected\n");
2648c2ecf20Sopenharmony_ci		if (buf[i] & RR_RDP)
2658c2ecf20Sopenharmony_ci			pr_info("RDP: redirected page\n");
2668c2ecf20Sopenharmony_ci		if (buf[i] & RR_EOS)
2678c2ecf20Sopenharmony_ci			pr_info("EOS: end of search error\n");
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic int ds_recv_status(struct ds_device *dev, struct ds_status *st,
2728c2ecf20Sopenharmony_ci			  bool dump)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	int count, err;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (st)
2778c2ecf20Sopenharmony_ci		memset(st, 0, sizeof(*st));
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	count = 0;
2808c2ecf20Sopenharmony_ci	err = usb_interrupt_msg(dev->udev,
2818c2ecf20Sopenharmony_ci				usb_rcvintpipe(dev->udev,
2828c2ecf20Sopenharmony_ci					       dev->ep[EP_STATUS]),
2838c2ecf20Sopenharmony_ci				dev->st_buf, sizeof(dev->st_buf),
2848c2ecf20Sopenharmony_ci				&count, 1000);
2858c2ecf20Sopenharmony_ci	if (err < 0) {
2868c2ecf20Sopenharmony_ci		pr_err("Failed to read 1-wire data from 0x%x: err=%d.\n",
2878c2ecf20Sopenharmony_ci		       dev->ep[EP_STATUS], err);
2888c2ecf20Sopenharmony_ci		return err;
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	if (dump)
2928c2ecf20Sopenharmony_ci		ds_dump_status(dev, dev->st_buf, count);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if (st && count >= sizeof(*st))
2958c2ecf20Sopenharmony_ci		memcpy(st, dev->st_buf, sizeof(*st));
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	return count;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic void ds_reset_device(struct ds_device *dev)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
3038c2ecf20Sopenharmony_ci	/* Always allow strong pullup which allow individual writes to use
3048c2ecf20Sopenharmony_ci	 * the strong pullup.
3058c2ecf20Sopenharmony_ci	 */
3068c2ecf20Sopenharmony_ci	if (ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE))
3078c2ecf20Sopenharmony_ci		pr_err("ds_reset_device: Error allowing strong pullup\n");
3088c2ecf20Sopenharmony_ci	/* Chip strong pullup time was cleared. */
3098c2ecf20Sopenharmony_ci	if (dev->spu_sleep) {
3108c2ecf20Sopenharmony_ci		/* lower 4 bits are 0, see ds_set_pullup */
3118c2ecf20Sopenharmony_ci		u8 del = dev->spu_sleep>>4;
3128c2ecf20Sopenharmony_ci		if (ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del))
3138c2ecf20Sopenharmony_ci			pr_err("ds_reset_device: Error setting duration\n");
3148c2ecf20Sopenharmony_ci	}
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic int ds_recv_data(struct ds_device *dev, unsigned char *buf, int size)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	int count, err;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	/* Careful on size.  If size is less than what is available in
3228c2ecf20Sopenharmony_ci	 * the input buffer, the device fails the bulk transfer and
3238c2ecf20Sopenharmony_ci	 * clears the input buffer.  It could read the maximum size of
3248c2ecf20Sopenharmony_ci	 * the data buffer, but then do you return the first, last, or
3258c2ecf20Sopenharmony_ci	 * some set of the middle size bytes?  As long as the rest of
3268c2ecf20Sopenharmony_ci	 * the code is correct there will be size bytes waiting.  A
3278c2ecf20Sopenharmony_ci	 * call to ds_wait_status will wait until the device is idle
3288c2ecf20Sopenharmony_ci	 * and any data to be received would have been available.
3298c2ecf20Sopenharmony_ci	 */
3308c2ecf20Sopenharmony_ci	count = 0;
3318c2ecf20Sopenharmony_ci	err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]),
3328c2ecf20Sopenharmony_ci				buf, size, &count, 1000);
3338c2ecf20Sopenharmony_ci	if (err < 0) {
3348c2ecf20Sopenharmony_ci		pr_info("Clearing ep0x%x.\n", dev->ep[EP_DATA_IN]);
3358c2ecf20Sopenharmony_ci		usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]));
3368c2ecf20Sopenharmony_ci		ds_recv_status(dev, NULL, true);
3378c2ecf20Sopenharmony_ci		return err;
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci#if 0
3418c2ecf20Sopenharmony_ci	{
3428c2ecf20Sopenharmony_ci		int i;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci		printk("%s: count=%d: ", __func__, count);
3458c2ecf20Sopenharmony_ci		for (i = 0; i < count; ++i)
3468c2ecf20Sopenharmony_ci			printk("%02x ", buf[i]);
3478c2ecf20Sopenharmony_ci		printk("\n");
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci#endif
3508c2ecf20Sopenharmony_ci	return count;
3518c2ecf20Sopenharmony_ci}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_cistatic int ds_send_data(struct ds_device *dev, unsigned char *buf, int len)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	int count, err;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	count = 0;
3588c2ecf20Sopenharmony_ci	err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000);
3598c2ecf20Sopenharmony_ci	if (err < 0) {
3608c2ecf20Sopenharmony_ci		pr_err("Failed to write 1-wire data to ep0x%x: "
3618c2ecf20Sopenharmony_ci			"err=%d.\n", dev->ep[EP_DATA_OUT], err);
3628c2ecf20Sopenharmony_ci		return err;
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	return err;
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci#if 0
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ciint ds_stop_pulse(struct ds_device *dev, int limit)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	struct ds_status st;
3738c2ecf20Sopenharmony_ci	int count = 0, err = 0;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	do {
3768c2ecf20Sopenharmony_ci		err = ds_send_control(dev, CTL_HALT_EXE_IDLE, 0);
3778c2ecf20Sopenharmony_ci		if (err)
3788c2ecf20Sopenharmony_ci			break;
3798c2ecf20Sopenharmony_ci		err = ds_send_control(dev, CTL_RESUME_EXE, 0);
3808c2ecf20Sopenharmony_ci		if (err)
3818c2ecf20Sopenharmony_ci			break;
3828c2ecf20Sopenharmony_ci		err = ds_recv_status(dev, &st, false);
3838c2ecf20Sopenharmony_ci		if (err)
3848c2ecf20Sopenharmony_ci			break;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci		if ((st.status & ST_SPUA) == 0) {
3878c2ecf20Sopenharmony_ci			err = ds_send_control_mode(dev, MOD_PULSE_EN, 0);
3888c2ecf20Sopenharmony_ci			if (err)
3898c2ecf20Sopenharmony_ci				break;
3908c2ecf20Sopenharmony_ci		}
3918c2ecf20Sopenharmony_ci	} while (++count < limit);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	return err;
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ciint ds_detect(struct ds_device *dev, struct ds_status *st)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	int err;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
4018c2ecf20Sopenharmony_ci	if (err)
4028c2ecf20Sopenharmony_ci		return err;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, 0);
4058c2ecf20Sopenharmony_ci	if (err)
4068c2ecf20Sopenharmony_ci		return err;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40);
4098c2ecf20Sopenharmony_ci	if (err)
4108c2ecf20Sopenharmony_ci		return err;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG);
4138c2ecf20Sopenharmony_ci	if (err)
4148c2ecf20Sopenharmony_ci		return err;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	err = ds_dump_status(dev, st);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	return err;
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci#endif  /*  0  */
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cistatic int ds_wait_status(struct ds_device *dev, struct ds_status *st)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	int err, count = 0;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	do {
4288c2ecf20Sopenharmony_ci		st->status = 0;
4298c2ecf20Sopenharmony_ci		err = ds_recv_status(dev, st, false);
4308c2ecf20Sopenharmony_ci#if 0
4318c2ecf20Sopenharmony_ci		if (err >= 0) {
4328c2ecf20Sopenharmony_ci			int i;
4338c2ecf20Sopenharmony_ci			printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], err);
4348c2ecf20Sopenharmony_ci			for (i = 0; i < err; ++i)
4358c2ecf20Sopenharmony_ci				printk("%02x ", dev->st_buf[i]);
4368c2ecf20Sopenharmony_ci			printk("\n");
4378c2ecf20Sopenharmony_ci		}
4388c2ecf20Sopenharmony_ci#endif
4398c2ecf20Sopenharmony_ci	} while (!(st->status & ST_IDLE) && !(err < 0) && ++count < 100);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	if (err >= 16 && st->status & ST_EPOF) {
4428c2ecf20Sopenharmony_ci		pr_info("Resetting device after ST_EPOF.\n");
4438c2ecf20Sopenharmony_ci		ds_reset_device(dev);
4448c2ecf20Sopenharmony_ci		/* Always dump the device status. */
4458c2ecf20Sopenharmony_ci		count = 101;
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	/* Dump the status for errors or if there is extended return data.
4498c2ecf20Sopenharmony_ci	 * The extended status includes new device detection (maybe someone
4508c2ecf20Sopenharmony_ci	 * can do something with it).
4518c2ecf20Sopenharmony_ci	 */
4528c2ecf20Sopenharmony_ci	if (err > 16 || count >= 100 || err < 0)
4538c2ecf20Sopenharmony_ci		ds_dump_status(dev, dev->st_buf, err);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	/* Extended data isn't an error.  Well, a short is, but the dump
4568c2ecf20Sopenharmony_ci	 * would have already told the user that and we can't do anything
4578c2ecf20Sopenharmony_ci	 * about it in software anyway.
4588c2ecf20Sopenharmony_ci	 */
4598c2ecf20Sopenharmony_ci	if (count >= 100 || err < 0)
4608c2ecf20Sopenharmony_ci		return -1;
4618c2ecf20Sopenharmony_ci	else
4628c2ecf20Sopenharmony_ci		return 0;
4638c2ecf20Sopenharmony_ci}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_cistatic int ds_reset(struct ds_device *dev)
4668c2ecf20Sopenharmony_ci{
4678c2ecf20Sopenharmony_ci	int err;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	/* Other potentionally interesting flags for reset.
4708c2ecf20Sopenharmony_ci	 *
4718c2ecf20Sopenharmony_ci	 * COMM_NTF: Return result register feedback.  This could be used to
4728c2ecf20Sopenharmony_ci	 * detect some conditions such as short, alarming presence, or
4738c2ecf20Sopenharmony_ci	 * detect if a new device was detected.
4748c2ecf20Sopenharmony_ci	 *
4758c2ecf20Sopenharmony_ci	 * COMM_SE which allows SPEED_NORMAL, SPEED_FLEXIBLE, SPEED_OVERDRIVE:
4768c2ecf20Sopenharmony_ci	 * Select the data transfer rate.
4778c2ecf20Sopenharmony_ci	 */
4788c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_IM, SPEED_NORMAL);
4798c2ecf20Sopenharmony_ci	if (err)
4808c2ecf20Sopenharmony_ci		return err;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	return 0;
4838c2ecf20Sopenharmony_ci}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci#if 0
4868c2ecf20Sopenharmony_cistatic int ds_set_speed(struct ds_device *dev, int speed)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	int err;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (speed != SPEED_NORMAL && speed != SPEED_FLEXIBLE && speed != SPEED_OVERDRIVE)
4918c2ecf20Sopenharmony_ci		return -EINVAL;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	if (speed != SPEED_OVERDRIVE)
4948c2ecf20Sopenharmony_ci		speed = SPEED_FLEXIBLE;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	speed &= 0xff;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	err = ds_send_control_mode(dev, MOD_1WIRE_SPEED, speed);
4998c2ecf20Sopenharmony_ci	if (err)
5008c2ecf20Sopenharmony_ci		return err;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	return err;
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci#endif  /*  0  */
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_cistatic int ds_set_pullup(struct ds_device *dev, int delay)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	int err = 0;
5098c2ecf20Sopenharmony_ci	u8 del = 1 + (u8)(delay >> 4);
5108c2ecf20Sopenharmony_ci	/* Just storing delay would not get the trunication and roundup. */
5118c2ecf20Sopenharmony_ci	int ms = del<<4;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	/* Enable spu_bit if a delay is set. */
5148c2ecf20Sopenharmony_ci	dev->spu_bit = delay ? COMM_SPU : 0;
5158c2ecf20Sopenharmony_ci	/* If delay is zero, it has already been disabled, if the time is
5168c2ecf20Sopenharmony_ci	 * the same as the hardware was last programmed to, there is also
5178c2ecf20Sopenharmony_ci	 * nothing more to do.  Compare with the recalculated value ms
5188c2ecf20Sopenharmony_ci	 * rather than del or delay which can have a different value.
5198c2ecf20Sopenharmony_ci	 */
5208c2ecf20Sopenharmony_ci	if (delay == 0 || ms == dev->spu_sleep)
5218c2ecf20Sopenharmony_ci		return err;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del);
5248c2ecf20Sopenharmony_ci	if (err)
5258c2ecf20Sopenharmony_ci		return err;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	dev->spu_sleep = ms;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	return err;
5308c2ecf20Sopenharmony_ci}
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_cistatic int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
5338c2ecf20Sopenharmony_ci{
5348c2ecf20Sopenharmony_ci	int err;
5358c2ecf20Sopenharmony_ci	struct ds_status st;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit ? COMM_D : 0),
5388c2ecf20Sopenharmony_ci		0);
5398c2ecf20Sopenharmony_ci	if (err)
5408c2ecf20Sopenharmony_ci		return err;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	ds_wait_status(dev, &st);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	err = ds_recv_data(dev, tbit, sizeof(*tbit));
5458c2ecf20Sopenharmony_ci	if (err < 0)
5468c2ecf20Sopenharmony_ci		return err;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	return 0;
5498c2ecf20Sopenharmony_ci}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci#if 0
5528c2ecf20Sopenharmony_cistatic int ds_write_bit(struct ds_device *dev, u8 bit)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	int err;
5558c2ecf20Sopenharmony_ci	struct ds_status st;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	/* Set COMM_ICP to write without a readback.  Note, this will
5588c2ecf20Sopenharmony_ci	 * produce one time slot, a down followed by an up with COMM_D
5598c2ecf20Sopenharmony_ci	 * only determing the timing.
5608c2ecf20Sopenharmony_ci	 */
5618c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_ICP |
5628c2ecf20Sopenharmony_ci		(bit ? COMM_D : 0), 0);
5638c2ecf20Sopenharmony_ci	if (err)
5648c2ecf20Sopenharmony_ci		return err;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	ds_wait_status(dev, &st);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	return 0;
5698c2ecf20Sopenharmony_ci}
5708c2ecf20Sopenharmony_ci#endif
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistatic int ds_write_byte(struct ds_device *dev, u8 byte)
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci	int err;
5758c2ecf20Sopenharmony_ci	struct ds_status st;
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM | dev->spu_bit, byte);
5788c2ecf20Sopenharmony_ci	if (err)
5798c2ecf20Sopenharmony_ci		return err;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	if (dev->spu_bit)
5828c2ecf20Sopenharmony_ci		msleep(dev->spu_sleep);
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	err = ds_wait_status(dev, &st);
5858c2ecf20Sopenharmony_ci	if (err)
5868c2ecf20Sopenharmony_ci		return err;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	err = ds_recv_data(dev, &dev->byte_buf, 1);
5898c2ecf20Sopenharmony_ci	if (err < 0)
5908c2ecf20Sopenharmony_ci		return err;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	return !(byte == dev->byte_buf);
5938c2ecf20Sopenharmony_ci}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_cistatic int ds_read_byte(struct ds_device *dev, u8 *byte)
5968c2ecf20Sopenharmony_ci{
5978c2ecf20Sopenharmony_ci	int err;
5988c2ecf20Sopenharmony_ci	struct ds_status st;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM, 0xff);
6018c2ecf20Sopenharmony_ci	if (err)
6028c2ecf20Sopenharmony_ci		return err;
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	ds_wait_status(dev, &st);
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	err = ds_recv_data(dev, byte, sizeof(*byte));
6078c2ecf20Sopenharmony_ci	if (err < 0)
6088c2ecf20Sopenharmony_ci		return err;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	return 0;
6118c2ecf20Sopenharmony_ci}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_cistatic int ds_read_block(struct ds_device *dev, u8 *buf, int len)
6148c2ecf20Sopenharmony_ci{
6158c2ecf20Sopenharmony_ci	struct ds_status st;
6168c2ecf20Sopenharmony_ci	int err;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	if (len > 64*1024)
6198c2ecf20Sopenharmony_ci		return -E2BIG;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	memset(buf, 0xFF, len);
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	err = ds_send_data(dev, buf, len);
6248c2ecf20Sopenharmony_ci	if (err < 0)
6258c2ecf20Sopenharmony_ci		return err;
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM, len);
6288c2ecf20Sopenharmony_ci	if (err)
6298c2ecf20Sopenharmony_ci		return err;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	ds_wait_status(dev, &st);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	memset(buf, 0x00, len);
6348c2ecf20Sopenharmony_ci	err = ds_recv_data(dev, buf, len);
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	return err;
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_cistatic int ds_write_block(struct ds_device *dev, u8 *buf, int len)
6408c2ecf20Sopenharmony_ci{
6418c2ecf20Sopenharmony_ci	int err;
6428c2ecf20Sopenharmony_ci	struct ds_status st;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	err = ds_send_data(dev, buf, len);
6458c2ecf20Sopenharmony_ci	if (err < 0)
6468c2ecf20Sopenharmony_ci		return err;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | dev->spu_bit, len);
6498c2ecf20Sopenharmony_ci	if (err)
6508c2ecf20Sopenharmony_ci		return err;
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	if (dev->spu_bit)
6538c2ecf20Sopenharmony_ci		msleep(dev->spu_sleep);
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	ds_wait_status(dev, &st);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	err = ds_recv_data(dev, buf, len);
6588c2ecf20Sopenharmony_ci	if (err < 0)
6598c2ecf20Sopenharmony_ci		return err;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	return !(err == len);
6628c2ecf20Sopenharmony_ci}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_cistatic void ds9490r_search(void *data, struct w1_master *master,
6658c2ecf20Sopenharmony_ci	u8 search_type, w1_slave_found_callback callback)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	/* When starting with an existing id, the first id returned will
6688c2ecf20Sopenharmony_ci	 * be that device (if it is still on the bus most likely).
6698c2ecf20Sopenharmony_ci	 *
6708c2ecf20Sopenharmony_ci	 * If the number of devices found is less than or equal to the
6718c2ecf20Sopenharmony_ci	 * search_limit, that number of IDs will be returned.  If there are
6728c2ecf20Sopenharmony_ci	 * more, search_limit IDs will be returned followed by a non-zero
6738c2ecf20Sopenharmony_ci	 * discrepency value.
6748c2ecf20Sopenharmony_ci	 */
6758c2ecf20Sopenharmony_ci	struct ds_device *dev = data;
6768c2ecf20Sopenharmony_ci	int err;
6778c2ecf20Sopenharmony_ci	u16 value, index;
6788c2ecf20Sopenharmony_ci	struct ds_status st;
6798c2ecf20Sopenharmony_ci	int search_limit;
6808c2ecf20Sopenharmony_ci	int found = 0;
6818c2ecf20Sopenharmony_ci	int i;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	/* DS18b20 spec, 13.16 ms per device, 75 per second, sleep for
6848c2ecf20Sopenharmony_ci	 * discovering 8 devices (1 bulk transfer and 1/2 FIFO size) at a time.
6858c2ecf20Sopenharmony_ci	 */
6868c2ecf20Sopenharmony_ci	const unsigned long jtime = msecs_to_jiffies(1000*8/75);
6878c2ecf20Sopenharmony_ci	/* FIFO 128 bytes, bulk packet size 64, read a multiple of the
6888c2ecf20Sopenharmony_ci	 * packet size.
6898c2ecf20Sopenharmony_ci	 */
6908c2ecf20Sopenharmony_ci	const size_t bufsize = 2 * 64;
6918c2ecf20Sopenharmony_ci	u64 *buf;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	buf = kmalloc(bufsize, GFP_KERNEL);
6948c2ecf20Sopenharmony_ci	if (!buf)
6958c2ecf20Sopenharmony_ci		return;
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	mutex_lock(&master->bus_mutex);
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	/* address to start searching at */
7008c2ecf20Sopenharmony_ci	if (ds_send_data(dev, (u8 *)&master->search_id, 8) < 0)
7018c2ecf20Sopenharmony_ci		goto search_out;
7028c2ecf20Sopenharmony_ci	master->search_id = 0;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	value = COMM_SEARCH_ACCESS | COMM_IM | COMM_RST | COMM_SM | COMM_F |
7058c2ecf20Sopenharmony_ci		COMM_RTS;
7068c2ecf20Sopenharmony_ci	search_limit = master->max_slave_count;
7078c2ecf20Sopenharmony_ci	if (search_limit > 255)
7088c2ecf20Sopenharmony_ci		search_limit = 0;
7098c2ecf20Sopenharmony_ci	index = search_type | (search_limit << 8);
7108c2ecf20Sopenharmony_ci	if (ds_send_control(dev, value, index) < 0)
7118c2ecf20Sopenharmony_ci		goto search_out;
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	do {
7148c2ecf20Sopenharmony_ci		schedule_timeout(jtime);
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci		err = ds_recv_status(dev, &st, false);
7178c2ecf20Sopenharmony_ci		if (err < 0 || err < sizeof(st))
7188c2ecf20Sopenharmony_ci			break;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci		if (st.data_in_buffer_status) {
7218c2ecf20Sopenharmony_ci			/* Bulk in can receive partial ids, but when it does
7228c2ecf20Sopenharmony_ci			 * they fail crc and will be discarded anyway.
7238c2ecf20Sopenharmony_ci			 * That has only been seen when status in buffer
7248c2ecf20Sopenharmony_ci			 * is 0 and bulk is read anyway, so don't read
7258c2ecf20Sopenharmony_ci			 * bulk without first checking if status says there
7268c2ecf20Sopenharmony_ci			 * is data to read.
7278c2ecf20Sopenharmony_ci			 */
7288c2ecf20Sopenharmony_ci			err = ds_recv_data(dev, (u8 *)buf, bufsize);
7298c2ecf20Sopenharmony_ci			if (err < 0)
7308c2ecf20Sopenharmony_ci				break;
7318c2ecf20Sopenharmony_ci			for (i = 0; i < err/8; ++i) {
7328c2ecf20Sopenharmony_ci				++found;
7338c2ecf20Sopenharmony_ci				if (found <= search_limit)
7348c2ecf20Sopenharmony_ci					callback(master, buf[i]);
7358c2ecf20Sopenharmony_ci				/* can't know if there will be a discrepancy
7368c2ecf20Sopenharmony_ci				 * value after until the next id */
7378c2ecf20Sopenharmony_ci				if (found == search_limit)
7388c2ecf20Sopenharmony_ci					master->search_id = buf[i];
7398c2ecf20Sopenharmony_ci			}
7408c2ecf20Sopenharmony_ci		}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci		if (test_bit(W1_ABORT_SEARCH, &master->flags))
7438c2ecf20Sopenharmony_ci			break;
7448c2ecf20Sopenharmony_ci	} while (!(st.status & (ST_IDLE | ST_HALT)));
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	/* only continue the search if some weren't found */
7478c2ecf20Sopenharmony_ci	if (found <= search_limit) {
7488c2ecf20Sopenharmony_ci		master->search_id = 0;
7498c2ecf20Sopenharmony_ci	} else if (!test_bit(W1_WARN_MAX_COUNT, &master->flags)) {
7508c2ecf20Sopenharmony_ci		/* Only max_slave_count will be scanned in a search,
7518c2ecf20Sopenharmony_ci		 * but it will start where it left off next search
7528c2ecf20Sopenharmony_ci		 * until all ids are identified and then it will start
7538c2ecf20Sopenharmony_ci		 * over.  A continued search will report the previous
7548c2ecf20Sopenharmony_ci		 * last id as the first id (provided it is still on the
7558c2ecf20Sopenharmony_ci		 * bus).
7568c2ecf20Sopenharmony_ci		 */
7578c2ecf20Sopenharmony_ci		dev_info(&dev->udev->dev, "%s: max_slave_count %d reached, "
7588c2ecf20Sopenharmony_ci			"will continue next search.\n", __func__,
7598c2ecf20Sopenharmony_ci			master->max_slave_count);
7608c2ecf20Sopenharmony_ci		set_bit(W1_WARN_MAX_COUNT, &master->flags);
7618c2ecf20Sopenharmony_ci	}
7628c2ecf20Sopenharmony_cisearch_out:
7638c2ecf20Sopenharmony_ci	mutex_unlock(&master->bus_mutex);
7648c2ecf20Sopenharmony_ci	kfree(buf);
7658c2ecf20Sopenharmony_ci}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci#if 0
7688c2ecf20Sopenharmony_ci/*
7698c2ecf20Sopenharmony_ci * FIXME: if this disabled code is ever used in the future all ds_send_data()
7708c2ecf20Sopenharmony_ci * calls must be changed to use a DMAable buffer.
7718c2ecf20Sopenharmony_ci */
7728c2ecf20Sopenharmony_cistatic int ds_match_access(struct ds_device *dev, u64 init)
7738c2ecf20Sopenharmony_ci{
7748c2ecf20Sopenharmony_ci	int err;
7758c2ecf20Sopenharmony_ci	struct ds_status st;
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	err = ds_send_data(dev, (unsigned char *)&init, sizeof(init));
7788c2ecf20Sopenharmony_ci	if (err)
7798c2ecf20Sopenharmony_ci		return err;
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	ds_wait_status(dev, &st);
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_MATCH_ACCESS | COMM_IM | COMM_RST, 0x0055);
7848c2ecf20Sopenharmony_ci	if (err)
7858c2ecf20Sopenharmony_ci		return err;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	ds_wait_status(dev, &st);
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	return 0;
7908c2ecf20Sopenharmony_ci}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_cistatic int ds_set_path(struct ds_device *dev, u64 init)
7938c2ecf20Sopenharmony_ci{
7948c2ecf20Sopenharmony_ci	int err;
7958c2ecf20Sopenharmony_ci	struct ds_status st;
7968c2ecf20Sopenharmony_ci	u8 buf[9];
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	memcpy(buf, &init, 8);
7998c2ecf20Sopenharmony_ci	buf[8] = BRANCH_MAIN;
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	err = ds_send_data(dev, buf, sizeof(buf));
8028c2ecf20Sopenharmony_ci	if (err)
8038c2ecf20Sopenharmony_ci		return err;
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	ds_wait_status(dev, &st);
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	err = ds_send_control(dev, COMM_SET_PATH | COMM_IM | COMM_RST, 0);
8088c2ecf20Sopenharmony_ci	if (err)
8098c2ecf20Sopenharmony_ci		return err;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	ds_wait_status(dev, &st);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	return 0;
8148c2ecf20Sopenharmony_ci}
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci#endif  /*  0  */
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_cistatic u8 ds9490r_touch_bit(void *data, u8 bit)
8198c2ecf20Sopenharmony_ci{
8208c2ecf20Sopenharmony_ci	struct ds_device *dev = data;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	if (ds_touch_bit(dev, bit, &dev->byte_buf))
8238c2ecf20Sopenharmony_ci		return 0;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	return dev->byte_buf;
8268c2ecf20Sopenharmony_ci}
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci#if 0
8298c2ecf20Sopenharmony_cistatic void ds9490r_write_bit(void *data, u8 bit)
8308c2ecf20Sopenharmony_ci{
8318c2ecf20Sopenharmony_ci	struct ds_device *dev = data;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	ds_write_bit(dev, bit);
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_cistatic u8 ds9490r_read_bit(void *data)
8378c2ecf20Sopenharmony_ci{
8388c2ecf20Sopenharmony_ci	struct ds_device *dev = data;
8398c2ecf20Sopenharmony_ci	int err;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	err = ds_touch_bit(dev, 1, &dev->byte_buf);
8428c2ecf20Sopenharmony_ci	if (err)
8438c2ecf20Sopenharmony_ci		return 0;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	return dev->byte_buf & 1;
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci#endif
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_cistatic void ds9490r_write_byte(void *data, u8 byte)
8508c2ecf20Sopenharmony_ci{
8518c2ecf20Sopenharmony_ci	struct ds_device *dev = data;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	ds_write_byte(dev, byte);
8548c2ecf20Sopenharmony_ci}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_cistatic u8 ds9490r_read_byte(void *data)
8578c2ecf20Sopenharmony_ci{
8588c2ecf20Sopenharmony_ci	struct ds_device *dev = data;
8598c2ecf20Sopenharmony_ci	int err;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	err = ds_read_byte(dev, &dev->byte_buf);
8628c2ecf20Sopenharmony_ci	if (err)
8638c2ecf20Sopenharmony_ci		return 0;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	return dev->byte_buf;
8668c2ecf20Sopenharmony_ci}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_cistatic void ds9490r_write_block(void *data, const u8 *buf, int len)
8698c2ecf20Sopenharmony_ci{
8708c2ecf20Sopenharmony_ci	struct ds_device *dev = data;
8718c2ecf20Sopenharmony_ci	u8 *tbuf;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	if (len <= 0)
8748c2ecf20Sopenharmony_ci		return;
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	tbuf = kmemdup(buf, len, GFP_KERNEL);
8778c2ecf20Sopenharmony_ci	if (!tbuf)
8788c2ecf20Sopenharmony_ci		return;
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	ds_write_block(dev, tbuf, len);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	kfree(tbuf);
8838c2ecf20Sopenharmony_ci}
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_cistatic u8 ds9490r_read_block(void *data, u8 *buf, int len)
8868c2ecf20Sopenharmony_ci{
8878c2ecf20Sopenharmony_ci	struct ds_device *dev = data;
8888c2ecf20Sopenharmony_ci	int err;
8898c2ecf20Sopenharmony_ci	u8 *tbuf;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	if (len <= 0)
8928c2ecf20Sopenharmony_ci		return 0;
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	tbuf = kmalloc(len, GFP_KERNEL);
8958c2ecf20Sopenharmony_ci	if (!tbuf)
8968c2ecf20Sopenharmony_ci		return 0;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	err = ds_read_block(dev, tbuf, len);
8998c2ecf20Sopenharmony_ci	if (err >= 0)
9008c2ecf20Sopenharmony_ci		memcpy(buf, tbuf, len);
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	kfree(tbuf);
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	return err >= 0 ? len : 0;
9058c2ecf20Sopenharmony_ci}
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_cistatic u8 ds9490r_reset(void *data)
9088c2ecf20Sopenharmony_ci{
9098c2ecf20Sopenharmony_ci	struct ds_device *dev = data;
9108c2ecf20Sopenharmony_ci	int err;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	err = ds_reset(dev);
9138c2ecf20Sopenharmony_ci	if (err)
9148c2ecf20Sopenharmony_ci		return 1;
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	return 0;
9178c2ecf20Sopenharmony_ci}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_cistatic u8 ds9490r_set_pullup(void *data, int delay)
9208c2ecf20Sopenharmony_ci{
9218c2ecf20Sopenharmony_ci	struct ds_device *dev = data;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	if (ds_set_pullup(dev, delay))
9248c2ecf20Sopenharmony_ci		return 1;
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	return 0;
9278c2ecf20Sopenharmony_ci}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_cistatic int ds_w1_init(struct ds_device *dev)
9308c2ecf20Sopenharmony_ci{
9318c2ecf20Sopenharmony_ci	memset(&dev->master, 0, sizeof(struct w1_bus_master));
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	/* Reset the device as it can be in a bad state.
9348c2ecf20Sopenharmony_ci	 * This is necessary because a block write will wait for data
9358c2ecf20Sopenharmony_ci	 * to be placed in the output buffer and block any later
9368c2ecf20Sopenharmony_ci	 * commands which will keep accumulating and the device will
9378c2ecf20Sopenharmony_ci	 * not be idle.  Another case is removing the ds2490 module
9388c2ecf20Sopenharmony_ci	 * while a bus search is in progress, somehow a few commands
9398c2ecf20Sopenharmony_ci	 * get through, but the input transfers fail leaving data in
9408c2ecf20Sopenharmony_ci	 * the input buffer.  This will cause the next read to fail
9418c2ecf20Sopenharmony_ci	 * see the note in ds_recv_data.
9428c2ecf20Sopenharmony_ci	 */
9438c2ecf20Sopenharmony_ci	ds_reset_device(dev);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	dev->master.data	= dev;
9468c2ecf20Sopenharmony_ci	dev->master.touch_bit	= &ds9490r_touch_bit;
9478c2ecf20Sopenharmony_ci	/* read_bit and write_bit in w1_bus_master are expected to set and
9488c2ecf20Sopenharmony_ci	 * sample the line level.  For write_bit that means it is expected to
9498c2ecf20Sopenharmony_ci	 * set it to that value and leave it there.  ds2490 only supports an
9508c2ecf20Sopenharmony_ci	 * individual time slot at the lowest level.  The requirement from
9518c2ecf20Sopenharmony_ci	 * pulling the bus state down to reading the state is 15us, something
9528c2ecf20Sopenharmony_ci	 * that isn't realistic on the USB bus anyway.
9538c2ecf20Sopenharmony_ci	dev->master.read_bit	= &ds9490r_read_bit;
9548c2ecf20Sopenharmony_ci	dev->master.write_bit	= &ds9490r_write_bit;
9558c2ecf20Sopenharmony_ci	*/
9568c2ecf20Sopenharmony_ci	dev->master.read_byte	= &ds9490r_read_byte;
9578c2ecf20Sopenharmony_ci	dev->master.write_byte	= &ds9490r_write_byte;
9588c2ecf20Sopenharmony_ci	dev->master.read_block	= &ds9490r_read_block;
9598c2ecf20Sopenharmony_ci	dev->master.write_block	= &ds9490r_write_block;
9608c2ecf20Sopenharmony_ci	dev->master.reset_bus	= &ds9490r_reset;
9618c2ecf20Sopenharmony_ci	dev->master.set_pullup	= &ds9490r_set_pullup;
9628c2ecf20Sopenharmony_ci	dev->master.search	= &ds9490r_search;
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	return w1_add_master_device(&dev->master);
9658c2ecf20Sopenharmony_ci}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_cistatic void ds_w1_fini(struct ds_device *dev)
9688c2ecf20Sopenharmony_ci{
9698c2ecf20Sopenharmony_ci	w1_remove_master_device(&dev->master);
9708c2ecf20Sopenharmony_ci}
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_cistatic int ds_probe(struct usb_interface *intf,
9738c2ecf20Sopenharmony_ci		    const struct usb_device_id *udev_id)
9748c2ecf20Sopenharmony_ci{
9758c2ecf20Sopenharmony_ci	struct usb_device *udev = interface_to_usbdev(intf);
9768c2ecf20Sopenharmony_ci	struct usb_endpoint_descriptor *endpoint;
9778c2ecf20Sopenharmony_ci	struct usb_host_interface *iface_desc;
9788c2ecf20Sopenharmony_ci	struct ds_device *dev;
9798c2ecf20Sopenharmony_ci	int i, err, alt;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	dev = kzalloc(sizeof(struct ds_device), GFP_KERNEL);
9828c2ecf20Sopenharmony_ci	if (!dev) {
9838c2ecf20Sopenharmony_ci		pr_info("Failed to allocate new DS9490R structure.\n");
9848c2ecf20Sopenharmony_ci		return -ENOMEM;
9858c2ecf20Sopenharmony_ci	}
9868c2ecf20Sopenharmony_ci	dev->udev = usb_get_dev(udev);
9878c2ecf20Sopenharmony_ci	if (!dev->udev) {
9888c2ecf20Sopenharmony_ci		err = -ENOMEM;
9898c2ecf20Sopenharmony_ci		goto err_out_free;
9908c2ecf20Sopenharmony_ci	}
9918c2ecf20Sopenharmony_ci	memset(dev->ep, 0, sizeof(dev->ep));
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, dev);
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	err = usb_reset_configuration(dev->udev);
9968c2ecf20Sopenharmony_ci	if (err) {
9978c2ecf20Sopenharmony_ci		dev_err(&dev->udev->dev,
9988c2ecf20Sopenharmony_ci			"Failed to reset configuration: err=%d.\n", err);
9998c2ecf20Sopenharmony_ci		goto err_out_clear;
10008c2ecf20Sopenharmony_ci	}
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci	/* alternative 3, 1ms interrupt (greatly speeds search), 64 byte bulk */
10038c2ecf20Sopenharmony_ci	alt = 3;
10048c2ecf20Sopenharmony_ci	err = usb_set_interface(dev->udev,
10058c2ecf20Sopenharmony_ci		intf->cur_altsetting->desc.bInterfaceNumber, alt);
10068c2ecf20Sopenharmony_ci	if (err) {
10078c2ecf20Sopenharmony_ci		dev_err(&dev->udev->dev, "Failed to set alternative setting %d "
10088c2ecf20Sopenharmony_ci			"for %d interface: err=%d.\n", alt,
10098c2ecf20Sopenharmony_ci			intf->cur_altsetting->desc.bInterfaceNumber, err);
10108c2ecf20Sopenharmony_ci		goto err_out_clear;
10118c2ecf20Sopenharmony_ci	}
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	iface_desc = intf->cur_altsetting;
10148c2ecf20Sopenharmony_ci	if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
10158c2ecf20Sopenharmony_ci		pr_info("Num endpoints=%d. It is not DS9490R.\n",
10168c2ecf20Sopenharmony_ci			iface_desc->desc.bNumEndpoints);
10178c2ecf20Sopenharmony_ci		err = -EINVAL;
10188c2ecf20Sopenharmony_ci		goto err_out_clear;
10198c2ecf20Sopenharmony_ci	}
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	/*
10228c2ecf20Sopenharmony_ci	 * This loop doesn'd show control 0 endpoint,
10238c2ecf20Sopenharmony_ci	 * so we will fill only 1-3 endpoints entry.
10248c2ecf20Sopenharmony_ci	 */
10258c2ecf20Sopenharmony_ci	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
10268c2ecf20Sopenharmony_ci		endpoint = &iface_desc->endpoint[i].desc;
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci		dev->ep[i+1] = endpoint->bEndpointAddress;
10298c2ecf20Sopenharmony_ci#if 0
10308c2ecf20Sopenharmony_ci		printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
10318c2ecf20Sopenharmony_ci			i, endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize),
10328c2ecf20Sopenharmony_ci			(endpoint->bEndpointAddress & USB_DIR_IN)?"IN":"OUT",
10338c2ecf20Sopenharmony_ci			endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
10348c2ecf20Sopenharmony_ci#endif
10358c2ecf20Sopenharmony_ci	}
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	err = ds_w1_init(dev);
10388c2ecf20Sopenharmony_ci	if (err)
10398c2ecf20Sopenharmony_ci		goto err_out_clear;
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	mutex_lock(&ds_mutex);
10428c2ecf20Sopenharmony_ci	list_add_tail(&dev->ds_entry, &ds_devices);
10438c2ecf20Sopenharmony_ci	mutex_unlock(&ds_mutex);
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	return 0;
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_cierr_out_clear:
10488c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, NULL);
10498c2ecf20Sopenharmony_ci	usb_put_dev(dev->udev);
10508c2ecf20Sopenharmony_cierr_out_free:
10518c2ecf20Sopenharmony_ci	kfree(dev);
10528c2ecf20Sopenharmony_ci	return err;
10538c2ecf20Sopenharmony_ci}
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_cistatic void ds_disconnect(struct usb_interface *intf)
10568c2ecf20Sopenharmony_ci{
10578c2ecf20Sopenharmony_ci	struct ds_device *dev;
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	dev = usb_get_intfdata(intf);
10608c2ecf20Sopenharmony_ci	if (!dev)
10618c2ecf20Sopenharmony_ci		return;
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci	mutex_lock(&ds_mutex);
10648c2ecf20Sopenharmony_ci	list_del(&dev->ds_entry);
10658c2ecf20Sopenharmony_ci	mutex_unlock(&ds_mutex);
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	ds_w1_fini(dev);
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, NULL);
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	usb_put_dev(dev->udev);
10728c2ecf20Sopenharmony_ci	kfree(dev);
10738c2ecf20Sopenharmony_ci}
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_cistatic const struct usb_device_id ds_id_table[] = {
10768c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x04fa, 0x2490) },
10778c2ecf20Sopenharmony_ci	{ },
10788c2ecf20Sopenharmony_ci};
10798c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, ds_id_table);
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_cistatic struct usb_driver ds_driver = {
10828c2ecf20Sopenharmony_ci	.name =		"DS9490R",
10838c2ecf20Sopenharmony_ci	.probe =	ds_probe,
10848c2ecf20Sopenharmony_ci	.disconnect =	ds_disconnect,
10858c2ecf20Sopenharmony_ci	.id_table =	ds_id_table,
10868c2ecf20Sopenharmony_ci};
10878c2ecf20Sopenharmony_cimodule_usb_driver(ds_driver);
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ciMODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
10908c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("DS2490 USB <-> W1 bus master driver (DS9490*)");
10918c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1092