1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/module.h>
20#include <linux/threads.h>
21#include <linux/parport.h>
22#include <linux/delay.h>
23#include <linux/kernel.h>
24#include <linux/interrupt.h>
25#include <linux/timer.h>
26#include <linux/sched/signal.h>
27
28#undef DEBUG
29
30#ifdef CONFIG_LP_CONSOLE
31#undef DEBUG
32#endif
33
34
35
36static void parport_ieee1284_wakeup (struct parport *port)
37{
38 up (&port->physport->ieee1284.irq);
39}
40
41static void timeout_waiting_on_port (struct timer_list *t)
42{
43 struct parport *port = from_timer(port, t, timer);
44
45 parport_ieee1284_wakeup (port);
46}
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64int parport_wait_event (struct parport *port, signed long timeout)
65{
66 int ret;
67
68 if (!port->physport->cad->timeout)
69
70
71 return 1;
72
73 timer_setup(&port->timer, timeout_waiting_on_port, 0);
74 mod_timer(&port->timer, jiffies + timeout);
75 ret = down_interruptible (&port->physport->ieee1284.irq);
76 if (!del_timer_sync(&port->timer) && !ret)
77
78 ret = 1;
79
80 return ret;
81}
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107int parport_poll_peripheral(struct parport *port,
108 unsigned char mask,
109 unsigned char result,
110 int usec)
111{
112
113 int count = usec / 5 + 2;
114 int i;
115 unsigned char status;
116 for (i = 0; i < count; i++) {
117 status = parport_read_status (port);
118 if ((status & mask) == result)
119 return 0;
120 if (signal_pending (current))
121 return -EINTR;
122 if (need_resched())
123 break;
124 if (i >= 2)
125 udelay (5);
126 }
127
128 return 1;
129}
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156int parport_wait_peripheral(struct parport *port,
157 unsigned char mask,
158 unsigned char result)
159{
160 int ret;
161 int usec;
162 unsigned long deadline;
163 unsigned char status;
164
165 usec = port->physport->spintime;
166 if (!port->physport->cad->timeout)
167
168
169 usec = 35000;
170
171
172
173
174
175
176
177 ret = parport_poll_peripheral (port, mask, result, usec);
178 if (ret != 1)
179 return ret;
180
181 if (!port->physport->cad->timeout)
182
183
184 return 1;
185
186
187 deadline = jiffies + msecs_to_jiffies(40);
188 while (time_before (jiffies, deadline)) {
189 if (signal_pending (current))
190 return -EINTR;
191
192
193
194 if ((ret = parport_wait_event (port, msecs_to_jiffies(10))) < 0)
195 return ret;
196
197 status = parport_read_status (port);
198 if ((status & mask) == result)
199 return 0;
200
201 if (!ret) {
202
203
204
205 schedule_timeout_interruptible(msecs_to_jiffies(10));
206 }
207 }
208
209 return 1;
210}
211
212#ifdef CONFIG_PARPORT_1284
213
214static void parport_ieee1284_terminate (struct parport *port)
215{
216 int r;
217 port = port->physport;
218
219
220 switch (port->ieee1284.mode) {
221 case IEEE1284_MODE_EPP:
222 case IEEE1284_MODE_EPPSL:
223 case IEEE1284_MODE_EPPSWE:
224
225
226
227 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
228 udelay (50);
229
230
231 parport_frob_control (port,
232 PARPORT_CONTROL_SELECT
233 | PARPORT_CONTROL_INIT,
234 PARPORT_CONTROL_SELECT
235 | PARPORT_CONTROL_INIT);
236 break;
237
238 case IEEE1284_MODE_ECP:
239 case IEEE1284_MODE_ECPRLE:
240 case IEEE1284_MODE_ECPSWE:
241
242 if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE) {
243
244 parport_frob_control (port,
245 PARPORT_CONTROL_INIT
246 | PARPORT_CONTROL_AUTOFD,
247 PARPORT_CONTROL_INIT
248 | PARPORT_CONTROL_AUTOFD);
249
250
251 r = parport_wait_peripheral (port,
252 PARPORT_STATUS_PAPEROUT,
253 PARPORT_STATUS_PAPEROUT);
254 if (r)
255 pr_debug("%s: Timeout at event 49\n",
256 port->name);
257
258 parport_data_forward (port);
259 pr_debug("%s: ECP direction: forward\n", port->name);
260 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
261 }
262
263 fallthrough;
264
265 default:
266
267
268
269 parport_frob_control (port,
270 PARPORT_CONTROL_SELECT
271 | PARPORT_CONTROL_AUTOFD,
272 PARPORT_CONTROL_SELECT);
273
274
275 r = parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0);
276 if (r)
277 pr_debug("%s: Timeout at event 24\n", port->name);
278
279
280 parport_frob_control (port,
281 PARPORT_CONTROL_AUTOFD,
282 PARPORT_CONTROL_AUTOFD);
283
284
285 r = parport_wait_peripheral (port,
286 PARPORT_STATUS_ACK,
287 PARPORT_STATUS_ACK);
288 if (r)
289 pr_debug("%s: Timeout at event 27\n", port->name);
290
291
292 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
293 }
294
295 port->ieee1284.mode = IEEE1284_MODE_COMPAT;
296 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
297
298 pr_debug("%s: In compatibility (forward idle) mode\n", port->name);
299}
300#endif
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317int parport_negotiate (struct parport *port, int mode)
318{
319#ifndef CONFIG_PARPORT_1284
320 if (mode == IEEE1284_MODE_COMPAT)
321 return 0;
322 pr_err("parport: IEEE1284 not supported in this kernel\n");
323 return -1;
324#else
325 int m = mode & ~IEEE1284_ADDR;
326 int r;
327 unsigned char xflag;
328
329 port = port->physport;
330
331
332 if (port->ieee1284.mode == mode)
333 return 0;
334
335
336 if ((port->ieee1284.mode & ~IEEE1284_ADDR) == (mode & ~IEEE1284_ADDR)){
337 port->ieee1284.mode = mode;
338 return 0;
339 }
340
341
342 if (port->ieee1284.mode != IEEE1284_MODE_COMPAT)
343 parport_ieee1284_terminate (port);
344
345 if (mode == IEEE1284_MODE_COMPAT)
346
347 return 0;
348
349 switch (mode) {
350 case IEEE1284_MODE_ECPSWE:
351 m = IEEE1284_MODE_ECP;
352 break;
353 case IEEE1284_MODE_EPPSL:
354 case IEEE1284_MODE_EPPSWE:
355 m = IEEE1284_MODE_EPP;
356 break;
357 case IEEE1284_MODE_BECP:
358 return -ENOSYS;
359 }
360
361 if (mode & IEEE1284_EXT_LINK)
362 m = 1<<7;
363
364 port->ieee1284.phase = IEEE1284_PH_NEGOTIATION;
365
366
367 parport_frob_control (port,
368 PARPORT_CONTROL_STROBE
369 | PARPORT_CONTROL_AUTOFD
370 | PARPORT_CONTROL_SELECT,
371 PARPORT_CONTROL_SELECT);
372 udelay(1);
373
374
375 parport_data_forward (port);
376 parport_write_data (port, m);
377 udelay (400);
378
379
380 parport_frob_control (port,
381 PARPORT_CONTROL_SELECT
382 | PARPORT_CONTROL_AUTOFD,
383 PARPORT_CONTROL_AUTOFD);
384
385
386 if (parport_wait_peripheral (port,
387 PARPORT_STATUS_ERROR
388 | PARPORT_STATUS_SELECT
389 | PARPORT_STATUS_PAPEROUT
390 | PARPORT_STATUS_ACK,
391 PARPORT_STATUS_ERROR
392 | PARPORT_STATUS_SELECT
393 | PARPORT_STATUS_PAPEROUT)) {
394
395 parport_frob_control (port,
396 PARPORT_CONTROL_SELECT
397 | PARPORT_CONTROL_AUTOFD,
398 PARPORT_CONTROL_SELECT);
399 pr_debug("%s: Peripheral not IEEE1284 compliant (0x%02X)\n",
400 port->name, parport_read_status (port));
401 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
402 return -1;
403 }
404
405
406 parport_frob_control (port,
407 PARPORT_CONTROL_STROBE,
408 PARPORT_CONTROL_STROBE);
409
410
411 udelay (5);
412 parport_frob_control (port,
413 PARPORT_CONTROL_STROBE
414 | PARPORT_CONTROL_AUTOFD,
415 0);
416
417
418 if (parport_wait_peripheral (port,
419 PARPORT_STATUS_ACK,
420 PARPORT_STATUS_ACK)) {
421
422 pr_debug("%s: Mode 0x%02x not supported? (0x%02x)\n",
423 port->name, mode, port->ops->read_status (port));
424 parport_ieee1284_terminate (port);
425 return 1;
426 }
427
428 xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
429
430
431 if (mode && !xflag) {
432
433 pr_debug("%s: Mode 0x%02x rejected by peripheral\n",
434 port->name, mode);
435 parport_ieee1284_terminate (port);
436 return 1;
437 }
438
439
440 if (mode & IEEE1284_EXT_LINK) {
441 m = mode & 0x7f;
442 udelay (1);
443 parport_write_data (port, m);
444 udelay (1);
445
446
447 parport_frob_control (port,
448 PARPORT_CONTROL_STROBE,
449 PARPORT_CONTROL_STROBE);
450
451
452 if (parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0)) {
453
454 pr_debug("%s: Event 52 didn't happen\n", port->name);
455 parport_ieee1284_terminate (port);
456 return 1;
457 }
458
459
460 parport_frob_control (port,
461 PARPORT_CONTROL_STROBE,
462 0);
463
464
465 if (parport_wait_peripheral (port,
466 PARPORT_STATUS_ACK,
467 PARPORT_STATUS_ACK)) {
468
469
470 pr_debug("%s: Mode 0x%02x not supported? (0x%02x)\n",
471 port->name, mode,
472 port->ops->read_status(port));
473 parport_ieee1284_terminate (port);
474 return 1;
475 }
476
477
478 xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
479
480
481 if (!xflag) {
482
483 pr_debug("%s: Extended mode 0x%02x not supported\n",
484 port->name, mode);
485 parport_ieee1284_terminate (port);
486 return 1;
487 }
488
489
490 }
491
492
493 pr_debug("%s: In mode 0x%02x\n", port->name, mode);
494 port->ieee1284.mode = mode;
495
496
497 if (!(mode & IEEE1284_EXT_LINK) && (m & IEEE1284_MODE_ECP)) {
498 port->ieee1284.phase = IEEE1284_PH_ECP_SETUP;
499
500
501 parport_frob_control (port,
502 PARPORT_CONTROL_AUTOFD,
503 PARPORT_CONTROL_AUTOFD);
504
505
506 r = parport_wait_peripheral (port,
507 PARPORT_STATUS_PAPEROUT,
508 PARPORT_STATUS_PAPEROUT);
509 if (r) {
510 pr_debug("%s: Timeout at event 31\n", port->name);
511 }
512
513 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
514 pr_debug("%s: ECP direction: forward\n", port->name);
515 } else switch (mode) {
516 case IEEE1284_MODE_NIBBLE:
517 case IEEE1284_MODE_BYTE:
518 port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
519 break;
520 default:
521 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
522 }
523
524
525 return 0;
526#endif
527}
528
529
530
531
532
533
534
535#ifdef CONFIG_PARPORT_1284
536static int parport_ieee1284_ack_data_avail (struct parport *port)
537{
538 if (parport_read_status (port) & PARPORT_STATUS_ERROR)
539
540 return -1;
541
542
543 port->ops->frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
544 port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
545 return 0;
546}
547#endif
548
549
550void parport_ieee1284_interrupt (void *handle)
551{
552 struct parport *port = handle;
553 parport_ieee1284_wakeup (port);
554
555#ifdef CONFIG_PARPORT_1284
556 if (port->ieee1284.phase == IEEE1284_PH_REV_IDLE) {
557
558
559 pr_debug("%s: Data available\n", port->name);
560 parport_ieee1284_ack_data_avail (port);
561 }
562#endif
563}
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583ssize_t parport_write (struct parport *port, const void *buffer, size_t len)
584{
585#ifndef CONFIG_PARPORT_1284
586 return port->ops->compat_write_data (port, buffer, len, 0);
587#else
588 ssize_t retval;
589 int mode = port->ieee1284.mode;
590 int addr = mode & IEEE1284_ADDR;
591 size_t (*fn) (struct parport *, const void *, size_t, int);
592
593
594 mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
595
596
597 switch (mode) {
598 case IEEE1284_MODE_NIBBLE:
599 case IEEE1284_MODE_BYTE:
600 parport_negotiate (port, IEEE1284_MODE_COMPAT);
601 fallthrough;
602 case IEEE1284_MODE_COMPAT:
603 pr_debug("%s: Using compatibility mode\n", port->name);
604 fn = port->ops->compat_write_data;
605 break;
606
607 case IEEE1284_MODE_EPP:
608 pr_debug("%s: Using EPP mode\n", port->name);
609 if (addr) {
610 fn = port->ops->epp_write_addr;
611 } else {
612 fn = port->ops->epp_write_data;
613 }
614 break;
615 case IEEE1284_MODE_EPPSWE:
616 pr_debug("%s: Using software-emulated EPP mode\n", port->name);
617 if (addr) {
618 fn = parport_ieee1284_epp_write_addr;
619 } else {
620 fn = parport_ieee1284_epp_write_data;
621 }
622 break;
623 case IEEE1284_MODE_ECP:
624 case IEEE1284_MODE_ECPRLE:
625 pr_debug("%s: Using ECP mode\n", port->name);
626 if (addr) {
627 fn = port->ops->ecp_write_addr;
628 } else {
629 fn = port->ops->ecp_write_data;
630 }
631 break;
632
633 case IEEE1284_MODE_ECPSWE:
634 pr_debug("%s: Using software-emulated ECP mode\n", port->name);
635
636
637 if (addr) {
638 fn = parport_ieee1284_ecp_write_addr;
639 } else {
640 fn = parport_ieee1284_ecp_write_data;
641 }
642 break;
643
644 default:
645 pr_debug("%s: Unknown mode 0x%02x\n",
646 port->name, port->ieee1284.mode);
647 return -ENOSYS;
648 }
649
650 retval = (*fn) (port, buffer, len, 0);
651 pr_debug("%s: wrote %zd/%zu bytes\n", port->name, retval, len);
652 return retval;
653#endif
654}
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674ssize_t parport_read (struct parport *port, void *buffer, size_t len)
675{
676#ifndef CONFIG_PARPORT_1284
677 pr_err("parport: IEEE1284 not supported in this kernel\n");
678 return -ENODEV;
679#else
680 int mode = port->physport->ieee1284.mode;
681 int addr = mode & IEEE1284_ADDR;
682 size_t (*fn) (struct parport *, void *, size_t, int);
683
684
685 mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
686
687
688 switch (mode) {
689 case IEEE1284_MODE_COMPAT:
690
691
692
693
694
695 if ((port->physport->modes & PARPORT_MODE_TRISTATE) &&
696 !parport_negotiate (port, IEEE1284_MODE_BYTE)) {
697
698 pr_debug("%s: Using byte mode\n", port->name);
699 fn = port->ops->byte_read_data;
700 break;
701 }
702 if (parport_negotiate (port, IEEE1284_MODE_NIBBLE)) {
703 return -EIO;
704 }
705 fallthrough;
706 case IEEE1284_MODE_NIBBLE:
707 pr_debug("%s: Using nibble mode\n", port->name);
708 fn = port->ops->nibble_read_data;
709 break;
710
711 case IEEE1284_MODE_BYTE:
712 pr_debug("%s: Using byte mode\n", port->name);
713 fn = port->ops->byte_read_data;
714 break;
715
716 case IEEE1284_MODE_EPP:
717 pr_debug("%s: Using EPP mode\n", port->name);
718 if (addr) {
719 fn = port->ops->epp_read_addr;
720 } else {
721 fn = port->ops->epp_read_data;
722 }
723 break;
724 case IEEE1284_MODE_EPPSWE:
725 pr_debug("%s: Using software-emulated EPP mode\n", port->name);
726 if (addr) {
727 fn = parport_ieee1284_epp_read_addr;
728 } else {
729 fn = parport_ieee1284_epp_read_data;
730 }
731 break;
732 case IEEE1284_MODE_ECP:
733 case IEEE1284_MODE_ECPRLE:
734 pr_debug("%s: Using ECP mode\n", port->name);
735 fn = port->ops->ecp_read_data;
736 break;
737
738 case IEEE1284_MODE_ECPSWE:
739 pr_debug("%s: Using software-emulated ECP mode\n", port->name);
740 fn = parport_ieee1284_ecp_read_data;
741 break;
742
743 default:
744 pr_debug("%s: Unknown mode 0x%02x\n",
745 port->name, port->physport->ieee1284.mode);
746 return -ENOSYS;
747 }
748
749 return (*fn) (port, buffer, len, 0);
750#endif
751}
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769long parport_set_timeout (struct pardevice *dev, long inactivity)
770{
771 long int old = dev->timeout;
772
773 dev->timeout = inactivity;
774
775 if (dev->port->physport->cad == dev)
776 parport_ieee1284_wakeup (dev->port);
777
778 return old;
779}
780
781
782
783EXPORT_SYMBOL(parport_negotiate);
784EXPORT_SYMBOL(parport_write);
785EXPORT_SYMBOL(parport_read);
786EXPORT_SYMBOL(parport_wait_peripheral);
787EXPORT_SYMBOL(parport_wait_event);
788EXPORT_SYMBOL(parport_set_timeout);
789EXPORT_SYMBOL(parport_ieee1284_interrupt);
790