ds2490.c 22.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 *	dscore.c
Linus Torvalds's avatar
Linus Torvalds committed
3
4
 *
 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5
 *
Linus Torvalds's avatar
Linus Torvalds committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mod_devicetable.h>
#include <linux/usb.h>

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "../w1_int.h"
#include "../w1.h"

/* 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
#define COMM_NTP			0x0400
#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

110
111
112
113
114
115
116
117
118
119
120
/* 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 */

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#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

struct ds_device
{
	struct list_head	ds_entry;

	struct usb_device	*udev;
	struct usb_interface	*intf;

	int			ep[NUM_EP];

140
141
142
143
144
	/* Strong PullUp
	 * 0: pullup not active, else duration in milliseconds
	 */
	int			spu_sleep;

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
	struct w1_bus_master	master;
};

struct ds_status
{
	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;

};
Linus Torvalds's avatar
Linus Torvalds committed
168
169
170
171
172
173
174

static struct usb_device_id ds_id_table [] = {
	{ USB_DEVICE(0x04fa, 0x2490) },
	{ },
};
MODULE_DEVICE_TABLE(usb, ds_id_table);

175
176
static int ds_probe(struct usb_interface *, const struct usb_device_id *);
static void ds_disconnect(struct usb_interface *);
Linus Torvalds's avatar
Linus Torvalds committed
177
178
179
180

static int ds_send_control(struct ds_device *, u16, u16);
static int ds_send_control_cmd(struct ds_device *, u16, u16);

181
static LIST_HEAD(ds_devices);
182
static DEFINE_MUTEX(ds_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
183
184
185
186
187
188
189
190
191
192
193

static struct usb_driver ds_driver = {
	.name =		"DS9490R",
	.probe =	ds_probe,
	.disconnect =	ds_disconnect,
	.id_table =	ds_id_table,
};

static int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index)
{
	int err;
194
195

	err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
Linus Torvalds's avatar
Linus Torvalds committed
196
197
			CONTROL_CMD, 0x40, value, index, NULL, 0, 1000);
	if (err < 0) {
198
		printk(KERN_ERR "Failed to send command control message %x.%x: err=%d.\n",
Linus Torvalds's avatar
Linus Torvalds committed
199
200
201
202
203
204
				value, index, err);
		return err;
	}

	return err;
}
205

Linus Torvalds's avatar
Linus Torvalds committed
206
207
208
static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
{
	int err;
209
210

	err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
Linus Torvalds's avatar
Linus Torvalds committed
211
212
			MODE_CMD, 0x40, value, index, NULL, 0, 1000);
	if (err < 0) {
213
		printk(KERN_ERR "Failed to send mode control message %x.%x: err=%d.\n",
Linus Torvalds's avatar
Linus Torvalds committed
214
215
216
217
218
219
				value, index, err);
		return err;
	}

	return err;
}
220

Linus Torvalds's avatar
Linus Torvalds committed
221
222
223
static int ds_send_control(struct ds_device *dev, u16 value, u16 index)
{
	int err;
224
225

	err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
Linus Torvalds's avatar
Linus Torvalds committed
226
227
			COMM_CMD, 0x40, value, index, NULL, 0, 1000);
	if (err < 0) {
228
		printk(KERN_ERR "Failed to send control message %x.%x: err=%d.\n",
Linus Torvalds's avatar
Linus Torvalds committed
229
230
231
232
233
234
235
				value, index, err);
		return err;
	}

	return err;
}

236
237
static int ds_recv_status_nodump(struct ds_device *dev, struct ds_status *st,
				 unsigned char *buf, int size)
Linus Torvalds's avatar
Linus Torvalds committed
238
239
{
	int count, err;
240

Li Zefan's avatar
Li Zefan committed
241
	memset(st, 0, sizeof(*st));
242

Linus Torvalds's avatar
Linus Torvalds committed
243
244
245
246
247
248
	count = 0;
	err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_STATUS]), buf, size, &count, 100);
	if (err < 0) {
		printk(KERN_ERR "Failed to read 1-wire data from 0x%x: err=%d.\n", dev->ep[EP_STATUS], err);
		return err;
	}
249

Linus Torvalds's avatar
Linus Torvalds committed
250
251
252
253
254
255
	if (count >= sizeof(*st))
		memcpy(st, buf, sizeof(*st));

	return count;
}

256
static inline void ds_print_msg(unsigned char *buf, unsigned char *str, int off)
Linus Torvalds's avatar
Linus Torvalds committed
257
{
258
259
	printk(KERN_INFO "%45s: %8x\n", str, buf[off]);
}
260

261
262
263
static void ds_dump_status(struct ds_device *dev, unsigned char *buf, int count)
{
	int i;
264

265
	printk(KERN_INFO "0x%x: count=%d, status: ", dev->ep[EP_STATUS], count);
Linus Torvalds's avatar
Linus Torvalds committed
266
267
	for (i=0; i<count; ++i)
		printk("%02x ", buf[i]);
268
	printk(KERN_INFO "\n");
Linus Torvalds's avatar
Linus Torvalds committed
269
270

	if (count >= 16) {
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
		ds_print_msg(buf, "enable flag", 0);
		ds_print_msg(buf, "1-wire speed", 1);
		ds_print_msg(buf, "strong pullup duration", 2);
		ds_print_msg(buf, "programming pulse duration", 3);
		ds_print_msg(buf, "pulldown slew rate control", 4);
		ds_print_msg(buf, "write-1 low time", 5);
		ds_print_msg(buf, "data sample offset/write-0 recovery time",
			6);
		ds_print_msg(buf, "reserved (test register)", 7);
		ds_print_msg(buf, "device status flags", 8);
		ds_print_msg(buf, "communication command byte 1", 9);
		ds_print_msg(buf, "communication command byte 2", 10);
		ds_print_msg(buf, "communication command buffer status", 11);
		ds_print_msg(buf, "1-wire data output buffer status", 12);
		ds_print_msg(buf, "1-wire data input buffer status", 13);
		ds_print_msg(buf, "reserved", 14);
		ds_print_msg(buf, "reserved", 15);
Linus Torvalds's avatar
Linus Torvalds committed
288
	}
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
	for (i = 16; i < count; ++i) {
		if (buf[i] == RR_DETECT) {
			ds_print_msg(buf, "new device detect", i);
			continue;
		}
		ds_print_msg(buf, "Result Register Value: ", i);
		if (buf[i] & RR_NRS)
			printk(KERN_INFO "NRS: Reset no presence or ...\n");
		if (buf[i] & RR_SH)
			printk(KERN_INFO "SH: short on reset or set path\n");
		if (buf[i] & RR_APP)
			printk(KERN_INFO "APP: alarming presence on reset\n");
		if (buf[i] & RR_VPP)
			printk(KERN_INFO "VPP: 12V expected not seen\n");
		if (buf[i] & RR_CMP)
			printk(KERN_INFO "CMP: compare error\n");
		if (buf[i] & RR_CRC)
			printk(KERN_INFO "CRC: CRC error detected\n");
		if (buf[i] & RR_RDP)
			printk(KERN_INFO "RDP: redirected page\n");
		if (buf[i] & RR_EOS)
			printk(KERN_INFO "EOS: end of search error\n");
Linus Torvalds's avatar
Linus Torvalds committed
311
312
313
314
315
316
317
	}
}

static int ds_recv_data(struct ds_device *dev, unsigned char *buf, int size)
{
	int count, err;
	struct ds_status st;
318

319
320
321
322
323
324
325
326
327
	/* 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.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
328
	count = 0;
329
	err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]),
Linus Torvalds's avatar
Linus Torvalds committed
330
331
				buf, size, &count, 1000);
	if (err < 0) {
332
333
334
		u8 buf[0x20];
		int count;

Linus Torvalds's avatar
Linus Torvalds committed
335
336
		printk(KERN_INFO "Clearing ep0x%x.\n", dev->ep[EP_DATA_IN]);
		usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]));
337
338
339

		count = ds_recv_status_nodump(dev, &st, buf, sizeof(buf));
		ds_dump_status(dev, buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
		return err;
	}

#if 0
	{
		int i;

		printk("%s: count=%d: ", __func__, count);
		for (i=0; i<count; ++i)
			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;
359

Linus Torvalds's avatar
Linus Torvalds committed
360
361
362
	count = 0;
	err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000);
	if (err < 0) {
363
364
		printk(KERN_ERR "Failed to write 1-wire data to ep0x%x: "
			"err=%d.\n", dev->ep[EP_DATA_OUT], err);
Linus Torvalds's avatar
Linus Torvalds committed
365
366
367
368
369
370
		return err;
	}

	return err;
}

371
372
#if 0

Linus Torvalds's avatar
Linus Torvalds committed
373
374
375
376
377
int ds_stop_pulse(struct ds_device *dev, int limit)
{
	struct ds_status st;
	int count = 0, err = 0;
	u8 buf[0x20];
378

Linus Torvalds's avatar
Linus Torvalds committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
	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;
		err = ds_recv_status_nodump(dev, &st, buf, sizeof(buf));
		if (err)
			break;

		if ((st.status & ST_SPUA) == 0) {
			err = ds_send_control_mode(dev, MOD_PULSE_EN, 0);
			if (err)
				break;
		}
	} while(++count < limit);

	return err;
}

int ds_detect(struct ds_device *dev, struct ds_status *st)
{
	int err;
403

Linus Torvalds's avatar
Linus Torvalds committed
404
405
406
407
408
409
410
	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;
411

Linus Torvalds's avatar
Linus Torvalds committed
412
413
414
	err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40);
	if (err)
		return err;
415

Linus Torvalds's avatar
Linus Torvalds committed
416
417
418
419
	err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG);
	if (err)
		return err;

420
	err = ds_dump_status(dev, st);
Linus Torvalds's avatar
Linus Torvalds committed
421
422
423
424

	return err;
}

425
426
427
#endif  /*  0  */

static int ds_wait_status(struct ds_device *dev, struct ds_status *st)
Linus Torvalds's avatar
Linus Torvalds committed
428
429
430
431
432
433
434
{
	u8 buf[0x20];
	int err, count = 0;

	do {
		err = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
#if 0
435
		if (err >= 0) {
Linus Torvalds's avatar
Linus Torvalds committed
436
437
438
439
440
441
442
443
444
			int i;
			printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], err);
			for (i=0; i<err; ++i)
				printk("%02x ", buf[i]);
			printk("\n");
		}
#endif
	} while(!(buf[0x08] & 0x20) && !(err < 0) && ++count < 100);

445
446
447
448
449
450
	if (err >= 16 && st->status & ST_EPOF) {
		printk(KERN_INFO "Resetting device after ST_EPOF.\n");
		ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
		/* Always dump the device status. */
		count = 101;
	}
Linus Torvalds's avatar
Linus Torvalds committed
451

452
453
454
455
456
457
458
459
460
461
462
463
	/* 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)
		ds_dump_status(dev, buf, err);

	/* 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)
Linus Torvalds's avatar
Linus Torvalds committed
464
		return -1;
465
	else
Linus Torvalds's avatar
Linus Torvalds committed
466
467
468
		return 0;
}

469
static int ds_reset(struct ds_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
470
471
472
473
474
475
476
477
478
479
480
{
	int err;

	//err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_F | COMM_IM | COMM_SE, SPEED_FLEXIBLE);
	err = ds_send_control(dev, 0x43, SPEED_NORMAL);
	if (err)
		return err;

	return 0;
}

481
#if 0
482
static int ds_set_speed(struct ds_device *dev, int speed)
Linus Torvalds's avatar
Linus Torvalds committed
483
484
{
	int err;
485

Linus Torvalds's avatar
Linus Torvalds committed
486
487
488
489
490
491
492
	if (speed != SPEED_NORMAL && speed != SPEED_FLEXIBLE && speed != SPEED_OVERDRIVE)
		return -EINVAL;

	if (speed != SPEED_OVERDRIVE)
		speed = SPEED_FLEXIBLE;

	speed &= 0xff;
493

Linus Torvalds's avatar
Linus Torvalds committed
494
495
496
497
498
499
	err = ds_send_control_mode(dev, MOD_1WIRE_SPEED, speed);
	if (err)
		return err;

	return err;
}
500
#endif  /*  0  */
Linus Torvalds's avatar
Linus Torvalds committed
501

502
static int ds_set_pullup(struct ds_device *dev, int delay)
Linus Torvalds's avatar
Linus Torvalds committed
503
504
505
506
{
	int err;
	u8 del = 1 + (u8)(delay >> 4);

507
508
	dev->spu_sleep = 0;
	err = ds_send_control_mode(dev, MOD_PULSE_EN, delay ? PULSE_SPUE : 0);
Linus Torvalds's avatar
Linus Torvalds committed
509
510
511
	if (err)
		return err;

512
513
514
515
	if (delay) {
		err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del);
		if (err)
			return err;
Linus Torvalds's avatar
Linus Torvalds committed
516

517
518
519
520
521
		/* Just storing delay would not get the trunication and
		 * roundup.
		 */
		dev->spu_sleep = del<<4;
	}
522

Linus Torvalds's avatar
Linus Torvalds committed
523
524
525
	return err;
}

526
static int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
Linus Torvalds's avatar
Linus Torvalds committed
527
{
528
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
529
	struct ds_status st;
530

531
532
	err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit ? COMM_D : 0),
		0);
Linus Torvalds's avatar
Linus Torvalds committed
533
534
535
	if (err)
		return err;

536
	ds_wait_status(dev, &st);
Linus Torvalds's avatar
Linus Torvalds committed
537
538
539
540
541
542
543
544

	err = ds_recv_data(dev, tbit, sizeof(*tbit));
	if (err < 0)
		return err;

	return 0;
}

545
#if 0
546
static int ds_write_bit(struct ds_device *dev, u8 bit)
Linus Torvalds's avatar
Linus Torvalds committed
547
548
549
{
	int err;
	struct ds_status st;
550

551
552
553
554
555
556
	/* 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);
Linus Torvalds's avatar
Linus Torvalds committed
557
558
559
560
561
562
563
	if (err)
		return err;

	ds_wait_status(dev, &st);

	return 0;
}
564
#endif
Linus Torvalds's avatar
Linus Torvalds committed
565

566
static int ds_write_byte(struct ds_device *dev, u8 byte)
Linus Torvalds's avatar
Linus Torvalds committed
567
568
569
570
{
	int err;
	struct ds_status st;
	u8 rbyte;
571

Linus Torvalds's avatar
Linus Torvalds committed
572
573
574
575
	err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM | COMM_SPU, byte);
	if (err)
		return err;

576
577
578
	if (dev->spu_sleep)
		msleep(dev->spu_sleep);

Linus Torvalds's avatar
Linus Torvalds committed
579
580
581
	err = ds_wait_status(dev, &st);
	if (err)
		return err;
582

Linus Torvalds's avatar
Linus Torvalds committed
583
584
585
	err = ds_recv_data(dev, &rbyte, sizeof(rbyte));
	if (err < 0)
		return err;
586

Linus Torvalds's avatar
Linus Torvalds committed
587
588
589
	return !(byte == rbyte);
}

590
static int ds_read_byte(struct ds_device *dev, u8 *byte)
Linus Torvalds's avatar
Linus Torvalds committed
591
592
593
594
595
596
597
598
599
{
	int err;
	struct ds_status st;

	err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM , 0xff);
	if (err)
		return err;

	ds_wait_status(dev, &st);
600

Linus Torvalds's avatar
Linus Torvalds committed
601
602
603
604
605
606
607
	err = ds_recv_data(dev, byte, sizeof(*byte));
	if (err < 0)
		return err;

	return 0;
}

608
static int ds_read_block(struct ds_device *dev, u8 *buf, int len)
Linus Torvalds's avatar
Linus Torvalds committed
609
610
611
612
613
614
615
616
{
	struct ds_status st;
	int err;

	if (len > 64*1024)
		return -E2BIG;

	memset(buf, 0xFF, len);
617

Linus Torvalds's avatar
Linus Torvalds committed
618
619
620
	err = ds_send_data(dev, buf, len);
	if (err < 0)
		return err;
621

622
	err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM, len);
Linus Torvalds's avatar
Linus Torvalds committed
623
624
625
626
	if (err)
		return err;

	ds_wait_status(dev, &st);
627

Linus Torvalds's avatar
Linus Torvalds committed
628
629
630
631
632
633
	memset(buf, 0x00, len);
	err = ds_recv_data(dev, buf, len);

	return err;
}

634
static int ds_write_block(struct ds_device *dev, u8 *buf, int len)
Linus Torvalds's avatar
Linus Torvalds committed
635
636
637
{
	int err;
	struct ds_status st;
638

Linus Torvalds's avatar
Linus Torvalds committed
639
640
641
	err = ds_send_data(dev, buf, len);
	if (err < 0)
		return err;
642

Linus Torvalds's avatar
Linus Torvalds committed
643
644
645
646
647
648
	ds_wait_status(dev, &st);

	err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | COMM_SPU, len);
	if (err)
		return err;

649
650
651
	if (dev->spu_sleep)
		msleep(dev->spu_sleep);

Linus Torvalds's avatar
Linus Torvalds committed
652
653
654
655
656
657
658
659
660
	ds_wait_status(dev, &st);

	err = ds_recv_data(dev, buf, len);
	if (err < 0)
		return err;

	return !(err == len);
}

661
662
#if 0

663
static int ds_search(struct ds_device *dev, u64 init, u64 *buf, u8 id_number, int conditional_search)
Linus Torvalds's avatar
Linus Torvalds committed
664
665
666
667
668
669
{
	int err;
	u16 value, index;
	struct ds_status st;

	memset(buf, 0, sizeof(buf));
670

Linus Torvalds's avatar
Linus Torvalds committed
671
672
673
	err = ds_send_data(ds_dev, (unsigned char *)&init, 8);
	if (err)
		return err;
674

Linus Torvalds's avatar
Linus Torvalds committed
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
	ds_wait_status(ds_dev, &st);

	value = COMM_SEARCH_ACCESS | COMM_IM | COMM_SM | COMM_F | COMM_RTS;
	index = (conditional_search ? 0xEC : 0xF0) | (id_number << 8);
	err = ds_send_control(ds_dev, value, index);
	if (err)
		return err;

	ds_wait_status(ds_dev, &st);

	err = ds_recv_data(ds_dev, (unsigned char *)buf, 8*id_number);
	if (err < 0)
		return err;

	return err/8;
}

692
static int ds_match_access(struct ds_device *dev, u64 init)
Linus Torvalds's avatar
Linus Torvalds committed
693
694
695
696
697
698
699
{
	int err;
	struct ds_status st;

	err = ds_send_data(dev, (unsigned char *)&init, sizeof(init));
	if (err)
		return err;
700

Linus Torvalds's avatar
Linus Torvalds committed
701
702
703
704
705
706
707
708
709
710
711
	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;
}

712
static int ds_set_path(struct ds_device *dev, u64 init)
Linus Torvalds's avatar
Linus Torvalds committed
713
714
715
716
717
718
719
{
	int err;
	struct ds_status st;
	u8 buf[9];

	memcpy(buf, &init, 8);
	buf[8] = BRANCH_MAIN;
720

Linus Torvalds's avatar
Linus Torvalds committed
721
722
723
	err = ds_send_data(dev, buf, sizeof(buf));
	if (err)
		return err;
724

Linus Torvalds's avatar
Linus Torvalds committed
725
726
727
728
729
730
731
732
733
734
735
	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;
}

736
737
#endif  /*  0  */

738
739
740
741
742
743
744
745
746
747
748
static u8 ds9490r_touch_bit(void *data, u8 bit)
{
	u8 ret;
	struct ds_device *dev = data;

	if (ds_touch_bit(dev, bit, &ret))
		return 0;

	return ret;
}

749
#if 0
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
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;
	u8 bit = 0;

	err = ds_touch_bit(dev, 1, &bit);
	if (err)
		return 0;

	return bit & 1;
}
769
770
771
772
773
774
775
776
#endif

static void ds9490r_write_byte(void *data, u8 byte)
{
	struct ds_device *dev = data;

	ds_write_byte(dev, byte);
}
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814

static u8 ds9490r_read_byte(void *data)
{
	struct ds_device *dev = data;
	int err;
	u8 byte = 0;

	err = ds_read_byte(dev, &byte);
	if (err)
		return 0;

	return byte;
}

static void ds9490r_write_block(void *data, const u8 *buf, int len)
{
	struct ds_device *dev = data;

	ds_write_block(dev, (u8 *)buf, len);
}

static u8 ds9490r_read_block(void *data, u8 *buf, int len)
{
	struct ds_device *dev = data;
	int err;

	err = ds_read_block(dev, buf, len);
	if (err < 0)
		return 0;

	return len;
}

static u8 ds9490r_reset(void *data)
{
	struct ds_device *dev = data;
	int err;

815
	err = ds_reset(dev);
816
817
818
819
820
821
	if (err)
		return 1;

	return 0;
}

822
823
824
825
826
827
828
829
830
831
static u8 ds9490r_set_pullup(void *data, int delay)
{
	struct ds_device *dev = data;

	if (ds_set_pullup(dev, delay))
		return 1;

	return 0;
}

832
833
834
835
static int ds_w1_init(struct ds_device *dev)
{
	memset(&dev->master, 0, sizeof(struct w1_bus_master));

836
837
838
839
840
841
842
843
844
845
846
847
	/* 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.
	 */
	ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);

848
849
	dev->master.data	= dev;
	dev->master.touch_bit	= &ds9490r_touch_bit;
850
851
852
853
854
855
	/* 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.
856
857
	dev->master.read_bit	= &ds9490r_read_bit;
	dev->master.write_bit	= &ds9490r_write_bit;
858
	*/
859
860
861
862
863
	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;
864
	dev->master.set_pullup	= &ds9490r_set_pullup;
865
866
867
868
869
870
871
872
873

	return w1_add_master_device(&dev->master);
}

static void ds_w1_fini(struct ds_device *dev)
{
	w1_remove_master_device(&dev->master);
}

874
875
static int ds_probe(struct usb_interface *intf,
		    const struct usb_device_id *udev_id)
Linus Torvalds's avatar
Linus Torvalds committed
876
877
878
879
{
	struct usb_device *udev = interface_to_usbdev(intf);
	struct usb_endpoint_descriptor *endpoint;
	struct usb_host_interface *iface_desc;
880
	struct ds_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
881
882
	int i, err;

883
884
	dev = kmalloc(sizeof(struct ds_device), GFP_KERNEL);
	if (!dev) {
Linus Torvalds's avatar
Linus Torvalds committed
885
886
887
		printk(KERN_INFO "Failed to allocate new DS9490R structure.\n");
		return -ENOMEM;
	}
888
	dev->spu_sleep = 0;
889
890
891
892
893
894
	dev->udev = usb_get_dev(udev);
	if (!dev->udev) {
		err = -ENOMEM;
		goto err_out_free;
	}
	memset(dev->ep, 0, sizeof(dev->ep));
Linus Torvalds's avatar
Linus Torvalds committed
895

896
	usb_set_intfdata(intf, dev);
Linus Torvalds's avatar
Linus Torvalds committed
897

898
	err = usb_set_interface(dev->udev, intf->altsetting[0].desc.bInterfaceNumber, 3);
Linus Torvalds's avatar
Linus Torvalds committed
899
900
901
	if (err) {
		printk(KERN_ERR "Failed to set alternative setting 3 for %d interface: err=%d.\n",
				intf->altsetting[0].desc.bInterfaceNumber, err);
902
		goto err_out_clear;
Linus Torvalds's avatar
Linus Torvalds committed
903
904
	}

905
	err = usb_reset_configuration(dev->udev);
Linus Torvalds's avatar
Linus Torvalds committed
906
907
	if (err) {
		printk(KERN_ERR "Failed to reset configuration: err=%d.\n", err);
908
		goto err_out_clear;
Linus Torvalds's avatar
Linus Torvalds committed
909
	}
910

Linus Torvalds's avatar
Linus Torvalds committed
911
912
913
	iface_desc = &intf->altsetting[0];
	if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
		printk(KERN_INFO "Num endpoints=%d. It is not DS9490R.\n", iface_desc->desc.bNumEndpoints);
914
915
		err = -EINVAL;
		goto err_out_clear;
Linus Torvalds's avatar
Linus Torvalds committed
916
917
918
	}

	/*
919
	 * This loop doesn'd show control 0 endpoint,
Linus Torvalds's avatar
Linus Torvalds committed
920
921
922
923
924
	 * so we will fill only 1-3 endpoints entry.
	 */
	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
		endpoint = &iface_desc->endpoint[i].desc;

925
926
		dev->ep[i+1] = endpoint->bEndpointAddress;
#if 0
Linus Torvalds's avatar
Linus Torvalds committed
927
928
929
930
		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);
931
#endif
Linus Torvalds's avatar
Linus Torvalds committed
932
	}
933

934
935
936
	err = ds_w1_init(dev);
	if (err)
		goto err_out_clear;
937

938
	mutex_lock(&ds_mutex);
939
	list_add_tail(&dev->ds_entry, &ds_devices);
940
	mutex_unlock(&ds_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
941
942

	return 0;
943
944
945
946
947
948
949

err_out_clear:
	usb_set_intfdata(intf, NULL);
	usb_put_dev(dev->udev);
err_out_free:
	kfree(dev);
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
950
951
}

952
static void ds_disconnect(struct usb_interface *intf)
Linus Torvalds's avatar
Linus Torvalds committed
953
954
{
	struct ds_device *dev;
955

Linus Torvalds's avatar
Linus Torvalds committed
956
	dev = usb_get_intfdata(intf);
957
958
	if (!dev)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
959

960
	mutex_lock(&ds_mutex);
961
	list_del(&dev->ds_entry);
962
	mutex_unlock(&ds_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
963

964
965
966
	ds_w1_fini(dev);

	usb_set_intfdata(intf, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
967
968
969
970
971

	usb_put_dev(dev->udev);
	kfree(dev);
}

972
static int ds_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
973
974
975
976
977
978
979
980
981
982
983
984
{
	int err;

	err = usb_register(&ds_driver);
	if (err) {
		printk(KERN_INFO "Failed to register DS9490R USB device: err=%d.\n", err);
		return err;
	}

	return 0;
}

985
static void ds_fini(void)
Linus Torvalds's avatar
Linus Torvalds committed
986
987
988
989
990
991
992
993
994
{
	usb_deregister(&ds_driver);
}

module_init(ds_init);
module_exit(ds_fini);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
995
MODULE_DESCRIPTION("DS2490 USB <-> W1 bus master driver (DS9490*)");