1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/module.h>
19#include <linux/parport.h>
20#include <linux/delay.h>
21#include <linux/sched/signal.h>
22#include <linux/uaccess.h>
23
24#undef DEBUG
25
26#ifdef CONFIG_LP_CONSOLE
27#undef DEBUG
28#endif
29
30
31
32
33
34
35size_t parport_ieee1284_write_compat (struct parport *port,
36 const void *buffer, size_t len,
37 int flags)
38{
39 int no_irq = 1;
40 ssize_t count = 0;
41 const unsigned char *addr = buffer;
42 unsigned char byte;
43 struct pardevice *dev = port->physport->cad;
44 unsigned char ctl = (PARPORT_CONTROL_SELECT
45 | PARPORT_CONTROL_INIT);
46
47 if (port->irq != PARPORT_IRQ_NONE) {
48 parport_enable_irq (port);
49 no_irq = 0;
50 }
51
52 port->physport->ieee1284.phase = IEEE1284_PH_FWD_DATA;
53 parport_write_control (port, ctl);
54 parport_data_forward (port);
55 while (count < len) {
56 unsigned long expire = jiffies + dev->timeout;
57 long wait = msecs_to_jiffies(10);
58 unsigned char mask = (PARPORT_STATUS_ERROR
59 | PARPORT_STATUS_BUSY);
60 unsigned char val = (PARPORT_STATUS_ERROR
61 | PARPORT_STATUS_BUSY);
62
63
64 do {
65
66 if (!parport_wait_peripheral (port, mask, val))
67
68 goto ready;
69
70
71 if ((parport_read_status (port) &
72 (PARPORT_STATUS_PAPEROUT |
73 PARPORT_STATUS_SELECT |
74 PARPORT_STATUS_ERROR))
75 != (PARPORT_STATUS_SELECT |
76 PARPORT_STATUS_ERROR))
77
78
79
80
81
82 goto stop;
83
84
85 if (!time_before (jiffies, expire))
86 break;
87
88
89
90
91
92 if (count && no_irq) {
93 parport_release (dev);
94 schedule_timeout_interruptible(wait);
95 parport_claim_or_block (dev);
96 }
97 else
98
99 parport_wait_event (port, wait);
100
101
102 if (signal_pending (current))
103 break;
104
105
106 wait *= 2;
107 } while (time_before (jiffies, expire));
108
109 if (signal_pending (current))
110 break;
111
112 pr_debug("%s: Timed out\n", port->name);
113 break;
114
115 ready:
116
117 byte = *addr++;
118 parport_write_data (port, byte);
119 udelay (1);
120
121
122 parport_write_control (port, ctl | PARPORT_CONTROL_STROBE);
123 udelay (1);
124
125 parport_write_control (port, ctl);
126 udelay (1);
127
128
129 count++;
130
131
132 if (time_before (jiffies, expire))
133 if (!parport_yield_blocking (dev)
134 && need_resched())
135 schedule ();
136 }
137 stop:
138 port->physport->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
139
140 return count;
141}
142
143
144size_t parport_ieee1284_read_nibble (struct parport *port,
145 void *buffer, size_t len,
146 int flags)
147{
148#ifndef CONFIG_PARPORT_1284
149 return 0;
150#else
151 unsigned char *buf = buffer;
152 int i;
153 unsigned char byte = 0;
154
155 len *= 2;
156 for (i=0; i < len; i++) {
157 unsigned char nibble;
158
159
160 if (((i & 1) == 0) &&
161 (parport_read_status(port) & PARPORT_STATUS_ERROR)) {
162 goto end_of_data;
163 }
164
165
166 parport_frob_control (port,
167 PARPORT_CONTROL_AUTOFD,
168 PARPORT_CONTROL_AUTOFD);
169
170
171 port->ieee1284.phase = IEEE1284_PH_REV_DATA;
172 if (parport_wait_peripheral (port,
173 PARPORT_STATUS_ACK, 0)) {
174
175 pr_debug("%s: Nibble timeout at event 9 (%d bytes)\n",
176 port->name, i / 2);
177 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
178 break;
179 }
180
181
182
183 nibble = parport_read_status (port) >> 3;
184 nibble &= ~8;
185 if ((nibble & 0x10) == 0)
186 nibble |= 8;
187 nibble &= 0xf;
188
189
190 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
191
192
193 if (parport_wait_peripheral (port,
194 PARPORT_STATUS_ACK,
195 PARPORT_STATUS_ACK)) {
196
197 pr_debug("%s: Nibble timeout at event 11\n",
198 port->name);
199 break;
200 }
201
202 if (i & 1) {
203
204 byte |= nibble << 4;
205 *buf++ = byte;
206 } else
207 byte = nibble;
208 }
209
210 if (i == len) {
211
212 if (parport_read_status (port) & PARPORT_STATUS_ERROR) {
213 end_of_data:
214 pr_debug("%s: No more nibble data (%d bytes)\n",
215 port->name, i / 2);
216
217
218 parport_frob_control (port,
219 PARPORT_CONTROL_AUTOFD,
220 PARPORT_CONTROL_AUTOFD);
221 port->physport->ieee1284.phase = IEEE1284_PH_REV_IDLE;
222 }
223 else
224 port->physport->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
225 }
226
227 return i/2;
228#endif
229}
230
231
232size_t parport_ieee1284_read_byte (struct parport *port,
233 void *buffer, size_t len,
234 int flags)
235{
236#ifndef CONFIG_PARPORT_1284
237 return 0;
238#else
239 unsigned char *buf = buffer;
240 ssize_t count = 0;
241
242 for (count = 0; count < len; count++) {
243 unsigned char byte;
244
245
246 if (parport_read_status (port) & PARPORT_STATUS_ERROR) {
247 goto end_of_data;
248 }
249
250
251 parport_data_reverse (port);
252
253
254 parport_frob_control (port,
255 PARPORT_CONTROL_AUTOFD,
256 PARPORT_CONTROL_AUTOFD);
257
258
259 port->physport->ieee1284.phase = IEEE1284_PH_REV_DATA;
260 if (parport_wait_peripheral (port,
261 PARPORT_STATUS_ACK,
262 0)) {
263
264 parport_frob_control (port, PARPORT_CONTROL_AUTOFD,
265 0);
266 pr_debug("%s: Byte timeout at event 9\n", port->name);
267 break;
268 }
269
270 byte = parport_read_data (port);
271 *buf++ = byte;
272
273
274 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
275
276
277 if (parport_wait_peripheral (port,
278 PARPORT_STATUS_ACK,
279 PARPORT_STATUS_ACK)) {
280
281 pr_debug("%s: Byte timeout at event 11\n", port->name);
282 break;
283 }
284
285
286 parport_frob_control (port,
287 PARPORT_CONTROL_STROBE,
288 PARPORT_CONTROL_STROBE);
289 udelay (5);
290
291
292 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
293 }
294
295 if (count == len) {
296
297 if (parport_read_status (port) & PARPORT_STATUS_ERROR) {
298 end_of_data:
299 pr_debug("%s: No more byte data (%zd bytes)\n",
300 port->name, count);
301
302
303 parport_frob_control (port,
304 PARPORT_CONTROL_AUTOFD,
305 PARPORT_CONTROL_AUTOFD);
306 port->physport->ieee1284.phase = IEEE1284_PH_REV_IDLE;
307 }
308 else
309 port->physport->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
310 }
311
312 return count;
313#endif
314}
315
316
317
318
319
320#ifdef CONFIG_PARPORT_1284
321
322static inline
323int ecp_forward_to_reverse (struct parport *port)
324{
325 int retval;
326
327
328 parport_frob_control (port,
329 PARPORT_CONTROL_AUTOFD,
330 PARPORT_CONTROL_AUTOFD);
331 parport_data_reverse (port);
332 udelay (5);
333
334
335 parport_frob_control (port,
336 PARPORT_CONTROL_INIT,
337 0);
338
339
340 retval = parport_wait_peripheral (port,
341 PARPORT_STATUS_PAPEROUT, 0);
342
343 if (!retval) {
344 pr_debug("%s: ECP direction: reverse\n", port->name);
345 port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
346 } else {
347 pr_debug("%s: ECP direction: failed to reverse\n", port->name);
348 port->ieee1284.phase = IEEE1284_PH_ECP_DIR_UNKNOWN;
349 }
350
351 return retval;
352}
353
354static inline
355int ecp_reverse_to_forward (struct parport *port)
356{
357 int retval;
358
359
360 parport_frob_control (port,
361 PARPORT_CONTROL_INIT
362 | PARPORT_CONTROL_AUTOFD,
363 PARPORT_CONTROL_INIT
364 | PARPORT_CONTROL_AUTOFD);
365
366
367 retval = parport_wait_peripheral (port,
368 PARPORT_STATUS_PAPEROUT,
369 PARPORT_STATUS_PAPEROUT);
370
371 if (!retval) {
372 parport_data_forward (port);
373 pr_debug("%s: ECP direction: forward\n", port->name);
374 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
375 } else {
376 pr_debug("%s: ECP direction: failed to switch forward\n",
377 port->name);
378 port->ieee1284.phase = IEEE1284_PH_ECP_DIR_UNKNOWN;
379 }
380
381
382 return retval;
383}
384
385#endif
386
387
388size_t parport_ieee1284_ecp_write_data (struct parport *port,
389 const void *buffer, size_t len,
390 int flags)
391{
392#ifndef CONFIG_PARPORT_1284
393 return 0;
394#else
395 const unsigned char *buf = buffer;
396 size_t written;
397 int retry;
398
399 port = port->physport;
400
401 if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE)
402 if (ecp_reverse_to_forward (port))
403 return 0;
404
405 port->ieee1284.phase = IEEE1284_PH_FWD_DATA;
406
407
408 parport_frob_control (port,
409 PARPORT_CONTROL_AUTOFD
410 | PARPORT_CONTROL_STROBE
411 | PARPORT_CONTROL_INIT,
412 PARPORT_CONTROL_INIT);
413 for (written = 0; written < len; written++, buf++) {
414 unsigned long expire = jiffies + port->cad->timeout;
415 unsigned char byte;
416
417 byte = *buf;
418 try_again:
419 parport_write_data (port, byte);
420 parport_frob_control (port, PARPORT_CONTROL_STROBE,
421 PARPORT_CONTROL_STROBE);
422 udelay (5);
423 for (retry = 0; retry < 100; retry++) {
424 if (!parport_wait_peripheral (port,
425 PARPORT_STATUS_BUSY, 0))
426 goto success;
427
428 if (signal_pending (current)) {
429 parport_frob_control (port,
430 PARPORT_CONTROL_STROBE,
431 0);
432 break;
433 }
434 }
435
436
437 pr_debug("%s: ECP transfer stalled!\n", port->name);
438
439 parport_frob_control (port, PARPORT_CONTROL_INIT,
440 PARPORT_CONTROL_INIT);
441 udelay (50);
442 if (parport_read_status (port) & PARPORT_STATUS_PAPEROUT) {
443
444 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
445 break;
446 }
447
448 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
449 udelay (50);
450 if (!(parport_read_status (port) & PARPORT_STATUS_PAPEROUT))
451 break;
452
453 pr_debug("%s: Host transfer recovered\n", port->name);
454
455 if (time_after_eq (jiffies, expire)) break;
456 goto try_again;
457 success:
458 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
459 udelay (5);
460 if (parport_wait_peripheral (port,
461 PARPORT_STATUS_BUSY,
462 PARPORT_STATUS_BUSY))
463
464 break;
465 }
466
467 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
468
469 return written;
470#endif
471}
472
473
474size_t parport_ieee1284_ecp_read_data (struct parport *port,
475 void *buffer, size_t len, int flags)
476{
477#ifndef CONFIG_PARPORT_1284
478 return 0;
479#else
480 struct pardevice *dev = port->cad;
481 unsigned char *buf = buffer;
482 int rle_count = 0;
483 unsigned char ctl;
484 int rle = 0;
485 ssize_t count = 0;
486
487 port = port->physport;
488
489 if (port->ieee1284.phase != IEEE1284_PH_REV_IDLE)
490 if (ecp_forward_to_reverse (port))
491 return 0;
492
493 port->ieee1284.phase = IEEE1284_PH_REV_DATA;
494
495
496 ctl = parport_read_control (port);
497 ctl &= ~(PARPORT_CONTROL_STROBE | PARPORT_CONTROL_INIT |
498 PARPORT_CONTROL_AUTOFD);
499 parport_write_control (port,
500 ctl | PARPORT_CONTROL_AUTOFD);
501 while (count < len) {
502 unsigned long expire = jiffies + dev->timeout;
503 unsigned char byte;
504 int command;
505
506
507
508 while (parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0)) {
509
510
511
512 if (count)
513 goto out;
514
515
516
517 if (!time_before (jiffies, expire))
518 goto out;
519
520
521 if (count && dev->port->irq != PARPORT_IRQ_NONE) {
522 parport_release (dev);
523 schedule_timeout_interruptible(msecs_to_jiffies(40));
524 parport_claim_or_block (dev);
525 }
526 else
527
528 parport_wait_event (port, msecs_to_jiffies(40));
529
530
531 if (signal_pending (current))
532 goto out;
533 }
534
535
536 if (rle)
537
538
539 command = 0;
540 else
541 command = (parport_read_status (port) &
542 PARPORT_STATUS_BUSY) ? 1 : 0;
543
544
545 byte = parport_read_data (port);
546
547
548
549 if (command) {
550 if (byte & 0x80) {
551 pr_debug("%s: stopping short at channel command (%02x)\n",
552 port->name, byte);
553 goto out;
554 }
555 else if (port->ieee1284.mode != IEEE1284_MODE_ECPRLE)
556 pr_debug("%s: device illegally using RLE; accepting anyway\n",
557 port->name);
558
559 rle_count = byte + 1;
560
561
562 if (rle_count > (len - count)) {
563 pr_debug("%s: leaving %d RLE bytes for next time\n",
564 port->name, rle_count);
565 break;
566 }
567
568 rle = 1;
569 }
570
571
572 parport_write_control (port, ctl);
573
574
575 if (parport_wait_peripheral (port, PARPORT_STATUS_ACK,
576 PARPORT_STATUS_ACK)) {
577
578
579 pr_debug("ECP read timed out at 45\n");
580
581 if (command)
582 pr_warn("%s: command ignored (%02x)\n",
583 port->name, byte);
584
585 break;
586 }
587
588
589 parport_write_control (port,
590 ctl | PARPORT_CONTROL_AUTOFD);
591
592
593 if (command)
594 continue;
595
596
597 if (rle) {
598 rle = 0;
599 memset (buf, byte, rle_count);
600 buf += rle_count;
601 count += rle_count;
602 pr_debug("%s: decompressed to %d bytes\n",
603 port->name, rle_count);
604 } else {
605
606 *buf = byte;
607 buf++, count++;
608 }
609 }
610
611 out:
612 port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
613 return count;
614#endif
615}
616
617
618size_t parport_ieee1284_ecp_write_addr (struct parport *port,
619 const void *buffer, size_t len,
620 int flags)
621{
622#ifndef CONFIG_PARPORT_1284
623 return 0;
624#else
625 const unsigned char *buf = buffer;
626 size_t written;
627 int retry;
628
629 port = port->physport;
630
631 if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE)
632 if (ecp_reverse_to_forward (port))
633 return 0;
634
635 port->ieee1284.phase = IEEE1284_PH_FWD_DATA;
636
637
638 parport_frob_control (port,
639 PARPORT_CONTROL_AUTOFD
640 | PARPORT_CONTROL_STROBE
641 | PARPORT_CONTROL_INIT,
642 PARPORT_CONTROL_AUTOFD
643 | PARPORT_CONTROL_INIT);
644 for (written = 0; written < len; written++, buf++) {
645 unsigned long expire = jiffies + port->cad->timeout;
646 unsigned char byte;
647
648 byte = *buf;
649 try_again:
650 parport_write_data (port, byte);
651 parport_frob_control (port, PARPORT_CONTROL_STROBE,
652 PARPORT_CONTROL_STROBE);
653 udelay (5);
654 for (retry = 0; retry < 100; retry++) {
655 if (!parport_wait_peripheral (port,
656 PARPORT_STATUS_BUSY, 0))
657 goto success;
658
659 if (signal_pending (current)) {
660 parport_frob_control (port,
661 PARPORT_CONTROL_STROBE,
662 0);
663 break;
664 }
665 }
666
667
668 pr_debug("%s: ECP transfer stalled!\n", port->name);
669
670 parport_frob_control (port, PARPORT_CONTROL_INIT,
671 PARPORT_CONTROL_INIT);
672 udelay (50);
673 if (parport_read_status (port) & PARPORT_STATUS_PAPEROUT) {
674
675 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
676 break;
677 }
678
679 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
680 udelay (50);
681 if (!(parport_read_status (port) & PARPORT_STATUS_PAPEROUT))
682 break;
683
684 pr_debug("%s: Host transfer recovered\n", port->name);
685
686 if (time_after_eq (jiffies, expire)) break;
687 goto try_again;
688 success:
689 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
690 udelay (5);
691 if (parport_wait_peripheral (port,
692 PARPORT_STATUS_BUSY,
693 PARPORT_STATUS_BUSY))
694
695 break;
696 }
697
698 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
699
700 return written;
701#endif
702}
703
704
705
706
707
708
709size_t parport_ieee1284_epp_write_data (struct parport *port,
710 const void *buffer, size_t len,
711 int flags)
712{
713 unsigned char *bp = (unsigned char *) buffer;
714 size_t ret = 0;
715
716
717 parport_frob_control (port,
718 PARPORT_CONTROL_STROBE |
719 PARPORT_CONTROL_AUTOFD |
720 PARPORT_CONTROL_SELECT |
721 PARPORT_CONTROL_INIT,
722 PARPORT_CONTROL_STROBE |
723 PARPORT_CONTROL_INIT);
724 port->ops->data_forward (port);
725 for (; len > 0; len--, bp++) {
726
727 parport_write_data (port, *bp);
728 parport_frob_control (port, PARPORT_CONTROL_AUTOFD,
729 PARPORT_CONTROL_AUTOFD);
730
731
732 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY, 0, 10))
733 break;
734
735
736 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
737
738
739 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY,
740 PARPORT_STATUS_BUSY, 5))
741 break;
742
743 ret++;
744 }
745
746
747 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
748
749 return ret;
750}
751
752
753size_t parport_ieee1284_epp_read_data (struct parport *port,
754 void *buffer, size_t len,
755 int flags)
756{
757 unsigned char *bp = (unsigned char *) buffer;
758 unsigned ret = 0;
759
760
761 parport_frob_control (port,
762 PARPORT_CONTROL_STROBE |
763 PARPORT_CONTROL_AUTOFD |
764 PARPORT_CONTROL_SELECT |
765 PARPORT_CONTROL_INIT,
766 PARPORT_CONTROL_INIT);
767 port->ops->data_reverse (port);
768 for (; len > 0; len--, bp++) {
769
770 parport_frob_control (port,
771 PARPORT_CONTROL_AUTOFD,
772 PARPORT_CONTROL_AUTOFD);
773
774 if (parport_wait_peripheral (port, PARPORT_STATUS_BUSY, 0)) {
775 break;
776 }
777
778 *bp = parport_read_data (port);
779
780
781 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
782
783
784 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY,
785 PARPORT_STATUS_BUSY, 5)) {
786 break;
787 }
788
789 ret++;
790 }
791 port->ops->data_forward (port);
792
793 return ret;
794}
795
796
797size_t parport_ieee1284_epp_write_addr (struct parport *port,
798 const void *buffer, size_t len,
799 int flags)
800{
801 unsigned char *bp = (unsigned char *) buffer;
802 size_t ret = 0;
803
804
805 parport_frob_control (port,
806 PARPORT_CONTROL_STROBE |
807 PARPORT_CONTROL_AUTOFD |
808 PARPORT_CONTROL_SELECT |
809 PARPORT_CONTROL_INIT,
810 PARPORT_CONTROL_STROBE |
811 PARPORT_CONTROL_INIT);
812 port->ops->data_forward (port);
813 for (; len > 0; len--, bp++) {
814
815 parport_write_data (port, *bp);
816 parport_frob_control (port, PARPORT_CONTROL_SELECT,
817 PARPORT_CONTROL_SELECT);
818
819
820 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY, 0, 10))
821 break;
822
823
824 parport_frob_control (port, PARPORT_CONTROL_SELECT, 0);
825
826
827 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY,
828 PARPORT_STATUS_BUSY, 5))
829 break;
830
831 ret++;
832 }
833
834
835 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
836
837 return ret;
838}
839
840
841size_t parport_ieee1284_epp_read_addr (struct parport *port,
842 void *buffer, size_t len,
843 int flags)
844{
845 unsigned char *bp = (unsigned char *) buffer;
846 unsigned ret = 0;
847
848
849 parport_frob_control (port,
850 PARPORT_CONTROL_STROBE |
851 PARPORT_CONTROL_AUTOFD |
852 PARPORT_CONTROL_SELECT |
853 PARPORT_CONTROL_INIT,
854 PARPORT_CONTROL_INIT);
855 port->ops->data_reverse (port);
856 for (; len > 0; len--, bp++) {
857
858 parport_frob_control (port, PARPORT_CONTROL_SELECT,
859 PARPORT_CONTROL_SELECT);
860
861
862 if (parport_wait_peripheral (port, PARPORT_STATUS_BUSY, 0)) {
863 break;
864 }
865
866 *bp = parport_read_data (port);
867
868
869 parport_frob_control (port, PARPORT_CONTROL_SELECT,
870 0);
871
872
873 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY,
874 PARPORT_STATUS_BUSY, 5))
875 break;
876
877 ret++;
878 }
879 port->ops->data_forward (port);
880
881 return ret;
882}
883
884EXPORT_SYMBOL(parport_ieee1284_ecp_write_data);
885EXPORT_SYMBOL(parport_ieee1284_ecp_read_data);
886EXPORT_SYMBOL(parport_ieee1284_ecp_write_addr);
887EXPORT_SYMBOL(parport_ieee1284_write_compat);
888EXPORT_SYMBOL(parport_ieee1284_read_nibble);
889EXPORT_SYMBOL(parport_ieee1284_read_byte);
890EXPORT_SYMBOL(parport_ieee1284_epp_write_data);
891EXPORT_SYMBOL(parport_ieee1284_epp_read_data);
892EXPORT_SYMBOL(parport_ieee1284_epp_write_addr);
893EXPORT_SYMBOL(parport_ieee1284_epp_read_addr);
894