Linux Audio

Check our new training course

Loading...
v3.1
   1/* ppa.c   --  low level driver for the IOMEGA PPA3 
   2 * parallel port SCSI host adapter.
   3 * 
   4 * (The PPA3 is the embedded controller in the ZIP drive.)
   5 * 
   6 * (c) 1995,1996 Grant R. Guenther, grant@torque.net,
   7 * under the terms of the GNU General Public License.
   8 * 
   9 */
  10
  11#include <linux/init.h>
  12#include <linux/kernel.h>
  13#include <linux/slab.h>
  14#include <linux/module.h>
  15#include <linux/blkdev.h>
  16#include <linux/parport.h>
  17#include <linux/workqueue.h>
  18#include <linux/delay.h>
  19#include <linux/jiffies.h>
  20#include <asm/io.h>
  21
  22#include <scsi/scsi.h>
  23#include <scsi/scsi_cmnd.h>
  24#include <scsi/scsi_device.h>
  25#include <scsi/scsi_host.h>
  26
  27
  28static void ppa_reset_pulse(unsigned int base);
  29
  30typedef struct {
  31	struct pardevice *dev;	/* Parport device entry         */
  32	int base;		/* Actual port address          */
  33	int mode;		/* Transfer mode                */
  34	struct scsi_cmnd *cur_cmd;	/* Current queued command       */
  35	struct delayed_work ppa_tq;	/* Polling interrupt stuff       */
  36	unsigned long jstart;	/* Jiffies at start             */
  37	unsigned long recon_tmo;	/* How many usecs to wait for reconnection (6th bit) */
  38	unsigned int failed:1;	/* Failure flag                 */
  39	unsigned wanted:1;	/* Parport sharing busy flag    */
 
  40	wait_queue_head_t *waiting;
  41	struct Scsi_Host *host;
  42	struct list_head list;
  43} ppa_struct;
  44
  45#include  "ppa.h"
  46
 
 
 
 
 
 
 
 
 
 
  47static inline ppa_struct *ppa_dev(struct Scsi_Host *host)
  48{
  49	return *(ppa_struct **)&host->hostdata;
  50}
  51
  52static DEFINE_SPINLOCK(arbitration_lock);
  53
  54static void got_it(ppa_struct *dev)
  55{
  56	dev->base = dev->dev->port->base;
  57	if (dev->cur_cmd)
  58		dev->cur_cmd->SCp.phase = 1;
  59	else
  60		wake_up(dev->waiting);
  61}
  62
  63static void ppa_wakeup(void *ref)
  64{
  65	ppa_struct *dev = (ppa_struct *) ref;
  66	unsigned long flags;
  67
  68	spin_lock_irqsave(&arbitration_lock, flags);
  69	if (dev->wanted) {
  70		parport_claim(dev->dev);
  71		got_it(dev);
  72		dev->wanted = 0;
  73	}
  74	spin_unlock_irqrestore(&arbitration_lock, flags);
  75	return;
  76}
  77
  78static int ppa_pb_claim(ppa_struct *dev)
  79{
  80	unsigned long flags;
  81	int res = 1;
  82	spin_lock_irqsave(&arbitration_lock, flags);
  83	if (parport_claim(dev->dev) == 0) {
  84		got_it(dev);
  85		res = 0;
  86	}
  87	dev->wanted = res;
  88	spin_unlock_irqrestore(&arbitration_lock, flags);
  89	return res;
  90}
  91
  92static void ppa_pb_dismiss(ppa_struct *dev)
  93{
  94	unsigned long flags;
  95	int wanted;
  96	spin_lock_irqsave(&arbitration_lock, flags);
  97	wanted = dev->wanted;
  98	dev->wanted = 0;
  99	spin_unlock_irqrestore(&arbitration_lock, flags);
 100	if (!wanted)
 101		parport_release(dev->dev);
 102}
 103
 104static inline void ppa_pb_release(ppa_struct *dev)
 105{
 106	parport_release(dev->dev);
 107}
 108
 109/*
 110 * Start of Chipset kludges
 111 */
 112
 113/* This is to give the ppa driver a way to modify the timings (and other
 114 * parameters) by writing to the /proc/scsi/ppa/0 file.
 115 * Very simple method really... (To simple, no error checking :( )
 116 * Reason: Kernel hackers HATE having to unload and reload modules for
 117 * testing...
 118 * Also gives a method to use a script to obtain optimum timings (TODO)
 119 */
 120
 121static inline int ppa_proc_write(ppa_struct *dev, char *buffer, int length)
 122{
 
 123	unsigned long x;
 124
 125	if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
 126		x = simple_strtoul(buffer + 5, NULL, 0);
 127		dev->mode = x;
 128		return length;
 129	}
 130	if ((length > 10) && (strncmp(buffer, "recon_tmo=", 10) == 0)) {
 131		x = simple_strtoul(buffer + 10, NULL, 0);
 132		dev->recon_tmo = x;
 133		printk(KERN_INFO "ppa: recon_tmo set to %ld\n", x);
 134		return length;
 135	}
 136	printk(KERN_WARNING "ppa /proc: invalid variable\n");
 137	return -EINVAL;
 138}
 139
 140static int ppa_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset, int length, int inout)
 141{
 142	int len = 0;
 143	ppa_struct *dev = ppa_dev(host);
 144
 145	if (inout)
 146		return ppa_proc_write(dev, buffer, length);
 147
 148	len += sprintf(buffer + len, "Version : %s\n", PPA_VERSION);
 149	len +=
 150	    sprintf(buffer + len, "Parport : %s\n",
 151		    dev->dev->port->name);
 152	len +=
 153	    sprintf(buffer + len, "Mode    : %s\n",
 154		    PPA_MODE_STRING[dev->mode]);
 155#if PPA_DEBUG > 0
 156	len +=
 157	    sprintf(buffer + len, "recon_tmo : %lu\n", dev->recon_tmo);
 158#endif
 159
 160	/* Request for beyond end of buffer */
 161	if (offset > length)
 162		return 0;
 163
 164	*start = buffer + offset;
 165	len -= offset;
 166	if (len > length)
 167		len = length;
 168	return len;
 169}
 170
 171static int device_check(ppa_struct *dev);
 172
 173#if PPA_DEBUG > 0
 174#define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
 175	   y, __func__, __LINE__); ppa_fail_func(x,y);
 176static inline void ppa_fail_func(ppa_struct *dev, int error_code)
 177#else
 178static inline void ppa_fail(ppa_struct *dev, int error_code)
 179#endif
 180{
 181	/* If we fail a device then we trash status / message bytes */
 182	if (dev->cur_cmd) {
 183		dev->cur_cmd->result = error_code << 16;
 184		dev->failed = 1;
 185	}
 186}
 187
 188/*
 189 * Wait for the high bit to be set.
 190 * 
 191 * In principle, this could be tied to an interrupt, but the adapter
 192 * doesn't appear to be designed to support interrupts.  We spin on
 193 * the 0x80 ready bit. 
 194 */
 195static unsigned char ppa_wait(ppa_struct *dev)
 196{
 197	int k;
 198	unsigned short ppb = dev->base;
 199	unsigned char r;
 200
 201	k = PPA_SPIN_TMO;
 202	/* Wait for bit 6 and 7 - PJC */
 203	for (r = r_str(ppb); ((r & 0xc0) != 0xc0) && (k); k--) {
 204		udelay(1);
 205		r = r_str(ppb);
 206	}
 207
 208	/*
 209	 * return some status information.
 210	 * Semantics: 0xc0 = ZIP wants more data
 211	 *            0xd0 = ZIP wants to send more data
 212	 *            0xe0 = ZIP is expecting SCSI command data
 213	 *            0xf0 = end of transfer, ZIP is sending status
 214	 */
 215	if (k)
 216		return (r & 0xf0);
 217
 218	/* Counter expired - Time out occurred */
 219	ppa_fail(dev, DID_TIME_OUT);
 220	printk(KERN_WARNING "ppa timeout in ppa_wait\n");
 221	return 0;		/* command timed out */
 222}
 223
 224/*
 225 * Clear EPP Timeout Bit 
 226 */
 227static inline void epp_reset(unsigned short ppb)
 228{
 229	int i;
 230
 231	i = r_str(ppb);
 232	w_str(ppb, i);
 233	w_str(ppb, i & 0xfe);
 234}
 235
 236/* 
 237 * Wait for empty ECP fifo (if we are in ECP fifo mode only)
 238 */
 239static inline void ecp_sync(ppa_struct *dev)
 240{
 241	int i, ppb_hi = dev->dev->port->base_hi;
 242
 243	if (ppb_hi == 0)
 244		return;
 245
 246	if ((r_ecr(ppb_hi) & 0xe0) == 0x60) {	/* mode 011 == ECP fifo mode */
 247		for (i = 0; i < 100; i++) {
 248			if (r_ecr(ppb_hi) & 0x01)
 249				return;
 250			udelay(5);
 251		}
 252		printk(KERN_WARNING "ppa: ECP sync failed as data still present in FIFO.\n");
 253	}
 254}
 255
 256static int ppa_byte_out(unsigned short base, const char *buffer, int len)
 257{
 258	int i;
 259
 260	for (i = len; i; i--) {
 261		w_dtr(base, *buffer++);
 262		w_ctr(base, 0xe);
 263		w_ctr(base, 0xc);
 264	}
 265	return 1;		/* All went well - we hope! */
 266}
 267
 268static int ppa_byte_in(unsigned short base, char *buffer, int len)
 269{
 270	int i;
 271
 272	for (i = len; i; i--) {
 273		*buffer++ = r_dtr(base);
 274		w_ctr(base, 0x27);
 275		w_ctr(base, 0x25);
 276	}
 277	return 1;		/* All went well - we hope! */
 278}
 279
 280static int ppa_nibble_in(unsigned short base, char *buffer, int len)
 281{
 282	for (; len; len--) {
 283		unsigned char h;
 284
 285		w_ctr(base, 0x4);
 286		h = r_str(base) & 0xf0;
 287		w_ctr(base, 0x6);
 288		*buffer++ = h | ((r_str(base) & 0xf0) >> 4);
 289	}
 290	return 1;		/* All went well - we hope! */
 291}
 292
 293static int ppa_out(ppa_struct *dev, char *buffer, int len)
 294{
 295	int r;
 296	unsigned short ppb = dev->base;
 297
 298	r = ppa_wait(dev);
 299
 300	if ((r & 0x50) != 0x40) {
 301		ppa_fail(dev, DID_ERROR);
 302		return 0;
 303	}
 304	switch (dev->mode) {
 305	case PPA_NIBBLE:
 306	case PPA_PS2:
 307		/* 8 bit output, with a loop */
 308		r = ppa_byte_out(ppb, buffer, len);
 309		break;
 310
 311	case PPA_EPP_32:
 312	case PPA_EPP_16:
 313	case PPA_EPP_8:
 314		epp_reset(ppb);
 315		w_ctr(ppb, 0x4);
 316#ifdef CONFIG_SCSI_IZIP_EPP16
 317		if (!(((long) buffer | len) & 0x01))
 318			outsw(ppb + 4, buffer, len >> 1);
 319#else
 320		if (!(((long) buffer | len) & 0x03))
 321			outsl(ppb + 4, buffer, len >> 2);
 322#endif
 
 323		else
 324			outsb(ppb + 4, buffer, len);
 325		w_ctr(ppb, 0xc);
 326		r = !(r_str(ppb) & 0x01);
 327		w_ctr(ppb, 0xc);
 328		ecp_sync(dev);
 329		break;
 330
 331	default:
 332		printk(KERN_ERR "PPA: bug in ppa_out()\n");
 333		r = 0;
 334	}
 335	return r;
 336}
 337
 338static int ppa_in(ppa_struct *dev, char *buffer, int len)
 339{
 340	int r;
 341	unsigned short ppb = dev->base;
 342
 343	r = ppa_wait(dev);
 344
 345	if ((r & 0x50) != 0x50) {
 346		ppa_fail(dev, DID_ERROR);
 347		return 0;
 348	}
 349	switch (dev->mode) {
 350	case PPA_NIBBLE:
 351		/* 4 bit input, with a loop */
 352		r = ppa_nibble_in(ppb, buffer, len);
 353		w_ctr(ppb, 0xc);
 354		break;
 355
 356	case PPA_PS2:
 357		/* 8 bit input, with a loop */
 358		w_ctr(ppb, 0x25);
 359		r = ppa_byte_in(ppb, buffer, len);
 360		w_ctr(ppb, 0x4);
 361		w_ctr(ppb, 0xc);
 362		break;
 363
 364	case PPA_EPP_32:
 365	case PPA_EPP_16:
 366	case PPA_EPP_8:
 367		epp_reset(ppb);
 368		w_ctr(ppb, 0x24);
 369#ifdef CONFIG_SCSI_IZIP_EPP16
 370		if (!(((long) buffer | len) & 0x01))
 371			insw(ppb + 4, buffer, len >> 1);
 372#else
 373		if (!(((long) buffer | len) & 0x03))
 374			insl(ppb + 4, buffer, len >> 2);
 375#endif
 
 376		else
 377			insb(ppb + 4, buffer, len);
 378		w_ctr(ppb, 0x2c);
 379		r = !(r_str(ppb) & 0x01);
 380		w_ctr(ppb, 0x2c);
 381		ecp_sync(dev);
 382		break;
 383
 384	default:
 385		printk(KERN_ERR "PPA: bug in ppa_ins()\n");
 386		r = 0;
 387		break;
 388	}
 389	return r;
 390}
 391
 392/* end of ppa_io.h */
 393static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
 394{
 395	w_dtr(ppb, b);
 396	w_ctr(ppb, 0xc);
 397	w_ctr(ppb, 0xe);
 398	w_ctr(ppb, 0xc);
 399	w_ctr(ppb, 0x4);
 400	w_ctr(ppb, 0xc);
 401}
 402
 403static void ppa_disconnect(ppa_struct *dev)
 404{
 405	unsigned short ppb = dev->base;
 406
 407	ppa_d_pulse(ppb, 0);
 408	ppa_d_pulse(ppb, 0x3c);
 409	ppa_d_pulse(ppb, 0x20);
 410	ppa_d_pulse(ppb, 0xf);
 411}
 412
 413static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
 414{
 415	w_dtr(ppb, b);
 416	w_ctr(ppb, 0x4);
 417	w_ctr(ppb, 0x6);
 418	w_ctr(ppb, 0x4);
 419	w_ctr(ppb, 0xc);
 420}
 421
 422static inline void ppa_connect(ppa_struct *dev, int flag)
 423{
 424	unsigned short ppb = dev->base;
 425
 426	ppa_c_pulse(ppb, 0);
 427	ppa_c_pulse(ppb, 0x3c);
 428	ppa_c_pulse(ppb, 0x20);
 429	if ((flag == CONNECT_EPP_MAYBE) && IN_EPP_MODE(dev->mode))
 430		ppa_c_pulse(ppb, 0xcf);
 431	else
 432		ppa_c_pulse(ppb, 0x8f);
 433}
 434
 435static int ppa_select(ppa_struct *dev, int target)
 436{
 437	int k;
 438	unsigned short ppb = dev->base;
 439
 440	/*
 441	 * Bit 6 (0x40) is the device selected bit.
 442	 * First we must wait till the current device goes off line...
 443	 */
 444	k = PPA_SELECT_TMO;
 445	do {
 446		k--;
 447		udelay(1);
 448	} while ((r_str(ppb) & 0x40) && (k));
 449	if (!k)
 450		return 0;
 451
 452	w_dtr(ppb, (1 << target));
 453	w_ctr(ppb, 0xe);
 454	w_ctr(ppb, 0xc);
 455	w_dtr(ppb, 0x80);	/* This is NOT the initator */
 456	w_ctr(ppb, 0x8);
 457
 458	k = PPA_SELECT_TMO;
 459	do {
 460		k--;
 461		udelay(1);
 462	}
 463	while (!(r_str(ppb) & 0x40) && (k));
 464	if (!k)
 465		return 0;
 466
 467	return 1;
 468}
 469
 470/* 
 471 * This is based on a trace of what the Iomega DOS 'guest' driver does.
 472 * I've tried several different kinds of parallel ports with guest and
 473 * coded this to react in the same ways that it does.
 474 * 
 475 * The return value from this function is just a hint about where the
 476 * handshaking failed.
 477 * 
 478 */
 479static int ppa_init(ppa_struct *dev)
 480{
 481	int retv;
 482	unsigned short ppb = dev->base;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 483
 484	ppa_disconnect(dev);
 485	ppa_connect(dev, CONNECT_NORMAL);
 486
 487	retv = 2;		/* Failed */
 488
 489	w_ctr(ppb, 0xe);
 490	if ((r_str(ppb) & 0x08) == 0x08)
 491		retv--;
 492
 493	w_ctr(ppb, 0xc);
 494	if ((r_str(ppb) & 0x08) == 0x00)
 495		retv--;
 496
 497	if (!retv)
 498		ppa_reset_pulse(ppb);
 499	udelay(1000);		/* Allow devices to settle down */
 500	ppa_disconnect(dev);
 501	udelay(1000);		/* Another delay to allow devices to settle */
 502
 503	if (retv)
 504		return -EIO;
 505
 506	return device_check(dev);
 507}
 508
 509static inline int ppa_send_command(struct scsi_cmnd *cmd)
 510{
 511	ppa_struct *dev = ppa_dev(cmd->device->host);
 512	int k;
 513
 514	w_ctr(dev->base, 0x0c);
 515
 516	for (k = 0; k < cmd->cmd_len; k++)
 517		if (!ppa_out(dev, &cmd->cmnd[k], 1))
 518			return 0;
 519	return 1;
 520}
 521
 522/*
 523 * The bulk flag enables some optimisations in the data transfer loops,
 524 * it should be true for any command that transfers data in integral
 525 * numbers of sectors.
 526 * 
 527 * The driver appears to remain stable if we speed up the parallel port
 528 * i/o in this function, but not elsewhere.
 529 */
 530static int ppa_completion(struct scsi_cmnd *cmd)
 531{
 532	/* Return codes:
 533	 * -1     Error
 534	 *  0     Told to schedule
 535	 *  1     Finished data transfer
 536	 */
 
 537	ppa_struct *dev = ppa_dev(cmd->device->host);
 538	unsigned short ppb = dev->base;
 539	unsigned long start_jiffies = jiffies;
 540
 541	unsigned char r, v;
 542	int fast, bulk, status;
 543
 544	v = cmd->cmnd[0];
 545	bulk = ((v == READ_6) ||
 546		(v == READ_10) || (v == WRITE_6) || (v == WRITE_10));
 547
 548	/*
 549	 * We only get here if the drive is ready to comunicate,
 550	 * hence no need for a full ppa_wait.
 551	 */
 552	r = (r_str(ppb) & 0xf0);
 553
 554	while (r != (unsigned char) 0xf0) {
 555		/*
 556		 * If we have been running for more than a full timer tick
 557		 * then take a rest.
 558		 */
 559		if (time_after(jiffies, start_jiffies + 1))
 560			return 0;
 561
 562		if ((cmd->SCp.this_residual <= 0)) {
 563			ppa_fail(dev, DID_ERROR);
 564			return -1;	/* ERROR_RETURN */
 565		}
 566
 567		/* On some hardware we have SCSI disconnected (6th bit low)
 568		 * for about 100usecs. It is too expensive to wait a 
 569		 * tick on every loop so we busy wait for no more than
 570		 * 500usecs to give the drive a chance first. We do not 
 571		 * change things for "normal" hardware since generally 
 572		 * the 6th bit is always high.
 573		 * This makes the CPU load higher on some hardware 
 574		 * but otherwise we can not get more than 50K/secs 
 575		 * on this problem hardware.
 576		 */
 577		if ((r & 0xc0) != 0xc0) {
 578			/* Wait for reconnection should be no more than 
 579			 * jiffy/2 = 5ms = 5000 loops
 580			 */
 581			unsigned long k = dev->recon_tmo;
 582			for (; k && ((r = (r_str(ppb) & 0xf0)) & 0xc0) != 0xc0;
 583			     k--)
 584				udelay(1);
 585
 586			if (!k)
 587				return 0;
 588		}
 589
 590		/* determine if we should use burst I/O */
 591		fast = (bulk && (cmd->SCp.this_residual >= PPA_BURST_SIZE))
 592		    ? PPA_BURST_SIZE : 1;
 593
 594		if (r == (unsigned char) 0xc0)
 595			status = ppa_out(dev, cmd->SCp.ptr, fast);
 596		else
 597			status = ppa_in(dev, cmd->SCp.ptr, fast);
 598
 599		cmd->SCp.ptr += fast;
 600		cmd->SCp.this_residual -= fast;
 601
 602		if (!status) {
 603			ppa_fail(dev, DID_BUS_BUSY);
 604			return -1;	/* ERROR_RETURN */
 605		}
 606		if (cmd->SCp.buffer && !cmd->SCp.this_residual) {
 607			/* if scatter/gather, advance to the next segment */
 608			if (cmd->SCp.buffers_residual--) {
 609				cmd->SCp.buffer++;
 610				cmd->SCp.this_residual =
 611				    cmd->SCp.buffer->length;
 612				cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
 
 
 613			}
 614		}
 615		/* Now check to see if the drive is ready to comunicate */
 616		r = (r_str(ppb) & 0xf0);
 617		/* If not, drop back down to the scheduler and wait a timer tick */
 618		if (!(r & 0x80))
 619			return 0;
 620	}
 621	return 1;		/* FINISH_RETURN */
 622}
 623
 624/*
 625 * Since the PPA itself doesn't generate interrupts, we use
 626 * the scheduler's task queue to generate a stream of call-backs and
 627 * complete the request when the drive is ready.
 628 */
 629static void ppa_interrupt(struct work_struct *work)
 630{
 631	ppa_struct *dev = container_of(work, ppa_struct, ppa_tq.work);
 632	struct scsi_cmnd *cmd = dev->cur_cmd;
 633
 634	if (!cmd) {
 635		printk(KERN_ERR "PPA: bug in ppa_interrupt\n");
 636		return;
 637	}
 638	if (ppa_engine(dev, cmd)) {
 639		schedule_delayed_work(&dev->ppa_tq, 1);
 640		return;
 641	}
 642	/* Command must of completed hence it is safe to let go... */
 643#if PPA_DEBUG > 0
 644	switch ((cmd->result >> 16) & 0xff) {
 645	case DID_OK:
 646		break;
 647	case DID_NO_CONNECT:
 648		printk(KERN_DEBUG "ppa: no device at SCSI ID %i\n", cmd->device->target);
 649		break;
 650	case DID_BUS_BUSY:
 651		printk(KERN_DEBUG "ppa: BUS BUSY - EPP timeout detected\n");
 652		break;
 653	case DID_TIME_OUT:
 654		printk(KERN_DEBUG "ppa: unknown timeout\n");
 655		break;
 656	case DID_ABORT:
 657		printk(KERN_DEBUG "ppa: told to abort\n");
 658		break;
 659	case DID_PARITY:
 660		printk(KERN_DEBUG "ppa: parity error (???)\n");
 661		break;
 662	case DID_ERROR:
 663		printk(KERN_DEBUG "ppa: internal driver error\n");
 664		break;
 665	case DID_RESET:
 666		printk(KERN_DEBUG "ppa: told to reset device\n");
 667		break;
 668	case DID_BAD_INTR:
 669		printk(KERN_WARNING "ppa: bad interrupt (???)\n");
 670		break;
 671	default:
 672		printk(KERN_WARNING "ppa: bad return code (%02x)\n",
 673		       (cmd->result >> 16) & 0xff);
 674	}
 675#endif
 676
 677	if (cmd->SCp.phase > 1)
 678		ppa_disconnect(dev);
 679
 680	ppa_pb_dismiss(dev);
 681
 682	dev->cur_cmd = NULL;
 683
 684	cmd->scsi_done(cmd);
 685}
 686
 687static int ppa_engine(ppa_struct *dev, struct scsi_cmnd *cmd)
 688{
 
 689	unsigned short ppb = dev->base;
 690	unsigned char l = 0, h = 0;
 691	int retv;
 692
 693	/* First check for any errors that may of occurred
 694	 * Here we check for internal errors
 695	 */
 696	if (dev->failed)
 697		return 0;
 698
 699	switch (cmd->SCp.phase) {
 700	case 0:		/* Phase 0 - Waiting for parport */
 701		if (time_after(jiffies, dev->jstart + HZ)) {
 702			/*
 703			 * We waited more than a second
 704			 * for parport to call us
 705			 */
 706			ppa_fail(dev, DID_BUS_BUSY);
 707			return 0;
 708		}
 709		return 1;	/* wait until ppa_wakeup claims parport */
 710	case 1:		/* Phase 1 - Connected */
 711		{		/* Perform a sanity check for cable unplugged */
 712			int retv = 2;	/* Failed */
 713
 714			ppa_connect(dev, CONNECT_EPP_MAYBE);
 715
 716			w_ctr(ppb, 0xe);
 717			if ((r_str(ppb) & 0x08) == 0x08)
 718				retv--;
 719
 720			w_ctr(ppb, 0xc);
 721			if ((r_str(ppb) & 0x08) == 0x00)
 722				retv--;
 723
 724			if (retv) {
 725				if (time_after(jiffies, dev->jstart + (1 * HZ))) {
 726					printk(KERN_ERR "ppa: Parallel port cable is unplugged.\n");
 727					ppa_fail(dev, DID_BUS_BUSY);
 728					return 0;
 729				} else {
 730					ppa_disconnect(dev);
 731					return 1;	/* Try again in a jiffy */
 732				}
 733			}
 734			cmd->SCp.phase++;
 735		}
 
 736
 737	case 2:		/* Phase 2 - We are now talking to the scsi bus */
 738		if (!ppa_select(dev, scmd_id(cmd))) {
 739			ppa_fail(dev, DID_NO_CONNECT);
 740			return 0;
 741		}
 742		cmd->SCp.phase++;
 
 743
 744	case 3:		/* Phase 3 - Ready to accept a command */
 745		w_ctr(ppb, 0x0c);
 746		if (!(r_str(ppb) & 0x80))
 747			return 1;
 748
 749		if (!ppa_send_command(cmd))
 750			return 0;
 751		cmd->SCp.phase++;
 
 752
 753	case 4:		/* Phase 4 - Setup scatter/gather buffers */
 754		if (scsi_bufflen(cmd)) {
 755			cmd->SCp.buffer = scsi_sglist(cmd);
 756			cmd->SCp.this_residual = cmd->SCp.buffer->length;
 757			cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
 
 758		} else {
 759			cmd->SCp.buffer = NULL;
 760			cmd->SCp.this_residual = 0;
 761			cmd->SCp.ptr = NULL;
 762		}
 763		cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
 764		cmd->SCp.phase++;
 
 765
 766	case 5:		/* Phase 5 - Data transfer stage */
 767		w_ctr(ppb, 0x0c);
 768		if (!(r_str(ppb) & 0x80))
 769			return 1;
 770
 771		retv = ppa_completion(cmd);
 772		if (retv == -1)
 773			return 0;
 774		if (retv == 0)
 775			return 1;
 776		cmd->SCp.phase++;
 
 777
 778	case 6:		/* Phase 6 - Read status/message */
 779		cmd->result = DID_OK << 16;
 780		/* Check for data overrun */
 781		if (ppa_wait(dev) != (unsigned char) 0xf0) {
 782			ppa_fail(dev, DID_ERROR);
 783			return 0;
 784		}
 785		if (ppa_in(dev, &l, 1)) {	/* read status byte */
 786			/* Check for optional message byte */
 787			if (ppa_wait(dev) == (unsigned char) 0xf0)
 788				ppa_in(dev, &h, 1);
 789			cmd->result =
 790			    (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
 791		}
 792		return 0;	/* Finished */
 793		break;
 794
 795	default:
 796		printk(KERN_ERR "ppa: Invalid scsi phase\n");
 797	}
 798	return 0;
 799}
 800
 801static int ppa_queuecommand_lck(struct scsi_cmnd *cmd,
 802		void (*done) (struct scsi_cmnd *))
 803{
 804	ppa_struct *dev = ppa_dev(cmd->device->host);
 805
 806	if (dev->cur_cmd) {
 807		printk(KERN_ERR "PPA: bug in ppa_queuecommand\n");
 808		return 0;
 809	}
 810	dev->failed = 0;
 811	dev->jstart = jiffies;
 812	dev->cur_cmd = cmd;
 813	cmd->scsi_done = done;
 814	cmd->result = DID_ERROR << 16;	/* default return code */
 815	cmd->SCp.phase = 0;	/* bus free */
 816
 817	schedule_delayed_work(&dev->ppa_tq, 0);
 818
 819	ppa_pb_claim(dev);
 820
 821	return 0;
 822}
 823
 824static DEF_SCSI_QCMD(ppa_queuecommand)
 825
 826/*
 827 * Apparently the disk->capacity attribute is off by 1 sector 
 828 * for all disk drives.  We add the one here, but it should really
 829 * be done in sd.c.  Even if it gets fixed there, this will still
 830 * work.
 831 */
 832static int ppa_biosparam(struct scsi_device *sdev, struct block_device *dev,
 833	      sector_t capacity, int ip[])
 834{
 835	ip[0] = 0x40;
 836	ip[1] = 0x20;
 837	ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
 838	if (ip[2] > 1024) {
 839		ip[0] = 0xff;
 840		ip[1] = 0x3f;
 841		ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
 842		if (ip[2] > 1023)
 843			ip[2] = 1023;
 844	}
 845	return 0;
 846}
 847
 848static int ppa_abort(struct scsi_cmnd *cmd)
 849{
 850	ppa_struct *dev = ppa_dev(cmd->device->host);
 851	/*
 852	 * There is no method for aborting commands since Iomega
 853	 * have tied the SCSI_MESSAGE line high in the interface
 854	 */
 855
 856	switch (cmd->SCp.phase) {
 857	case 0:		/* Do not have access to parport */
 858	case 1:		/* Have not connected to interface */
 859		dev->cur_cmd = NULL;	/* Forget the problem */
 860		return SUCCESS;
 861		break;
 862	default:		/* SCSI command sent, can not abort */
 863		return FAILED;
 864		break;
 865	}
 866}
 867
 868static void ppa_reset_pulse(unsigned int base)
 869{
 870	w_dtr(base, 0x40);
 871	w_ctr(base, 0x8);
 872	udelay(30);
 873	w_ctr(base, 0xc);
 874}
 875
 876static int ppa_reset(struct scsi_cmnd *cmd)
 877{
 878	ppa_struct *dev = ppa_dev(cmd->device->host);
 879
 880	if (cmd->SCp.phase)
 881		ppa_disconnect(dev);
 882	dev->cur_cmd = NULL;	/* Forget the problem */
 883
 884	ppa_connect(dev, CONNECT_NORMAL);
 885	ppa_reset_pulse(dev->base);
 886	mdelay(1);		/* device settle delay */
 887	ppa_disconnect(dev);
 888	mdelay(1);		/* device settle delay */
 889	return SUCCESS;
 890}
 891
 892static int device_check(ppa_struct *dev)
 893{
 894	/* This routine looks for a device and then attempts to use EPP
 895	   to send a command. If all goes as planned then EPP is available. */
 896
 897	static u8 cmd[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 898	int loop, old_mode, status, k, ppb = dev->base;
 899	unsigned char l;
 900
 901	old_mode = dev->mode;
 902	for (loop = 0; loop < 8; loop++) {
 903		/* Attempt to use EPP for Test Unit Ready */
 904		if ((ppb & 0x0007) == 0x0000)
 905			dev->mode = PPA_EPP_32;
 906
 907second_pass:
 908		ppa_connect(dev, CONNECT_EPP_MAYBE);
 909		/* Select SCSI device */
 910		if (!ppa_select(dev, loop)) {
 911			ppa_disconnect(dev);
 912			continue;
 913		}
 914		printk(KERN_INFO "ppa: Found device at ID %i, Attempting to use %s\n",
 915		       loop, PPA_MODE_STRING[dev->mode]);
 916
 917		/* Send SCSI command */
 918		status = 1;
 919		w_ctr(ppb, 0x0c);
 920		for (l = 0; (l < 6) && (status); l++)
 921			status = ppa_out(dev, cmd, 1);
 922
 923		if (!status) {
 924			ppa_disconnect(dev);
 925			ppa_connect(dev, CONNECT_EPP_MAYBE);
 926			w_dtr(ppb, 0x40);
 927			w_ctr(ppb, 0x08);
 928			udelay(30);
 929			w_ctr(ppb, 0x0c);
 930			udelay(1000);
 931			ppa_disconnect(dev);
 932			udelay(1000);
 933			if (dev->mode == PPA_EPP_32) {
 934				dev->mode = old_mode;
 935				goto second_pass;
 936			}
 937			return -EIO;
 938		}
 939		w_ctr(ppb, 0x0c);
 940		k = 1000000;	/* 1 Second */
 941		do {
 942			l = r_str(ppb);
 943			k--;
 944			udelay(1);
 945		} while (!(l & 0x80) && (k));
 946
 947		l &= 0xf0;
 948
 949		if (l != 0xf0) {
 950			ppa_disconnect(dev);
 951			ppa_connect(dev, CONNECT_EPP_MAYBE);
 952			ppa_reset_pulse(ppb);
 953			udelay(1000);
 954			ppa_disconnect(dev);
 955			udelay(1000);
 956			if (dev->mode == PPA_EPP_32) {
 957				dev->mode = old_mode;
 958				goto second_pass;
 959			}
 960			return -EIO;
 961		}
 962		ppa_disconnect(dev);
 963		printk(KERN_INFO "ppa: Communication established with ID %i using %s\n",
 964		       loop, PPA_MODE_STRING[dev->mode]);
 965		ppa_connect(dev, CONNECT_EPP_MAYBE);
 966		ppa_reset_pulse(ppb);
 967		udelay(1000);
 968		ppa_disconnect(dev);
 969		udelay(1000);
 970		return 0;
 971	}
 972	return -ENODEV;
 973}
 974
 975static int ppa_adjust_queue(struct scsi_device *device)
 976{
 977	blk_queue_bounce_limit(device->request_queue, BLK_BOUNCE_HIGH);
 978	return 0;
 979}
 980
 981static struct scsi_host_template ppa_template = {
 982	.module			= THIS_MODULE,
 983	.proc_name		= "ppa",
 984	.proc_info		= ppa_proc_info,
 
 985	.name			= "Iomega VPI0 (ppa) interface",
 986	.queuecommand		= ppa_queuecommand,
 987	.eh_abort_handler	= ppa_abort,
 988	.eh_bus_reset_handler	= ppa_reset,
 989	.eh_host_reset_handler	= ppa_reset,
 990	.bios_param		= ppa_biosparam,
 991	.this_id		= -1,
 992	.sg_tablesize		= SG_ALL,
 993	.cmd_per_lun		= 1,
 994	.use_clustering		= ENABLE_CLUSTERING,
 995	.can_queue		= 1,
 996	.slave_alloc		= ppa_adjust_queue,
 997};
 998
 999/***************************************************************************
1000 *                   Parallel port probing routines                        *
1001 ***************************************************************************/
1002
1003static LIST_HEAD(ppa_hosts);
1004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1005static int __ppa_attach(struct parport *pb)
1006{
1007	struct Scsi_Host *host;
1008	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waiting);
1009	DEFINE_WAIT(wait);
1010	ppa_struct *dev;
1011	int ports;
1012	int modes, ppb, ppb_hi;
1013	int err = -ENOMEM;
 
1014
1015	dev = kzalloc(sizeof(ppa_struct), GFP_KERNEL);
1016	if (!dev)
1017		return -ENOMEM;
1018	dev->base = -1;
1019	dev->mode = PPA_AUTODETECT;
1020	dev->recon_tmo = PPA_RECON_TMO;
1021	init_waitqueue_head(&waiting);
1022	dev->dev = parport_register_device(pb, "ppa", NULL, ppa_wakeup,
1023					    NULL, 0, dev);
 
 
 
 
 
 
 
1024
1025	if (!dev->dev)
1026		goto out;
1027
1028	/* Claim the bus so it remembers what we do to the control
1029	 * registers. [ CTR and ECP ]
1030	 */
1031	err = -EBUSY;
1032	dev->waiting = &waiting;
1033	prepare_to_wait(&waiting, &wait, TASK_UNINTERRUPTIBLE);
1034	if (ppa_pb_claim(dev))
1035		schedule_timeout(3 * HZ);
1036	if (dev->wanted) {
1037		printk(KERN_ERR "ppa%d: failed to claim parport because "
1038				"a pardevice is owning the port for too long "
1039				"time!\n", pb->number);
1040		ppa_pb_dismiss(dev);
1041		dev->waiting = NULL;
1042		finish_wait(&waiting, &wait);
1043		goto out1;
1044	}
1045	dev->waiting = NULL;
1046	finish_wait(&waiting, &wait);
1047	ppb = dev->base = dev->dev->port->base;
1048	ppb_hi = dev->dev->port->base_hi;
1049	w_ctr(ppb, 0x0c);
1050	modes = dev->dev->port->modes;
1051
1052	/* Mode detection works up the chain of speed
1053	 * This avoids a nasty if-then-else-if-... tree
1054	 */
1055	dev->mode = PPA_NIBBLE;
1056
1057	if (modes & PARPORT_MODE_TRISTATE)
1058		dev->mode = PPA_PS2;
1059
1060	if (modes & PARPORT_MODE_ECP) {
1061		w_ecr(ppb_hi, 0x20);
1062		dev->mode = PPA_PS2;
1063	}
1064	if ((modes & PARPORT_MODE_EPP) && (modes & PARPORT_MODE_ECP))
1065		w_ecr(ppb_hi, 0x80);
1066
1067	/* Done configuration */
1068
1069	err = ppa_init(dev);
1070	ppa_pb_release(dev);
1071
1072	if (err)
1073		goto out1;
1074
1075	/* now the glue ... */
1076	if (dev->mode == PPA_NIBBLE || dev->mode == PPA_PS2)
1077		ports = 3;
1078	else
1079		ports = 8;
1080
1081	INIT_DELAYED_WORK(&dev->ppa_tq, ppa_interrupt);
1082
1083	err = -ENOMEM;
1084	host = scsi_host_alloc(&ppa_template, sizeof(ppa_struct *));
1085	if (!host)
1086		goto out1;
 
1087	host->io_port = pb->base;
1088	host->n_io_port = ports;
1089	host->dma_channel = -1;
1090	host->unique_id = pb->number;
1091	*(ppa_struct **)&host->hostdata = dev;
1092	dev->host = host;
1093	list_add_tail(&dev->list, &ppa_hosts);
1094	err = scsi_add_host(host, NULL);
1095	if (err)
1096		goto out2;
1097	scsi_scan_host(host);
1098	return 0;
1099out2:
1100	list_del_init(&dev->list);
1101	scsi_host_put(host);
1102out1:
1103	parport_unregister_device(dev->dev);
1104out:
1105	kfree(dev);
1106	return err;
1107}
1108
1109static void ppa_attach(struct parport *pb)
1110{
1111	__ppa_attach(pb);
1112}
1113
1114static void ppa_detach(struct parport *pb)
1115{
1116	ppa_struct *dev;
1117	list_for_each_entry(dev, &ppa_hosts, list) {
1118		if (dev->dev->port == pb) {
1119			list_del_init(&dev->list);
1120			scsi_remove_host(dev->host);
1121			scsi_host_put(dev->host);
1122			parport_unregister_device(dev->dev);
1123			kfree(dev);
1124			break;
1125		}
1126	}
1127}
1128
1129static struct parport_driver ppa_driver = {
1130	.name	= "ppa",
1131	.attach	= ppa_attach,
1132	.detach	= ppa_detach,
1133};
 
1134
1135static int __init ppa_driver_init(void)
1136{
1137	printk(KERN_INFO "ppa: Version %s\n", PPA_VERSION);
1138	return parport_register_driver(&ppa_driver);
1139}
1140
1141static void __exit ppa_driver_exit(void)
1142{
1143	parport_unregister_driver(&ppa_driver);
1144}
1145
1146module_init(ppa_driver_init);
1147module_exit(ppa_driver_exit);
1148MODULE_LICENSE("GPL");
v6.13.7
   1/* ppa.c   --  low level driver for the IOMEGA PPA3 
   2 * parallel port SCSI host adapter.
   3 * 
   4 * (The PPA3 is the embedded controller in the ZIP drive.)
   5 * 
   6 * (c) 1995,1996 Grant R. Guenther, grant@torque.net,
   7 * under the terms of the GNU General Public License.
   8 * 
   9 */
  10
  11#include <linux/init.h>
  12#include <linux/kernel.h>
  13#include <linux/slab.h>
  14#include <linux/module.h>
  15#include <linux/blkdev.h>
  16#include <linux/parport.h>
  17#include <linux/workqueue.h>
  18#include <linux/delay.h>
  19#include <linux/jiffies.h>
  20#include <asm/io.h>
  21
  22#include <scsi/scsi.h>
  23#include <scsi/scsi_cmnd.h>
  24#include <scsi/scsi_device.h>
  25#include <scsi/scsi_host.h>
  26
  27
  28static void ppa_reset_pulse(unsigned int base);
  29
  30typedef struct {
  31	struct pardevice *dev;	/* Parport device entry         */
  32	int base;		/* Actual port address          */
  33	int mode;		/* Transfer mode                */
  34	struct scsi_cmnd *cur_cmd;	/* Current queued command       */
  35	struct delayed_work ppa_tq;	/* Polling interrupt stuff       */
  36	unsigned long jstart;	/* Jiffies at start             */
  37	unsigned long recon_tmo;	/* How many usecs to wait for reconnection (6th bit) */
  38	unsigned int failed:1;	/* Failure flag                 */
  39	unsigned wanted:1;	/* Parport sharing busy flag    */
  40	unsigned int dev_no;	/* Device number		*/
  41	wait_queue_head_t *waiting;
  42	struct Scsi_Host *host;
  43	struct list_head list;
  44} ppa_struct;
  45
  46#include  "ppa.h"
  47
  48static unsigned int mode = PPA_AUTODETECT;
  49module_param(mode, uint, 0644);
  50MODULE_PARM_DESC(mode, "Transfer mode (0 = Autodetect, 1 = SPP 4-bit, "
  51	"2 = SPP 8-bit, 3 = EPP 8-bit, 4 = EPP 16-bit, 5 = EPP 32-bit");
  52
  53static struct scsi_pointer *ppa_scsi_pointer(struct scsi_cmnd *cmd)
  54{
  55	return scsi_cmd_priv(cmd);
  56}
  57
  58static inline ppa_struct *ppa_dev(struct Scsi_Host *host)
  59{
  60	return *(ppa_struct **)&host->hostdata;
  61}
  62
  63static DEFINE_SPINLOCK(arbitration_lock);
  64
  65static void got_it(ppa_struct *dev)
  66{
  67	dev->base = dev->dev->port->base;
  68	if (dev->cur_cmd)
  69		ppa_scsi_pointer(dev->cur_cmd)->phase = 1;
  70	else
  71		wake_up(dev->waiting);
  72}
  73
  74static void ppa_wakeup(void *ref)
  75{
  76	ppa_struct *dev = (ppa_struct *) ref;
  77	unsigned long flags;
  78
  79	spin_lock_irqsave(&arbitration_lock, flags);
  80	if (dev->wanted) {
  81		parport_claim(dev->dev);
  82		got_it(dev);
  83		dev->wanted = 0;
  84	}
  85	spin_unlock_irqrestore(&arbitration_lock, flags);
  86	return;
  87}
  88
  89static int ppa_pb_claim(ppa_struct *dev)
  90{
  91	unsigned long flags;
  92	int res = 1;
  93	spin_lock_irqsave(&arbitration_lock, flags);
  94	if (parport_claim(dev->dev) == 0) {
  95		got_it(dev);
  96		res = 0;
  97	}
  98	dev->wanted = res;
  99	spin_unlock_irqrestore(&arbitration_lock, flags);
 100	return res;
 101}
 102
 103static void ppa_pb_dismiss(ppa_struct *dev)
 104{
 105	unsigned long flags;
 106	int wanted;
 107	spin_lock_irqsave(&arbitration_lock, flags);
 108	wanted = dev->wanted;
 109	dev->wanted = 0;
 110	spin_unlock_irqrestore(&arbitration_lock, flags);
 111	if (!wanted)
 112		parport_release(dev->dev);
 113}
 114
 115static inline void ppa_pb_release(ppa_struct *dev)
 116{
 117	parport_release(dev->dev);
 118}
 119
 120/*
 121 * Start of Chipset kludges
 122 */
 123
 124/* This is to give the ppa driver a way to modify the timings (and other
 125 * parameters) by writing to the /proc/scsi/ppa/0 file.
 126 * Very simple method really... (To simple, no error checking :( )
 127 * Reason: Kernel hackers HATE having to unload and reload modules for
 128 * testing...
 129 * Also gives a method to use a script to obtain optimum timings (TODO)
 130 */
 131
 132static inline int ppa_write_info(struct Scsi_Host *host, char *buffer, int length)
 133{
 134	ppa_struct *dev = ppa_dev(host);
 135	unsigned long x;
 136
 137	if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
 138		x = simple_strtoul(buffer + 5, NULL, 0);
 139		dev->mode = x;
 140		return length;
 141	}
 142	if ((length > 10) && (strncmp(buffer, "recon_tmo=", 10) == 0)) {
 143		x = simple_strtoul(buffer + 10, NULL, 0);
 144		dev->recon_tmo = x;
 145		printk(KERN_INFO "ppa: recon_tmo set to %ld\n", x);
 146		return length;
 147	}
 148	printk(KERN_WARNING "ppa /proc: invalid variable\n");
 149	return -EINVAL;
 150}
 151
 152static int ppa_show_info(struct seq_file *m, struct Scsi_Host *host)
 153{
 
 154	ppa_struct *dev = ppa_dev(host);
 155
 156	seq_printf(m, "Version : %s\n", PPA_VERSION);
 157	seq_printf(m, "Parport : %s\n", dev->dev->port->name);
 158	seq_printf(m, "Mode    : %s\n", PPA_MODE_STRING[dev->mode]);
 
 
 
 
 
 
 
 159#if PPA_DEBUG > 0
 160	seq_printf(m, "recon_tmo : %lu\n", dev->recon_tmo);
 
 161#endif
 162	return 0;
 
 
 
 
 
 
 
 
 
 163}
 164
 165static int device_check(ppa_struct *dev, bool autodetect);
 166
 167#if PPA_DEBUG > 0
 168#define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
 169	   y, __func__, __LINE__); ppa_fail_func(x,y);
 170static inline void ppa_fail_func(ppa_struct *dev, int error_code)
 171#else
 172static inline void ppa_fail(ppa_struct *dev, int error_code)
 173#endif
 174{
 175	/* If we fail a device then we trash status / message bytes */
 176	if (dev->cur_cmd) {
 177		dev->cur_cmd->result = error_code << 16;
 178		dev->failed = 1;
 179	}
 180}
 181
 182/*
 183 * Wait for the high bit to be set.
 184 * 
 185 * In principle, this could be tied to an interrupt, but the adapter
 186 * doesn't appear to be designed to support interrupts.  We spin on
 187 * the 0x80 ready bit. 
 188 */
 189static unsigned char ppa_wait(ppa_struct *dev)
 190{
 191	int k;
 192	unsigned short ppb = dev->base;
 193	unsigned char r;
 194
 195	k = PPA_SPIN_TMO;
 196	/* Wait for bit 6 and 7 - PJC */
 197	for (r = r_str(ppb); ((r & 0xc0) != 0xc0) && (k); k--) {
 198		udelay(1);
 199		r = r_str(ppb);
 200	}
 201
 202	/*
 203	 * return some status information.
 204	 * Semantics: 0xc0 = ZIP wants more data
 205	 *            0xd0 = ZIP wants to send more data
 206	 *            0xe0 = ZIP is expecting SCSI command data
 207	 *            0xf0 = end of transfer, ZIP is sending status
 208	 */
 209	if (k)
 210		return (r & 0xf0);
 211
 212	/* Counter expired - Time out occurred */
 213	ppa_fail(dev, DID_TIME_OUT);
 214	printk(KERN_WARNING "ppa timeout in ppa_wait\n");
 215	return 0;		/* command timed out */
 216}
 217
 218/*
 219 * Clear EPP Timeout Bit 
 220 */
 221static inline void epp_reset(unsigned short ppb)
 222{
 223	int i;
 224
 225	i = r_str(ppb);
 226	w_str(ppb, i);
 227	w_str(ppb, i & 0xfe);
 228}
 229
 230/* 
 231 * Wait for empty ECP fifo (if we are in ECP fifo mode only)
 232 */
 233static inline void ecp_sync(ppa_struct *dev)
 234{
 235	int i, ppb_hi = dev->dev->port->base_hi;
 236
 237	if (ppb_hi == 0)
 238		return;
 239
 240	if ((r_ecr(ppb_hi) & 0xe0) == 0x60) {	/* mode 011 == ECP fifo mode */
 241		for (i = 0; i < 100; i++) {
 242			if (r_ecr(ppb_hi) & 0x01)
 243				return;
 244			udelay(5);
 245		}
 246		printk(KERN_WARNING "ppa: ECP sync failed as data still present in FIFO.\n");
 247	}
 248}
 249
 250static int ppa_byte_out(unsigned short base, const char *buffer, int len)
 251{
 252	int i;
 253
 254	for (i = len; i; i--) {
 255		w_dtr(base, *buffer++);
 256		w_ctr(base, 0xe);
 257		w_ctr(base, 0xc);
 258	}
 259	return 1;		/* All went well - we hope! */
 260}
 261
 262static int ppa_byte_in(unsigned short base, char *buffer, int len)
 263{
 264	int i;
 265
 266	for (i = len; i; i--) {
 267		*buffer++ = r_dtr(base);
 268		w_ctr(base, 0x27);
 269		w_ctr(base, 0x25);
 270	}
 271	return 1;		/* All went well - we hope! */
 272}
 273
 274static int ppa_nibble_in(unsigned short base, char *buffer, int len)
 275{
 276	for (; len; len--) {
 277		unsigned char h;
 278
 279		w_ctr(base, 0x4);
 280		h = r_str(base) & 0xf0;
 281		w_ctr(base, 0x6);
 282		*buffer++ = h | ((r_str(base) & 0xf0) >> 4);
 283	}
 284	return 1;		/* All went well - we hope! */
 285}
 286
 287static int ppa_out(ppa_struct *dev, char *buffer, int len)
 288{
 289	int r;
 290	unsigned short ppb = dev->base;
 291
 292	r = ppa_wait(dev);
 293
 294	if ((r & 0x50) != 0x40) {
 295		ppa_fail(dev, DID_ERROR);
 296		return 0;
 297	}
 298	switch (dev->mode) {
 299	case PPA_NIBBLE:
 300	case PPA_PS2:
 301		/* 8 bit output, with a loop */
 302		r = ppa_byte_out(ppb, buffer, len);
 303		break;
 304
 305	case PPA_EPP_32:
 306	case PPA_EPP_16:
 307	case PPA_EPP_8:
 308		epp_reset(ppb);
 309		w_ctr(ppb, 0x4);
 310		if (dev->mode == PPA_EPP_32 && !(((long) buffer | len) & 0x03))
 
 
 
 
 311			outsl(ppb + 4, buffer, len >> 2);
 312		else if (dev->mode == PPA_EPP_16 && !(((long) buffer | len) & 0x01))
 313			outsw(ppb + 4, buffer, len >> 1);
 314		else
 315			outsb(ppb + 4, buffer, len);
 316		w_ctr(ppb, 0xc);
 317		r = !(r_str(ppb) & 0x01);
 318		w_ctr(ppb, 0xc);
 319		ecp_sync(dev);
 320		break;
 321
 322	default:
 323		printk(KERN_ERR "PPA: bug in ppa_out()\n");
 324		r = 0;
 325	}
 326	return r;
 327}
 328
 329static int ppa_in(ppa_struct *dev, char *buffer, int len)
 330{
 331	int r;
 332	unsigned short ppb = dev->base;
 333
 334	r = ppa_wait(dev);
 335
 336	if ((r & 0x50) != 0x50) {
 337		ppa_fail(dev, DID_ERROR);
 338		return 0;
 339	}
 340	switch (dev->mode) {
 341	case PPA_NIBBLE:
 342		/* 4 bit input, with a loop */
 343		r = ppa_nibble_in(ppb, buffer, len);
 344		w_ctr(ppb, 0xc);
 345		break;
 346
 347	case PPA_PS2:
 348		/* 8 bit input, with a loop */
 349		w_ctr(ppb, 0x25);
 350		r = ppa_byte_in(ppb, buffer, len);
 351		w_ctr(ppb, 0x4);
 352		w_ctr(ppb, 0xc);
 353		break;
 354
 355	case PPA_EPP_32:
 356	case PPA_EPP_16:
 357	case PPA_EPP_8:
 358		epp_reset(ppb);
 359		w_ctr(ppb, 0x24);
 360		if (dev->mode == PPA_EPP_32 && !(((long) buffer | len) & 0x03))
 
 
 
 
 361			insl(ppb + 4, buffer, len >> 2);
 362		else if (dev->mode == PPA_EPP_16 && !(((long) buffer | len) & 0x01))
 363			insw(ppb + 4, buffer, len >> 1);
 364		else
 365			insb(ppb + 4, buffer, len);
 366		w_ctr(ppb, 0x2c);
 367		r = !(r_str(ppb) & 0x01);
 368		w_ctr(ppb, 0x2c);
 369		ecp_sync(dev);
 370		break;
 371
 372	default:
 373		printk(KERN_ERR "PPA: bug in ppa_ins()\n");
 374		r = 0;
 375		break;
 376	}
 377	return r;
 378}
 379
 380/* end of ppa_io.h */
 381static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
 382{
 383	w_dtr(ppb, b);
 384	w_ctr(ppb, 0xc);
 385	w_ctr(ppb, 0xe);
 386	w_ctr(ppb, 0xc);
 387	w_ctr(ppb, 0x4);
 388	w_ctr(ppb, 0xc);
 389}
 390
 391static void ppa_disconnect(ppa_struct *dev)
 392{
 393	unsigned short ppb = dev->base;
 394
 395	ppa_d_pulse(ppb, 0);
 396	ppa_d_pulse(ppb, 0x3c);
 397	ppa_d_pulse(ppb, 0x20);
 398	ppa_d_pulse(ppb, 0xf);
 399}
 400
 401static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
 402{
 403	w_dtr(ppb, b);
 404	w_ctr(ppb, 0x4);
 405	w_ctr(ppb, 0x6);
 406	w_ctr(ppb, 0x4);
 407	w_ctr(ppb, 0xc);
 408}
 409
 410static inline void ppa_connect(ppa_struct *dev, int flag)
 411{
 412	unsigned short ppb = dev->base;
 413
 414	ppa_c_pulse(ppb, 0);
 415	ppa_c_pulse(ppb, 0x3c);
 416	ppa_c_pulse(ppb, 0x20);
 417	if ((flag == CONNECT_EPP_MAYBE) && IN_EPP_MODE(dev->mode))
 418		ppa_c_pulse(ppb, 0xcf);
 419	else
 420		ppa_c_pulse(ppb, 0x8f);
 421}
 422
 423static int ppa_select(ppa_struct *dev, int target)
 424{
 425	int k;
 426	unsigned short ppb = dev->base;
 427
 428	/*
 429	 * Bit 6 (0x40) is the device selected bit.
 430	 * First we must wait till the current device goes off line...
 431	 */
 432	k = PPA_SELECT_TMO;
 433	do {
 434		k--;
 435		udelay(1);
 436	} while ((r_str(ppb) & 0x40) && (k));
 437	if (!k)
 438		return 0;
 439
 440	w_dtr(ppb, (1 << target));
 441	w_ctr(ppb, 0xe);
 442	w_ctr(ppb, 0xc);
 443	w_dtr(ppb, 0x80);	/* This is NOT the initator */
 444	w_ctr(ppb, 0x8);
 445
 446	k = PPA_SELECT_TMO;
 447	do {
 448		k--;
 449		udelay(1);
 450	}
 451	while (!(r_str(ppb) & 0x40) && (k));
 452	if (!k)
 453		return 0;
 454
 455	return 1;
 456}
 457
 458/* 
 459 * This is based on a trace of what the Iomega DOS 'guest' driver does.
 460 * I've tried several different kinds of parallel ports with guest and
 461 * coded this to react in the same ways that it does.
 462 * 
 463 * The return value from this function is just a hint about where the
 464 * handshaking failed.
 465 * 
 466 */
 467static int ppa_init(ppa_struct *dev)
 468{
 469	int retv;
 470	unsigned short ppb = dev->base;
 471	bool autodetect = dev->mode == PPA_AUTODETECT;
 472
 473	if (autodetect) {
 474		int modes = dev->dev->port->modes;
 475		int ppb_hi = dev->dev->port->base_hi;
 476
 477		/* Mode detection works up the chain of speed
 478		 * This avoids a nasty if-then-else-if-... tree
 479		 */
 480		dev->mode = PPA_NIBBLE;
 481
 482		if (modes & PARPORT_MODE_TRISTATE)
 483			dev->mode = PPA_PS2;
 484
 485		if (modes & PARPORT_MODE_ECP) {
 486			w_ecr(ppb_hi, 0x20);
 487			dev->mode = PPA_PS2;
 488		}
 489		if ((modes & PARPORT_MODE_EPP) && (modes & PARPORT_MODE_ECP))
 490			w_ecr(ppb_hi, 0x80);
 491	}
 492
 493	ppa_disconnect(dev);
 494	ppa_connect(dev, CONNECT_NORMAL);
 495
 496	retv = 2;		/* Failed */
 497
 498	w_ctr(ppb, 0xe);
 499	if ((r_str(ppb) & 0x08) == 0x08)
 500		retv--;
 501
 502	w_ctr(ppb, 0xc);
 503	if ((r_str(ppb) & 0x08) == 0x00)
 504		retv--;
 505
 506	if (!retv)
 507		ppa_reset_pulse(ppb);
 508	udelay(1000);		/* Allow devices to settle down */
 509	ppa_disconnect(dev);
 510	udelay(1000);		/* Another delay to allow devices to settle */
 511
 512	if (retv)
 513		return -EIO;
 514
 515	return device_check(dev, autodetect);
 516}
 517
 518static inline int ppa_send_command(struct scsi_cmnd *cmd)
 519{
 520	ppa_struct *dev = ppa_dev(cmd->device->host);
 521	int k;
 522
 523	w_ctr(dev->base, 0x0c);
 524
 525	for (k = 0; k < cmd->cmd_len; k++)
 526		if (!ppa_out(dev, &cmd->cmnd[k], 1))
 527			return 0;
 528	return 1;
 529}
 530
 531/*
 532 * The bulk flag enables some optimisations in the data transfer loops,
 533 * it should be true for any command that transfers data in integral
 534 * numbers of sectors.
 535 * 
 536 * The driver appears to remain stable if we speed up the parallel port
 537 * i/o in this function, but not elsewhere.
 538 */
 539static int ppa_completion(struct scsi_cmnd *const cmd)
 540{
 541	/* Return codes:
 542	 * -1     Error
 543	 *  0     Told to schedule
 544	 *  1     Finished data transfer
 545	 */
 546	struct scsi_pointer *scsi_pointer = ppa_scsi_pointer(cmd);
 547	ppa_struct *dev = ppa_dev(cmd->device->host);
 548	unsigned short ppb = dev->base;
 549	unsigned long start_jiffies = jiffies;
 550
 551	unsigned char r, v;
 552	int fast, bulk, status;
 553
 554	v = cmd->cmnd[0];
 555	bulk = ((v == READ_6) ||
 556		(v == READ_10) || (v == WRITE_6) || (v == WRITE_10));
 557
 558	/*
 559	 * We only get here if the drive is ready to comunicate,
 560	 * hence no need for a full ppa_wait.
 561	 */
 562	r = (r_str(ppb) & 0xf0);
 563
 564	while (r != (unsigned char) 0xf0) {
 565		/*
 566		 * If we have been running for more than a full timer tick
 567		 * then take a rest.
 568		 */
 569		if (time_after(jiffies, start_jiffies + 1))
 570			return 0;
 571
 572		if (scsi_pointer->this_residual <= 0) {
 573			ppa_fail(dev, DID_ERROR);
 574			return -1;	/* ERROR_RETURN */
 575		}
 576
 577		/* On some hardware we have SCSI disconnected (6th bit low)
 578		 * for about 100usecs. It is too expensive to wait a 
 579		 * tick on every loop so we busy wait for no more than
 580		 * 500usecs to give the drive a chance first. We do not 
 581		 * change things for "normal" hardware since generally 
 582		 * the 6th bit is always high.
 583		 * This makes the CPU load higher on some hardware 
 584		 * but otherwise we can not get more than 50K/secs 
 585		 * on this problem hardware.
 586		 */
 587		if ((r & 0xc0) != 0xc0) {
 588			/* Wait for reconnection should be no more than 
 589			 * jiffy/2 = 5ms = 5000 loops
 590			 */
 591			unsigned long k = dev->recon_tmo;
 592			for (; k && ((r = (r_str(ppb) & 0xf0)) & 0xc0) != 0xc0;
 593			     k--)
 594				udelay(1);
 595
 596			if (!k)
 597				return 0;
 598		}
 599
 600		/* determine if we should use burst I/O */
 601		fast = bulk && scsi_pointer->this_residual >= PPA_BURST_SIZE ?
 602			PPA_BURST_SIZE : 1;
 603
 604		if (r == (unsigned char) 0xc0)
 605			status = ppa_out(dev, scsi_pointer->ptr, fast);
 606		else
 607			status = ppa_in(dev, scsi_pointer->ptr, fast);
 608
 609		scsi_pointer->ptr += fast;
 610		scsi_pointer->this_residual -= fast;
 611
 612		if (!status) {
 613			ppa_fail(dev, DID_BUS_BUSY);
 614			return -1;	/* ERROR_RETURN */
 615		}
 616		if (scsi_pointer->buffer && !scsi_pointer->this_residual) {
 617			/* if scatter/gather, advance to the next segment */
 618			if (scsi_pointer->buffers_residual--) {
 619				scsi_pointer->buffer =
 620					sg_next(scsi_pointer->buffer);
 621				scsi_pointer->this_residual =
 622				    scsi_pointer->buffer->length;
 623				scsi_pointer->ptr =
 624					sg_virt(scsi_pointer->buffer);
 625			}
 626		}
 627		/* Now check to see if the drive is ready to comunicate */
 628		r = (r_str(ppb) & 0xf0);
 629		/* If not, drop back down to the scheduler and wait a timer tick */
 630		if (!(r & 0x80))
 631			return 0;
 632	}
 633	return 1;		/* FINISH_RETURN */
 634}
 635
 636/*
 637 * Since the PPA itself doesn't generate interrupts, we use
 638 * the scheduler's task queue to generate a stream of call-backs and
 639 * complete the request when the drive is ready.
 640 */
 641static void ppa_interrupt(struct work_struct *work)
 642{
 643	ppa_struct *dev = container_of(work, ppa_struct, ppa_tq.work);
 644	struct scsi_cmnd *cmd = dev->cur_cmd;
 645
 646	if (!cmd) {
 647		printk(KERN_ERR "PPA: bug in ppa_interrupt\n");
 648		return;
 649	}
 650	if (ppa_engine(dev, cmd)) {
 651		schedule_delayed_work(&dev->ppa_tq, 1);
 652		return;
 653	}
 654	/* Command must of completed hence it is safe to let go... */
 655#if PPA_DEBUG > 0
 656	switch ((cmd->result >> 16) & 0xff) {
 657	case DID_OK:
 658		break;
 659	case DID_NO_CONNECT:
 660		printk(KERN_DEBUG "ppa: no device at SCSI ID %i\n", scmd_id(cmd));
 661		break;
 662	case DID_BUS_BUSY:
 663		printk(KERN_DEBUG "ppa: BUS BUSY - EPP timeout detected\n");
 664		break;
 665	case DID_TIME_OUT:
 666		printk(KERN_DEBUG "ppa: unknown timeout\n");
 667		break;
 668	case DID_ABORT:
 669		printk(KERN_DEBUG "ppa: told to abort\n");
 670		break;
 671	case DID_PARITY:
 672		printk(KERN_DEBUG "ppa: parity error (???)\n");
 673		break;
 674	case DID_ERROR:
 675		printk(KERN_DEBUG "ppa: internal driver error\n");
 676		break;
 677	case DID_RESET:
 678		printk(KERN_DEBUG "ppa: told to reset device\n");
 679		break;
 680	case DID_BAD_INTR:
 681		printk(KERN_WARNING "ppa: bad interrupt (???)\n");
 682		break;
 683	default:
 684		printk(KERN_WARNING "ppa: bad return code (%02x)\n",
 685		       (cmd->result >> 16) & 0xff);
 686	}
 687#endif
 688
 689	if (ppa_scsi_pointer(cmd)->phase > 1)
 690		ppa_disconnect(dev);
 691
 692	ppa_pb_dismiss(dev);
 693
 694	dev->cur_cmd = NULL;
 695
 696	scsi_done(cmd);
 697}
 698
 699static int ppa_engine(ppa_struct *dev, struct scsi_cmnd *cmd)
 700{
 701	struct scsi_pointer *scsi_pointer = ppa_scsi_pointer(cmd);
 702	unsigned short ppb = dev->base;
 703	unsigned char l = 0, h = 0;
 704	int retv;
 705
 706	/* First check for any errors that may of occurred
 707	 * Here we check for internal errors
 708	 */
 709	if (dev->failed)
 710		return 0;
 711
 712	switch (scsi_pointer->phase) {
 713	case 0:		/* Phase 0 - Waiting for parport */
 714		if (time_after(jiffies, dev->jstart + HZ)) {
 715			/*
 716			 * We waited more than a second
 717			 * for parport to call us
 718			 */
 719			ppa_fail(dev, DID_BUS_BUSY);
 720			return 0;
 721		}
 722		return 1;	/* wait until ppa_wakeup claims parport */
 723	case 1:		/* Phase 1 - Connected */
 724		{		/* Perform a sanity check for cable unplugged */
 725			int retv = 2;	/* Failed */
 726
 727			ppa_connect(dev, CONNECT_EPP_MAYBE);
 728
 729			w_ctr(ppb, 0xe);
 730			if ((r_str(ppb) & 0x08) == 0x08)
 731				retv--;
 732
 733			w_ctr(ppb, 0xc);
 734			if ((r_str(ppb) & 0x08) == 0x00)
 735				retv--;
 736
 737			if (retv) {
 738				if (time_after(jiffies, dev->jstart + (1 * HZ))) {
 739					printk(KERN_ERR "ppa: Parallel port cable is unplugged.\n");
 740					ppa_fail(dev, DID_BUS_BUSY);
 741					return 0;
 742				} else {
 743					ppa_disconnect(dev);
 744					return 1;	/* Try again in a jiffy */
 745				}
 746			}
 747			scsi_pointer->phase++;
 748		}
 749		fallthrough;
 750
 751	case 2:		/* Phase 2 - We are now talking to the scsi bus */
 752		if (!ppa_select(dev, scmd_id(cmd))) {
 753			ppa_fail(dev, DID_NO_CONNECT);
 754			return 0;
 755		}
 756		scsi_pointer->phase++;
 757		fallthrough;
 758
 759	case 3:		/* Phase 3 - Ready to accept a command */
 760		w_ctr(ppb, 0x0c);
 761		if (!(r_str(ppb) & 0x80))
 762			return 1;
 763
 764		if (!ppa_send_command(cmd))
 765			return 0;
 766		scsi_pointer->phase++;
 767		fallthrough;
 768
 769	case 4:		/* Phase 4 - Setup scatter/gather buffers */
 770		if (scsi_bufflen(cmd)) {
 771			scsi_pointer->buffer = scsi_sglist(cmd);
 772			scsi_pointer->this_residual =
 773				scsi_pointer->buffer->length;
 774			scsi_pointer->ptr = sg_virt(scsi_pointer->buffer);
 775		} else {
 776			scsi_pointer->buffer = NULL;
 777			scsi_pointer->this_residual = 0;
 778			scsi_pointer->ptr = NULL;
 779		}
 780		scsi_pointer->buffers_residual = scsi_sg_count(cmd) - 1;
 781		scsi_pointer->phase++;
 782		fallthrough;
 783
 784	case 5:		/* Phase 5 - Data transfer stage */
 785		w_ctr(ppb, 0x0c);
 786		if (!(r_str(ppb) & 0x80))
 787			return 1;
 788
 789		retv = ppa_completion(cmd);
 790		if (retv == -1)
 791			return 0;
 792		if (retv == 0)
 793			return 1;
 794		scsi_pointer->phase++;
 795		fallthrough;
 796
 797	case 6:		/* Phase 6 - Read status/message */
 798		cmd->result = DID_OK << 16;
 799		/* Check for data overrun */
 800		if (ppa_wait(dev) != (unsigned char) 0xf0) {
 801			ppa_fail(dev, DID_ERROR);
 802			return 0;
 803		}
 804		if (ppa_in(dev, &l, 1)) {	/* read status byte */
 805			/* Check for optional message byte */
 806			if (ppa_wait(dev) == (unsigned char) 0xf0)
 807				ppa_in(dev, &h, 1);
 808			cmd->result =
 809			    (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
 810		}
 811		return 0;	/* Finished */
 
 812
 813	default:
 814		printk(KERN_ERR "ppa: Invalid scsi phase\n");
 815	}
 816	return 0;
 817}
 818
 819static int ppa_queuecommand_lck(struct scsi_cmnd *cmd)
 
 820{
 821	ppa_struct *dev = ppa_dev(cmd->device->host);
 822
 823	if (dev->cur_cmd) {
 824		printk(KERN_ERR "PPA: bug in ppa_queuecommand\n");
 825		return 0;
 826	}
 827	dev->failed = 0;
 828	dev->jstart = jiffies;
 829	dev->cur_cmd = cmd;
 
 830	cmd->result = DID_ERROR << 16;	/* default return code */
 831	ppa_scsi_pointer(cmd)->phase = 0;	/* bus free */
 832
 833	schedule_delayed_work(&dev->ppa_tq, 0);
 834
 835	ppa_pb_claim(dev);
 836
 837	return 0;
 838}
 839
 840static DEF_SCSI_QCMD(ppa_queuecommand)
 841
 842/*
 843 * Apparently the disk->capacity attribute is off by 1 sector 
 844 * for all disk drives.  We add the one here, but it should really
 845 * be done in sd.c.  Even if it gets fixed there, this will still
 846 * work.
 847 */
 848static int ppa_biosparam(struct scsi_device *sdev, struct block_device *dev,
 849	      sector_t capacity, int ip[])
 850{
 851	ip[0] = 0x40;
 852	ip[1] = 0x20;
 853	ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
 854	if (ip[2] > 1024) {
 855		ip[0] = 0xff;
 856		ip[1] = 0x3f;
 857		ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
 858		if (ip[2] > 1023)
 859			ip[2] = 1023;
 860	}
 861	return 0;
 862}
 863
 864static int ppa_abort(struct scsi_cmnd *cmd)
 865{
 866	ppa_struct *dev = ppa_dev(cmd->device->host);
 867	/*
 868	 * There is no method for aborting commands since Iomega
 869	 * have tied the SCSI_MESSAGE line high in the interface
 870	 */
 871
 872	switch (ppa_scsi_pointer(cmd)->phase) {
 873	case 0:		/* Do not have access to parport */
 874	case 1:		/* Have not connected to interface */
 875		dev->cur_cmd = NULL;	/* Forget the problem */
 876		return SUCCESS;
 
 877	default:		/* SCSI command sent, can not abort */
 878		return FAILED;
 
 879	}
 880}
 881
 882static void ppa_reset_pulse(unsigned int base)
 883{
 884	w_dtr(base, 0x40);
 885	w_ctr(base, 0x8);
 886	udelay(30);
 887	w_ctr(base, 0xc);
 888}
 889
 890static int ppa_reset(struct scsi_cmnd *cmd)
 891{
 892	ppa_struct *dev = ppa_dev(cmd->device->host);
 893
 894	if (ppa_scsi_pointer(cmd)->phase)
 895		ppa_disconnect(dev);
 896	dev->cur_cmd = NULL;	/* Forget the problem */
 897
 898	ppa_connect(dev, CONNECT_NORMAL);
 899	ppa_reset_pulse(dev->base);
 900	mdelay(1);		/* device settle delay */
 901	ppa_disconnect(dev);
 902	mdelay(1);		/* device settle delay */
 903	return SUCCESS;
 904}
 905
 906static int device_check(ppa_struct *dev, bool autodetect)
 907{
 908	/* This routine looks for a device and then attempts to use EPP
 909	   to send a command. If all goes as planned then EPP is available. */
 910
 911	static u8 cmd[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 912	int loop, old_mode, status, k, ppb = dev->base;
 913	unsigned char l;
 914
 915	old_mode = dev->mode;
 916	for (loop = 0; loop < 8; loop++) {
 917		/* Attempt to use EPP for Test Unit Ready */
 918		if (autodetect && (ppb & 0x0007) == 0x0000)
 919			dev->mode = PPA_EPP_8;
 920
 921second_pass:
 922		ppa_connect(dev, CONNECT_EPP_MAYBE);
 923		/* Select SCSI device */
 924		if (!ppa_select(dev, loop)) {
 925			ppa_disconnect(dev);
 926			continue;
 927		}
 928		printk(KERN_INFO "ppa: Found device at ID %i, Attempting to use %s\n",
 929		       loop, PPA_MODE_STRING[dev->mode]);
 930
 931		/* Send SCSI command */
 932		status = 1;
 933		w_ctr(ppb, 0x0c);
 934		for (l = 0; (l < 6) && (status); l++)
 935			status = ppa_out(dev, cmd, 1);
 936
 937		if (!status) {
 938			ppa_disconnect(dev);
 939			ppa_connect(dev, CONNECT_EPP_MAYBE);
 940			w_dtr(ppb, 0x40);
 941			w_ctr(ppb, 0x08);
 942			udelay(30);
 943			w_ctr(ppb, 0x0c);
 944			udelay(1000);
 945			ppa_disconnect(dev);
 946			udelay(1000);
 947			if (dev->mode != old_mode) {
 948				dev->mode = old_mode;
 949				goto second_pass;
 950			}
 951			return -EIO;
 952		}
 953		w_ctr(ppb, 0x0c);
 954		k = 1000000;	/* 1 Second */
 955		do {
 956			l = r_str(ppb);
 957			k--;
 958			udelay(1);
 959		} while (!(l & 0x80) && (k));
 960
 961		l &= 0xf0;
 962
 963		if (l != 0xf0) {
 964			ppa_disconnect(dev);
 965			ppa_connect(dev, CONNECT_EPP_MAYBE);
 966			ppa_reset_pulse(ppb);
 967			udelay(1000);
 968			ppa_disconnect(dev);
 969			udelay(1000);
 970			if (dev->mode != old_mode) {
 971				dev->mode = old_mode;
 972				goto second_pass;
 973			}
 974			return -EIO;
 975		}
 976		ppa_disconnect(dev);
 977		printk(KERN_INFO "ppa: Communication established with ID %i using %s\n",
 978		       loop, PPA_MODE_STRING[dev->mode]);
 979		ppa_connect(dev, CONNECT_EPP_MAYBE);
 980		ppa_reset_pulse(ppb);
 981		udelay(1000);
 982		ppa_disconnect(dev);
 983		udelay(1000);
 984		return 0;
 985	}
 986	return -ENODEV;
 987}
 988
 989static const struct scsi_host_template ppa_template = {
 
 
 
 
 
 
 990	.module			= THIS_MODULE,
 991	.proc_name		= "ppa",
 992	.show_info		= ppa_show_info,
 993	.write_info		= ppa_write_info,
 994	.name			= "Iomega VPI0 (ppa) interface",
 995	.queuecommand		= ppa_queuecommand,
 996	.eh_abort_handler	= ppa_abort,
 
 997	.eh_host_reset_handler	= ppa_reset,
 998	.bios_param		= ppa_biosparam,
 999	.this_id		= -1,
1000	.sg_tablesize		= SG_ALL,
 
 
1001	.can_queue		= 1,
1002	.cmd_size		= sizeof(struct scsi_pointer),
1003};
1004
1005/***************************************************************************
1006 *                   Parallel port probing routines                        *
1007 ***************************************************************************/
1008
1009static LIST_HEAD(ppa_hosts);
1010
1011/*
1012 * Finds the first available device number that can be alloted to the
1013 * new ppa device and returns the address of the previous node so that
1014 * we can add to the tail and have a list in the ascending order.
1015 */
1016
1017static inline ppa_struct *find_parent(void)
1018{
1019	ppa_struct *dev, *par = NULL;
1020	unsigned int cnt = 0;
1021
1022	if (list_empty(&ppa_hosts))
1023		return NULL;
1024
1025	list_for_each_entry(dev, &ppa_hosts, list) {
1026		if (dev->dev_no != cnt)
1027			return par;
1028		cnt++;
1029		par = dev;
1030	}
1031
1032	return par;
1033}
1034
1035static int __ppa_attach(struct parport *pb)
1036{
1037	struct Scsi_Host *host;
1038	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waiting);
1039	DEFINE_WAIT(wait);
1040	ppa_struct *dev, *temp;
1041	int ports;
 
1042	int err = -ENOMEM;
1043	struct pardev_cb ppa_cb;
1044
1045	dev = kzalloc(sizeof(ppa_struct), GFP_KERNEL);
1046	if (!dev)
1047		return -ENOMEM;
1048	dev->base = -1;
1049	dev->mode = mode < PPA_UNKNOWN ? mode : PPA_AUTODETECT;
1050	dev->recon_tmo = PPA_RECON_TMO;
1051	init_waitqueue_head(&waiting);
1052	temp = find_parent();
1053	if (temp)
1054		dev->dev_no = temp->dev_no + 1;
1055
1056	memset(&ppa_cb, 0, sizeof(ppa_cb));
1057	ppa_cb.private = dev;
1058	ppa_cb.wakeup = ppa_wakeup;
1059
1060	dev->dev = parport_register_dev_model(pb, "ppa", &ppa_cb, dev->dev_no);
1061
1062	if (!dev->dev)
1063		goto out;
1064
1065	/* Claim the bus so it remembers what we do to the control
1066	 * registers. [ CTR and ECP ]
1067	 */
1068	err = -EBUSY;
1069	dev->waiting = &waiting;
1070	prepare_to_wait(&waiting, &wait, TASK_UNINTERRUPTIBLE);
1071	if (ppa_pb_claim(dev))
1072		schedule_timeout(3 * HZ);
1073	if (dev->wanted) {
1074		printk(KERN_ERR "ppa%d: failed to claim parport because "
1075				"a pardevice is owning the port for too long "
1076				"time!\n", pb->number);
1077		ppa_pb_dismiss(dev);
1078		dev->waiting = NULL;
1079		finish_wait(&waiting, &wait);
1080		goto out1;
1081	}
1082	dev->waiting = NULL;
1083	finish_wait(&waiting, &wait);
1084	dev->base = dev->dev->port->base;
1085	w_ctr(dev->base, 0x0c);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1086
1087	/* Done configuration */
1088
1089	err = ppa_init(dev);
1090	ppa_pb_release(dev);
1091
1092	if (err)
1093		goto out1;
1094
1095	/* now the glue ... */
1096	if (dev->mode == PPA_NIBBLE || dev->mode == PPA_PS2)
1097		ports = 3;
1098	else
1099		ports = 8;
1100
1101	INIT_DELAYED_WORK(&dev->ppa_tq, ppa_interrupt);
1102
1103	err = -ENOMEM;
1104	host = scsi_host_alloc(&ppa_template, sizeof(ppa_struct *));
1105	if (!host)
1106		goto out1;
1107	host->no_highmem = true;
1108	host->io_port = pb->base;
1109	host->n_io_port = ports;
1110	host->dma_channel = -1;
1111	host->unique_id = pb->number;
1112	*(ppa_struct **)&host->hostdata = dev;
1113	dev->host = host;
1114	list_add_tail(&dev->list, &ppa_hosts);
1115	err = scsi_add_host(host, NULL);
1116	if (err)
1117		goto out2;
1118	scsi_scan_host(host);
1119	return 0;
1120out2:
1121	list_del_init(&dev->list);
1122	scsi_host_put(host);
1123out1:
1124	parport_unregister_device(dev->dev);
1125out:
1126	kfree(dev);
1127	return err;
1128}
1129
1130static void ppa_attach(struct parport *pb)
1131{
1132	__ppa_attach(pb);
1133}
1134
1135static void ppa_detach(struct parport *pb)
1136{
1137	ppa_struct *dev;
1138	list_for_each_entry(dev, &ppa_hosts, list) {
1139		if (dev->dev->port == pb) {
1140			list_del_init(&dev->list);
1141			scsi_remove_host(dev->host);
1142			scsi_host_put(dev->host);
1143			parport_unregister_device(dev->dev);
1144			kfree(dev);
1145			break;
1146		}
1147	}
1148}
1149
1150static struct parport_driver ppa_driver = {
1151	.name		= "ppa",
1152	.match_port	= ppa_attach,
1153	.detach		= ppa_detach,
1154};
1155module_parport_driver(ppa_driver);
1156
1157MODULE_DESCRIPTION("IOMEGA PPA3 parallel port SCSI host adapter driver");
 
 
 
 
 
 
 
 
 
 
 
 
1158MODULE_LICENSE("GPL");