uss720.c 21.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
L
Linus Torvalds 已提交
2 3 4 5 6
/*****************************************************************************/

/*
 *	uss720.c  --  USS720 USB Parport Cable.
 *
7
 *	Copyright (C) 1999, 2005, 2010
8
 *	    Thomas Sailer (t.sailer@alumni.ethz.ch)
L
Linus Torvalds 已提交
9 10 11 12
 *
 *  Based on parport_pc.c
 *
 *  History:
13 14 15 16 17 18 19 20 21 22 23 24 25
 *   0.1  04.08.1999  Created
 *   0.2  07.08.1999  Some fixes mainly suggested by Tim Waugh
 *		      Interrupt handling currently disabled because
 *		      usb_request_irq crashes somewhere within ohci.c
 *		      for no apparent reason (that is for me, anyway)
 *		      ECP currently untested
 *   0.3  10.08.1999  fixing merge errors
 *   0.4  13.08.1999  Added Vendor/Product ID of Brad Hard's cable
 *   0.5  20.09.1999  usb_control_msg wrapper used
 *        Nov01.2000  usb_device_table support by Adam J. Richter
 *        08.04.2001  Identify version on module load.  gb
 *   0.6  02.09.2005  Fix "scheduling in interrupt" problem by making save/restore
 *                    context asynchronous
L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35 36
 *
 */

/*****************************************************************************/

#include <linux/module.h>
#include <linux/socket.h>
#include <linux/parport.h>
#include <linux/init.h>
#include <linux/usb.h>
#include <linux/delay.h>
37 38
#include <linux/completion.h>
#include <linux/kref.h>
39
#include <linux/slab.h>
40
#include <linux/sched/signal.h>
L
Linus Torvalds 已提交
41

42
#define DRIVER_AUTHOR "Thomas M. Sailer, t.sailer@alumni.ethz.ch"
L
Linus Torvalds 已提交
43 44 45 46 47 48
#define DRIVER_DESC "USB Parport Cable driver for Cables using the Lucent Technologies USS720 Chip"

/* --------------------------------------------------------------------- */

struct parport_uss720_private {
	struct usb_device *usbdev;
49 50 51 52 53 54 55 56 57 58 59 60 61
	struct parport *pp;
	struct kref ref_count;
	__u8 reg[7];  /* USB registers */
	struct list_head asynclist;
	spinlock_t asynclock;
};

struct uss720_async_request {
	struct parport_uss720_private *priv;
	struct kref ref_count;
	struct list_head asynclist;
	struct completion compl;
	struct urb *urb;
62
	struct usb_ctrlrequest *dr;
63
	__u8 reg[7];
L
Linus Torvalds 已提交
64 65 66 67
};

/* --------------------------------------------------------------------- */

68
static void destroy_priv(struct kref *kref)
L
Linus Torvalds 已提交
69
{
70
	struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count);
L
Linus Torvalds 已提交
71

72
	dev_dbg(&priv->usbdev->dev, "destroying priv datastructure\n");
73 74 75 76 77 78 79 80 81 82 83 84
	usb_put_dev(priv->usbdev);
	kfree(priv);
}

static void destroy_async(struct kref *kref)
{
	struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
	struct parport_uss720_private *priv = rq->priv;
	unsigned long flags;

	if (likely(rq->urb))
		usb_free_urb(rq->urb);
85
	kfree(rq->dr);
86 87 88 89 90 91 92 93 94
	spin_lock_irqsave(&priv->asynclock, flags);
	list_del_init(&rq->asynclist);
	spin_unlock_irqrestore(&priv->asynclock, flags);
	kfree(rq);
	kref_put(&priv->ref_count, destroy_priv);
}

/* --------------------------------------------------------------------- */

95
static void async_complete(struct urb *urb)
96 97 98 99
{
	struct uss720_async_request *rq;
	struct parport *pp;
	struct parport_uss720_private *priv;
100
	int status = urb->status;
101 102 103 104

	rq = urb->context;
	priv = rq->priv;
	pp = priv->pp;
105
	if (status) {
106 107
		dev_err(&urb->dev->dev, "async_complete: urb error %d\n",
			status);
108
	} else if (rq->dr->bRequest == 3) {
109
		memcpy(priv->reg, rq->reg, sizeof(priv->reg));
L
Linus Torvalds 已提交
110
#if 0
111 112
		dev_dbg(&priv->usbdev->dev, "async_complete regs %7ph\n",
			priv->reg);
L
Linus Torvalds 已提交
113 114
#endif
		/* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
115
		if (rq->reg[2] & rq->reg[1] & 0x10 && pp)
116
			parport_generic_irq(pp);
L
Linus Torvalds 已提交
117
	}
118 119
	complete(&rq->compl);
	kref_put(&rq->ref_count, destroy_async);
L
Linus Torvalds 已提交
120 121
}

122 123
static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv,
							 __u8 request, __u8 requesttype, __u16 value, __u16 index,
A
Al Viro 已提交
124
							 gfp_t mem_flags)
L
Linus Torvalds 已提交
125
{
126 127 128
	struct usb_device *usbdev;
	struct uss720_async_request *rq;
	unsigned long flags;
L
Linus Torvalds 已提交
129 130
	int ret;

131 132 133
	if (!priv)
		return NULL;
	usbdev = priv->usbdev;
L
Linus Torvalds 已提交
134
	if (!usbdev)
135
		return NULL;
136
	rq = kzalloc(sizeof(struct uss720_async_request), mem_flags);
137
	if (!rq)
138 139 140 141 142 143 144 145 146 147 148
		return NULL;
	kref_init(&rq->ref_count);
	INIT_LIST_HEAD(&rq->asynclist);
	init_completion(&rq->compl);
	kref_get(&priv->ref_count);
	rq->priv = priv;
	rq->urb = usb_alloc_urb(0, mem_flags);
	if (!rq->urb) {
		kref_put(&rq->ref_count, destroy_async);
		return NULL;
	}
149 150 151 152 153 154 155 156 157 158
	rq->dr = kmalloc(sizeof(*rq->dr), mem_flags);
	if (!rq->dr) {
		kref_put(&rq->ref_count, destroy_async);
		return NULL;
	}
	rq->dr->bRequestType = requesttype;
	rq->dr->bRequest = request;
	rq->dr->wValue = cpu_to_le16(value);
	rq->dr->wIndex = cpu_to_le16(index);
	rq->dr->wLength = cpu_to_le16((request == 3) ? sizeof(rq->reg) : 0);
159
	usb_fill_control_urb(rq->urb, usbdev, (requesttype & 0x80) ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0),
160
			     (unsigned char *)rq->dr,
161 162 163 164 165
			     (request == 3) ? rq->reg : NULL, (request == 3) ? sizeof(rq->reg) : 0, async_complete, rq);
	/* rq->urb->transfer_flags |= URB_ASYNC_UNLINK; */
	spin_lock_irqsave(&priv->asynclock, flags);
	list_add_tail(&rq->asynclist, &priv->asynclist);
	spin_unlock_irqrestore(&priv->asynclock, flags);
166
	kref_get(&rq->ref_count);
167
	ret = usb_submit_urb(rq->urb, mem_flags);
168
	if (!ret)
169
		return rq;
170
	destroy_async(&rq->ref_count);
171
	dev_err(&usbdev->dev, "submit_async_request submit_urb failed with %d\n", ret);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	return NULL;
}

static unsigned int kill_all_async_requests_priv(struct parport_uss720_private *priv)
{
	struct uss720_async_request *rq;
	unsigned long flags;
	unsigned int ret = 0;

	spin_lock_irqsave(&priv->asynclock, flags);
	list_for_each_entry(rq, &priv->asynclist, asynclist) {
		usb_unlink_urb(rq->urb);
		ret++;
	}
	spin_unlock_irqrestore(&priv->asynclock, flags);
L
Linus Torvalds 已提交
187 188 189 190 191
	return ret;
}

/* --------------------------------------------------------------------- */

A
Al Viro 已提交
192
static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags)
193 194 195 196 197 198 199 200 201 202 203 204 205
{
	struct parport_uss720_private *priv;
	struct uss720_async_request *rq;
	static const unsigned char regindex[9] = {
		4, 0, 1, 5, 5, 0, 2, 3, 6
	};
	int ret;

	if (!pp)
		return -EIO;
	priv = pp->private_data;
	rq = submit_async_request(priv, 3, 0xc0, ((unsigned int)reg) << 8, 0, mem_flags);
	if (!rq) {
206 207
		dev_err(&priv->usbdev->dev, "get_1284_register(%u) failed",
			(unsigned int)reg);
208 209 210 211 212 213 214 215 216 217
		return -EIO;
	}
	if (!val) {
		kref_put(&rq->ref_count, destroy_async);
		return 0;
	}
	if (wait_for_completion_timeout(&rq->compl, HZ)) {
		ret = rq->urb->status;
		*val = priv->reg[(reg >= 9) ? 0 : regindex[reg]];
		if (ret)
218 219
			printk(KERN_WARNING "get_1284_register: "
			       "usb error %d\n", ret);
220 221 222
		kref_put(&rq->ref_count, destroy_async);
		return ret;
	}
223
	printk(KERN_WARNING "get_1284_register timeout\n");
224 225 226 227
	kill_all_async_requests_priv(priv);
	return -EIO;
}

A
Al Viro 已提交
228
static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
229 230 231 232 233 234 235 236 237
{
	struct parport_uss720_private *priv;
	struct uss720_async_request *rq;

	if (!pp)
		return -EIO;
	priv = pp->private_data;
	rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
	if (!rq) {
238 239
		dev_err(&priv->usbdev->dev, "set_1284_register(%u,%u) failed",
			(unsigned int)reg, (unsigned int)val);
240 241 242 243 244 245 246 247
		return -EIO;
	}
	kref_put(&rq->ref_count, destroy_async);
	return 0;
}

/* --------------------------------------------------------------------- */

L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257 258 259
/* ECR modes */
#define ECR_SPP 00
#define ECR_PS2 01
#define ECR_PPF 02
#define ECR_ECP 03
#define ECR_EPP 04

/* Safely change the mode bits in the ECR */
static int change_mode(struct parport *pp, int m)
{
	struct parport_uss720_private *priv = pp->private_data;
	int mode;
260
	__u8 reg;
L
Linus Torvalds 已提交
261

262
	if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
L
Linus Torvalds 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
		return -EIO;
	/* Bits <7:5> contain the mode. */
	mode = (priv->reg[2] >> 5) & 0x7;
	if (mode == m)
		return 0;
	/* We have to go through mode 000 or 001 */
	if (mode > ECR_PS2 && m > ECR_PS2)
		if (change_mode(pp, ECR_PS2))
			return -EIO;

	if (m <= ECR_PS2 && !(priv->reg[1] & 0x20)) {
		/* This mode resets the FIFO, so we may
		 * have to wait for it to drain first. */
		unsigned long expire = jiffies + pp->physport->cad->timeout;
		switch (mode) {
		case ECR_PPF: /* Parallel Port FIFO mode */
		case ECR_ECP: /* ECP Parallel Port mode */
			/* Poll slowly. */
			for (;;) {
282
				if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
L
Linus Torvalds 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295
					return -EIO;
				if (priv->reg[2] & 0x01)
					break;
				if (time_after_eq (jiffies, expire))
					/* The FIFO is stuck. */
					return -EBUSY;
				msleep_interruptible(10);
				if (signal_pending (current))
					break;
			}
		}
	}
	/* Set the mode. */
296 297 298
	if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
		return -EIO;
	if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
L
Linus Torvalds 已提交
299 300 301 302 303 304 305 306 307 308 309
		return -EIO;
	return 0;
}

/*
 * Clear TIMEOUT BIT in EPP MODE
 */
static int clear_epp_timeout(struct parport *pp)
{
	unsigned char stat;

310
	if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
L
Linus Torvalds 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
		return 1;
	return stat & 1;
}

/*
 * Access functions.
 */
#if 0
static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
{
	struct parport *pp = (struct parport *)dev_id;
	struct parport_uss720_private *priv = pp->private_data;	

	if (usbstatus != 0 || len < 4 || !buffer)
		return 1;
	memcpy(priv->reg, buffer, 4);
	/* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
	if (priv->reg[2] & priv->reg[1] & 0x10)
329
		parport_generic_irq(pp);
L
Linus Torvalds 已提交
330 331 332 333 334 335
	return 1;
}
#endif

static void parport_uss720_write_data(struct parport *pp, unsigned char d)
{
336
	set_1284_register(pp, 0, d, GFP_KERNEL);
L
Linus Torvalds 已提交
337 338 339 340 341 342
}

static unsigned char parport_uss720_read_data(struct parport *pp)
{
	unsigned char ret;

343
	if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351 352
		return 0;
	return ret;
}

static void parport_uss720_write_control(struct parport *pp, unsigned char d)
{
	struct parport_uss720_private *priv = pp->private_data;	

	d = (d & 0xf) | (priv->reg[1] & 0xf0);
353
	if (set_1284_register(pp, 2, d, GFP_KERNEL))
L
Linus Torvalds 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
		return;
	priv->reg[1] = d;
}

static unsigned char parport_uss720_read_control(struct parport *pp)
{
	struct parport_uss720_private *priv = pp->private_data;	
	return priv->reg[1] & 0xf; /* Use soft copy */
}

static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
{
	struct parport_uss720_private *priv = pp->private_data;	
	unsigned char d;

	mask &= 0x0f;
	val &= 0x0f;
	d = (priv->reg[1] & (~mask)) ^ val;
372
	if (set_1284_register(pp, 2, d, GFP_KERNEL))
L
Linus Torvalds 已提交
373 374 375 376 377 378 379 380 381
		return 0;
	priv->reg[1] = d;
	return d & 0xf;
}

static unsigned char parport_uss720_read_status(struct parport *pp)
{
	unsigned char ret;

382
	if (get_1284_register(pp, 1, &ret, GFP_KERNEL))
L
Linus Torvalds 已提交
383 384 385 386 387 388 389 390 391 392
		return 0;
	return ret & 0xf8;
}

static void parport_uss720_disable_irq(struct parport *pp)
{
	struct parport_uss720_private *priv = pp->private_data;	
	unsigned char d;

	d = priv->reg[1] & ~0x10;
393
	if (set_1284_register(pp, 2, d, GFP_KERNEL))
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403
		return;
	priv->reg[1] = d;
}

static void parport_uss720_enable_irq(struct parport *pp)
{
	struct parport_uss720_private *priv = pp->private_data;	
	unsigned char d;

	d = priv->reg[1] | 0x10;
404
	if (set_1284_register(pp, 2, d, GFP_KERNEL))
L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412 413 414
		return;
	priv->reg[1] = d;
}

static void parport_uss720_data_forward (struct parport *pp)
{
	struct parport_uss720_private *priv = pp->private_data;	
	unsigned char d;

	d = priv->reg[1] & ~0x20;
415
	if (set_1284_register(pp, 2, d, GFP_KERNEL))
L
Linus Torvalds 已提交
416 417 418 419 420 421 422 423 424 425
		return;
	priv->reg[1] = d;
}

static void parport_uss720_data_reverse (struct parport *pp)
{
	struct parport_uss720_private *priv = pp->private_data;	
	unsigned char d;

	d = priv->reg[1] | 0x20;
426
	if (set_1284_register(pp, 2, d, GFP_KERNEL))
L
Linus Torvalds 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440
		return;
	priv->reg[1] = d;
}

static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
{
	s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
	s->u.pc.ecr = 0x24;
}

static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
{
	struct parport_uss720_private *priv = pp->private_data;	

441 442
#if 0
	if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
L
Linus Torvalds 已提交
443
		return;
444
#endif
L
Linus Torvalds 已提交
445 446 447 448 449 450
	s->u.pc.ctr = priv->reg[1];
	s->u.pc.ecr = priv->reg[2];
}

static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
{
451 452 453 454 455 456 457
	struct parport_uss720_private *priv = pp->private_data;

	set_1284_register(pp, 2, s->u.pc.ctr, GFP_ATOMIC);
	set_1284_register(pp, 6, s->u.pc.ecr, GFP_ATOMIC);
	get_1284_register(pp, 2, NULL, GFP_ATOMIC);
	priv->reg[1] = s->u.pc.ctr;
	priv->reg[2] = s->u.pc.ecr;
L
Linus Torvalds 已提交
458 459 460 461 462 463 464 465 466 467
}

static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
{
	struct parport_uss720_private *priv = pp->private_data;	
	size_t got = 0;

	if (change_mode(pp, ECR_EPP))
		return 0;
	for (; got < length; got++) {
468
		if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
			break;
		buf++;
		if (priv->reg[0] & 0x01) {
			clear_epp_timeout(pp);
			break;
		}
	}
	change_mode(pp, ECR_PS2);
	return got;
}

static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
{
#if 0
	struct parport_uss720_private *priv = pp->private_data;	
	size_t written = 0;

	if (change_mode(pp, ECR_EPP))
		return 0;
	for (; written < length; written++) {
489
		if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
L
Linus Torvalds 已提交
490 491
			break;
		((char*)buf)++;
492
		if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
L
Linus Torvalds 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
			break;
		if (priv->reg[0] & 0x01) {
			clear_epp_timeout(pp);
			break;
		}
	}
	change_mode(pp, ECR_PS2);
	return written;
#else
	struct parport_uss720_private *priv = pp->private_data;
	struct usb_device *usbdev = priv->usbdev;
	int rlen;
	int i;

	if (!usbdev)
		return 0;
	if (change_mode(pp, ECR_EPP))
		return 0;
	i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
	if (i)
513
		printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buf, length, rlen);
L
Linus Torvalds 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526
	change_mode(pp, ECR_PS2);
	return rlen;
#endif
}

static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
{
	struct parport_uss720_private *priv = pp->private_data;	
	size_t got = 0;

	if (change_mode(pp, ECR_EPP))
		return 0;
	for (; got < length; got++) {
527
		if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
L
Linus Torvalds 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
			break;
		buf++;
		if (priv->reg[0] & 0x01) {
			clear_epp_timeout(pp);
			break;
		}
	}
	change_mode(pp, ECR_PS2);
	return got;
}

static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
{
	struct parport_uss720_private *priv = pp->private_data;	
	size_t written = 0;

	if (change_mode(pp, ECR_EPP))
		return 0;
	for (; written < length; written++) {
547
		if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
L
Linus Torvalds 已提交
548 549
			break;
		buf++;
550
		if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
L
Linus Torvalds 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
			break;
		if (priv->reg[0] & 0x01) {
			clear_epp_timeout(pp);
			break;
		}
	}
	change_mode(pp, ECR_PS2);
	return written;
}

static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
{
	struct parport_uss720_private *priv = pp->private_data;
	struct usb_device *usbdev = priv->usbdev;
	int rlen;
	int i;

	if (!usbdev)
		return 0;
	if (change_mode(pp, ECR_ECP))
		return 0;
	i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
	if (i)
574
		printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buffer, len, rlen);
L
Linus Torvalds 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	change_mode(pp, ECR_PS2);
	return rlen;
}

static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
{
	struct parport_uss720_private *priv = pp->private_data;
	struct usb_device *usbdev = priv->usbdev;
	int rlen;
	int i;

	if (!usbdev)
		return 0;
	if (change_mode(pp, ECR_ECP))
		return 0;
	i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
	if (i)
592
		printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %zu rlen %u\n", buffer, len, rlen);
L
Linus Torvalds 已提交
593 594 595 596 597 598 599 600 601 602 603
	change_mode(pp, ECR_PS2);
	return rlen;
}

static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
{
	size_t written = 0;

	if (change_mode(pp, ECR_ECP))
		return 0;
	for (; written < len; written++) {
604
		if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
L
Linus Torvalds 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
			break;
		buffer++;
	}
	change_mode(pp, ECR_PS2);
	return written;
}

static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
{
	struct parport_uss720_private *priv = pp->private_data;
	struct usb_device *usbdev = priv->usbdev;
	int rlen;
	int i;

	if (!usbdev)
		return 0;
	if (change_mode(pp, ECR_PPF))
		return 0;
	i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
	if (i)
625
		printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buffer, len, rlen);
L
Linus Torvalds 已提交
626 627 628 629 630 631 632 633 634 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 662 663 664 665 666 667 668 669 670 671 672
	change_mode(pp, ECR_PS2);
	return rlen;
}

/* --------------------------------------------------------------------- */

static struct parport_operations parport_uss720_ops = 
{
	.owner =		THIS_MODULE,
	.write_data =		parport_uss720_write_data,
	.read_data =		parport_uss720_read_data,

	.write_control =	parport_uss720_write_control,
	.read_control =		parport_uss720_read_control,
	.frob_control =		parport_uss720_frob_control,

	.read_status =		parport_uss720_read_status,

	.enable_irq =		parport_uss720_enable_irq,
	.disable_irq =		parport_uss720_disable_irq,

	.data_forward =		parport_uss720_data_forward,
	.data_reverse =		parport_uss720_data_reverse,

	.init_state =		parport_uss720_init_state,
	.save_state =		parport_uss720_save_state,
	.restore_state =	parport_uss720_restore_state,

	.epp_write_data =	parport_uss720_epp_write_data,
	.epp_read_data =	parport_uss720_epp_read_data,
	.epp_write_addr =	parport_uss720_epp_write_addr,
	.epp_read_addr =	parport_uss720_epp_read_addr,

	.ecp_write_data =	parport_uss720_ecp_write_data,
	.ecp_read_data =	parport_uss720_ecp_read_data,
	.ecp_write_addr =	parport_uss720_ecp_write_addr,

	.compat_write_data =	parport_uss720_write_compat,
	.nibble_read_data =	parport_ieee1284_read_nibble,
	.byte_read_data =	parport_ieee1284_read_byte,
};

/* --------------------------------------------------------------------- */

static int uss720_probe(struct usb_interface *intf,
			const struct usb_device_id *id)
{
673
	struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
L
Linus Torvalds 已提交
674
	struct usb_host_interface *interface;
675
	struct usb_endpoint_descriptor *epd;
L
Linus Torvalds 已提交
676 677
	struct parport_uss720_private *priv;
	struct parport *pp;
678
	unsigned char reg;
L
Linus Torvalds 已提交
679 680
	int i;

681 682 683
	dev_dbg(&intf->dev, "probe: vendor id 0x%x, device id 0x%x\n",
		le16_to_cpu(usbdev->descriptor.idVendor),
		le16_to_cpu(usbdev->descriptor.idProduct));
L
Linus Torvalds 已提交
684 685

	/* our known interfaces have 3 alternate settings */
686 687
	if (intf->num_altsetting != 3) {
		usb_put_dev(usbdev);
L
Linus Torvalds 已提交
688
		return -ENODEV;
689
	}
L
Linus Torvalds 已提交
690
	i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
691
	dev_dbg(&intf->dev, "set interface result %d\n", i);
L
Linus Torvalds 已提交
692 693 694

	interface = intf->cur_altsetting;

695 696 697 698 699
	if (interface->desc.bNumEndpoints < 3) {
		usb_put_dev(usbdev);
		return -ENODEV;
	}

L
Linus Torvalds 已提交
700 701 702
	/*
	 * Allocate parport interface 
	 */
703 704
	priv = kzalloc(sizeof(struct parport_uss720_private), GFP_KERNEL);
	if (!priv) {
705
		usb_put_dev(usbdev);
L
Linus Torvalds 已提交
706
		return -ENOMEM;
707 708 709 710 711 712
	}
	priv->pp = NULL;
	priv->usbdev = usbdev;
	kref_init(&priv->ref_count);
	spin_lock_init(&priv->asynclock);
	INIT_LIST_HEAD(&priv->asynclist);
713 714
	pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops);
	if (!pp) {
715
		printk(KERN_WARNING "uss720: could not register parport\n");
L
Linus Torvalds 已提交
716 717 718
		goto probe_abort;
	}

719
	priv->pp = pp;
L
Linus Torvalds 已提交
720 721 722 723
	pp->private_data = priv;
	pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;

	/* set the USS720 control register to manual mode, no ECP compression, enable all ints */
724 725 726
	set_1284_register(pp, 7, 0x00, GFP_KERNEL);
	set_1284_register(pp, 6, 0x30, GFP_KERNEL);  /* PS/2 mode */
	set_1284_register(pp, 2, 0x0c, GFP_KERNEL);
L
Linus Torvalds 已提交
727
	/* debugging */
728
	get_1284_register(pp, 0, &reg, GFP_KERNEL);
729
	dev_dbg(&intf->dev, "reg: %7ph\n", priv->reg);
L
Linus Torvalds 已提交
730

731 732 733 734 735
	i = usb_find_last_int_in_endpoint(interface, &epd);
	if (!i) {
		dev_dbg(&intf->dev, "epaddr %d interval %d\n",
				epd->bEndpointAddress, epd->bInterval);
	}
L
Linus Torvalds 已提交
736 737
	parport_announce_port(pp);

738
	usb_set_intfdata(intf, pp);
L
Linus Torvalds 已提交
739 740 741
	return 0;

probe_abort:
742 743
	kill_all_async_requests_priv(priv);
	kref_put(&priv->ref_count, destroy_priv);
L
Linus Torvalds 已提交
744 745 746 747 748
	return -ENODEV;
}

static void uss720_disconnect(struct usb_interface *intf)
{
749
	struct parport *pp = usb_get_intfdata(intf);
L
Linus Torvalds 已提交
750
	struct parport_uss720_private *priv;
751
	struct usb_device *usbdev;
L
Linus Torvalds 已提交
752

753
	dev_dbg(&intf->dev, "disconnect\n");
754
	usb_set_intfdata(intf, NULL);
L
Linus Torvalds 已提交
755 756
	if (pp) {
		priv = pp->private_data;
757
		usbdev = priv->usbdev;
L
Linus Torvalds 已提交
758
		priv->usbdev = NULL;
759
		priv->pp = NULL;
760
		dev_dbg(&intf->dev, "parport_remove_port\n");
761
		parport_remove_port(pp);
L
Linus Torvalds 已提交
762
		parport_put_port(pp);
763 764
		kill_all_async_requests_priv(priv);
		kref_put(&priv->ref_count, destroy_priv);
L
Linus Torvalds 已提交
765
	}
766
	dev_dbg(&intf->dev, "disconnect done\n");
L
Linus Torvalds 已提交
767 768 769
}

/* table of cables that work through this driver */
770
static const struct usb_device_id uss720_table[] = {
L
Linus Torvalds 已提交
771 772 773 774
	{ USB_DEVICE(0x047e, 0x1001) },
	{ USB_DEVICE(0x0557, 0x2001) },
	{ USB_DEVICE(0x0729, 0x1284) },
	{ USB_DEVICE(0x1293, 0x0002) },
775
	{ USB_DEVICE(0x050d, 0x0002) },
L
Linus Torvalds 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
	{ }						/* Terminating entry */
};

MODULE_DEVICE_TABLE (usb, uss720_table);


static struct usb_driver uss720_driver = {
	.name =		"uss720",
	.probe =	uss720_probe,
	.disconnect =	uss720_disconnect,
	.id_table =	uss720_table,
};

/* --------------------------------------------------------------------- */

791 792
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
793 794 795 796 797 798 799 800 801
MODULE_LICENSE("GPL");

static int __init uss720_init(void)
{
	int retval;
	retval = usb_register(&uss720_driver);
	if (retval)
		goto out;

802
	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
803 804 805 806 807 808
	printk(KERN_INFO KBUILD_MODNAME ": NOTE: this is a special purpose "
	       "driver to allow nonstandard\n");
	printk(KERN_INFO KBUILD_MODNAME ": protocols (eg. bitbang) over "
	       "USS720 usb to parallel cables\n");
	printk(KERN_INFO KBUILD_MODNAME ": If you just want to connect to a "
	       "printer, use usblp instead\n");
L
Linus Torvalds 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821
out:
	return retval;
}

static void __exit uss720_cleanup(void)
{
	usb_deregister(&uss720_driver);
}

module_init(uss720_init);
module_exit(uss720_cleanup);

/* --------------------------------------------------------------------- */