kernel: Remove newlines from the panic messages that have one.
[dragonfly.git] / sys / bus / ppbus / ppb_1284.c
1 /*-
2  * Copyright (c) 1997 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  * $FreeBSD: src/sys/dev/ppbus/ppb_1284.c,v 1.11 2000/01/14 08:03:14 nsouch Exp $
27  */
28
29 /*
30  * General purpose routines for the IEEE1284-1994 Standard
31  */
32
33 #include "opt_ppb_1284.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38
39 #include <machine/clock.h>
40
41 #include "ppbconf.h"
42 #include "ppb_1284.h"
43
44 #include "ppbus_if.h"
45
46 #include "ppbio.h"
47
48 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
49
50 /*
51  * do_1284_wait()
52  *
53  * Wait for the peripherial up to 40ms
54  */
55 static int
56 do_1284_wait(device_t bus, char mask, char status)
57 {
58         return (ppb_poll_bus(bus, 4, mask, status, PPB_NOINTR | PPB_POLL));
59 }
60
61 static int
62 do_peripheral_wait(device_t bus, char mask, char status)
63 {
64         return (ppb_poll_bus(bus, 100, mask, status, PPB_NOINTR | PPB_POLL));
65 }
66
67 #define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4)
68
69 /*
70  * ppb_1284_reset_error()
71  *
72  * Unconditionaly reset the error field
73  */
74 static int
75 ppb_1284_reset_error(device_t bus, int state)
76 {
77         struct ppb_data *ppb = DEVTOSOFTC(bus);
78
79         ppb->error = PPB_NO_ERROR;
80         ppb->state = state;
81
82         return (0);
83 }
84
85 /*
86  * ppb_1284_get_state()
87  *
88  * Get IEEE1284 state
89  */
90 int
91 ppb_1284_get_state(device_t bus)
92 {
93         return (DEVTOSOFTC(bus)->state);
94 }
95
96 /*
97  * ppb_1284_set_state()
98  *
99  * Change IEEE1284 state if no error occured
100  */
101 int
102 ppb_1284_set_state(device_t bus, int state)
103 {
104         struct ppb_data *ppb = DEVTOSOFTC(bus);
105
106         /* call ppb_1284_reset_error() if you absolutly want to change
107          * the state from PPB_ERROR to another */
108         if ((ppb->state != PPB_ERROR) &&
109                         (ppb->error == PPB_NO_ERROR)) {
110                 ppb->state = state;
111                 ppb->error = PPB_NO_ERROR;
112         }
113
114         return (0);
115 }
116
117 static int
118 ppb_1284_set_error(device_t bus, int error, int event)
119 {
120         struct ppb_data *ppb = DEVTOSOFTC(bus);
121
122         /* do not accumulate errors */
123         if ((ppb->error == PPB_NO_ERROR) &&
124                         (ppb->state != PPB_ERROR)) {
125                 ppb->error = error;
126                 ppb->state = PPB_ERROR;
127         }
128
129 #ifdef DEBUG_1284
130         kprintf("ppb1284: error=%d status=0x%x event=%d\n", error,
131                 ppb_rstr(bus) & 0xff, event);
132 #endif
133
134         return (0);
135 }
136
137 /*
138  * ppb_request_mode()
139  *
140  * Converts mode+options into ext. value
141  */
142 static int
143 ppb_request_mode(int mode, int options)
144 {
145         int request_mode = 0;
146
147         if (options & PPB_EXTENSIBILITY_LINK) {
148                 request_mode = EXT_LINK_1284_NORMAL;
149
150         } else {
151                 switch (mode) {
152                 case PPB_NIBBLE:
153                         request_mode = (options & PPB_REQUEST_ID) ?
154                                         NIBBLE_1284_REQUEST_ID :
155                                         NIBBLE_1284_NORMAL;
156                         break;
157                 case PPB_PS2:
158                         request_mode = (options & PPB_REQUEST_ID) ?
159                                         BYTE_1284_REQUEST_ID :
160                                         BYTE_1284_NORMAL;
161                         break;
162                 case PPB_ECP:
163                         if (options & PPB_USE_RLE)
164                                 request_mode = (options & PPB_REQUEST_ID) ?
165                                         ECP_1284_RLE_REQUEST_ID :
166                                         ECP_1284_RLE;
167                         else
168                                 request_mode = (options & PPB_REQUEST_ID) ?
169                                         ECP_1284_REQUEST_ID :
170                                         ECP_1284_NORMAL;
171                         break;
172                 case PPB_EPP:
173                         request_mode = EPP_1284_NORMAL;
174                         break;
175                 default:
176                         panic("%s: unsupported mode %d", __func__, mode);
177                 }
178         }
179
180         return (request_mode);
181 }
182
183 /*
184  * ppb_peripheral_negociate()
185  *
186  * Negociate the peripheral side
187  */
188 int
189 ppb_peripheral_negociate(device_t bus, int mode, int options)
190 {
191         int spin, request_mode, error = 0;
192         char r;
193
194         ppb_set_mode(bus, PPB_COMPATIBLE);
195         ppb_1284_set_state(bus, PPB_PERIPHERAL_NEGOCIATION);
196
197         /* compute ext. value */
198         request_mode = ppb_request_mode(mode, options);
199
200         /* wait host */
201         spin = 10;
202         while (spin-- && (ppb_rstr(bus) & nBUSY))
203                 DELAY(1);
204
205         /* check termination */
206         if (!(ppb_rstr(bus) & SELECT) || !spin) {
207                 error = ENODEV;
208                 goto error;
209         }
210
211         /* Event 4 - read ext. value */
212         r = ppb_rdtr(bus);
213
214         /* nibble mode is not supported */
215         if ((r == (char)request_mode) ||
216                         (r == NIBBLE_1284_NORMAL)) {
217
218                 /* Event 5 - restore direction bit, no data avail */
219                 ppb_wctr(bus, (STROBE | nINIT) & ~(SELECTIN));
220                 DELAY(1);
221
222                 /* Event 6 */
223                 ppb_wctr(bus, (nINIT) & ~(SELECTIN | STROBE));
224
225                 if (r == NIBBLE_1284_NORMAL) {
226 #ifdef DEBUG_1284
227                         kprintf("R");
228 #endif
229                         ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
230                         error = EINVAL;
231                         goto error;
232                 } else {
233                         ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
234                         switch (r) {
235                         case BYTE_1284_NORMAL:
236                                 ppb_set_mode(bus, PPB_BYTE);
237                                 break;
238                         default:
239                                 break;
240                         }
241 #ifdef DEBUG_1284
242                         kprintf("A");
243 #endif
244                         /* negociation succeeds */
245                 }
246         } else {
247                 /* Event 5 - mode not supported */
248                 ppb_wctr(bus, SELECTIN);
249                 DELAY(1);
250
251                 /* Event 6 */
252                 ppb_wctr(bus, (SELECTIN) & ~(STROBE | nINIT));
253                 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
254
255 #ifdef DEBUG_1284
256                 kprintf("r");
257 #endif
258                 error = EINVAL;
259                 goto error;
260         }
261
262         return (0);
263
264 error:
265         ppb_peripheral_terminate(bus, PPB_WAIT);
266         return (error);
267 }
268
269 /*
270  * ppb_peripheral_terminate()
271  *
272  * Terminate peripheral transfer side
273  *
274  * Always return 0 in compatible mode
275  */
276 int
277 ppb_peripheral_terminate(device_t bus, int how)
278 {
279         int error = 0;
280
281 #ifdef DEBUG_1284
282         kprintf("t");
283 #endif
284
285         ppb_1284_set_state(bus, PPB_PERIPHERAL_TERMINATION);
286
287         /* Event 22 - wait up to host response time (1s) */
288         if ((error = do_peripheral_wait(bus, SELECT | nBUSY, 0))) {
289                 ppb_1284_set_error(bus, PPB_TIMEOUT, 22);
290                 goto error;
291         }
292
293         /* Event 24 */
294         ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
295
296         /* Event 25 - wait up to host response time (1s) */
297         if ((error = do_peripheral_wait(bus, nBUSY, nBUSY))) {
298                 ppb_1284_set_error(bus, PPB_TIMEOUT, 25);
299                 goto error;
300         }
301
302         /* Event 26 */
303         ppb_wctr(bus, (SELECTIN | nINIT | STROBE) & ~(AUTOFEED));
304         DELAY(1);
305         /* Event 27 */
306         ppb_wctr(bus, (SELECTIN | nINIT) & ~(STROBE | AUTOFEED));
307
308         /* Event 28 - wait up to host response time (1s) */
309         if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
310                 ppb_1284_set_error(bus, PPB_TIMEOUT, 28);
311                 goto error;
312         }
313         
314 error:
315         ppb_set_mode(bus, PPB_COMPATIBLE);
316         ppb_1284_set_state(bus, PPB_FORWARD_IDLE);
317
318         return (0);
319 }
320
321 /*
322  * byte_peripheral_outbyte()
323  *
324  * Write 1 byte in BYTE mode
325  */
326 static int
327 byte_peripheral_outbyte(device_t bus, char *buffer, int last)
328 {
329         int error = 0;
330
331         /* Event 7 */
332         if ((error = do_1284_wait(bus, nBUSY, nBUSY))) {
333                 ppb_1284_set_error(bus, PPB_TIMEOUT, 7);
334                 goto error;
335         }
336
337         /* check termination */
338         if (!(ppb_rstr(bus) & SELECT)) {
339                 ppb_peripheral_terminate(bus, PPB_WAIT);
340                 goto error;
341         }
342
343         /* Event 15 - put byte on data lines */
344 #ifdef DEBUG_1284
345         kprintf("B");
346 #endif
347         ppb_wdtr(bus, *buffer);
348
349         /* Event 9 */
350         ppb_wctr(bus, (AUTOFEED | STROBE) & ~(nINIT | SELECTIN));
351
352         /* Event 10 - wait data read */
353         if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
354                 ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
355                 goto error;
356         }
357
358         /* Event 11 */
359         if (!last) {
360                 ppb_wctr(bus, (AUTOFEED) & ~(nINIT | STROBE | SELECTIN));
361         } else {
362                 ppb_wctr(bus, (nINIT) & ~(STROBE | SELECTIN | AUTOFEED));
363         }
364
365 #if 0
366         /* Event 16 - wait strobe */
367         if ((error = do_peripheral_wait(bus, nACK | nBUSY, 0))) {
368                 ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
369                 goto error;
370         }
371 #endif
372
373         /* check termination */
374         if (!(ppb_rstr(bus) & SELECT)) {
375                 ppb_peripheral_terminate(bus, PPB_WAIT);
376                 goto error;
377         }
378
379 error:
380         return (error);
381 }
382
383 /*
384  * byte_peripheral_write()
385  *
386  * Write n bytes in BYTE mode
387  */
388 int
389 byte_peripheral_write(device_t bus, char *buffer, int len, int *sent)
390 {
391         int error = 0, i;
392         char r;
393
394         ppb_1284_set_state(bus, PPB_PERIPHERAL_TRANSFER);
395
396         /* wait forever, the remote host is master and should initiate
397          * termination
398          */
399         for (i=0; i<len; i++) {
400                 /* force remote nFAULT low to release the remote waiting
401                  * process, if any
402                  */
403                 r = ppb_rctr(bus);
404                 ppb_wctr(bus, r & ~nINIT);
405
406 #ifdef DEBUG_1284
407                 kprintf("y");
408 #endif
409                 /* Event 7 */
410                 error = ppb_poll_bus(bus, PPB_FOREVER, nBUSY, nBUSY,
411                                         PPB_INTR);
412
413                 if (error && error != EWOULDBLOCK)
414                         goto error;
415
416 #ifdef DEBUG_1284
417                 kprintf("b");
418 #endif
419                 if ((error = byte_peripheral_outbyte(bus, buffer+i, (i == len-1))))
420                         goto error;
421         }
422 error:
423         if (!error)
424                 ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
425
426         *sent = i;
427         return (error);
428 }
429
430 /*
431  * byte_1284_inbyte()
432  *
433  * Read 1 byte in BYTE mode
434  */
435 int
436 byte_1284_inbyte(device_t bus, char *buffer)
437 {
438         int error = 0;
439
440         /* Event 7 - ready to take data (nAUTO low) */
441         ppb_wctr(bus, (PCD | nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
442
443         /* Event 9 - peripheral set nAck low */
444         if ((error = do_1284_wait(bus, nACK, 0))) {
445                 ppb_1284_set_error(bus, PPB_TIMEOUT, 9);
446                 goto error;
447         }
448
449         /* read the byte */
450         *buffer = ppb_rdtr(bus);
451
452         /* Event 10 - data received, can't accept more */
453         ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
454
455         /* Event 11 - peripheral ack */
456         if ((error = do_1284_wait(bus, nACK, nACK))) {
457                 ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
458                 goto error;
459         }
460
461         /* Event 16 - strobe */
462         ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
463         DELAY(3);
464         ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
465
466 error:
467         return (error);
468 }
469
470 /*
471  * nibble_1284_inbyte()
472  *
473  * Read 1 byte in NIBBLE mode
474  */
475 int
476 nibble_1284_inbyte(device_t bus, char *buffer)
477 {
478         char nibble[2];
479         int i, error;
480
481         for (i = 0; i < 2; i++) {
482
483                 /* Event 7 - ready to take data (nAUTO low) */
484                 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
485
486                 /* Event 8 - peripheral writes the first nibble */
487
488                 /* Event 9 - peripheral set nAck low */
489                 if ((error = do_1284_wait(bus, nACK, 0))) {
490                         ppb_1284_set_error(bus, PPB_TIMEOUT, 9);
491                         goto error;
492                 }
493
494                 /* read nibble */
495                 nibble[i] = ppb_rstr(bus);
496
497                 /* Event 10 - ack, nibble received */
498                 ppb_wctr(bus, nINIT & ~(AUTOFEED | STROBE | SELECTIN));
499
500                 /* Event 11 - wait ack from peripherial */
501                 if ((error = do_1284_wait(bus, nACK, nACK))) {
502                         ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
503                         goto error;
504                 }
505         }
506
507         *buffer = ((nibble2char(nibble[1]) << 4) & 0xf0) |
508                                 (nibble2char(nibble[0]) & 0x0f);
509
510 error:
511         return (error);
512 }
513
514 /*
515  * spp_1284_read()
516  *
517  * Read in IEEE1284 NIBBLE/BYTE mode
518  */
519 int
520 spp_1284_read(device_t bus, int mode, char *buffer, int max, int *read)
521 {
522         int error = 0, len = 0;
523         int terminate_after_transfer = 1;
524         int state;
525
526         *read = len = 0;
527
528         state = ppb_1284_get_state(bus);
529
530         switch (state) {
531         case PPB_FORWARD_IDLE:
532                 if ((error = ppb_1284_negociate(bus, mode, 0)))
533                         return (error);
534                 break;
535
536         case PPB_REVERSE_IDLE:
537                 terminate_after_transfer = 0;
538                 break;
539                 
540         default:
541                 ppb_1284_terminate(bus);
542                 if ((error = ppb_1284_negociate(bus, mode, 0)))
543                         return (error);
544                 break;
545         }
546
547         while ((len < max) && !(ppb_rstr(bus) & (nFAULT))) {
548
549                 ppb_1284_set_state(bus, PPB_REVERSE_TRANSFER);
550
551 #ifdef DEBUG_1284
552                 kprintf("B");
553 #endif
554
555                 switch (mode) {
556                 case PPB_NIBBLE:
557                         /* read a byte, error means no more data */
558                         if (nibble_1284_inbyte(bus, buffer+len))
559                                 goto end_while;
560                         break;
561                 case PPB_BYTE:
562                         if (byte_1284_inbyte(bus, buffer+len))
563                                 goto end_while;
564                         break;
565                 default:
566                         error = EINVAL;
567                         goto end_while;
568                 }
569                 len ++;
570         }
571 end_while:
572
573         if (!error)
574                 ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
575
576         *read = len;
577
578         if (terminate_after_transfer || error)
579                 ppb_1284_terminate(bus);
580
581         return (error);
582 }
583
584 /*
585  * ppb_1284_read_id()
586  *
587  */
588 int
589 ppb_1284_read_id(device_t bus, int mode, char *buffer,
590                 int max, int *read)
591 {
592         int error = 0;
593
594         /* fill the buffer with 0s */
595         bzero(buffer, max);
596
597         switch (mode) {
598         case PPB_NIBBLE:
599         case PPB_ECP:
600                 if ((error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID)))
601                         return (error);
602                 error = spp_1284_read(bus, PPB_NIBBLE, buffer, max, read);
603                 break;
604         case PPB_BYTE:
605                 if ((error = ppb_1284_negociate(bus, PPB_BYTE, PPB_REQUEST_ID)))
606                         return (error);
607                 error = spp_1284_read(bus, PPB_BYTE, buffer, max, read);
608                 break;
609         default:
610                 panic("%s: unsupported mode %d", __func__, mode);
611         }
612
613         ppb_1284_terminate(bus);
614         return (error);
615 }
616
617 /*
618  * ppb_1284_read()
619  *
620  * IEEE1284 read
621  */
622 int
623 ppb_1284_read(device_t bus, int mode, char *buffer,
624                 int max, int *read)
625 {
626         int error = 0;
627
628         switch (mode) {
629         case PPB_NIBBLE:
630         case PPB_BYTE:
631                 error = spp_1284_read(bus, mode, buffer, max, read);
632                 break;
633         default:
634                 return (EINVAL);
635         }
636
637         return (error);
638 }
639
640 /*
641  * ppb_1284_negociate()
642  *
643  * IEEE1284 negociation phase
644  *
645  * Normal nibble mode or request device id mode (see ppb_1284.h)
646  *
647  * After negociation, nFAULT is low if data is available
648  */
649 int
650 ppb_1284_negociate(device_t bus, int mode, int options)
651 {
652         int error;
653         int request_mode;
654
655 #ifdef DEBUG_1284
656         kprintf("n");
657 #endif
658
659         if (ppb_1284_get_state(bus) >= PPB_PERIPHERAL_NEGOCIATION)
660                 ppb_peripheral_terminate(bus, PPB_WAIT);
661
662         if (ppb_1284_get_state(bus) != PPB_FORWARD_IDLE)
663                 ppb_1284_terminate(bus);
664
665 #ifdef DEBUG_1284
666         kprintf("%d", mode);
667 #endif
668
669         /* ensure the host is in compatible mode */
670         ppb_set_mode(bus, PPB_COMPATIBLE);
671
672         /* reset error to catch the actual negociation error */
673         ppb_1284_reset_error(bus, PPB_FORWARD_IDLE);
674
675         /* calculate ext. value */
676         request_mode = ppb_request_mode(mode, options);
677
678         /* default state */
679         ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
680         DELAY(1);
681
682         /* enter negociation phase */
683         ppb_1284_set_state(bus, PPB_NEGOCIATION);
684
685         /* Event 0 - put the exten. value on the data lines */
686         ppb_wdtr(bus, request_mode);
687
688 #ifdef PERIPH_1284
689         /* request remote host attention */
690         ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
691         DELAY(1);
692         ppb_wctr(bus, (nINIT) & ~(STROBE | AUTOFEED | SELECTIN));
693 #else
694         DELAY(1);
695
696 #endif /* !PERIPH_1284 */
697
698         /* Event 1 - enter IEEE1284 mode */
699         ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
700
701 #ifdef PERIPH_1284
702         /* ignore the PError line, wait a bit more, remote host's 
703          * interrupts don't respond fast enough */
704         if (ppb_poll_bus(bus, 40, nACK | SELECT | nFAULT,
705                                 SELECT | nFAULT, PPB_NOINTR | PPB_POLL)) {
706                 ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
707                 error = ENODEV;
708                 goto error;
709         }
710 #else
711         /* Event 2 - trying IEEE1284 dialog */
712         if (do_1284_wait(bus, nACK | PERROR | SELECT | nFAULT,
713                         PERROR  | SELECT | nFAULT)) {
714                 ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
715                 error = ENODEV;
716                 goto error;
717         }
718 #endif /* !PERIPH_1284 */
719
720         /* Event 3 - latch the ext. value to the peripheral */
721         ppb_wctr(bus, (nINIT | STROBE | AUTOFEED) & ~SELECTIN);
722         DELAY(1);
723
724         /* Event 4 - IEEE1284 device recognized */
725         ppb_wctr(bus, nINIT & ~(SELECTIN | AUTOFEED | STROBE));
726
727         /* Event 6 - waiting for status lines */
728         if (do_1284_wait(bus, nACK, nACK)) {
729                 ppb_1284_set_error(bus, PPB_TIMEOUT, 6);
730                 error = EBUSY;
731                 goto error;
732         }
733
734         /* Event 7 - quering result consider nACK not to misunderstand
735          * a remote computer terminate sequence */
736         if (options & PPB_EXTENSIBILITY_LINK) {
737
738                 /* XXX not fully supported yet */
739                 ppb_1284_terminate(bus);
740                 return (0);
741
742         }
743         if (request_mode == NIBBLE_1284_NORMAL) {
744                 if (do_1284_wait(bus, nACK | SELECT, nACK)) {
745                         ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
746                         error = ENODEV;
747                         goto error;
748                 }
749         } else {
750                 if (do_1284_wait(bus, nACK | SELECT, SELECT | nACK)) {
751                         ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
752                         error = ENODEV;
753                         goto error;
754                 }
755         }
756
757         switch (mode) {
758         case PPB_NIBBLE:
759         case PPB_PS2:
760                 /* enter reverse idle phase */
761                 ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
762                 break;
763         case PPB_ECP:
764                 /* negociation ok, now setup the communication */
765                 ppb_1284_set_state(bus, PPB_SETUP);
766                 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(SELECTIN | STROBE));
767
768 #ifdef PERIPH_1284
769                 /* ignore PError line */
770                 if (do_1284_wait(bus, nACK | SELECT | nBUSY,
771                                         nACK | SELECT | nBUSY)) {
772                         ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
773                         error = ENODEV;
774                         goto error;
775                 }
776 #else
777                 if (do_1284_wait(bus, nACK | SELECT | PERROR | nBUSY,
778                                         nACK | SELECT | PERROR | nBUSY)) {
779                         ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
780                         error = ENODEV;
781                         goto error;
782                 }
783 #endif /* !PERIPH_1284 */
784
785                 /* ok, the host enters the ForwardIdle state */
786                 ppb_1284_set_state(bus, PPB_ECP_FORWARD_IDLE);
787                 break;
788         case PPB_EPP:
789                 ppb_1284_set_state(bus, PPB_EPP_IDLE);
790                 break;
791
792         default:
793                 panic("%s: unknown mode (%d)!", __func__, mode);
794         }
795         ppb_set_mode(bus, mode);
796
797         return (0);
798
799 error:
800         ppb_1284_terminate(bus);
801
802         return (error);
803 }
804
805 /*
806  * ppb_1284_terminate()
807  *
808  * IEEE1284 termination phase, return code should ignored since the host
809  * is _always_ in compatible mode after ppb_1284_terminate()
810  */
811 int
812 ppb_1284_terminate(device_t bus)
813 {
814
815 #ifdef DEBUG_1284
816         kprintf("T");
817 #endif
818
819         /* do not reset error here to keep the error that
820          * may occured before the ppb_1284_terminate() call */
821         ppb_1284_set_state(bus, PPB_TERMINATION);
822
823 #ifdef PERIPH_1284
824         /* request remote host attention */
825         ppb_wctr(bus, (nINIT | STROBE | SELECTIN) & ~(AUTOFEED));
826         DELAY(1);
827 #endif /* PERIPH_1284 */
828
829         /* Event 22 - set nSelectin low and nAutoFeed high */
830         ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
831
832         /* Event 24 - waiting for peripheral, Xflag ignored */
833         if (do_1284_wait(bus, nACK | nBUSY | nFAULT, nFAULT)) {
834                 ppb_1284_set_error(bus, PPB_TIMEOUT, 24);
835                 goto error;
836         }
837
838         /* Event 25 - set nAutoFd low */
839         ppb_wctr(bus, (nINIT | SELECTIN | AUTOFEED) & ~STROBE);
840
841         /* Event 26 - compatible mode status is set */
842
843         /* Event 27 - peripheral set nAck high */
844         if (do_1284_wait(bus, nACK, nACK)) {
845                 ppb_1284_set_error(bus, PPB_TIMEOUT, 27);
846         }
847
848         /* Event 28 - end termination, return to idle phase */
849         ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
850
851 error:
852         /* return to compatible mode */
853         ppb_set_mode(bus, PPB_COMPATIBLE);
854         ppb_1284_set_state(bus, PPB_FORWARD_IDLE);
855
856         return (0);
857 }