kmalloc: Avoid code duplication.
[dragonfly.git] / sys / dev / disk / vpo / immio.c
blob566eac0102b6b2b6be0add65139ae8c102e24036
1 /*-
2 * Copyright (c) 1998, 1999 Nicolas Souchu
3 * Copyright (c) 2001 Alcove - Nicolas Souchu
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
27 * $FreeBSD: src/sys/dev/ppbus/immio.c,v 1.10.2.3 2001/10/02 05:27:20 nsouch Exp $
31 * Iomega ZIP+ Matchmaker Parallel Port Interface driver
33 * Thanks to David Campbell work on the Linux driver and the Iomega specs
34 * Thanks to Thiebault Moeglin for the drive
36 #ifdef _KERNEL
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/malloc.h>
43 #endif /* _KERNEL */
45 #include "opt_vpo.h"
47 #include <bus/ppbus/ppbio.h>
48 #include <bus/ppbus/ppbconf.h>
49 #include <bus/ppbus/ppb_msq.h>
50 #include "vpoio.h"
51 #include <bus/ppbus/ppb_1284.h>
53 #include "ppbus_if.h"
55 #define VP0_SELTMO 5000 /* select timeout */
56 #define VP0_FAST_SPINTMO 500000 /* wait status timeout */
57 #define VP0_LOW_SPINTMO 5000000 /* wait status timeout */
59 #define VP0_SECTOR_SIZE 512
62 * Microcode to execute very fast I/O sequences at the lowest bus level.
65 #define WAIT_RET MS_PARAM(7, 2, MS_TYP_PTR)
66 #define WAIT_TMO MS_PARAM(1, 0, MS_TYP_INT)
68 #define DECLARE_WAIT_MICROSEQUENCE \
69 struct ppb_microseq wait_microseq[] = { \
70 MS_CASS(0x0c), \
71 MS_SET(MS_UNKNOWN), \
72 /* loop */ \
73 MS_BRSET(nBUSY, 4 /* ready */), \
74 MS_DBRA(-2 /* loop */), \
75 MS_CASS(0x04), \
76 MS_RET(1), /* timed out */ \
77 /* ready */ \
78 MS_CASS(0x04), \
79 MS_RFETCH(MS_REG_STR, 0xb8, MS_UNKNOWN ), \
80 MS_RET(0) /* no error */ \
83 #define SELECT_TARGET MS_PARAM(6, 1, MS_TYP_CHA)
85 #define DECLARE_SELECT_MICROSEQUENCE \
86 struct ppb_microseq select_microseq[] = { \
87 MS_CASS(0xc), \
88 /* first, check there is nothing holding onto the bus */ \
89 MS_SET(VP0_SELTMO), \
90 /* _loop: */ \
91 MS_BRCLEAR(0x8, 2 /* _ready */), \
92 MS_DBRA(-2 /* _loop */), \
93 MS_RET(2), /* bus busy */ \
94 /* _ready: */ \
95 MS_CASS(0x4), \
96 MS_DASS(MS_UNKNOWN /* 0x80 | 1 << target */), \
97 MS_DELAY(1), \
98 MS_CASS(0xc), \
99 MS_CASS(0xd), \
100 /* now, wait until the drive is ready */ \
101 MS_SET(VP0_SELTMO), \
102 /* loop: */ \
103 MS_BRSET(0x8, 3 /* ready */), \
104 MS_DBRA(-2 /* loop */), \
105 /* error: */ \
106 MS_CASS(0xc), \
107 MS_RET(VP0_ESELECT_TIMEOUT), \
108 /* ready: */ \
109 MS_CASS(0xc), \
110 MS_RET(0) \
113 static struct ppb_microseq transfer_epilog[] = {
114 MS_CASS(0x4),
115 MS_CASS(0xc),
116 MS_CASS(0xe),
117 MS_CASS(0x4),
118 MS_RET(0)
121 #define CPP_S1 MS_PARAM(10, 2, MS_TYP_PTR)
122 #define CPP_S2 MS_PARAM(13, 2, MS_TYP_PTR)
123 #define CPP_S3 MS_PARAM(16, 2, MS_TYP_PTR)
124 #define CPP_PARAM MS_PARAM(17, 1, MS_TYP_CHA)
126 #define DECLARE_CPP_MICROSEQ \
127 struct ppb_microseq cpp_microseq[] = { \
128 MS_CASS(0x0c), MS_DELAY(2), \
129 MS_DASS(0xaa), MS_DELAY(10), \
130 MS_DASS(0x55), MS_DELAY(10), \
131 MS_DASS(0x00), MS_DELAY(10), \
132 MS_DASS(0xff), MS_DELAY(10), \
133 MS_RFETCH(MS_REG_STR, 0xb8, MS_UNKNOWN /* &s1 */), \
134 MS_DASS(0x87), MS_DELAY(10), \
135 MS_RFETCH(MS_REG_STR, 0xb8, MS_UNKNOWN /* &s2 */), \
136 MS_DASS(0x78), MS_DELAY(10), \
137 MS_RFETCH(MS_REG_STR, 0x38, MS_UNKNOWN /* &s3 */), \
138 MS_DASS(MS_UNKNOWN /* param */), \
139 MS_DELAY(2), \
140 MS_CASS(0x0c), MS_DELAY(10), \
141 MS_CASS(0x0d), MS_DELAY(2), \
142 MS_CASS(0x0c), MS_DELAY(10), \
143 MS_DASS(0xff), MS_DELAY(10), \
144 MS_RET(0) \
147 #define NEGOCIATED_MODE MS_PARAM(2, 1, MS_TYP_CHA)
149 #define DECLARE_NEGOCIATE_MICROSEQ \
150 struct ppb_microseq negociate_microseq[] = { \
151 MS_CASS(0x4), \
152 MS_DELAY(5), \
153 MS_DASS(MS_UNKNOWN /* mode */), \
154 MS_DELAY(100), \
155 MS_CASS(0x6), \
156 MS_DELAY(5), \
157 MS_BRSET(0x20, 5 /* continue */), \
158 MS_DELAY(5), \
159 MS_CASS(0x7), \
160 MS_DELAY(5), \
161 MS_CASS(0x6), \
162 MS_RET(VP0_ENEGOCIATE), \
163 /* continue: */ \
164 MS_DELAY(5), \
165 MS_CASS(0x7), \
166 MS_DELAY(5), \
167 MS_CASS(0x6), \
168 MS_RET(0) \
171 #define INB_NIBBLE_L MS_PARAM(3, 2, MS_TYP_PTR)
172 #define INB_NIBBLE_H MS_PARAM(6, 2, MS_TYP_PTR)
173 #define INB_NIBBLE_F MS_PARAM(9, 0, MS_TYP_FUN)
174 #define INB_NIBBLE_P MS_PARAM(9, 1, MS_TYP_PTR)
177 * This is the sub-microseqence for MS_GET in NIBBLE mode
178 * Retrieve the two nibbles and call the C function to generate the character
179 * and store it in the buffer (see nibble_inbyte_hook())
182 #define DECLARE_NIBBLE_INBYTE_SUBMICROSEQ \
183 struct ppb_microseq nibble_inbyte_submicroseq[] = { \
184 MS_CASS(0x4), \
185 /* loop: */ \
186 MS_CASS(0x6), \
187 MS_DELAY(1), \
188 MS_RFETCH(MS_REG_STR, MS_FETCH_ALL, MS_UNKNOWN /* low nibble */),\
189 MS_CASS(0x5), \
190 MS_DELAY(1), \
191 MS_RFETCH(MS_REG_STR, MS_FETCH_ALL, MS_UNKNOWN /* high nibble */),\
192 MS_CASS(0x4), \
193 MS_DELAY(1), \
194 /* do a C call to format the received nibbles */ \
195 MS_C_CALL(MS_UNKNOWN /* C hook */, MS_UNKNOWN /* param */), \
196 MS_DBRA(-7 /* loop */), \
197 MS_RET(0) \
200 static struct ppb_microseq reset_microseq[] = {
201 MS_CASS(0x04),
202 MS_DASS(0x40),
203 MS_DELAY(1),
204 MS_CASS(0x0c),
205 MS_CASS(0x0d),
206 MS_DELAY(50),
207 MS_CASS(0x0c),
208 MS_CASS(0x04),
209 MS_RET(0)
213 * nibble_inbyte_hook()
215 * Formats high and low nibble into a character
217 static int
218 nibble_inbyte_hook (void *p, char *ptr)
220 struct vpo_nibble *s = (struct vpo_nibble *)p;
222 /* increment the buffer pointer */
223 *ptr = ((s->l >> 4) & 0x0f) + (s->h & 0xf0);
225 return (0);
229 * This is the sub-microseqence for MS_GET in PS2 mode
231 static struct ppb_microseq ps2_inbyte_submicroseq[] = {
232 MS_CASS(0x4),
234 /* loop: */
235 MS_CASS(PCD | 0x6),
236 MS_RFETCH_P(1, MS_REG_DTR, MS_FETCH_ALL),
237 MS_CASS(PCD | 0x5),
238 MS_DBRA(-4 /* loop */),
240 MS_RET(0)
244 * This is the sub-microsequence for MS_PUT in both NIBBLE and PS2 modes
246 static struct ppb_microseq spp_outbyte_submicroseq[] = {
247 MS_CASS(0x4),
249 /* loop: */
250 MS_RASSERT_P(1, MS_REG_DTR),
251 MS_CASS(0x5),
252 MS_DBRA(0), /* decrement counter */
253 MS_RASSERT_P(1, MS_REG_DTR),
254 MS_CASS(0x0),
255 MS_DBRA(-6 /* loop */),
257 /* return from the put call */
258 MS_CASS(0x4),
259 MS_RET(0)
262 /* EPP 1.7 microsequences, ptr and len set at runtime */
263 static struct ppb_microseq epp17_outstr[] = {
264 MS_CASS(0x4),
265 MS_RASSERT_P(MS_ACCUM, MS_REG_EPP_D),
266 MS_CASS(0xc),
267 MS_RET(0),
270 static struct ppb_microseq epp17_instr[] = {
271 MS_CASS(PCD | 0x4),
272 MS_RFETCH_P(MS_ACCUM, MS_REG_EPP_D, MS_FETCH_ALL),
273 MS_CASS(PCD | 0xc),
274 MS_RET(0),
277 static int
278 imm_disconnect(struct vpoio_data *vpo, int *connected, int release_bus)
280 DECLARE_CPP_MICROSEQ;
282 device_t ppbus = device_get_parent(vpo->vpo_dev);
283 char s1, s2, s3;
284 int ret;
286 /* all should be ok */
287 if (connected)
288 *connected = 0;
290 ppb_MS_init_msq(cpp_microseq, 4, CPP_S1, (void *)&s1,
291 CPP_S2, (void *)&s2, CPP_S3, (void *)&s3,
292 CPP_PARAM, 0x30);
294 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
296 if ((s1 != (char)0xb8 || s2 != (char)0x18 || s3 != (char)0x38)) {
297 if (bootverbose)
298 kprintf("imm%d: (disconnect) s1=0x%x s2=0x%x, s3=0x%x\n",
299 vpo->vpo_unit, s1 & 0xff, s2 & 0xff, s3 & 0xff);
300 if (connected)
301 *connected = VP0_ECONNECT;
304 if (release_bus)
305 return (ppb_release_bus(ppbus, vpo->vpo_dev));
306 else
307 return (0);
311 * how : PPB_WAIT or PPB_DONTWAIT
313 static int
314 imm_connect(struct vpoio_data *vpo, int how, int *disconnected, int request_bus)
316 DECLARE_CPP_MICROSEQ;
318 device_t ppbus = device_get_parent(vpo->vpo_dev);
319 char s1, s2, s3;
320 int error;
321 int ret;
323 /* all should be ok */
324 if (disconnected)
325 *disconnected = 0;
327 if (request_bus)
328 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, how)))
329 return (error);
331 ppb_MS_init_msq(cpp_microseq, 3, CPP_S1, (void *)&s1,
332 CPP_S2, (void *)&s2, CPP_S3, (void *)&s3);
334 /* select device 0 in compatible mode */
335 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0xe0);
336 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
338 /* disconnect all devices */
339 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0x30);
340 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
342 if (PPB_IN_EPP_MODE(ppbus))
343 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0x28);
344 else
345 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0xe0);
347 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
349 if ((s1 != (char)0xb8 || s2 != (char)0x18 || s3 != (char)0x30)) {
350 if (bootverbose)
351 kprintf("imm%d: (connect) s1=0x%x s2=0x%x, s3=0x%x\n",
352 vpo->vpo_unit, s1 & 0xff, s2 & 0xff, s3 & 0xff);
353 if (disconnected)
354 *disconnected = VP0_ECONNECT;
357 return (0);
361 * imm_detect()
363 * Detect and initialise the VP0 adapter.
365 static int
366 imm_detect(struct vpoio_data *vpo)
368 device_t ppbus = device_get_parent(vpo->vpo_dev);
369 int error;
371 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, PPB_DONTWAIT)))
372 return (error);
374 /* disconnect the drive, keep the bus */
375 imm_disconnect(vpo, NULL, 0);
377 vpo->vpo_mode_found = VP0_MODE_UNDEFINED;
378 error = 1;
380 /* try to enter EPP mode since vpoio failure put the bus in NIBBLE */
381 if (ppb_set_mode(ppbus, PPB_EPP) != -1) {
382 imm_connect(vpo, PPB_DONTWAIT, &error, 0);
385 /* if connection failed try PS/2 then NIBBLE modes */
386 if (error) {
387 if (ppb_set_mode(ppbus, PPB_PS2) != -1) {
388 imm_connect(vpo, PPB_DONTWAIT, &error, 0);
390 if (error) {
391 if (ppb_set_mode(ppbus, PPB_NIBBLE) != -1) {
392 imm_connect(vpo, PPB_DONTWAIT, &error, 0);
393 if (error)
394 goto error;
395 vpo->vpo_mode_found = VP0_MODE_NIBBLE;
396 } else {
397 kprintf("imm%d: NIBBLE mode unavailable!\n", vpo->vpo_unit);
398 goto error;
400 } else {
401 vpo->vpo_mode_found = VP0_MODE_PS2;
403 } else {
404 vpo->vpo_mode_found = VP0_MODE_EPP;
407 /* send SCSI reset signal */
408 ppb_MS_microseq(ppbus, vpo->vpo_dev, reset_microseq, NULL);
410 /* release the bus now */
411 imm_disconnect(vpo, &error, 1);
413 /* ensure we are disconnected or daisy chained peripheral
414 * may cause serious problem to the disk */
416 if (error) {
417 if (bootverbose)
418 kprintf("imm%d: can't disconnect from the drive\n",
419 vpo->vpo_unit);
420 goto error;
423 return (0);
425 error:
426 ppb_release_bus(ppbus, vpo->vpo_dev);
427 return (VP0_EINITFAILED);
431 * imm_outstr()
433 static int
434 imm_outstr(struct vpoio_data *vpo, char *_buffer, int _size)
436 union ppb_insarg buffer = { .c = _buffer };
437 union ppb_insarg size = { .i = _size };
438 union ppb_insarg unknown = { .i = MS_UNKNOWN };
439 device_t ppbus = device_get_parent(vpo->vpo_dev);
440 int error = 0;
442 if (PPB_IN_EPP_MODE(ppbus))
443 ppb_reset_epp_timeout(ppbus);
445 ppb_MS_exec(ppbus, vpo->vpo_dev, MS_OP_PUT, buffer, size, unknown,
446 &error);
448 return (error);
452 * imm_instr()
454 static int
455 imm_instr(struct vpoio_data *vpo, char *_buffer, int _size)
457 union ppb_insarg buffer = { .c = _buffer };
458 union ppb_insarg size = { .i = _size };
459 union ppb_insarg unknown = { .i = MS_UNKNOWN };
460 device_t ppbus = device_get_parent(vpo->vpo_dev);
461 int error = 0;
463 if (PPB_IN_EPP_MODE(ppbus))
464 ppb_reset_epp_timeout(ppbus);
466 ppb_MS_exec(ppbus, vpo->vpo_dev, MS_OP_GET, buffer, size, unknown,
467 &error);
469 return (error);
472 static char
473 imm_select(struct vpoio_data *vpo, int initiator, int target)
475 DECLARE_SELECT_MICROSEQUENCE;
476 device_t ppbus = device_get_parent(vpo->vpo_dev);
477 int ret;
479 /* initialize the select microsequence */
480 ppb_MS_init_msq(select_microseq, 1,
481 SELECT_TARGET, 1 << initiator | 1 << target);
483 ppb_MS_microseq(ppbus, vpo->vpo_dev, select_microseq, &ret);
485 return (ret);
489 * imm_wait()
491 * H_SELIN must be low.
494 static char
495 imm_wait(struct vpoio_data *vpo, int tmo)
497 DECLARE_WAIT_MICROSEQUENCE;
499 device_t ppbus = device_get_parent(vpo->vpo_dev);
500 int ret, err;
503 * Return some status information.
504 * Semantics : 0x88 = ZIP+ wants more data
505 * 0x98 = ZIP+ wants to send more data
506 * 0xa8 = ZIP+ wants command
507 * 0xb8 = end of transfer, ZIP+ is sending status
510 ppb_MS_init_msq(wait_microseq, 2,
511 WAIT_RET, (void *)&ret,
512 WAIT_TMO, tmo);
514 ppb_MS_microseq(ppbus, vpo->vpo_dev, wait_microseq, &err);
516 if (err)
517 return (0); /* command timed out */
519 return(ret);
522 static int
523 imm_negociate(struct vpoio_data *vpo)
525 DECLARE_NEGOCIATE_MICROSEQ;
526 device_t ppbus = device_get_parent(vpo->vpo_dev);
527 int negociate_mode;
528 int ret;
530 if (PPB_IN_NIBBLE_MODE(ppbus))
531 negociate_mode = 0;
532 else if (PPB_IN_PS2_MODE(ppbus))
533 negociate_mode = 1;
534 else
535 return (0);
537 #if 0 /* XXX use standalone code not to depend on ppb_1284 code yet */
538 ret = ppb_1284_negociate(ppbus, negociate_mode);
540 if (ret)
541 return (VP0_ENEGOCIATE);
542 #endif
544 ppb_MS_init_msq(negociate_microseq, 1,
545 NEGOCIATED_MODE, negociate_mode);
547 ppb_MS_microseq(ppbus, vpo->vpo_dev, negociate_microseq, &ret);
549 return (ret);
553 * imm_probe()
555 * Low level probe of vpo device
559 imm_probe(device_t dev, struct vpoio_data *vpo)
561 int error;
563 /* ppbus dependent initialisation */
564 vpo->vpo_dev = dev;
566 /* now, try to initialise the drive */
567 if ((error = imm_detect(vpo))) {
568 return (error);
571 return (0);
575 * imm_attach()
577 * Low level attachment of vpo device
581 imm_attach(struct vpoio_data *vpo)
583 DECLARE_NIBBLE_INBYTE_SUBMICROSEQ;
584 device_t ppbus = device_get_parent(vpo->vpo_dev);
585 int error = 0;
588 * Initialize microsequence code
590 vpo->vpo_nibble_inbyte_msq = kmalloc(sizeof(nibble_inbyte_submicroseq),
591 M_DEVBUF, M_WAITOK);
593 bcopy((void *)nibble_inbyte_submicroseq,
594 (void *)vpo->vpo_nibble_inbyte_msq,
595 sizeof(nibble_inbyte_submicroseq));
597 ppb_MS_init_msq(vpo->vpo_nibble_inbyte_msq, 4,
598 INB_NIBBLE_H, (void *)&(vpo)->vpo_nibble.h,
599 INB_NIBBLE_L, (void *)&(vpo)->vpo_nibble.l,
600 INB_NIBBLE_F, nibble_inbyte_hook,
601 INB_NIBBLE_P, (void *)&(vpo)->vpo_nibble);
604 * Initialize mode dependent in/out microsequences
606 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, PPB_WAIT)))
607 goto error;
609 /* ppbus automatically restore the last mode entered during detection */
610 switch (vpo->vpo_mode_found) {
611 case VP0_MODE_EPP:
612 ppb_MS_GET_init(ppbus, vpo->vpo_dev, epp17_instr);
613 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, epp17_outstr);
614 kprintf("imm%d: EPP mode\n", vpo->vpo_unit);
615 break;
616 case VP0_MODE_PS2:
617 ppb_MS_GET_init(ppbus, vpo->vpo_dev, ps2_inbyte_submicroseq);
618 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, spp_outbyte_submicroseq);
619 kprintf("imm%d: PS2 mode\n", vpo->vpo_unit);
620 break;
621 case VP0_MODE_NIBBLE:
622 ppb_MS_GET_init(ppbus, vpo->vpo_dev, vpo->vpo_nibble_inbyte_msq);
623 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, spp_outbyte_submicroseq);
624 kprintf("imm%d: NIBBLE mode\n", vpo->vpo_unit);
625 break;
626 default:
627 panic("imm: unknown mode %d", vpo->vpo_mode_found);
630 ppb_release_bus(ppbus, vpo->vpo_dev);
631 error:
632 return (error);
636 * imm_reset_bus()
640 imm_reset_bus(struct vpoio_data *vpo)
642 device_t ppbus = device_get_parent(vpo->vpo_dev);
643 int disconnected;
645 /* first, connect to the drive and request the bus */
646 imm_connect(vpo, PPB_WAIT|PPB_INTR, &disconnected, 1);
648 if (!disconnected) {
650 /* reset the SCSI bus */
651 ppb_MS_microseq(ppbus, vpo->vpo_dev, reset_microseq, NULL);
653 /* then disconnect */
654 imm_disconnect(vpo, NULL, 1);
657 return (0);
661 * imm_do_scsi()
663 * Send an SCSI command
666 int
667 imm_do_scsi(struct vpoio_data *vpo, int host, int target, char *command,
668 int clen, char *buffer, int blen, int *result, int *count,
669 int *ret)
671 device_t ppbus = device_get_parent(vpo->vpo_dev);
672 char r;
673 char l, h = 0;
674 int len, error = 0, not_connected = 0;
675 int k;
676 int negociated = 0;
679 * enter disk state, allocate the ppbus
681 * XXX
682 * Should we allow this call to be interruptible?
683 * The only way to report the interruption is to return
684 * EIO to upper SCSI code :^(
686 if ((error = imm_connect(vpo, PPB_WAIT|PPB_INTR, &not_connected, 1)))
687 return (error);
689 if (not_connected) {
690 *ret = VP0_ECONNECT; goto error;
694 * Select the drive ...
696 if ((*ret = imm_select(vpo,host,target)))
697 goto error;
700 * Send the command ...
702 for (k = 0; k < clen; k+=2) {
703 if (imm_wait(vpo, VP0_FAST_SPINTMO) != (char)0xa8) {
704 *ret = VP0_ECMD_TIMEOUT;
705 goto error;
707 if (imm_outstr(vpo, &command[k], 2)) {
708 *ret = VP0_EPPDATA_TIMEOUT;
709 goto error;
713 if (!(r = imm_wait(vpo, VP0_LOW_SPINTMO))) {
714 *ret = VP0_ESTATUS_TIMEOUT; goto error;
717 if ((r & 0x30) == 0x10) {
718 if (imm_negociate(vpo)) {
719 *ret = VP0_ENEGOCIATE;
720 goto error;
721 } else
722 negociated = 1;
726 * Complete transfer ...
728 *count = 0;
729 for (;;) {
731 if (!(r = imm_wait(vpo, VP0_LOW_SPINTMO))) {
732 *ret = VP0_ESTATUS_TIMEOUT; goto error;
735 /* stop when the ZIP+ wants to send status */
736 if (r == (char)0xb8)
737 break;
739 if (*count >= blen) {
740 *ret = VP0_EDATA_OVERFLOW;
741 goto error;
744 /* ZIP+ wants to send data? */
745 if (r == (char)0x88) {
746 len = (((blen - *count) >= VP0_SECTOR_SIZE)) ?
747 VP0_SECTOR_SIZE : 2;
749 error = imm_outstr(vpo, &buffer[*count], len);
750 } else {
751 if (!PPB_IN_EPP_MODE(ppbus))
752 len = 1;
753 else
754 len = (((blen - *count) >= VP0_SECTOR_SIZE)) ?
755 VP0_SECTOR_SIZE : 1;
757 error = imm_instr(vpo, &buffer[*count], len);
760 if (error) {
761 *ret = error;
762 goto error;
765 *count += len;
768 if ((PPB_IN_NIBBLE_MODE(ppbus) ||
769 PPB_IN_PS2_MODE(ppbus)) && negociated)
770 ppb_MS_microseq(ppbus, vpo->vpo_dev, transfer_epilog, NULL);
773 * Retrieve status ...
775 if (imm_negociate(vpo)) {
776 *ret = VP0_ENEGOCIATE;
777 goto error;
778 } else
779 negociated = 1;
781 if (imm_instr(vpo, &l, 1)) {
782 *ret = VP0_EOTHER; goto error;
785 /* check if the ZIP+ wants to send more status */
786 if (imm_wait(vpo, VP0_FAST_SPINTMO) == (char)0xb8)
787 if (imm_instr(vpo, &h, 1)) {
788 *ret = VP0_EOTHER+2; goto error;
791 /* Experience showed that we should discard this */
792 if (h == -1)
793 h = 0;
795 *result = ((int) h << 8) | ((int) l & 0xff);
797 error:
798 if ((PPB_IN_NIBBLE_MODE(ppbus) ||
799 PPB_IN_PS2_MODE(ppbus)) && negociated)
800 ppb_MS_microseq(ppbus, vpo->vpo_dev, transfer_epilog, NULL);
802 /* return to printer state, release the ppbus */
803 imm_disconnect(vpo, NULL, 1);
805 return (0);