1/*
2 * U2EC usbsock header
3 *
4 * Copyright (C) 2008 ASUSTek Corporation
5 *
6 * $Id: usbsock.h,v 1.2 2008/12/18 10:00:11 jiahao Exp $
7 */
8
9#ifndef   __USBSOCK_H__
10#define   __USBSOCK_H__
11
12#include <unistd.h>
13#include <stdio.h>
14#include <netinet/in.h>
15#include <u2ec_list.h>
16
17#define SWAP16(x) \
18({ \
19        __u16 __x = (x); \
20        ((__u16)( \
21                (((__u16)(__x) & (__u16)0x00ffU) << 8) | \
22                (((__u16)(__x) & (__u16)0xff00U) >> 8) )); \
23})
24
25#define SWAP32(x) \
26        ((__u32)( \
27                (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
28                (((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
29                (((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
30                (((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
31
32#define SWAP64(x) \
33({ \
34        __u64 __x = (x); \
35        ((__u64)( \
36                (__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \
37                (__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \
38                (__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) | \
39                (__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) <<  8) | \
40                (__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >>  8) | \
41                (__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
42                (__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) | \
43                (__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) )); \
44})
45
46#define SWAP_BACK16(x) \
47        ((__u16)( \
48                (((__u16)(x) & (__u16)0x00ffU) << 8) | \
49                (((__u16)(x) & (__u16)0xff00U) >> 8) ))
50#define SWAP_BACK32(x) \
51        ((__u32)( \
52                (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
53                (((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
54                (((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
55                (((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
56#define SWAP_BACK64(x) \
57        ((__u64)( \
58                (__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
59                (__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
60                (__u64)(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
61                (__u64)(((__u64)(x) & (__u64)0x00000000ff000000ULL) <<  8) | \
62                (__u64)(((__u64)(x) & (__u64)0x000000ff00000000ULL) >>  8) | \
63                (__u64)(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
64                (__u64)(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
65                (__u64)(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56) ))
66
67#define U2EC_FIFO	"/tmp/u2ec_fifo"	// used by handle_fifo
68
69#define MFP_IS_IDLE	0			// used by MFP_state
70#define MFP_IN_LPRNG	1
71#define MFP_IN_U2EC	2
72#define MFP_GET_STATE	7
73
74#define U2EC_TIMEOUT	6
75#define U2EC_TIMEOUT_MONO	300
76#define MAX_BUFFER_SIZE	65536
77#define MAX_BUF_LEN	MAX_BUFFER_SIZE	+104+72
78#define BACKLOG		255			// how many pending connections queue will hold
79
80#define uTcpPortConfig	5473
81#define uTcpUdp		5474
82#define uTcpControl	5475
83#define uTCPUSB		3394			// server shared TCP port, set when server share device
84
85#define USB_DIR_OUT	USB_ENDPOINT_OUT	// to device
86#define USB_DIR_IN	USB_ENDPOINT_IN		// to host
87
88#define USBLP_REQ_GET_ID			0x00
89#define USBLP_REQ_GET_STATUS			0x01
90#define USBLP_REQ_RESET				0x02
91#define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST	0x00	// HP Vendor-specific
92
93#define USBLP_DEVICE_ID_SIZE	1024
94
95#define FILE_DEVICE_UNKNOWN	0x00000022
96#define METHOD_NEITHER		3
97#define FILE_ANY_ACCESS		0
98
99#define CTL_CODE( DeviceType, Function, Method, Access ) (                 \
100    ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \
101)
102
103#define IOCTL_INTERNAL_USB_SUBMIT_URB  CTL_CODE(FILE_DEVICE_USB,  \
104                                                USB_SUBMIT_URB,  \
105                                                METHOD_NEITHER,  \
106                                                FILE_ANY_ACCESS)
107
108enum TypeTcpPackage {
109	IrpToTcp,
110	IrpAnswerTcp,
111	ControlPing
112};
113
114typedef struct _CONNECTION_INFO {
115	struct u2ec_list_head	list;		// descriptor list head
116	int			sockfd;		// connection socket fd
117	time_t			time;		// receive last packet from the socket
118	struct in_addr		ip;		// client ip address
119	int			busy;		// connection state defined as below
120	int			count_class_int_in;	// toolbox threshold for HP DeskJet series
121	LONG			irp;		// irp number of last packet
122#define	CONN_IS_IDLE		0
123#define	CONN_IS_BUSY		1
124#define	CONN_IS_WAITING		2
125#define	CONN_IS_RETRY		3
126}CONNECTION_INFO, *PCONNECTION_INFO;
127
128typedef struct _TCP_PACKAGE {
129	LONG	Type;
130	LONG	SizeBuffer;
131}TCP_PACKAGE, *PTCP_PACKAGE;
132
133typedef struct _IO_STACK_LOCATION_SAVE {
134	ULONG64	empty;
135	UCHAR	MajorFunction;
136	UCHAR	MinorFunction;
137
138	union {
139		struct {
140			ULONG64 Argument1;
141			ULONG64 Argument2;
142			ULONG64 Argument3;
143			ULONG64 Argument4;
144		} Others;
145/*
146        	struct {
147            		ULONG64 OutputBufferLength;
148            		ULONG64 InputBufferLength;
149            		ULONG64 IoControlCode;
150            		ULONG64 Type3InputBuffer;
151        	} DeviceIoControl;
152*/
153	} Parameters;
154
155}__attribute((aligned (8))) IO_STACK_LOCATION_SAVE, *PIO_STACK_LOCATION_SAVE;
156
157typedef struct _IRP_SAVE
158{
159	LONG			Size;		// size current buffer
160	LONG			NeedSize;	// need size buffer
161	LONG64			Device;		// Indefication of device
162	BYTE			Is64:1;		// Detect 64
163	BYTE			IsIsoch:1;      // isoch data
164	BYTE			Res1:6;         // Reserv
165	BYTE			empty[3];
166	LONG			Irp;		// Number Iro
167	NTSTATUS		Status;		// current status Irp
168	ULONG64   		Information;
169	BOOL			Cancel;		// Cancel irp flag
170	IO_STACK_LOCATION_SAVE	StackLocation;	// Stack location info
171	LONG			BufferSize;
172	LONG			Reserv;
173	BYTE			Buffer [8];	// Data
174}__attribute((packed)) IRP_SAVE, *PIRP_SAVE;
175
176#ifdef	SUPPORT_LPRng
177#	define	semaphore_create()	semaphore_create()
178#	define	semaphore_wait()	semaphore_wait()
179#	define	semaphore_post()	semaphore_post()
180#	define	semaphore_close()	semaphore_close()
181#else
182#	define	semaphore_create()
183#	define	semaphore_wait()
184#	define	semaphore_post()
185#	define	semaphore_close()
186#endif
187
188// Print log on console or file or nothing.
189#ifdef	PDEBUG_SENDSECV
190#	define	PSNDRECV(fmt, args...) PDEBUG(fmt, ## args)
191#else
192#	define	PSNDRECV(fmt, args...)
193#endif
194
195#ifdef	PDEBUG_DECODE
196#	define	PDECODE_IRP(arg1, arg2) print_irp(arg1, arg2)
197#else
198#	define	PDECODE_IRP(arg1, arg2)
199#endif
200
201#undef PDEBUG
202#ifdef U2EC_DEBUG
203#	define hotplug_debug(arg) hotplug_print(arg)
204#	ifdef U2EC_ONPC
205		extern FILE *fp;
206//#		define PDEBUG(fmt, args...) fprintf(fp, fmt, ## args)
207#		define PDEBUG(fmt, args...) printf(fmt, ## args)
208#	else
209//#		define PDEBUG(fmt, args...) printf(fmt, ## args)
210#		define PDEBUG(fmt, args...) do { \
211			FILE *fp = fopen("/dev/console", "w"); \
212			if (fp) { \
213				fprintf(fp, fmt, ## args); \
214				fclose(fp); \
215			} \
216		} while (0)
217#	endif
218#	define PERROR(message) PDEBUG("sock error: %s.\n", message)
219#else
220#	define hotplug_debug(arg)
221#	define PDEBUG(fmt, args...)
222#	define PERROR perror
223#endif	// U2EC_DEBUG
224
225#endif /*  __USBSOCK_H__ */
226