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 /*
117 * NIUSB_ABORTED_ERROR occurs when I/O is interrupted early by
118 * doing a NI_USB_STOP_REQUEST on the control endpoint.
119 */
120 NIUSB_ABORTED_ERROR = 1,
121 /*
122 * NIUSB_READ_ATN_ERROR occurs when you do a board read while
123 * ATN is set
124 */
125 NIUSB_ATN_STATE_ERROR = 2,
126 /*
127 * NIUSB_ADDRESSING_ERROR occurs when you do a board
128 * read/write as CIC but are not in LACS/TACS
129 */
130 NIUSB_ADDRESSING_ERROR = 3,
131 /*
132 * NIUSB_EOSMODE_ERROR occurs on reads if any eos mode or char
133 * bits are set when REOS is not set.
134 * Have also seen error 4 if you try to send more than 16
135 * command bytes at once on a usb-b.
136 */
137 NIUSB_EOSMODE_ERROR = 4,
138 /*
139 * NIUSB_NO_BUS_ERROR occurs when you try to write a command
140 * byte but there are no devices connected to the gpib bus
141 */
142 NIUSB_NO_BUS_ERROR = 5,
143 /*
144 * NIUSB_NO_LISTENER_ERROR occurs when you do a board write as
145 * CIC with no listener
146 */
147 NIUSB_NO_LISTENER_ERROR = 8,
148 // get NIUSB_TIMEOUT_ERROR on board read/write timeout
149 NIUSB_TIMEOUT_ERROR = 10,
150 };
151
152 enum ni_usb_control_requests {
153 NI_USB_STOP_REQUEST = 0x20,
154 NI_USB_WAIT_REQUEST = 0x21,
155 NI_USB_POLL_READY_REQUEST = 0x40,
156 NI_USB_SERIAL_NUMBER_REQUEST = 0x41,
157 NI_USB_HS_PLUS_0x48_REQUEST = 0x48,
158 NI_USB_HS_PLUS_LED_REQUEST = 0x4b,
159 NI_USB_HS_PLUS_0xf8_REQUEST = 0xf8
160 };
161
162 static const unsigned int ni_usb_ibsta_monitor_mask =
163 SRQI | LOK | REM | CIC | ATN | TACS | LACS | DTAS | DCAS;
164
nec7210_to_tnt4882_offset(int offset)165 static inline int nec7210_to_tnt4882_offset(int offset)
166 {
167 return 2 * offset;
168 };
169
ni_usb_bulk_termination(u8 * buffer)170 static inline int ni_usb_bulk_termination(u8 *buffer)
171 {
172 int i = 0;
173
174 buffer[i++] = NIUSB_TERM_ID;
175 buffer[i++] = 0x0;
176 buffer[i++] = 0x0;
177 buffer[i++] = 0x0;
178 return i;
179 }
180
181 enum ni_usb_unknown3_register {
182 SERIAL_NUMBER_4_REG = 0x8,
183 SERIAL_NUMBER_3_REG = 0x9,
184 SERIAL_NUMBER_2_REG = 0xa,
185 SERIAL_NUMBER_1_REG = 0xb,
186 };
187
ni_usb_bulk_register_write_header(u8 * buffer,int num_writes)188 static inline int ni_usb_bulk_register_write_header(u8 *buffer, int num_writes)
189 {
190 int i = 0;
191
192 buffer[i++] = NIUSB_REG_WRITE_ID;
193 buffer[i++] = num_writes;
194 buffer[i++] = 0x0;
195 return i;
196 }
197
ni_usb_bulk_register_write(u8 * buffer,struct ni_usb_register reg)198 static inline int ni_usb_bulk_register_write(u8 *buffer, struct ni_usb_register reg)
199 {
200 int i = 0;
201
202 buffer[i++] = reg.device;
203 buffer[i++] = reg.address;
204 buffer[i++] = reg.value;
205 return i;
206 }
207
ni_usb_bulk_register_read_header(u8 * buffer,int num_reads)208 static inline int ni_usb_bulk_register_read_header(u8 *buffer, int num_reads)
209 {
210 int i = 0;
211
212 buffer[i++] = NIUSB_REG_READ_ID;
213 buffer[i++] = num_reads;
214 return i;
215 }
216
ni_usb_bulk_register_read(u8 * buffer,int device,int address)217 static inline int ni_usb_bulk_register_read(u8 *buffer, int device, int address)
218 {
219 int i = 0;
220
221 buffer[i++] = device;
222 buffer[i++] = address;
223 return i;
224 }
225
226 #endif // _NI_USB_GPIB_H
227