Linux Audio

Check our new training course

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