comm.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* 
  2.         comm.c    (c) 1997-8  Grant R. Guenther <grant@torque.net>
  3.                               Under the terms of the GNU General Public License.
  4. comm.c is a low-level protocol driver for some older models
  5. of the DataStor "Commuter" parallel to IDE adapter.  Some of
  6. the parallel port devices marketed by Arista currently
  7. use this adapter.
  8. */
  9. /* Changes:
  10. 1.01 GRG 1998.05.05  init_proto, release_proto
  11. */
  12. #define COMM_VERSION      "1.01"
  13. #include <linux/module.h>
  14. #include <linux/delay.h>
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/wait.h>
  18. #include <asm/io.h>
  19. #include "paride.h"
  20. /* mode codes:  0  nybble reads, 8-bit writes
  21.                 1  8-bit reads and writes
  22.                 2  8-bit EPP mode
  23. */
  24. #define j44(a,b) (((a>>3)&0x0f)|((b<<1)&0xf0))
  25. #define P1 w2(5);w2(0xd);w2(0xd);w2(5);w2(4);
  26. #define P2 w2(5);w2(7);w2(7);w2(5);w2(4);
  27. /* cont = 0 - access the IDE register file 
  28.    cont = 1 - access the IDE command set 
  29. */
  30. static int  cont_map[2] = { 0x08, 0x10 };
  31. static int comm_read_regr( PIA *pi, int cont, int regr )
  32. {       int     l, h, r;
  33.         r = regr + cont_map[cont];
  34.         switch (pi->mode)  {
  35.         case 0: w0(r); P1; w0(0);
  36.          w2(6); l = r1(); w0(0x80); h = r1(); w2(4);
  37.                 return j44(l,h);
  38.         case 1: w0(r+0x20); P1; 
  39.          w0(0); w2(0x26); h = r0(); w2(4);
  40.                 return h;
  41. case 2:
  42. case 3:
  43.         case 4: w3(r+0x20); r1(); 
  44.          w2(0x24); h = r4(); w2(4);
  45.                 return h;
  46.         }
  47.         return -1;
  48. }       
  49. static void comm_write_regr( PIA *pi, int cont, int regr, int val )
  50. {       int  r;
  51.         r = regr + cont_map[cont];
  52.         switch (pi->mode)  {
  53.         case 0:
  54.         case 1: w0(r); P1; w0(val); P2;
  55. break;
  56. case 2:
  57. case 3:
  58.         case 4: w3(r); r1(); w4(val); 
  59.                 break;
  60.         }
  61. }
  62. static void comm_connect ( PIA *pi  )
  63. {       pi->saved_r0 = r0();
  64.         pi->saved_r2 = r2();
  65.         w2(4); w0(0xff); w2(6);
  66.         w2(4); w0(0xaa); w2(6);
  67.         w2(4); w0(0x00); w2(6);
  68.         w2(4); w0(0x87); w2(6);
  69.         w2(4); w0(0xe0); w2(0xc); w2(0xc); w2(4);
  70. }
  71. static void comm_disconnect ( PIA *pi )
  72. {       w2(0); w2(0); w2(0); w2(4); 
  73. w0(pi->saved_r0);
  74.         w2(pi->saved_r2);
  75. static void comm_read_block( PIA *pi, char * buf, int count )
  76. {       int     i, l, h;
  77.         switch (pi->mode) {
  78.         
  79.         case 0: w0(0x48); P1;
  80.                 for(i=0;i<count;i++) {
  81.                         w0(0); w2(6); l = r1();
  82.                         w0(0x80); h = r1(); w2(4);
  83.                         buf[i] = j44(l,h);
  84.                 }
  85.                 break;
  86.         case 1: w0(0x68); P1; w0(0);
  87.                 for(i=0;i<count;i++) {
  88.                         w2(0x26); buf[i] = r0(); w2(0x24);
  89.                 }
  90. w2(4);
  91. break;
  92. case 2: w3(0x68); r1(); w2(0x24);
  93. for (i=0;i<count;i++) buf[i] = r4();
  94. w2(4);
  95. break;
  96.         case 3: w3(0x68); r1(); w2(0x24);
  97.                 for (i=0;i<count/2;i++) ((u16 *)buf)[i] = r4w();
  98.                 w2(4);
  99.                 break;
  100.         case 4: w3(0x68); r1(); w2(0x24);
  101.                 for (i=0;i<count/4;i++) ((u32 *)buf)[i] = r4l();
  102.                 w2(4);
  103.                 break;
  104. }
  105. }
  106. /* NB: Watch out for the byte swapped writes ! */
  107. static void comm_write_block( PIA *pi, char * buf, int count )
  108. {       int k;
  109.         switch (pi->mode) {
  110.         case 0:
  111.         case 1: w0(0x68); P1;
  112.          for (k=0;k<count;k++) {
  113.                         w2(5); w0(buf[k^1]); w2(7);
  114.                 }
  115.                 w2(5); w2(4);
  116.                 break;
  117.         case 2: w3(0x48); r1();
  118.                 for (k=0;k<count;k++) w4(buf[k^1]);
  119.                 break;
  120.         case 3: w3(0x48); r1();
  121.                 for (k=0;k<count/2;k++) w4w(pi_swab16(buf,k));
  122.                 break;
  123.         case 4: w3(0x48); r1();
  124.                 for (k=0;k<count/4;k++) w4l(pi_swab32(buf,k));
  125.                 break;
  126.         }
  127. }
  128. static void comm_log_adapter( PIA *pi, char * scratch, int verbose )
  129. {       char    *mode_string[5] = {"4-bit","8-bit","EPP-8","EPP-16","EPP-32"};
  130.         printk("%s: comm %s, DataStor Commuter at 0x%x, ",
  131.                 pi->device,COMM_VERSION,pi->port);
  132.         printk("mode %d (%s), delay %dn",pi->mode,
  133. mode_string[pi->mode],pi->delay);
  134. }
  135. static void comm_init_proto(PIA *pi)
  136. {       MOD_INC_USE_COUNT;
  137. }
  138. static void comm_release_proto(PIA *pi)
  139. {       MOD_DEC_USE_COUNT;
  140. }
  141. struct pi_protocol comm = {"comm",0,5,2,1,1,
  142.                            comm_write_regr,
  143.                            comm_read_regr,
  144.                            comm_write_block,
  145.                            comm_read_block,
  146.                            comm_connect,
  147.                            comm_disconnect,
  148.                            0,
  149.                            0,
  150.                            0,
  151.                            comm_log_adapter,
  152.                            comm_init_proto,
  153.                            comm_release_proto
  154.                           };
  155. #ifdef MODULE
  156. int     init_module(void)
  157. {       return pi_register( &comm ) - 1;
  158. }
  159. void    cleanup_module(void)
  160. {       pi_unregister( &comm );
  161. }
  162. #endif
  163. /* end of comm.c */
  164. MODULE_LICENSE("GPL");