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