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