dscore.c 16.9 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *	dscore.c
L
Linus Torvalds 已提交
3 4
 *
 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5
 *
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * 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>

#include "dscore.h"

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

35 36
static int ds_probe(struct usb_interface *, const struct usb_device_id *);
static void ds_disconnect(struct usb_interface *);
L
Linus Torvalds 已提交
37 38 39 40 41 42

int ds_touch_bit(struct ds_device *, u8, u8 *);
int ds_read_byte(struct ds_device *, u8 *);
int ds_read_bit(struct ds_device *, u8 *);
int ds_write_byte(struct ds_device *, u8);
int ds_write_bit(struct ds_device *, u8);
43
static int ds_start_pulse(struct ds_device *, int);
L
Linus Torvalds 已提交
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
int ds_reset(struct ds_device *, struct ds_status *);
struct ds_device * ds_get_device(void);
void ds_put_device(struct ds_device *);

static inline void ds_dump_status(unsigned char *, unsigned char *, int);
static int ds_send_control(struct ds_device *, u16, u16);
static int ds_send_control_mode(struct ds_device *, u16, u16);
static int ds_send_control_cmd(struct ds_device *, u16, u16);


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

static struct ds_device *ds_dev;

struct ds_device * ds_get_device(void)
{
	if (ds_dev)
		atomic_inc(&ds_dev->refcnt);
	return ds_dev;
}

void ds_put_device(struct ds_device *dev)
{
	atomic_dec(&dev->refcnt);
}

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

	err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
L
Linus Torvalds 已提交
80 81
			CONTROL_CMD, 0x40, value, index, NULL, 0, 1000);
	if (err < 0) {
82
		printk(KERN_ERR "Failed to send command control message %x.%x: err=%d.\n",
L
Linus Torvalds 已提交
83 84 85 86 87 88 89 90 91 92
				value, index, err);
		return err;
	}

	return err;
}

static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
{
	int err;
93 94

	err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
L
Linus Torvalds 已提交
95 96
			MODE_CMD, 0x40, value, index, NULL, 0, 1000);
	if (err < 0) {
97
		printk(KERN_ERR "Failed to send mode control message %x.%x: err=%d.\n",
L
Linus Torvalds 已提交
98 99 100 101 102 103 104 105 106 107
				value, index, err);
		return err;
	}

	return err;
}

static int ds_send_control(struct ds_device *dev, u16 value, u16 index)
{
	int err;
108 109

	err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
L
Linus Torvalds 已提交
110 111
			COMM_CMD, 0x40, value, index, NULL, 0, 1000);
	if (err < 0) {
112
		printk(KERN_ERR "Failed to send control message %x.%x: err=%d.\n",
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121 122 123 124
				value, index, err);
		return err;
	}

	return err;
}

static inline void ds_dump_status(unsigned char *buf, unsigned char *str, int off)
{
	printk("%45s: %8x\n", str, buf[off]);
}

125 126
static int ds_recv_status_nodump(struct ds_device *dev, struct ds_status *st,
				 unsigned char *buf, int size)
L
Linus Torvalds 已提交
127 128
{
	int count, err;
129

L
Linus Torvalds 已提交
130
	memset(st, 0, sizeof(st));
131

L
Linus Torvalds 已提交
132 133 134 135 136 137
	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;
	}
138

L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148
	if (count >= sizeof(*st))
		memcpy(st, buf, sizeof(*st));

	return count;
}

static int ds_recv_status(struct ds_device *dev, struct ds_status *st)
{
	unsigned char buf[64];
	int count, err = 0, i;
149

L
Linus Torvalds 已提交
150
	memcpy(st, buf, sizeof(*st));
151

L
Linus Torvalds 已提交
152 153 154
	count = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
	if (count < 0)
		return err;
155

L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], count);
	for (i=0; i<count; ++i)
		printk("%02x ", buf[i]);
	printk("\n");

	if (count >= 16) {
		ds_dump_status(buf, "enable flag", 0);
		ds_dump_status(buf, "1-wire speed", 1);
		ds_dump_status(buf, "strong pullup duration", 2);
		ds_dump_status(buf, "programming pulse duration", 3);
		ds_dump_status(buf, "pulldown slew rate control", 4);
		ds_dump_status(buf, "write-1 low time", 5);
		ds_dump_status(buf, "data sample offset/write-0 recovery time", 6);
		ds_dump_status(buf, "reserved (test register)", 7);
		ds_dump_status(buf, "device status flags", 8);
		ds_dump_status(buf, "communication command byte 1", 9);
		ds_dump_status(buf, "communication command byte 2", 10);
		ds_dump_status(buf, "communication command buffer status", 11);
		ds_dump_status(buf, "1-wire data output buffer status", 12);
		ds_dump_status(buf, "1-wire data input buffer status", 13);
		ds_dump_status(buf, "reserved", 14);
		ds_dump_status(buf, "reserved", 15);
	}

	memcpy(st, buf, sizeof(*st));

	if (st->status & ST_EPOF) {
		printk(KERN_INFO "Resetting device after ST_EPOF.\n");
		err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
		if (err)
			return err;
		count = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
		if (count < 0)
			return err;
	}
#if 0
	if (st->status & ST_IDLE) {
		printk(KERN_INFO "Resetting pulse after ST_IDLE.\n");
		err = ds_start_pulse(dev, PULLUP_PULSE_DURATION);
		if (err)
			return err;
	}
#endif
199

L
Linus Torvalds 已提交
200 201 202 203 204 205 206
	return err;
}

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

L
Linus Torvalds 已提交
208
	count = 0;
209
	err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]),
L
Linus Torvalds 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
				buf, size, &count, 1000);
	if (err < 0) {
		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]));
		ds_recv_status(dev, &st);
		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;
234

L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242 243 244
	count = 0;
	err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000);
	if (err < 0) {
		printk(KERN_ERR "Failed to read 1-wire data from 0x02: err=%d.\n", err);
		return err;
	}

	return err;
}

245 246
#if 0

L
Linus Torvalds 已提交
247 248 249 250 251
int ds_stop_pulse(struct ds_device *dev, int limit)
{
	struct ds_status st;
	int count = 0, err = 0;
	u8 buf[0x20];
252

L
Linus Torvalds 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	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;
277

L
Linus Torvalds 已提交
278 279 280 281 282 283 284
	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;
285

L
Linus Torvalds 已提交
286 287 288
	err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40);
	if (err)
		return err;
289

L
Linus Torvalds 已提交
290 291 292 293 294 295 296 297 298
	err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG);
	if (err)
		return err;

	err = ds_recv_status(dev, st);

	return err;
}

299 300 301
#endif  /*  0  */

static int ds_wait_status(struct ds_device *dev, struct ds_status *st)
L
Linus Torvalds 已提交
302 303 304 305 306 307 308
{
	u8 buf[0x20];
	int err, count = 0;

	do {
		err = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
#if 0
309
		if (err >= 0) {
L
Linus Torvalds 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322
			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);


	if (((err > 16) && (buf[0x10] & 0x01)) || count >= 100 || err < 0) {
		ds_recv_status(dev, st);
		return -1;
323
	} else
L
Linus Torvalds 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
		return 0;
}

int ds_reset(struct ds_device *dev, struct ds_status *st)
{
	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;

	ds_wait_status(dev, st);
#if 0
	if (st->command_buffer_status) {
		printk(KERN_INFO "Short circuit.\n");
		return -EIO;
	}
#endif
343

L
Linus Torvalds 已提交
344 345 346
	return 0;
}

347
#if 0
L
Linus Torvalds 已提交
348 349 350
int ds_set_speed(struct ds_device *dev, int speed)
{
	int err;
351

L
Linus Torvalds 已提交
352 353 354 355 356 357 358
	if (speed != SPEED_NORMAL && speed != SPEED_FLEXIBLE && speed != SPEED_OVERDRIVE)
		return -EINVAL;

	if (speed != SPEED_OVERDRIVE)
		speed = SPEED_FLEXIBLE;

	speed &= 0xff;
359

L
Linus Torvalds 已提交
360 361 362 363 364 365
	err = ds_send_control_mode(dev, MOD_1WIRE_SPEED, speed);
	if (err)
		return err;

	return err;
}
366
#endif  /*  0  */
L
Linus Torvalds 已提交
367

368
static int ds_start_pulse(struct ds_device *dev, int delay)
L
Linus Torvalds 已提交
369 370 371 372
{
	int err;
	u8 del = 1 + (u8)(delay >> 4);
	struct ds_status st;
373

L
Linus Torvalds 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
#if 0
	err = ds_stop_pulse(dev, 10);
	if (err)
		return err;

	err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE);
	if (err)
		return err;
#endif
	err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del);
	if (err)
		return err;

	err = ds_send_control(dev, COMM_PULSE | COMM_IM | COMM_F, 0);
	if (err)
		return err;

	mdelay(delay);

	ds_wait_status(dev, &st);
394

L
Linus Torvalds 已提交
395 396 397 398 399 400 401 402 403
	return err;
}

int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
{
	int err, count;
	struct ds_status st;
	u16 value = (COMM_BIT_IO | COMM_IM) | ((bit) ? COMM_D : 0);
	u16 cmd;
404

L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	err = ds_send_control(dev, value, 0);
	if (err)
		return err;

	count = 0;
	do {
		err = ds_wait_status(dev, &st);
		if (err)
			return err;

		cmd = st.command0 | (st.command1 << 8);
	} while (cmd != value && ++count < 10);

	if (err < 0 || count >= 10) {
		printk(KERN_ERR "Failed to obtain status.\n");
		return -EINVAL;
	}

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

	return 0;
}

int ds_write_bit(struct ds_device *dev, u8 bit)
{
	int err;
	struct ds_status st;
434

L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448
	err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit) ? COMM_D : 0, 0);
	if (err)
		return err;

	ds_wait_status(dev, &st);

	return 0;
}

int ds_write_byte(struct ds_device *dev, u8 byte)
{
	int err;
	struct ds_status st;
	u8 rbyte;
449

L
Linus Torvalds 已提交
450 451 452 453 454 455 456
	err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM | COMM_SPU, byte);
	if (err)
		return err;

	err = ds_wait_status(dev, &st);
	if (err)
		return err;
457

L
Linus Torvalds 已提交
458 459 460
	err = ds_recv_data(dev, &rbyte, sizeof(rbyte));
	if (err < 0)
		return err;
461

L
Linus Torvalds 已提交
462 463 464 465 466 467 468 469 470 471 472 473
	ds_start_pulse(dev, PULLUP_PULSE_DURATION);

	return !(byte == rbyte);
}

int ds_read_bit(struct ds_device *dev, u8 *bit)
{
	int err;

	err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE);
	if (err)
		return err;
474

L
Linus Torvalds 已提交
475 476 477
	err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_SPU | COMM_D, 0);
	if (err)
		return err;
478

L
Linus Torvalds 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
	err = ds_recv_data(dev, bit, sizeof(*bit));
	if (err < 0)
		return err;

	return 0;
}

int ds_read_byte(struct ds_device *dev, u8 *byte)
{
	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);
496

L
Linus Torvalds 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	err = ds_recv_data(dev, byte, sizeof(*byte));
	if (err < 0)
		return err;

	return 0;
}

int ds_read_block(struct ds_device *dev, u8 *buf, int len)
{
	struct ds_status st;
	int err;

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

	memset(buf, 0xFF, len);
513

L
Linus Torvalds 已提交
514 515 516
	err = ds_send_data(dev, buf, len);
	if (err < 0)
		return err;
517

L
Linus Torvalds 已提交
518 519 520 521 522
	err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | COMM_SPU, len);
	if (err)
		return err;

	ds_wait_status(dev, &st);
523

L
Linus Torvalds 已提交
524 525 526 527 528 529 530 531 532 533
	memset(buf, 0x00, len);
	err = ds_recv_data(dev, buf, len);

	return err;
}

int ds_write_block(struct ds_device *dev, u8 *buf, int len)
{
	int err;
	struct ds_status st;
534

L
Linus Torvalds 已提交
535 536 537
	err = ds_send_data(dev, buf, len);
	if (err < 0)
		return err;
538

L
Linus Torvalds 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551
	ds_wait_status(dev, &st);

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

	ds_wait_status(dev, &st);

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

	ds_start_pulse(dev, PULLUP_PULSE_DURATION);
552

L
Linus Torvalds 已提交
553 554 555
	return !(err == len);
}

556 557
#if 0

L
Linus Torvalds 已提交
558 559 560 561 562 563 564
int ds_search(struct ds_device *dev, u64 init, u64 *buf, u8 id_number, int conditional_search)
{
	int err;
	u16 value, index;
	struct ds_status st;

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

L
Linus Torvalds 已提交
566 567 568
	err = ds_send_data(ds_dev, (unsigned char *)&init, 8);
	if (err)
		return err;
569

L
Linus Torvalds 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
	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;
}

int ds_match_access(struct ds_device *dev, u64 init)
{
	int err;
	struct ds_status st;

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

L
Linus Torvalds 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	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;
}

int ds_set_path(struct ds_device *dev, u64 init)
{
	int err;
	struct ds_status st;
	u8 buf[9];

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

L
Linus Torvalds 已提交
616 617 618
	err = ds_send_data(dev, buf, sizeof(buf));
	if (err)
		return err;
619

L
Linus Torvalds 已提交
620 621 622 623 624 625 626 627 628 629 630
	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;
}

631 632 633 634
#endif  /*  0  */

static int ds_probe(struct usb_interface *intf,
		    const struct usb_device_id *udev_id)
L
Linus Torvalds 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
{
	struct usb_device *udev = interface_to_usbdev(intf);
	struct usb_endpoint_descriptor *endpoint;
	struct usb_host_interface *iface_desc;
	int i, err;

	ds_dev = kmalloc(sizeof(struct ds_device), GFP_KERNEL);
	if (!ds_dev) {
		printk(KERN_INFO "Failed to allocate new DS9490R structure.\n");
		return -ENOMEM;
	}

	ds_dev->udev = usb_get_dev(udev);
	usb_set_intfdata(intf, ds_dev);

	err = usb_set_interface(ds_dev->udev, intf->altsetting[0].desc.bInterfaceNumber, 3);
	if (err) {
		printk(KERN_ERR "Failed to set alternative setting 3 for %d interface: err=%d.\n",
				intf->altsetting[0].desc.bInterfaceNumber, err);
		return err;
	}

	err = usb_reset_configuration(ds_dev->udev);
	if (err) {
		printk(KERN_ERR "Failed to reset configuration: err=%d.\n", err);
		return err;
	}
662

L
Linus Torvalds 已提交
663 664 665 666 667 668 669 670
	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);
		return -ENODEV;
	}

	atomic_set(&ds_dev->refcnt, 0);
	memset(ds_dev->ep, 0, sizeof(ds_dev->ep));
671

L
Linus Torvalds 已提交
672
	/*
673
	 * This loop doesn'd show control 0 endpoint,
L
Linus Torvalds 已提交
674 675 676 677 678 679
	 * so we will fill only 1-3 endpoints entry.
	 */
	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
		endpoint = &iface_desc->endpoint[i].desc;

		ds_dev->ep[i+1] = endpoint->bEndpointAddress;
680

L
Linus Torvalds 已提交
681 682 683 684 685
		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);
	}
686

L
Linus Torvalds 已提交
687 688 689 690 691 692
#if 0
	{
		int err, i;
		u64 buf[3];
		u64 init=0xb30000002078ee81ull;
		struct ds_status st;
693

L
Linus Torvalds 已提交
694 695 696 697 698 699
		ds_reset(ds_dev, &st);
		err = ds_search(ds_dev, init, buf, 3, 0);
		if (err < 0)
			return err;
		for (i=0; i<err; ++i)
			printk("%d: %llx\n", i, buf[i]);
700 701

		printk("Resetting...\n");
L
Linus Torvalds 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715
		ds_reset(ds_dev, &st);
		printk("Setting path for %llx.\n", init);
		err = ds_set_path(ds_dev, init);
		if (err)
			return err;
		printk("Calling MATCH_ACCESS.\n");
		err = ds_match_access(ds_dev, init);
		if (err)
			return err;

		printk("Searching the bus...\n");
		err = ds_search(ds_dev, init, buf, 3, 0);

		printk("ds_search() returned %d\n", err);
716

L
Linus Torvalds 已提交
717 718 719 720
		if (err < 0)
			return err;
		for (i=0; i<err; ++i)
			printk("%d: %llx\n", i, buf[i]);
721

L
Linus Torvalds 已提交
722 723 724 725 726 727 728
		return 0;
	}
#endif

	return 0;
}

729
static void ds_disconnect(struct usb_interface *intf)
L
Linus Torvalds 已提交
730 731
{
	struct ds_device *dev;
732

L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
	dev = usb_get_intfdata(intf);
	usb_set_intfdata(intf, NULL);

	while (atomic_read(&dev->refcnt)) {
		printk(KERN_INFO "Waiting for DS to become free: refcnt=%d.\n",
				atomic_read(&dev->refcnt));

		if (msleep_interruptible(1000))
			flush_signals(current);
	}

	usb_put_dev(dev->udev);
	kfree(dev);
	ds_dev = NULL;
}

749
static int ds_init(void)
L
Linus Torvalds 已提交
750 751 752 753 754 755 756 757 758 759 760 761
{
	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;
}

762
static void ds_fini(void)
L
Linus Torvalds 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
{
	usb_deregister(&ds_driver);
}

module_init(ds_init);
module_exit(ds_fini);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");

EXPORT_SYMBOL(ds_touch_bit);
EXPORT_SYMBOL(ds_read_byte);
EXPORT_SYMBOL(ds_read_bit);
EXPORT_SYMBOL(ds_read_block);
EXPORT_SYMBOL(ds_write_byte);
EXPORT_SYMBOL(ds_write_bit);
EXPORT_SYMBOL(ds_write_block);
EXPORT_SYMBOL(ds_reset);
EXPORT_SYMBOL(ds_get_device);
EXPORT_SYMBOL(ds_put_device);

/*
785 786
 * This functions can be used for EEPROM programming,
 * when driver will be included into mainline this will
L
Linus Torvalds 已提交
787 788 789 790 791 792 793 794 795
 * require uncommenting.
 */
#if 0
EXPORT_SYMBOL(ds_start_pulse);
EXPORT_SYMBOL(ds_set_speed);
EXPORT_SYMBOL(ds_detect);
EXPORT_SYMBOL(ds_stop_pulse);
EXPORT_SYMBOL(ds_search);
#endif