Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ieee1284.c
Go to the documentation of this file.
1 /*
2  * IEEE-1284 implementation for parport.
3  *
4  * Authors: Phil Blundell <[email protected]>
5  * Carsten Gross <[email protected]>
6  * Jose Renau <[email protected]>
7  * Tim Waugh <[email protected]> (largely rewritten)
8  *
9  * This file is responsible for IEEE 1284 negotiation, and for handing
10  * read/write requests to low-level drivers.
11  *
12  * Any part of this program may be used in documents licensed under
13  * the GNU Free Documentation License, Version 1.1 or any later version
14  * published by the Free Software Foundation.
15  *
16  * Various hacks, Fred Barnes <[email protected]>, 04/2000
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 /* undef me for production */
29 
30 #ifdef CONFIG_LP_CONSOLE
31 #undef DEBUG /* Don't want a garbled console */
32 #endif
33 
34 #ifdef DEBUG
35 #define DPRINTK(stuff...) printk (stuff)
36 #else
37 #define DPRINTK(stuff...)
38 #endif
39 
40 /* Make parport_wait_peripheral wake up.
41  * It will be useful to call this from an interrupt handler. */
42 static void parport_ieee1284_wakeup (struct parport *port)
43 {
44  up (&port->physport->ieee1284.irq);
45 }
46 
47 static struct parport *port_from_cookie[PARPORT_MAX];
48 static void timeout_waiting_on_port (unsigned long cookie)
49 {
50  parport_ieee1284_wakeup (port_from_cookie[cookie % PARPORT_MAX]);
51 }
52 
69 int 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  /* Zero timeout is special, and we can't down() the
76  semaphore. */
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  /* Timed out. */
89  ret = 1;
90 
91  destroy_timer_on_stack(&timer);
92 
93  return ret;
94 }
95 
121  unsigned char mask,
122  unsigned char result,
123  int usec)
124 {
125  /* Zero return code is success, >0 is timeout. */
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 
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; /* usecs of fast polling */
179  if (!port->physport->cad->timeout)
180  /* A zero timeout is "special": busy wait for the
181  entire 35ms. */
182  usec = 35000;
183 
184  /* Fast polling.
185  *
186  * This should be adjustable.
187  * How about making a note (in the device structure) of how long
188  * it takes, so we know for next time?
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  /* We may be in an interrupt handler, so we can't poll
196  * slowly anyway. */
197  return 1;
198 
199  /* 40ms of slow polling. */
200  deadline = jiffies + msecs_to_jiffies(40);
201  while (time_before (jiffies, deadline)) {
202  if (signal_pending (current))
203  return -EINTR;
204 
205  /* Wait for 10ms (or until an interrupt occurs if
206  * the handler is set) */
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  /* parport_wait_event didn't time out, but the
216  * peripheral wasn't actually ready either.
217  * Wait for another 10ms. */
219  }
220  }
221 
222  return 1;
223 }
224 
225 #ifdef CONFIG_PARPORT_1284
226 /* Terminate a negotiated mode. */
227 static void parport_ieee1284_terminate (struct parport *port)
228 {
229  int r;
230  port = port->physport;
231 
232  /* EPP terminates differently. */
233  switch (port->ieee1284.mode) {
234  case IEEE1284_MODE_EPP:
235  case IEEE1284_MODE_EPPSL:
237  /* Terminate from EPP mode. */
238 
239  /* Event 68: Set nInit low */
241  udelay (50);
242 
243  /* Event 69: Set nInit high, nSelectIn low */
244  parport_frob_control (port,
249  break;
250 
251  case IEEE1284_MODE_ECP:
254  /* In ECP we can only terminate from fwd idle phase. */
255  if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE) {
256  /* Event 47: Set nInit high */
257  parport_frob_control (port,
262 
263  /* Event 49: PError goes high */
264  r = parport_wait_peripheral (port,
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  /* fall-though.. */
278 
279  default:
280  /* Terminate from all other modes. */
281 
282  /* Event 22: Set nSelectIn low, nAutoFd high */
283  parport_frob_control (port,
287 
288  /* Event 24: nAck goes low */
290  if (r)
291  DPRINTK (KERN_INFO "%s: Timeout at event 24\n",
292  port->name);
293 
294  /* Event 25: Set nAutoFd low */
295  parport_frob_control (port,
298 
299  /* Event 27: nAck goes high */
300  r = parport_wait_peripheral (port,
303  if (r)
304  DPRINTK (KERN_INFO "%s: Timeout at event 27\n",
305  port->name);
306 
307  /* Event 29: Set nAutoFd high */
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 /* IEEE1284 support */
318 
334 int 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  /* Is there anything to do? */
349  if (port->ieee1284.mode == mode)
350  return 0;
351 
352  /* Is the difference just an address-or-not bit? */
353  if ((port->ieee1284.mode & ~IEEE1284_ADDR) == (mode & ~IEEE1284_ADDR)){
354  port->ieee1284.mode = mode;
355  return 0;
356  }
357 
358  /* Go to compatibility forward idle mode */
359  if (port->ieee1284.mode != IEEE1284_MODE_COMPAT)
360  parport_ieee1284_terminate (port);
361 
362  if (mode == IEEE1284_MODE_COMPAT)
363  /* Compatibility mode: no negotiation. */
364  return 0;
365 
366  switch (mode) {
368  m = IEEE1284_MODE_ECP;
369  break;
370  case IEEE1284_MODE_EPPSL:
372  m = IEEE1284_MODE_EPP;
373  break;
374  case IEEE1284_MODE_BECP:
375  return -ENOSYS; /* FIXME (implement BECP) */
376  }
377 
378  if (mode & IEEE1284_EXT_LINK)
379  m = 1<<7; /* request extensibility link */
380 
381  port->ieee1284.phase = IEEE1284_PH_NEGOTIATION;
382 
383  /* Start off with nStrobe and nAutoFd high, and nSelectIn low */
384  parport_frob_control (port,
389  udelay(1);
390 
391  /* Event 0: Set data */
392  parport_data_forward (port);
393  parport_write_data (port, m);
394  udelay (400); /* Shouldn't need to wait this long. */
395 
396  /* Event 1: Set nSelectIn high, nAutoFd low */
397  parport_frob_control (port,
401 
402  /* Event 2: PError, Select, nFault go high, nAck goes low */
403  if (parport_wait_peripheral (port,
411  /* Timeout */
412  parport_frob_control (port,
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; /* Not IEEE1284 compliant */
421  }
422 
423  /* Event 3: Set nStrobe low */
424  parport_frob_control (port,
427 
428  /* Event 4: Set nStrobe and nAutoFd high */
429  udelay (5);
430  parport_frob_control (port,
433  0);
434 
435  /* Event 6: nAck goes high */
436  if (parport_wait_peripheral (port,
439  /* This shouldn't really happen with a compliant device. */
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 
448 
449  /* xflag should be high for all modes other than nibble (0). */
450  if (mode && !xflag) {
451  /* Mode not supported. */
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  /* More to do if we've requested extensibility link. */
459  if (mode & IEEE1284_EXT_LINK) {
460  m = mode & 0x7f;
461  udelay (1);
462  parport_write_data (port, m);
463  udelay (1);
464 
465  /* Event 51: Set nStrobe low */
466  parport_frob_control (port,
469 
470  /* Event 52: nAck goes low */
472  /* This peripheral is _very_ slow. */
474  "%s: Event 52 didn't happen\n",
475  port->name);
476  parport_ieee1284_terminate (port);
477  return 1;
478  }
479 
480  /* Event 53: Set nStrobe high */
481  parport_frob_control (port,
483  0);
484 
485  /* Event 55: nAck goes high */
486  if (parport_wait_peripheral (port,
489  /* This shouldn't really happen with a compliant
490  * device. */
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  /* Event 54: Peripheral sets XFlag to reflect support */
501 
502  /* xflag should be high. */
503  if (!xflag) {
504  /* Extended mode not supported. */
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  /* Any further setup is left to the caller. */
512  }
513 
514  /* Mode is supported */
515  DPRINTK (KERN_DEBUG "%s: In mode 0x%02x\n", port->name, mode);
516  port->ieee1284.mode = mode;
517 
518  /* But ECP is special */
519  if (!(mode & IEEE1284_EXT_LINK) && (m & IEEE1284_MODE_ECP)) {
520  port->ieee1284.phase = IEEE1284_PH_ECP_SETUP;
521 
522  /* Event 30: Set nAutoFd low */
523  parport_frob_control (port,
526 
527  /* Event 31: PError goes high. */
528  r = parport_wait_peripheral (port,
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) {
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 /* IEEE1284 support */
551 }
552 
553 /* Acknowledge that the peripheral has data available.
554  * Events 18-20, in order to get from Reverse Idle phase
555  * to Host Busy Data Available.
556  * This will most likely be called from an interrupt.
557  * Returns zero if data was available.
558  */
559 #ifdef CONFIG_PARPORT_1284
560 static int parport_ieee1284_ack_data_avail (struct parport *port)
561 {
563  /* Event 18 didn't happen. */
564  return -1;
565 
566  /* Event 20: nAutoFd goes high. */
567  port->ops->frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
568  port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
569  return 0;
570 }
571 #endif /* IEEE1284 support */
572 
573 /* Handle an interrupt. */
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  /* An interrupt in this phase means that data
582  * is now available. */
583  DPRINTK (KERN_DEBUG "%s: Data available\n", port->name);
584  parport_ieee1284_ack_data_avail (port);
585  }
586 #endif /* IEEE1284 support */
587 }
588 
607 ssize_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  /* Ignore the device-ID-request bit and the address bit. */
618  mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
619 
620  /* Use the mode we're in. */
621  switch (mode) {
623  case IEEE1284_MODE_BYTE:
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;
640  DPRINTK (KERN_DEBUG "%s: Using software-emulated EPP mode\n",
641  port->name);
642  if (addr) {
644  } else {
646  }
647  break;
648  case IEEE1284_MODE_ECP:
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 
659  DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
660  port->name);
661  /* The caller has specified that it must be emulated,
662  * even if we have ECP hardware! */
663  if (addr) {
665  } else {
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 /* IEEE1284 support */
680 }
681 
700 ssize_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  /* Ignore the device-ID-request bit and the address bit. */
711  mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
712 
713  /* Use the mode we're in. */
714  switch (mode) {
716  /* if we can tri-state use BYTE mode instead of NIBBLE mode,
717  * if that fails, revert to NIBBLE mode -- ought to store somewhere
718  * the device's ability to do BYTE mode reverse transfers, so we don't
719  * end up needlessly calling negotiate(BYTE) repeately.. (fb)
720  */
721  if ((port->physport->modes & PARPORT_MODE_TRISTATE) &&
723  /* got into BYTE mode OK */
724  DPRINTK (KERN_DEBUG "%s: Using byte mode\n", port->name);
725  fn = port->ops->byte_read_data;
726  break;
727  }
729  return -EIO;
730  }
731  /* fall through to 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;
751  DPRINTK (KERN_DEBUG "%s: Using software-emulated EPP mode\n",
752  port->name);
753  if (addr) {
755  } else {
757  }
758  break;
759  case IEEE1284_MODE_ECP:
761  DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
762  fn = port->ops->ecp_read_data;
763  break;
764 
766  DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
767  port->name);
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 /* IEEE1284 support */
779 }
780 
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 /* Exported symbols for modules. */
810