immio.c revision 45342
1/*-
2 * Copyright (c) 1998 Nicolas Souchu
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *	$Id: immio.c,v 1.6 1999/01/30 15:35:39 nsouch Exp $
27 *
28 */
29
30/*
31 * Iomega ZIP+ Matchmaker Parallel Port Interface driver
32 *
33 * Thanks to David Campbell work on the Linux driver and the Iomega specs
34 * Thanks to Thiebault Moeglin for the drive
35 */
36#ifdef KERNEL
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/buf.h>
41
42#include <machine/clock.h>
43
44#endif	/* KERNEL */
45
46#ifdef	KERNEL
47#include <sys/kernel.h>
48#endif /*KERNEL */
49
50#include "opt_vpo.h"
51
52#include <dev/ppbus/ppbconf.h>
53#include <dev/ppbus/ppb_msq.h>
54#include <dev/ppbus/vpoio.h>
55#include <dev/ppbus/ppb_1284.h>
56
57#define VP0_SELTMO		5000	/* select timeout */
58#define VP0_FAST_SPINTMO	500000	/* wait status timeout */
59#define VP0_LOW_SPINTMO		5000000	/* wait status timeout */
60
61#define VP0_SECTOR_SIZE	512
62
63/*
64 * Microcode to execute very fast I/O sequences at the lowest bus level.
65 */
66
67#define SELECT_TARGET		MS_PARAM(6, 1, MS_TYP_CHA)
68
69#define DECLARE_SELECT_MICROSEQUENCE					\
70struct ppb_microseq select_microseq[] = {				\
71	MS_CASS(0xc),							\
72	/* first, check there is nothing holding onto the bus */	\
73	MS_SET(VP0_SELTMO),						\
74/* _loop: */								\
75	MS_BRCLEAR(0x8, 2 /* _ready */),				\
76	MS_DBRA(-2 /* _loop */),					\
77	MS_RET(2),			/* bus busy */			\
78/* _ready: */								\
79	MS_CASS(0x4),							\
80	MS_DASS(MS_UNKNOWN /* 0x80 | 1 << target */),			\
81	MS_DELAY(1),							\
82	MS_CASS(0xc),							\
83	MS_CASS(0xd),							\
84	/* now, wait until the drive is ready */			\
85	MS_SET(VP0_SELTMO),						\
86/* loop: */								\
87	MS_BRSET(0x8, 3 /* ready */),					\
88	MS_DBRA(-2 /* loop */),						\
89/* error: */								\
90	MS_CASS(0xc),							\
91	MS_RET(VP0_ESELECT_TIMEOUT),					\
92/* ready: */								\
93	MS_CASS(0xc),							\
94	MS_RET(0)							\
95}
96
97static struct ppb_microseq transfer_epilog[] = {
98	MS_CASS(0x4),
99	MS_CASS(0xc),
100	MS_CASS(0xe),
101	MS_CASS(0x4),
102	MS_RET(0)
103};
104
105#define CPP_S1		MS_PARAM(10, 2, MS_TYP_PTR)
106#define CPP_S2		MS_PARAM(13, 2, MS_TYP_PTR)
107#define CPP_S3		MS_PARAM(16, 2, MS_TYP_PTR)
108#define CPP_PARAM	MS_PARAM(17, 1, MS_TYP_CHA)
109
110#define DECLARE_CPP_MICROSEQ \
111struct ppb_microseq cpp_microseq[] = {					\
112	MS_CASS(0x0c), MS_DELAY(2),					\
113	MS_DASS(0xaa), MS_DELAY(10),					\
114	MS_DASS(0x55), MS_DELAY(10),					\
115	MS_DASS(0x00), MS_DELAY(10),					\
116	MS_DASS(0xff), MS_DELAY(10),					\
117	MS_RFETCH(MS_REG_STR, 0xb8, MS_UNKNOWN /* &s1 */),		\
118	MS_DASS(0x87), MS_DELAY(10),					\
119	MS_RFETCH(MS_REG_STR, 0xb8, MS_UNKNOWN /* &s2 */),		\
120	MS_DASS(0x78), MS_DELAY(10),					\
121	MS_RFETCH(MS_REG_STR, 0x38, MS_UNKNOWN /* &s3 */),		\
122	MS_DASS(MS_UNKNOWN /* param */),				\
123	MS_DELAY(2),							\
124	MS_CASS(0x0c), MS_DELAY(10),					\
125	MS_CASS(0x0d), MS_DELAY(2),					\
126	MS_CASS(0x0c), MS_DELAY(10),					\
127	MS_DASS(0xff), MS_DELAY(10),					\
128	MS_RET(0)							\
129}
130
131#define NEGOCIATED_MODE		MS_PARAM(2, 1, MS_TYP_CHA)
132
133#define DECLARE_NEGOCIATE_MICROSEQ \
134static struct ppb_microseq negociate_microseq[] = { 			\
135	MS_CASS(0x4),							\
136	MS_DELAY(5),							\
137	MS_DASS(MS_UNKNOWN /* mode */),					\
138	MS_DELAY(100),							\
139	MS_CASS(0x6),							\
140	MS_DELAY(5),							\
141	MS_BRSET(0x20, 5 /* continue */),				\
142	MS_DELAY(5),							\
143	MS_CASS(0x7),							\
144	MS_DELAY(5),							\
145	MS_CASS(0x6),							\
146	MS_RET(VP0_ENEGOCIATE),						\
147/* continue: */								\
148	MS_DELAY(5),							\
149	MS_CASS(0x7),							\
150	MS_DELAY(5),							\
151	MS_CASS(0x6),							\
152	MS_RET(0)							\
153}
154
155static struct ppb_microseq reset_microseq[] = {
156	MS_CASS(0x04),
157	MS_DASS(0x40),
158	MS_DELAY(1),
159	MS_CASS(0x0c),
160	MS_CASS(0x0d),
161	MS_DELAY(50),
162	MS_CASS(0x0c),
163	MS_CASS(0x04),
164	MS_RET(0)
165};
166
167/*
168 * nibble_inbyte_hook()
169 *
170 * Formats high and low nibble into a character
171 */
172static int
173nibble_inbyte_hook (void *p, char *ptr)
174{
175	struct vpo_nibble *s = (struct vpo_nibble *)p;
176
177	/* increment the buffer pointer */
178	*ptr = ((s->l >> 4) & 0x0f) + (s->h & 0xf0);
179
180	return (0);
181}
182
183/*
184 * Macro used to initialize each vpoio_data structure during
185 * low level attachment
186 *
187 * XXX should be converted to ppb_MS_init_msq()
188 */
189#define INIT_NIBBLE_INBYTE_SUBMICROSEQ(vpo) {		    	\
190	(vpo)->vpo_nibble_inbyte_msq[6].arg[2].p =		\
191			(void *)&(vpo)->vpo_nibble.h;		\
192	(vpo)->vpo_nibble_inbyte_msq[3].arg[2].p =		\
193			(void *)&(vpo)->vpo_nibble.l;		\
194	(vpo)->vpo_nibble_inbyte_msq[9].arg[0].f =		\
195			nibble_inbyte_hook;			\
196	(vpo)->vpo_nibble_inbyte_msq[9].arg[1].p =		\
197			(void *)&(vpo)->vpo_nibble;		\
198}
199
200/*
201 * This is the sub-microseqence for MS_GET in NIBBLE mode
202 * Retrieve the two nibbles and call the C function to generate the character
203 * and store it in the buffer (see nibble_inbyte_hook())
204 */
205static struct ppb_microseq nibble_inbyte_submicroseq[] = {
206	  MS_CASS(0x4),
207
208/* loop: */
209	  MS_CASS(0x6),
210	  MS_DELAY(1),
211	  MS_RFETCH(MS_REG_STR, MS_FETCH_ALL, MS_UNKNOWN /* low nibble */),
212	  MS_CASS(0x5),
213	  MS_DELAY(1),
214	  MS_RFETCH(MS_REG_STR, MS_FETCH_ALL, MS_UNKNOWN /* high nibble */),
215	  MS_CASS(0x4),
216	  MS_DELAY(1),
217
218	  /* do a C call to format the received nibbles */
219	  MS_C_CALL(MS_UNKNOWN /* C hook */, MS_UNKNOWN /* param */),
220	  MS_DBRA(-7 /* loop */),
221	  MS_RET(0)
222};
223
224/*
225 * This is the sub-microseqence for MS_GET in PS2 mode
226 */
227static struct ppb_microseq ps2_inbyte_submicroseq[] = {
228	  MS_CASS(0x4),
229
230/* loop: */
231	  MS_CASS(PCD | 0x6),
232	  MS_RFETCH_P(1, MS_REG_DTR, MS_FETCH_ALL),
233	  MS_CASS(PCD | 0x5),
234	  MS_DBRA(-4 /* loop */),
235
236	  MS_RET(0)
237};
238
239/*
240 * This is the sub-microsequence for MS_PUT in both NIBBLE and PS2 modes
241 */
242static struct ppb_microseq spp_outbyte_submicroseq[] = {
243	  MS_CASS(0x4),
244
245/* loop: */
246	  MS_RASSERT_P(1, MS_REG_DTR),
247	  MS_CASS(0x5),
248	  MS_DBRA(0),				/* decrement counter */
249	  MS_RASSERT_P(1, MS_REG_DTR),
250	  MS_CASS(0x0),
251	  MS_DBRA(-6 /* loop */),
252
253	  /* return from the put call */
254	  MS_CASS(0x4),
255	  MS_RET(0)
256};
257
258/* EPP 1.7 microsequences, ptr and len set at runtime */
259static struct ppb_microseq epp17_outstr[] = {
260	  MS_CASS(0x4),
261	  MS_RASSERT_P(MS_ACCUM, MS_REG_EPP_D),
262	  MS_CASS(0xc),
263	  MS_RET(0),
264};
265
266static struct ppb_microseq epp17_instr[] = {
267	  MS_CASS(PCD | 0x4),
268	  MS_RFETCH_P(MS_ACCUM, MS_REG_EPP_D, MS_FETCH_ALL),
269	  MS_CASS(PCD | 0xc),
270	  MS_RET(0),
271};
272
273static int
274imm_disconnect(struct vpoio_data *vpo, int *connected, int release_bus)
275{
276	DECLARE_CPP_MICROSEQ;
277
278	char s1, s2, s3;
279	int ret;
280
281	/* all should be ok */
282	if (connected)
283		*connected = 0;
284
285	ppb_MS_init_msq(cpp_microseq, 4, CPP_S1, (void *)&s1,
286			CPP_S2, (void *)&s2, CPP_S3, (void *)&s3,
287			CPP_PARAM, 0x30);
288
289	ppb_MS_microseq(&vpo->vpo_dev, cpp_microseq, &ret);
290
291	if ((s1 != (char)0xb8 || s2 != (char)0x18 || s3 != (char)0x38)) {
292		if (bootverbose)
293			printf("imm%d: (disconnect) s1=0x%x s2=0x%x, s3=0x%x\n",
294				vpo->vpo_unit, s1 & 0xff, s2 & 0xff, s3 & 0xff);
295		if (connected)
296			*connected = VP0_ECONNECT;
297	}
298
299	if (release_bus)
300		return (ppb_release_bus(&vpo->vpo_dev));
301	else
302		return (0);
303}
304
305/*
306 * how	: PPB_WAIT or PPB_DONTWAIT
307 */
308static int
309imm_connect(struct vpoio_data *vpo, int how, int *disconnected, int request_bus)
310{
311	DECLARE_CPP_MICROSEQ;
312
313	char s1, s2, s3;
314	int error;
315	int ret;
316
317	/* all should be ok */
318	if (disconnected)
319		*disconnected = 0;
320
321	if (request_bus)
322		if ((error = ppb_request_bus(&vpo->vpo_dev, how)))
323			return (error);
324
325	ppb_MS_init_msq(cpp_microseq, 3, CPP_S1, (void *)&s1,
326			CPP_S2, (void *)&s2, CPP_S3, (void *)&s3);
327
328	/* select device 0 in compatible mode */
329	ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0xe0);
330	ppb_MS_microseq(&vpo->vpo_dev, cpp_microseq, &ret);
331
332	/* disconnect all devices */
333	ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0x30);
334	ppb_MS_microseq(&vpo->vpo_dev, cpp_microseq, &ret);
335
336	if (PPB_IN_EPP_MODE(&vpo->vpo_dev))
337		ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0x28);
338	else
339		ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0xe0);
340
341	ppb_MS_microseq(&vpo->vpo_dev, cpp_microseq, &ret);
342
343	if ((s1 != (char)0xb8 || s2 != (char)0x18 || s3 != (char)0x30)) {
344		if (bootverbose)
345			printf("imm%d: (connect) s1=0x%x s2=0x%x, s3=0x%x\n",
346				vpo->vpo_unit, s1 & 0xff, s2 & 0xff, s3 & 0xff);
347		if (disconnected)
348			*disconnected = VP0_ECONNECT;
349	}
350
351	return (0);
352}
353
354/*
355 * imm_detect()
356 *
357 * Detect and initialise the VP0 adapter.
358 */
359static int
360imm_detect(struct vpoio_data *vpo)
361{
362	int error;
363
364	if ((error = ppb_request_bus(&vpo->vpo_dev, PPB_DONTWAIT)))
365		return (error);
366
367	/* disconnect the drive, keep the bus */
368	imm_disconnect(vpo, NULL, 0);
369
370	/* we already have the bus, just connect */
371	imm_connect(vpo, PPB_DONTWAIT, &error, 0);
372
373	if (error) {
374		if (bootverbose)
375			printf("imm%d: can't connect to the drive\n",
376				vpo->vpo_unit);
377		goto error;
378	}
379
380	/* send SCSI reset signal */
381	ppb_MS_microseq(&vpo->vpo_dev, reset_microseq, NULL);
382
383	/* release the bus now */
384	imm_disconnect(vpo, &error, 1);
385
386	/* ensure we are disconnected or daisy chained peripheral
387	 * may cause serious problem to the disk */
388
389	if (error) {
390		if (bootverbose)
391			printf("imm%d: can't disconnect from the drive\n",
392				vpo->vpo_unit);
393		goto error;
394	}
395
396	return (0);
397
398error:
399	ppb_release_bus(&vpo->vpo_dev);
400	return (VP0_EINITFAILED);
401}
402
403/*
404 * imm_outstr()
405 */
406static int
407imm_outstr(struct vpoio_data *vpo, char *buffer, int size)
408{
409	int error = 0;
410
411	if (PPB_IN_EPP_MODE(&vpo->vpo_dev))
412		ppb_reset_epp_timeout(&vpo->vpo_dev);
413
414	ppb_MS_exec(&vpo->vpo_dev, MS_OP_PUT, (union ppb_insarg)buffer,
415		(union ppb_insarg)size, (union ppb_insarg)MS_UNKNOWN, &error);
416
417	return (error);
418}
419
420/*
421 * imm_instr()
422 */
423static int
424imm_instr(struct vpoio_data *vpo, char *buffer, int size)
425{
426	int error = 0;
427
428	if (PPB_IN_EPP_MODE(&vpo->vpo_dev))
429		ppb_reset_epp_timeout(&vpo->vpo_dev);
430
431	ppb_MS_exec(&vpo->vpo_dev, MS_OP_GET, (union ppb_insarg)buffer,
432		(union ppb_insarg)size, (union ppb_insarg)MS_UNKNOWN, &error);
433
434	return (error);
435}
436
437static char
438imm_select(struct vpoio_data *vpo, int initiator, int target)
439{
440	DECLARE_SELECT_MICROSEQUENCE;
441	int ret;
442
443	/* initialize the select microsequence */
444	ppb_MS_init_msq(select_microseq, 1,
445			SELECT_TARGET, 1 << initiator | 1 << target);
446
447	ppb_MS_microseq(&vpo->vpo_dev, select_microseq, &ret);
448
449	return (ret);
450}
451
452/*
453 * imm_wait()
454 *
455 * H_SELIN must be low.
456 *
457 * XXX should be ported to microseq
458 */
459static char
460imm_wait(struct vpoio_data *vpo, int tmo)
461{
462
463	register int	k;
464	register char	r;
465
466	ppb_wctr(&vpo->vpo_dev, 0xc);
467
468	/* XXX should be ported to microseq */
469	k = 0;
470	while (!((r = ppb_rstr(&vpo->vpo_dev)) & 0x80) && (k++ < tmo))
471		DELAY(1);
472
473	/*
474	 * Return some status information.
475	 * Semantics :	0x88 = ZIP+ wants more data
476	 *		0x98 = ZIP+ wants to send more data
477	 *		0xa8 = ZIP+ wants command
478	 *		0xb8 = end of transfer, ZIP+ is sending status
479	 */
480	ppb_wctr(&vpo->vpo_dev, 0x4);
481	if (k < tmo)
482	  return (r & 0xb8);
483
484	return (0);			   /* command timed out */
485}
486
487static int
488imm_negociate(struct vpoio_data *vpo)
489{
490	DECLARE_NEGOCIATE_MICROSEQ;
491	int negociate_mode;
492	int ret;
493
494	if (PPB_IN_NIBBLE_MODE(&vpo->vpo_dev))
495		negociate_mode = 0;
496	else if (PPB_IN_PS2_MODE(&vpo->vpo_dev))
497		negociate_mode = 1;
498	else
499		return (0);
500
501#if 0 /* XXX use standalone code not to depend on ppb_1284 code yet */
502	ret = ppb_1284_negociate(&vpo->vpo_dev, negociate_mode);
503
504	if (ret)
505		return (VP0_ENEGOCIATE);
506#endif
507
508	ppb_MS_init_msq(negociate_microseq, 1, NEGOCIATED_MODE, negociate_mode);
509
510	ppb_MS_microseq(&vpo->vpo_dev, negociate_microseq, &ret);
511
512	return (ret);
513}
514
515/*
516 * imm_probe()
517 *
518 * Low level probe of vpo device
519 *
520 */
521struct ppb_device *
522imm_probe(struct ppb_data *ppb, struct vpoio_data *vpo)
523{
524
525	/* ppbus dependent initialisation */
526	vpo->vpo_dev.id_unit = vpo->vpo_unit;
527	vpo->vpo_dev.name = "vpo";
528	vpo->vpo_dev.ppb = ppb;
529
530	/* now, try to initialise the drive */
531	if (imm_detect(vpo)) {
532		return (NULL);
533	}
534
535	return (&vpo->vpo_dev);
536}
537
538/*
539 * imm_attach()
540 *
541 * Low level attachment of vpo device
542 *
543 */
544int
545imm_attach(struct vpoio_data *vpo)
546{
547	int epp;
548
549	/*
550	 * Report ourselves
551	 */
552	printf("imm%d: <Iomega Matchmaker Parallel to SCSI interface> on ppbus %d\n",
553		vpo->vpo_dev.id_unit, vpo->vpo_dev.ppb->ppb_link->adapter_unit);
554
555	/*
556	 * Initialize microsequence code
557	 */
558	vpo->vpo_nibble_inbyte_msq = (struct ppb_microseq *)malloc(
559		sizeof(nibble_inbyte_submicroseq), M_DEVBUF, M_NOWAIT);
560
561	if (!vpo->vpo_nibble_inbyte_msq)
562		return (0);
563
564	bcopy((void *)nibble_inbyte_submicroseq,
565		(void *)vpo->vpo_nibble_inbyte_msq,
566		sizeof(nibble_inbyte_submicroseq));
567
568	INIT_NIBBLE_INBYTE_SUBMICROSEQ(vpo);
569
570	/*
571	 * Initialize mode dependent in/out microsequences
572	 */
573	ppb_request_bus(&vpo->vpo_dev, PPB_WAIT);
574
575	/* enter NIBBLE mode to configure submsq */
576	if (ppb_set_mode(&vpo->vpo_dev, PPB_NIBBLE) != -1) {
577
578		ppb_MS_GET_init(&vpo->vpo_dev, vpo->vpo_nibble_inbyte_msq);
579		ppb_MS_PUT_init(&vpo->vpo_dev, spp_outbyte_submicroseq);
580	}
581
582	/* enter PS2 mode to configure submsq */
583	if (ppb_set_mode(&vpo->vpo_dev, PPB_PS2) != -1) {
584
585		ppb_MS_GET_init(&vpo->vpo_dev, ps2_inbyte_submicroseq);
586		ppb_MS_PUT_init(&vpo->vpo_dev, spp_outbyte_submicroseq);
587	}
588
589	epp = ppb_get_epp_protocol(&vpo->vpo_dev);
590
591	/* enter EPP mode to configure submsq */
592	if (ppb_set_mode(&vpo->vpo_dev, PPB_EPP) != -1) {
593
594		switch (epp) {
595		case EPP_1_9:
596		case EPP_1_7:
597			ppb_MS_GET_init(&vpo->vpo_dev, epp17_instr);
598			ppb_MS_PUT_init(&vpo->vpo_dev, epp17_outstr);
599			break;
600		default:
601			panic("%s: unknown EPP protocol (0x%x)", __FUNCTION__,
602				epp);
603		}
604	}
605
606	/* try to enter EPP or PS/2 mode, NIBBLE otherwise */
607	if (ppb_set_mode(&vpo->vpo_dev, PPB_EPP) != -1) {
608		switch (epp) {
609		case EPP_1_9:
610			printf("imm%d: EPP 1.9 mode\n", vpo->vpo_unit);
611			break;
612		case EPP_1_7:
613			printf("imm%d: EPP 1.7 mode\n", vpo->vpo_unit);
614			break;
615		default:
616			panic("%s: unknown EPP protocol (0x%x)", __FUNCTION__,
617				epp);
618		}
619	} else if (ppb_set_mode(&vpo->vpo_dev, PPB_PS2) != -1)
620		printf("imm%d: PS2 mode\n", vpo->vpo_unit);
621
622	else if (ppb_set_mode(&vpo->vpo_dev, PPB_NIBBLE) != -1)
623		printf("imm%d: NIBBLE mode\n", vpo->vpo_unit);
624
625	else {
626		printf("imm%d: can't enter NIBBLE, PS2 or EPP mode\n",
627			vpo->vpo_unit);
628
629		ppb_release_bus(&vpo->vpo_dev);
630
631		free(vpo->vpo_nibble_inbyte_msq, M_DEVBUF);
632		return (0);
633	}
634
635	ppb_release_bus(&vpo->vpo_dev);
636
637	return (1);
638}
639
640/*
641 * imm_reset_bus()
642 *
643 */
644int
645imm_reset_bus(struct vpoio_data *vpo)
646{
647	int disconnected;
648
649	/* first, connect to the drive and request the bus */
650	imm_connect(vpo, PPB_WAIT|PPB_INTR, &disconnected, 1);
651
652	if (!disconnected) {
653
654		/* reset the SCSI bus */
655		ppb_MS_microseq(&vpo->vpo_dev, reset_microseq, NULL);
656
657		/* then disconnect */
658		imm_disconnect(vpo, NULL, 1);
659	}
660
661	return (0);
662}
663
664/*
665 * imm_do_scsi()
666 *
667 * Send an SCSI command
668 *
669 */
670int
671imm_do_scsi(struct vpoio_data *vpo, int host, int target, char *command,
672		int clen, char *buffer, int blen, int *result, int *count,
673		int *ret)
674{
675
676	register char r;
677	char l, h = 0;
678	int len, error = 0, not_connected = 0;
679	register int k;
680	int negociated = 0;
681
682	/*
683	 * enter disk state, allocate the ppbus
684	 *
685	 * XXX
686	 * Should we allow this call to be interruptible?
687	 * The only way to report the interruption is to return
688	 * EIO do upper SCSI code :^(
689	 */
690	if ((error = imm_connect(vpo, PPB_WAIT|PPB_INTR, &not_connected, 1)))
691		return (error);
692
693	if (not_connected) {
694		*ret = VP0_ECONNECT; goto error;
695	}
696
697	/*
698	 * Select the drive ...
699	 */
700	if ((*ret = imm_select(vpo,host,target)))
701		goto error;
702
703	/*
704	 * Send the command ...
705	 */
706	for (k = 0; k < clen; k+=2) {
707		if (imm_wait(vpo, VP0_FAST_SPINTMO) != (char)0xa8) {
708			*ret = VP0_ECMD_TIMEOUT;
709			goto error;
710		}
711		if (imm_outstr(vpo, &command[k], 2)) {
712			*ret = VP0_EPPDATA_TIMEOUT;
713			goto error;
714		}
715	}
716
717	if (!(r = imm_wait(vpo, VP0_LOW_SPINTMO))) {
718		*ret = VP0_ESTATUS_TIMEOUT; goto error;
719	}
720
721	if ((r & 0x30) == 0x10) {
722		if (imm_negociate(vpo)) {
723			*ret = VP0_ENEGOCIATE;
724			goto error;
725		} else
726			negociated = 1;
727	}
728
729	/*
730	 * Complete transfer ...
731	 */
732	*count = 0;
733	for (;;) {
734
735		if (!(r = imm_wait(vpo, VP0_LOW_SPINTMO))) {
736			*ret = VP0_ESTATUS_TIMEOUT; goto error;
737		}
738
739		/* stop when the ZIP+ wants to send status */
740		if (r == (char)0xb8)
741			break;
742
743		if (*count >= blen) {
744			*ret = VP0_EDATA_OVERFLOW;
745			goto error;
746		}
747
748		/* ZIP+ wants to send data? */
749		if (r == (char)0x88) {
750			len = (((blen - *count) >= VP0_SECTOR_SIZE)) ?
751				VP0_SECTOR_SIZE : 2;
752
753			error = imm_outstr(vpo, &buffer[*count], len);
754		} else {
755			if (!PPB_IN_EPP_MODE(&vpo->vpo_dev))
756				len = 1;
757			else
758				len = (((blen - *count) >= VP0_SECTOR_SIZE)) ?
759					VP0_SECTOR_SIZE : 1;
760
761			error = imm_instr(vpo, &buffer[*count], len);
762		}
763
764		if (error) {
765			*ret = error;
766			goto error;
767		}
768
769		*count += len;
770	}
771
772	if ((PPB_IN_NIBBLE_MODE(&vpo->vpo_dev) ||
773			PPB_IN_PS2_MODE(&vpo->vpo_dev)) && negociated)
774		ppb_MS_microseq(&vpo->vpo_dev, transfer_epilog, NULL);
775
776	/*
777	 * Retrieve status ...
778	 */
779	if (imm_negociate(vpo)) {
780		*ret = VP0_ENEGOCIATE;
781		goto error;
782	} else
783		negociated = 1;
784
785	if (imm_instr(vpo, &l, 1)) {
786		*ret = VP0_EOTHER; goto error;
787	}
788
789	/* check if the ZIP+ wants to send more status */
790	if (imm_wait(vpo, VP0_FAST_SPINTMO) == (char)0xb8)
791		if (imm_instr(vpo, &h, 1)) {
792			*ret = VP0_EOTHER+2; goto error;
793		}
794
795	*result = ((int) h << 8) | ((int) l & 0xff);
796
797error:
798	if ((PPB_IN_NIBBLE_MODE(&vpo->vpo_dev) ||
799			PPB_IN_PS2_MODE(&vpo->vpo_dev)) && negociated)
800		ppb_MS_microseq(&vpo->vpo_dev, transfer_epilog, NULL);
801
802	/* return to printer state, release the ppbus */
803	imm_disconnect(vpo, NULL, 1);
804
805	return (0);
806}
807