ttyPos.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef _TTY_POS_H_
  2. #define _TTY_POS_H_
  3. #include <linux/module.h>
  4. #include <linux/version.h>
  5. #include <linux/kernel.h>
  6. #include <linux/errno.h>
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/tty.h>
  10. #include <linux/tty_driver.h>
  11. #include <linux/tty_flip.h>
  12. #include <linux/kref.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/usb.h>
  15. #include <linux/mutex.h>
  16. #include <linux/time.h>
  17. #include <linux/sched.h>
  18. #ifdef LINUX_VERSION_CODE
  19. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0))
  20. #include <linux/err.h>
  21. #include <linux/kthread.h>
  22. #endif
  23. #endif
  24. #define USB_VENDOR_ID 0x1234
  25. #define USB_PRODUCT_ID 0x0101
  26. static struct usb_device_id pos_usb_table[] = {
  27. { USB_DEVICE(USB_VENDOR_ID, USB_PRODUCT_ID) },
  28. {},
  29. };
  30. MODULE_DEVICE_TABLE(usb,pos_usb_table);
  31. //#define POS_TTY_MAJOR 192 /* experimental range */
  32. #define POS_TTY_MINORS 20 /* only have 20 devices */
  33. #define THREAD_INIT 0x00
  34. #define THREAD_STOPPED 0x01
  35. #define THREAD_CREATED 0x82
  36. #define THREAD_CLOSE 0x83
  37. #define THREAD_IS_RUNNING(ThreadState) (ThreadState & 0x80)
  38. #define WRITE_COMMAND 0 /* write to usb device command */
  39. #define READ_COMMAND 1 /* read from to usb device command */
  40. #define STATUS_COMMAND 2 /* get device buffer status command */
  41. #define POOL_SIZE 10241
  42. typedef struct _POOL {
  43. unsigned int ReadPos;
  44. unsigned int WritePos;
  45. unsigned char Buffer[POOL_SIZE];
  46. } __attribute__((packed)) POOL, *PPOOL;
  47. typedef struct {
  48. unsigned char SeqNo;
  49. unsigned char ReqType; /* 0:OUT, 1:IN, 2:STAT, 3:RESET */
  50. unsigned short Len;
  51. unsigned char Data[508];
  52. } __attribute__((packed)) ST_BULK_IO;
  53. typedef struct {
  54. unsigned int TxBufSize;
  55. unsigned int RxBufSize;
  56. unsigned int TxLeft;
  57. unsigned int RxLeft;
  58. } __attribute__((packed)) ST_BIO_STATE;
  59. struct tty_pos {
  60. struct tty_struct *tty;
  61. unsigned char devIndex;
  62. /* for tiocmget and tiocmset functions */
  63. int msr; /* MSR shadow */
  64. int mcr; /* MCR shadow */
  65. struct file *filp;
  66. struct usb_device *udev;
  67. struct usb_interface *interface;
  68. struct urb *urb;
  69. wait_queue_head_t urb_wait;
  70. atomic_t urb_done;
  71. int timeout_jiffies;
  72. atomic_t urb_busy;
  73. wait_queue_head_t write_wait;
  74. atomic_t write_flag;
  75. __u8 bulk_in_epAddr;
  76. __u8 bulk_out_epAddr;
  77. struct kref kref;
  78. struct mutex io_mutex; /* synchronize I/O with disconnect */
  79. volatile u8 ThreadState;
  80. struct completion ThreadExit_completion;
  81. unsigned char SeqCount;
  82. ST_BULK_IO *BioPack; /* for IO access */
  83. ST_BIO_STATE BioDevState;
  84. POOL TxPool;
  85. };
  86. #define to_pos_dev(d) container_of(d, struct tty_pos, kref)
  87. #define INIT_POOL_BUFFER(pool) pool.ReadPos = pool.WritePos = 0
  88. #define IS_POOL_EMPTY(pool) (pool.ReadPos == pool.WritePos)
  89. #define GET_USING_POOL(pool) \
  90. ((pool.WritePos + POOL_SIZE - pool.ReadPos) % POOL_SIZE)
  91. #define GET_SPACE_POOL(pool) (POOL_SIZE - 1 - GET_USING_POOL(pool))
  92. #if 0
  93. #define ERR(stuff...)
  94. #define INFO(stuff...)
  95. #else
  96. #define ERR(stuff...) printk(KERN_ERR "ttyPos: " stuff)
  97. #define INFO(stuff...) printk(KERN_INFO "ttyPos: " stuff)
  98. #endif
  99. //--error code define of API call
  100. #define USB_ERR_NOT_OPEN (-3403)//通道未打开
  101. #define USB_ERR_BUF (-3404)//发送缓冲区错误
  102. #define USB_ERR_NOT_FREE (-3405)//无可用的端口
  103. #define USB_ERR_NO_CONF (-3411)//设备未完成枚举和配置过程
  104. #define USB_ERR_DISCONN (-3412)//设备已经与主机断开
  105. #define USB_ERR_MEM_SYSTEM (-3413)//系统内存出现异常
  106. #define USB_ERR_BUSY (-3414)//USB系统忙碌
  107. #define USB_ERR_RC_SYSTEM (-3415)//系统资源申请失败
  108. #define USB_ERR_DEV_ABSENT (-3416)//USB主机上设备不在位
  109. #define USB_ERR_INVALID (-3417)//USB通讯状态无效
  110. #endif