1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 /***************************************************************************
4  *   copyright            : (C) 2004 by Frank Mori Hess
5  ***************************************************************************/
6 
7 #ifndef _NI_USB_GPIB_H
8 #define _NI_USB_GPIB_H
9 
10 #include <linux/mutex.h>
11 #include <linux/semaphore.h>
12 #include <linux/usb.h>
13 #include <linux/timer.h>
14 #include "gpibP.h"
15 
16 enum {
17 	USB_VENDOR_ID_NI = 0x3923
18 };
19 
20 enum {
21 	USB_DEVICE_ID_NI_USB_B = 0x702a,
22 	USB_DEVICE_ID_NI_USB_B_PREINIT = 0x702b,	// device id before firmware is loaded
23 	USB_DEVICE_ID_NI_USB_HS = 0x709b,
24 	USB_DEVICE_ID_NI_USB_HS_PLUS = 0x7618,
25 	USB_DEVICE_ID_KUSB_488A = 0x725c,
26 	USB_DEVICE_ID_MC_USB_488 = 0x725d
27 };
28 
29 enum ni_usb_device {
30 	NIUSB_SUBDEV_TNT4882 = 1,
31 	NIUSB_SUBDEV_UNKNOWN2 = 2,
32 	NIUSB_SUBDEV_UNKNOWN3 = 3,
33 };
34 
35 enum endpoint_addresses {
36 	NIUSB_B_BULK_OUT_ENDPOINT = 0x2,
37 	NIUSB_B_BULK_IN_ENDPOINT = 0x2,
38 	NIUSB_B_BULK_IN_ALT_ENDPOINT = 0x6,
39 	NIUSB_B_INTERRUPT_IN_ENDPOINT = 0x4,
40 };
41 
42 enum hs_enpoint_addresses {
43 	NIUSB_HS_BULK_OUT_ENDPOINT = 0x2,
44 	NIUSB_HS_BULK_OUT_ALT_ENDPOINT = 0x6,
45 	NIUSB_HS_BULK_IN_ENDPOINT = 0x4,
46 	NIUSB_HS_BULK_IN_ALT_ENDPOINT = 0x8,
47 	NIUSB_HS_INTERRUPT_IN_ENDPOINT = 0x1,
48 };
49 
50 enum hs_plus_endpoint_addresses {
51 	NIUSB_HS_PLUS_BULK_OUT_ENDPOINT = 0x1,
52 	NIUSB_HS_PLUS_BULK_OUT_ALT_ENDPOINT = 0x4,
53 	NIUSB_HS_PLUS_BULK_IN_ENDPOINT = 0x2,
54 	NIUSB_HS_PLUS_BULK_IN_ALT_ENDPOINT = 0x5,
55 	NIUSB_HS_PLUS_INTERRUPT_IN_ENDPOINT = 0x3,
56 };
57 
58 struct ni_usb_urb_ctx {
59 	struct completion complete;
60 	unsigned timed_out : 1;
61 };
62 
63 // struct which defines private_data for ni_usb devices
64 struct ni_usb_priv {
65 	struct usb_interface *bus_interface;
66 	int bulk_out_endpoint;
67 	int bulk_in_endpoint;
68 	int interrupt_in_endpoint;
69 	u8 eos_char;
70 	unsigned short eos_mode;
71 	unsigned int monitored_ibsta_bits;
72 	struct urb *bulk_urb;
73 	struct urb *interrupt_urb;
74 	u8 interrupt_buffer[0x11];
75 	struct mutex addressed_transfer_lock; // protect transfer lock
76 	struct mutex bulk_transfer_lock;  // protect bulk message sends
77 	struct mutex control_transfer_lock; // protect control messages
78 	struct mutex interrupt_transfer_lock; //  protect interrupt messages
79 	struct timer_list bulk_timer;
80 	struct ni_usb_urb_ctx context;
81 	int product_id;
82 	unsigned short ren_state;
83 };
84 
85 struct ni_usb_status_block {
86 	short id;
87 	unsigned short ibsta;
88 	short error_code;
89 	unsigned short count;
90 };
91 
92 struct ni_usb_register {
93 	enum ni_usb_device device;
94 	short address;
95 	unsigned short value;
96 };
97 
98 enum ni_usb_bulk_ids {
99 	NIUSB_IBCAC_ID = 0x1,
100 	NIUSB_UNKNOWN3_ID = 0x3, // device level function id?
101 	NIUSB_TERM_ID = 0x4,
102 	NIUSB_IBGTS_ID = 0x6,
103 	NIUSB_IBRPP_ID = 0x7,
104 	NIUSB_REG_READ_ID = 0x8,
105 	NIUSB_REG_WRITE_ID = 0x9,
106 	NIUSB_IBSIC_ID = 0xf,
107 	NIUSB_REGISTER_READ_DATA_START_ID = 0x34,
108 	NIUSB_REGISTER_READ_DATA_END_ID = 0x35,
109 	NIUSB_IBRD_DATA_ID = 0x36,
110 	NIUSB_IBRD_EXTENDED_DATA_ID = 0x37,
111 	NIUSB_IBRD_STATUS_ID = 0x38
112 };
113 
114 enum ni_usb_error_codes {
115 	NIUSB_NO_ERROR = 0,
116 	/* NIUSB_ABORTED_ERROR occurs when I/O is interrupted early by
117 	 *	doing a NI_USB_STOP_REQUEST on the control endpoint.
118 	 */
119 	NIUSB_ABORTED_ERROR = 1,
120 	// NIUSB_READ_ATN_ERROR occurs when you do a board read while
121 	// ATN is set
122 	NIUSB_ATN_STATE_ERROR = 2,
123 	// NIUSB_ADDRESSING_ERROR occurs when you do a board
124 	// read/write as CIC but are not in LACS/TACS
125 	NIUSB_ADDRESSING_ERROR = 3,
126 	/* NIUSB_EOSMODE_ERROR occurs on reads if any eos mode or char
127 	 * bits are set when REOS is not set.
128 	 * Have also seen error 4 if you try to send more than 16
129 	 * command bytes at once on a usb-b.
130 	 */
131 	NIUSB_EOSMODE_ERROR = 4,
132 	// NIUSB_NO_BUS_ERROR occurs when you try to write a command
133 	// byte but there are no devices connected to the gpib bus
134 	NIUSB_NO_BUS_ERROR = 5,
135 	// NIUSB_NO_LISTENER_ERROR occurs when you do a board write as
136 	// CIC with no listener
137 	NIUSB_NO_LISTENER_ERROR = 8,
138 	// get NIUSB_TIMEOUT_ERROR on board read/write timeout
139 	NIUSB_TIMEOUT_ERROR = 10,
140 };
141 
142 enum ni_usb_control_requests {
143 	NI_USB_STOP_REQUEST = 0x20,
144 	NI_USB_WAIT_REQUEST = 0x21,
145 	NI_USB_POLL_READY_REQUEST = 0x40,
146 	NI_USB_SERIAL_NUMBER_REQUEST = 0x41,
147 	NI_USB_HS_PLUS_0x48_REQUEST = 0x48,
148 	NI_USB_HS_PLUS_LED_REQUEST = 0x4b,
149 	NI_USB_HS_PLUS_0xf8_REQUEST = 0xf8
150 };
151 
152 static const unsigned int ni_usb_ibsta_monitor_mask =
153 	SRQI | LOK | REM | CIC | ATN | TACS | LACS | DTAS | DCAS;
154 
nec7210_to_tnt4882_offset(int offset)155 static inline int nec7210_to_tnt4882_offset(int offset)
156 {
157 	return 2 * offset;
158 };
159 
ni_usb_bulk_termination(u8 * buffer)160 static inline int ni_usb_bulk_termination(u8 *buffer)
161 {
162 	int i = 0;
163 
164 	buffer[i++] = NIUSB_TERM_ID;
165 	buffer[i++] = 0x0;
166 	buffer[i++] = 0x0;
167 	buffer[i++] = 0x0;
168 	return i;
169 }
170 
171 enum ni_usb_unknown3_register {
172 	SERIAL_NUMBER_4_REG = 0x8,
173 	SERIAL_NUMBER_3_REG = 0x9,
174 	SERIAL_NUMBER_2_REG = 0xa,
175 	SERIAL_NUMBER_1_REG = 0xb,
176 };
177 
ni_usb_bulk_register_write_header(u8 * buffer,int num_writes)178 static inline int ni_usb_bulk_register_write_header(u8 *buffer, int num_writes)
179 {
180 	int i = 0;
181 
182 	buffer[i++] = NIUSB_REG_WRITE_ID;
183 	buffer[i++] = num_writes;
184 	buffer[i++] = 0x0;
185 	return i;
186 }
187 
ni_usb_bulk_register_write(u8 * buffer,struct ni_usb_register reg)188 static inline int ni_usb_bulk_register_write(u8 *buffer, struct ni_usb_register reg)
189 {
190 	int i = 0;
191 
192 	buffer[i++] = reg.device;
193 	buffer[i++] = reg.address;
194 	buffer[i++] = reg.value;
195 	return i;
196 }
197 
ni_usb_bulk_register_read_header(u8 * buffer,int num_reads)198 static inline int ni_usb_bulk_register_read_header(u8 *buffer, int num_reads)
199 {
200 	int i = 0;
201 
202 	buffer[i++] = NIUSB_REG_READ_ID;
203 	buffer[i++] = num_reads;
204 	return i;
205 }
206 
ni_usb_bulk_register_read(u8 * buffer,int device,int address)207 static inline int ni_usb_bulk_register_read(u8 *buffer, int device, int address)
208 {
209 	int i = 0;
210 
211 	buffer[i++] = device;
212 	buffer[i++] = address;
213 	return i;
214 }
215 
216 #endif	// _NI_USB_GPIB_H
217