0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #include <linux/module.h>
0020 #include <linux/threads.h>
0021 #include <linux/parport.h>
0022 #include <linux/delay.h>
0023 #include <linux/kernel.h>
0024 #include <linux/interrupt.h>
0025 #include <linux/timer.h>
0026 #include <linux/sched/signal.h>
0027
0028 #undef DEBUG
0029
0030 #ifdef CONFIG_LP_CONSOLE
0031 #undef DEBUG
0032 #endif
0033
0034
0035
0036 static void parport_ieee1284_wakeup (struct parport *port)
0037 {
0038 up (&port->physport->ieee1284.irq);
0039 }
0040
0041 static void timeout_waiting_on_port (struct timer_list *t)
0042 {
0043 struct parport *port = from_timer(port, t, timer);
0044
0045 parport_ieee1284_wakeup (port);
0046 }
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064 int parport_wait_event (struct parport *port, signed long timeout)
0065 {
0066 int ret;
0067
0068 if (!port->physport->cad->timeout)
0069
0070
0071 return 1;
0072
0073 timer_setup(&port->timer, timeout_waiting_on_port, 0);
0074 mod_timer(&port->timer, jiffies + timeout);
0075 ret = down_interruptible (&port->physport->ieee1284.irq);
0076 if (!del_timer_sync(&port->timer) && !ret)
0077
0078 ret = 1;
0079
0080 return ret;
0081 }
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 int parport_poll_peripheral(struct parport *port,
0108 unsigned char mask,
0109 unsigned char result,
0110 int usec)
0111 {
0112
0113 int count = usec / 5 + 2;
0114 int i;
0115 unsigned char status;
0116 for (i = 0; i < count; i++) {
0117 status = parport_read_status (port);
0118 if ((status & mask) == result)
0119 return 0;
0120 if (signal_pending (current))
0121 return -EINTR;
0122 if (need_resched())
0123 break;
0124 if (i >= 2)
0125 udelay (5);
0126 }
0127
0128 return 1;
0129 }
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156 int parport_wait_peripheral(struct parport *port,
0157 unsigned char mask,
0158 unsigned char result)
0159 {
0160 int ret;
0161 int usec;
0162 unsigned long deadline;
0163 unsigned char status;
0164
0165 usec = port->physport->spintime;
0166 if (!port->physport->cad->timeout)
0167
0168
0169 usec = 35000;
0170
0171
0172
0173
0174
0175
0176
0177 ret = parport_poll_peripheral (port, mask, result, usec);
0178 if (ret != 1)
0179 return ret;
0180
0181 if (!port->physport->cad->timeout)
0182
0183
0184 return 1;
0185
0186
0187 deadline = jiffies + msecs_to_jiffies(40);
0188 while (time_before (jiffies, deadline)) {
0189 if (signal_pending (current))
0190 return -EINTR;
0191
0192
0193
0194 if ((ret = parport_wait_event (port, msecs_to_jiffies(10))) < 0)
0195 return ret;
0196
0197 status = parport_read_status (port);
0198 if ((status & mask) == result)
0199 return 0;
0200
0201 if (!ret) {
0202
0203
0204
0205 schedule_timeout_interruptible(msecs_to_jiffies(10));
0206 }
0207 }
0208
0209 return 1;
0210 }
0211
0212 #ifdef CONFIG_PARPORT_1284
0213
0214 static void parport_ieee1284_terminate (struct parport *port)
0215 {
0216 int r;
0217 port = port->physport;
0218
0219
0220 switch (port->ieee1284.mode) {
0221 case IEEE1284_MODE_EPP:
0222 case IEEE1284_MODE_EPPSL:
0223 case IEEE1284_MODE_EPPSWE:
0224
0225
0226
0227 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
0228 udelay (50);
0229
0230
0231 parport_frob_control (port,
0232 PARPORT_CONTROL_SELECT
0233 | PARPORT_CONTROL_INIT,
0234 PARPORT_CONTROL_SELECT
0235 | PARPORT_CONTROL_INIT);
0236 break;
0237
0238 case IEEE1284_MODE_ECP:
0239 case IEEE1284_MODE_ECPRLE:
0240 case IEEE1284_MODE_ECPSWE:
0241
0242 if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE) {
0243
0244 parport_frob_control (port,
0245 PARPORT_CONTROL_INIT
0246 | PARPORT_CONTROL_AUTOFD,
0247 PARPORT_CONTROL_INIT
0248 | PARPORT_CONTROL_AUTOFD);
0249
0250
0251 r = parport_wait_peripheral (port,
0252 PARPORT_STATUS_PAPEROUT,
0253 PARPORT_STATUS_PAPEROUT);
0254 if (r)
0255 pr_debug("%s: Timeout at event 49\n",
0256 port->name);
0257
0258 parport_data_forward (port);
0259 pr_debug("%s: ECP direction: forward\n", port->name);
0260 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
0261 }
0262
0263 fallthrough;
0264
0265 default:
0266
0267
0268
0269 parport_frob_control (port,
0270 PARPORT_CONTROL_SELECT
0271 | PARPORT_CONTROL_AUTOFD,
0272 PARPORT_CONTROL_SELECT);
0273
0274
0275 r = parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0);
0276 if (r)
0277 pr_debug("%s: Timeout at event 24\n", port->name);
0278
0279
0280 parport_frob_control (port,
0281 PARPORT_CONTROL_AUTOFD,
0282 PARPORT_CONTROL_AUTOFD);
0283
0284
0285 r = parport_wait_peripheral (port,
0286 PARPORT_STATUS_ACK,
0287 PARPORT_STATUS_ACK);
0288 if (r)
0289 pr_debug("%s: Timeout at event 27\n", port->name);
0290
0291
0292 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
0293 }
0294
0295 port->ieee1284.mode = IEEE1284_MODE_COMPAT;
0296 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
0297
0298 pr_debug("%s: In compatibility (forward idle) mode\n", port->name);
0299 }
0300 #endif
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317 int parport_negotiate (struct parport *port, int mode)
0318 {
0319 #ifndef CONFIG_PARPORT_1284
0320 if (mode == IEEE1284_MODE_COMPAT)
0321 return 0;
0322 pr_err("parport: IEEE1284 not supported in this kernel\n");
0323 return -1;
0324 #else
0325 int m = mode & ~IEEE1284_ADDR;
0326 int r;
0327 unsigned char xflag;
0328
0329 port = port->physport;
0330
0331
0332 if (port->ieee1284.mode == mode)
0333 return 0;
0334
0335
0336 if ((port->ieee1284.mode & ~IEEE1284_ADDR) == (mode & ~IEEE1284_ADDR)){
0337 port->ieee1284.mode = mode;
0338 return 0;
0339 }
0340
0341
0342 if (port->ieee1284.mode != IEEE1284_MODE_COMPAT)
0343 parport_ieee1284_terminate (port);
0344
0345 if (mode == IEEE1284_MODE_COMPAT)
0346
0347 return 0;
0348
0349 switch (mode) {
0350 case IEEE1284_MODE_ECPSWE:
0351 m = IEEE1284_MODE_ECP;
0352 break;
0353 case IEEE1284_MODE_EPPSL:
0354 case IEEE1284_MODE_EPPSWE:
0355 m = IEEE1284_MODE_EPP;
0356 break;
0357 case IEEE1284_MODE_BECP:
0358 return -ENOSYS;
0359 }
0360
0361 if (mode & IEEE1284_EXT_LINK)
0362 m = 1<<7;
0363
0364 port->ieee1284.phase = IEEE1284_PH_NEGOTIATION;
0365
0366
0367 parport_frob_control (port,
0368 PARPORT_CONTROL_STROBE
0369 | PARPORT_CONTROL_AUTOFD
0370 | PARPORT_CONTROL_SELECT,
0371 PARPORT_CONTROL_SELECT);
0372 udelay(1);
0373
0374
0375 parport_data_forward (port);
0376 parport_write_data (port, m);
0377 udelay (400);
0378
0379
0380 parport_frob_control (port,
0381 PARPORT_CONTROL_SELECT
0382 | PARPORT_CONTROL_AUTOFD,
0383 PARPORT_CONTROL_AUTOFD);
0384
0385
0386 if (parport_wait_peripheral (port,
0387 PARPORT_STATUS_ERROR
0388 | PARPORT_STATUS_SELECT
0389 | PARPORT_STATUS_PAPEROUT
0390 | PARPORT_STATUS_ACK,
0391 PARPORT_STATUS_ERROR
0392 | PARPORT_STATUS_SELECT
0393 | PARPORT_STATUS_PAPEROUT)) {
0394
0395 parport_frob_control (port,
0396 PARPORT_CONTROL_SELECT
0397 | PARPORT_CONTROL_AUTOFD,
0398 PARPORT_CONTROL_SELECT);
0399 pr_debug("%s: Peripheral not IEEE1284 compliant (0x%02X)\n",
0400 port->name, parport_read_status (port));
0401 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
0402 return -1;
0403 }
0404
0405
0406 parport_frob_control (port,
0407 PARPORT_CONTROL_STROBE,
0408 PARPORT_CONTROL_STROBE);
0409
0410
0411 udelay (5);
0412 parport_frob_control (port,
0413 PARPORT_CONTROL_STROBE
0414 | PARPORT_CONTROL_AUTOFD,
0415 0);
0416
0417
0418 if (parport_wait_peripheral (port,
0419 PARPORT_STATUS_ACK,
0420 PARPORT_STATUS_ACK)) {
0421
0422 pr_debug("%s: Mode 0x%02x not supported? (0x%02x)\n",
0423 port->name, mode, port->ops->read_status (port));
0424 parport_ieee1284_terminate (port);
0425 return 1;
0426 }
0427
0428 xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
0429
0430
0431 if (mode && !xflag) {
0432
0433 pr_debug("%s: Mode 0x%02x rejected by peripheral\n",
0434 port->name, mode);
0435 parport_ieee1284_terminate (port);
0436 return 1;
0437 }
0438
0439
0440 if (mode & IEEE1284_EXT_LINK) {
0441 m = mode & 0x7f;
0442 udelay (1);
0443 parport_write_data (port, m);
0444 udelay (1);
0445
0446
0447 parport_frob_control (port,
0448 PARPORT_CONTROL_STROBE,
0449 PARPORT_CONTROL_STROBE);
0450
0451
0452 if (parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0)) {
0453
0454 pr_debug("%s: Event 52 didn't happen\n", port->name);
0455 parport_ieee1284_terminate (port);
0456 return 1;
0457 }
0458
0459
0460 parport_frob_control (port,
0461 PARPORT_CONTROL_STROBE,
0462 0);
0463
0464
0465 if (parport_wait_peripheral (port,
0466 PARPORT_STATUS_ACK,
0467 PARPORT_STATUS_ACK)) {
0468
0469
0470 pr_debug("%s: Mode 0x%02x not supported? (0x%02x)\n",
0471 port->name, mode,
0472 port->ops->read_status(port));
0473 parport_ieee1284_terminate (port);
0474 return 1;
0475 }
0476
0477
0478 xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
0479
0480
0481 if (!xflag) {
0482
0483 pr_debug("%s: Extended mode 0x%02x not supported\n",
0484 port->name, mode);
0485 parport_ieee1284_terminate (port);
0486 return 1;
0487 }
0488
0489
0490 }
0491
0492
0493 pr_debug("%s: In mode 0x%02x\n", port->name, mode);
0494 port->ieee1284.mode = mode;
0495
0496
0497 if (!(mode & IEEE1284_EXT_LINK) && (m & IEEE1284_MODE_ECP)) {
0498 port->ieee1284.phase = IEEE1284_PH_ECP_SETUP;
0499
0500
0501 parport_frob_control (port,
0502 PARPORT_CONTROL_AUTOFD,
0503 PARPORT_CONTROL_AUTOFD);
0504
0505
0506 r = parport_wait_peripheral (port,
0507 PARPORT_STATUS_PAPEROUT,
0508 PARPORT_STATUS_PAPEROUT);
0509 if (r) {
0510 pr_debug("%s: Timeout at event 31\n", port->name);
0511 }
0512
0513 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
0514 pr_debug("%s: ECP direction: forward\n", port->name);
0515 } else switch (mode) {
0516 case IEEE1284_MODE_NIBBLE:
0517 case IEEE1284_MODE_BYTE:
0518 port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
0519 break;
0520 default:
0521 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
0522 }
0523
0524
0525 return 0;
0526 #endif
0527 }
0528
0529
0530
0531
0532
0533
0534
0535 #ifdef CONFIG_PARPORT_1284
0536 static int parport_ieee1284_ack_data_avail (struct parport *port)
0537 {
0538 if (parport_read_status (port) & PARPORT_STATUS_ERROR)
0539
0540 return -1;
0541
0542
0543 port->ops->frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
0544 port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
0545 return 0;
0546 }
0547 #endif
0548
0549
0550 void parport_ieee1284_interrupt (void *handle)
0551 {
0552 struct parport *port = handle;
0553 parport_ieee1284_wakeup (port);
0554
0555 #ifdef CONFIG_PARPORT_1284
0556 if (port->ieee1284.phase == IEEE1284_PH_REV_IDLE) {
0557
0558
0559 pr_debug("%s: Data available\n", port->name);
0560 parport_ieee1284_ack_data_avail (port);
0561 }
0562 #endif
0563 }
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583 ssize_t parport_write (struct parport *port, const void *buffer, size_t len)
0584 {
0585 #ifndef CONFIG_PARPORT_1284
0586 return port->ops->compat_write_data (port, buffer, len, 0);
0587 #else
0588 ssize_t retval;
0589 int mode = port->ieee1284.mode;
0590 int addr = mode & IEEE1284_ADDR;
0591 size_t (*fn) (struct parport *, const void *, size_t, int);
0592
0593
0594 mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
0595
0596
0597 switch (mode) {
0598 case IEEE1284_MODE_NIBBLE:
0599 case IEEE1284_MODE_BYTE:
0600 parport_negotiate (port, IEEE1284_MODE_COMPAT);
0601 fallthrough;
0602 case IEEE1284_MODE_COMPAT:
0603 pr_debug("%s: Using compatibility mode\n", port->name);
0604 fn = port->ops->compat_write_data;
0605 break;
0606
0607 case IEEE1284_MODE_EPP:
0608 pr_debug("%s: Using EPP mode\n", port->name);
0609 if (addr) {
0610 fn = port->ops->epp_write_addr;
0611 } else {
0612 fn = port->ops->epp_write_data;
0613 }
0614 break;
0615 case IEEE1284_MODE_EPPSWE:
0616 pr_debug("%s: Using software-emulated EPP mode\n", port->name);
0617 if (addr) {
0618 fn = parport_ieee1284_epp_write_addr;
0619 } else {
0620 fn = parport_ieee1284_epp_write_data;
0621 }
0622 break;
0623 case IEEE1284_MODE_ECP:
0624 case IEEE1284_MODE_ECPRLE:
0625 pr_debug("%s: Using ECP mode\n", port->name);
0626 if (addr) {
0627 fn = port->ops->ecp_write_addr;
0628 } else {
0629 fn = port->ops->ecp_write_data;
0630 }
0631 break;
0632
0633 case IEEE1284_MODE_ECPSWE:
0634 pr_debug("%s: Using software-emulated ECP mode\n", port->name);
0635
0636
0637 if (addr) {
0638 fn = parport_ieee1284_ecp_write_addr;
0639 } else {
0640 fn = parport_ieee1284_ecp_write_data;
0641 }
0642 break;
0643
0644 default:
0645 pr_debug("%s: Unknown mode 0x%02x\n",
0646 port->name, port->ieee1284.mode);
0647 return -ENOSYS;
0648 }
0649
0650 retval = (*fn) (port, buffer, len, 0);
0651 pr_debug("%s: wrote %zd/%zu bytes\n", port->name, retval, len);
0652 return retval;
0653 #endif
0654 }
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670
0671
0672
0673
0674 ssize_t parport_read (struct parport *port, void *buffer, size_t len)
0675 {
0676 #ifndef CONFIG_PARPORT_1284
0677 pr_err("parport: IEEE1284 not supported in this kernel\n");
0678 return -ENODEV;
0679 #else
0680 int mode = port->physport->ieee1284.mode;
0681 int addr = mode & IEEE1284_ADDR;
0682 size_t (*fn) (struct parport *, void *, size_t, int);
0683
0684
0685 mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
0686
0687
0688 switch (mode) {
0689 case IEEE1284_MODE_COMPAT:
0690
0691
0692
0693
0694
0695 if ((port->physport->modes & PARPORT_MODE_TRISTATE) &&
0696 !parport_negotiate (port, IEEE1284_MODE_BYTE)) {
0697
0698 pr_debug("%s: Using byte mode\n", port->name);
0699 fn = port->ops->byte_read_data;
0700 break;
0701 }
0702 if (parport_negotiate (port, IEEE1284_MODE_NIBBLE)) {
0703 return -EIO;
0704 }
0705 fallthrough;
0706 case IEEE1284_MODE_NIBBLE:
0707 pr_debug("%s: Using nibble mode\n", port->name);
0708 fn = port->ops->nibble_read_data;
0709 break;
0710
0711 case IEEE1284_MODE_BYTE:
0712 pr_debug("%s: Using byte mode\n", port->name);
0713 fn = port->ops->byte_read_data;
0714 break;
0715
0716 case IEEE1284_MODE_EPP:
0717 pr_debug("%s: Using EPP mode\n", port->name);
0718 if (addr) {
0719 fn = port->ops->epp_read_addr;
0720 } else {
0721 fn = port->ops->epp_read_data;
0722 }
0723 break;
0724 case IEEE1284_MODE_EPPSWE:
0725 pr_debug("%s: Using software-emulated EPP mode\n", port->name);
0726 if (addr) {
0727 fn = parport_ieee1284_epp_read_addr;
0728 } else {
0729 fn = parport_ieee1284_epp_read_data;
0730 }
0731 break;
0732 case IEEE1284_MODE_ECP:
0733 case IEEE1284_MODE_ECPRLE:
0734 pr_debug("%s: Using ECP mode\n", port->name);
0735 fn = port->ops->ecp_read_data;
0736 break;
0737
0738 case IEEE1284_MODE_ECPSWE:
0739 pr_debug("%s: Using software-emulated ECP mode\n", port->name);
0740 fn = parport_ieee1284_ecp_read_data;
0741 break;
0742
0743 default:
0744 pr_debug("%s: Unknown mode 0x%02x\n",
0745 port->name, port->physport->ieee1284.mode);
0746 return -ENOSYS;
0747 }
0748
0749 return (*fn) (port, buffer, len, 0);
0750 #endif
0751 }
0752
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769 long parport_set_timeout (struct pardevice *dev, long inactivity)
0770 {
0771 long int old = dev->timeout;
0772
0773 dev->timeout = inactivity;
0774
0775 if (dev->port->physport->cad == dev)
0776 parport_ieee1284_wakeup (dev->port);
0777
0778 return old;
0779 }
0780
0781
0782
0783 EXPORT_SYMBOL(parport_negotiate);
0784 EXPORT_SYMBOL(parport_write);
0785 EXPORT_SYMBOL(parport_read);
0786 EXPORT_SYMBOL(parport_wait_peripheral);
0787 EXPORT_SYMBOL(parport_wait_event);
0788 EXPORT_SYMBOL(parport_set_timeout);
0789 EXPORT_SYMBOL(parport_ieee1284_interrupt);