| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* driver for National Instruments usb to gpib adapters |
| 5 |
* copyright : (C) 2004 by Frank Mori Hess |
| 6 |
***************************************************************************/ |
| 7 |
|
| 8 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 |
#define dev_fmt pr_fmt |
| 10 |
#define DRV_NAME KBUILD_MODNAME |
| 11 |
|
| 12 |
#include <linux/kernel.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/slab.h> |
| 15 |
#include "ni_usb_gpib.h" |
| 16 |
#include "gpibP.h" |
| 17 |
#include "nec7210.h" |
| 18 |
#include "tnt4882_registers.h" |
| 19 |
|
| 20 |
MODULE_LICENSE("GPL"); |
| 21 |
MODULE_DESCRIPTION("GPIB driver for National Instruments USB devices"); |
| 22 |
|
| 23 |
#define MAX_NUM_NI_USB_INTERFACES 128 |
| 24 |
static struct usb_interface *ni_usb_driver_interfaces[MAX_NUM_NI_USB_INTERFACES]; |
| 25 |
|
| 26 |
static int ni_usb_parse_status_block(const u8 *buffer, struct ni_usb_status_block *status); |
| 27 |
static int ni_usb_set_interrupt_monitor(struct gpib_board *board, unsigned int monitored_bits); |
| 28 |
static void ni_usb_stop(struct ni_usb_priv *ni_priv); |
| 29 |
|
| 30 |
static DEFINE_MUTEX(ni_usb_hotplug_lock); |
| 31 |
|
| 32 |
// calculates a reasonable timeout in that can be passed to usb functions |
| 33 |
static inline unsigned long ni_usb_timeout_msecs(unsigned int usec) |
| 34 |
{ |
| 35 |
if (usec == 0) |
| 36 |
return 0; |
| 37 |
return 2000 + usec / 500; |
| 38 |
}; |
| 39 |
|
| 40 |
// returns timeout code byte for use in ni-usb-b instructions |
| 41 |
static unsigned short ni_usb_timeout_code(unsigned int usec) |
| 42 |
{ |
| 43 |
if (usec == 0) |
| 44 |
return 0xf0; |
| 45 |
else if (usec <= 10) |
| 46 |
return 0xf1; |
| 47 |
else if (usec <= 30) |
| 48 |
return 0xf2; |
| 49 |
else if (usec <= 100) |
| 50 |
return 0xf3; |
| 51 |
else if (usec <= 300) |
| 52 |
return 0xf4; |
| 53 |
else if (usec <= 1000) |
| 54 |
return 0xf5; |
| 55 |
else if (usec <= 3000) |
| 56 |
return 0xf6; |
| 57 |
else if (usec <= 10000) |
| 58 |
return 0xf7; |
| 59 |
else if (usec <= 30000) |
| 60 |
return 0xf8; |
| 61 |
else if (usec <= 100000) |
| 62 |
return 0xf9; |
| 63 |
else if (usec <= 300000) |
| 64 |
return 0xfa; |
| 65 |
else if (usec <= 1000000) |
| 66 |
return 0xfb; |
| 67 |
else if (usec <= 3000000) |
| 68 |
return 0xfc; |
| 69 |
else if (usec <= 10000000) |
| 70 |
return 0xfd; |
| 71 |
else if (usec <= 30000000) |
| 72 |
return 0xfe; |
| 73 |
else if (usec <= 100000000) |
| 74 |
return 0xff; |
| 75 |
else if (usec <= 300000000) |
| 76 |
return 0x01; |
| 77 |
/* |
| 78 |
* NI driver actually uses 0xff for timeout T1000s, which is a bug in their code. |
| 79 |
* I've verified on a usb-b that a code of 0x2 is correct for a 1000 sec timeout |
| 80 |
*/ |
| 81 |
else if (usec <= 1000000000) |
| 82 |
return 0x02; |
| 83 |
pr_err("bug? usec is greater than 1e9\n"); |
| 84 |
return 0xf0; |
| 85 |
} |
| 86 |
|
| 87 |
static void ni_usb_bulk_complete(struct urb *urb) |
| 88 |
{ |
| 89 |
struct ni_usb_urb_ctx *context = urb->context; |
| 90 |
|
| 91 |
complete(&context->complete); |
| 92 |
} |
| 93 |
|
| 94 |
static void ni_usb_timeout_handler(struct timer_list *t) |
| 95 |
{ |
| 96 |
struct ni_usb_priv *ni_priv = timer_container_of(ni_priv, t, |
| 97 |
bulk_timer); |
| 98 |
struct ni_usb_urb_ctx *context = &ni_priv->context; |
| 99 |
|
| 100 |
context->timed_out = 1; |
| 101 |
complete(&context->complete); |
| 102 |
}; |
| 103 |
|
| 104 |
// I'm using nonblocking loosely here, it only means -EAGAIN can be returned in certain cases |
| 105 |
static int ni_usb_nonblocking_send_bulk_msg(struct ni_usb_priv *ni_priv, void *data, |
| 106 |
int data_length, int *actual_data_length, |
| 107 |
int timeout_msecs) |
| 108 |
{ |
| 109 |
struct usb_device *usb_dev; |
| 110 |
int retval; |
| 111 |
unsigned int out_pipe; |
| 112 |
struct ni_usb_urb_ctx *context = &ni_priv->context; |
| 113 |
|
| 114 |
*actual_data_length = 0; |
| 115 |
mutex_lock(&ni_priv->bulk_transfer_lock); |
| 116 |
if (!ni_priv->bus_interface) { |
| 117 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 118 |
return -ENODEV; |
| 119 |
} |
| 120 |
if (ni_priv->bulk_urb) { |
| 121 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 122 |
return -EAGAIN; |
| 123 |
} |
| 124 |
ni_priv->bulk_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 125 |
if (!ni_priv->bulk_urb) { |
| 126 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 127 |
return -ENOMEM; |
| 128 |
} |
| 129 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 130 |
out_pipe = usb_sndbulkpipe(usb_dev, ni_priv->bulk_out_endpoint); |
| 131 |
init_completion(&context->complete); |
| 132 |
context->timed_out = 0; |
| 133 |
usb_fill_bulk_urb(ni_priv->bulk_urb, usb_dev, out_pipe, data, data_length, |
| 134 |
&ni_usb_bulk_complete, context); |
| 135 |
|
| 136 |
if (timeout_msecs) |
| 137 |
mod_timer(&ni_priv->bulk_timer, jiffies + msecs_to_jiffies(timeout_msecs)); |
| 138 |
|
| 139 |
retval = usb_submit_urb(ni_priv->bulk_urb, GFP_KERNEL); |
| 140 |
if (retval) { |
| 141 |
timer_delete_sync(&ni_priv->bulk_timer); |
| 142 |
usb_free_urb(ni_priv->bulk_urb); |
| 143 |
ni_priv->bulk_urb = NULL; |
| 144 |
dev_err(&usb_dev->dev, "failed to submit bulk out urb, retval=%i\n", |
| 145 |
retval); |
| 146 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 147 |
return retval; |
| 148 |
} |
| 149 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 150 |
wait_for_completion(&context->complete); // wait for ni_usb_bulk_complete |
| 151 |
if (context->timed_out) { |
| 152 |
usb_kill_urb(ni_priv->bulk_urb); |
| 153 |
dev_err(&usb_dev->dev, "killed urb due to timeout\n"); |
| 154 |
retval = -ETIMEDOUT; |
| 155 |
} else { |
| 156 |
retval = ni_priv->bulk_urb->status; |
| 157 |
} |
| 158 |
|
| 159 |
timer_delete_sync(&ni_priv->bulk_timer); |
| 160 |
*actual_data_length = ni_priv->bulk_urb->actual_length; |
| 161 |
mutex_lock(&ni_priv->bulk_transfer_lock); |
| 162 |
usb_free_urb(ni_priv->bulk_urb); |
| 163 |
ni_priv->bulk_urb = NULL; |
| 164 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 165 |
return retval; |
| 166 |
} |
| 167 |
|
| 168 |
static int ni_usb_send_bulk_msg(struct ni_usb_priv *ni_priv, void *data, int data_length, |
| 169 |
int *actual_data_length, int timeout_msecs) |
| 170 |
{ |
| 171 |
int retval; |
| 172 |
int timeout_msecs_remaining = timeout_msecs; |
| 173 |
|
| 174 |
retval = ni_usb_nonblocking_send_bulk_msg(ni_priv, data, data_length, actual_data_length, |
| 175 |
timeout_msecs_remaining); |
| 176 |
while (retval == -EAGAIN && (timeout_msecs == 0 || timeout_msecs_remaining > 0)) { |
| 177 |
usleep_range(1000, 1500); |
| 178 |
retval = ni_usb_nonblocking_send_bulk_msg(ni_priv, data, data_length, |
| 179 |
actual_data_length, |
| 180 |
timeout_msecs_remaining); |
| 181 |
if (timeout_msecs != 0) |
| 182 |
--timeout_msecs_remaining; |
| 183 |
} |
| 184 |
if (timeout_msecs != 0 && timeout_msecs_remaining <= 0) |
| 185 |
return -ETIMEDOUT; |
| 186 |
return retval; |
| 187 |
} |
| 188 |
|
| 189 |
// I'm using nonblocking loosely here, it only means -EAGAIN can be returned in certain cases |
| 190 |
static int ni_usb_nonblocking_receive_bulk_msg(struct ni_usb_priv *ni_priv, |
| 191 |
void *data, int data_length, |
| 192 |
int *actual_data_length, int timeout_msecs, |
| 193 |
int interruptible) |
| 194 |
{ |
| 195 |
struct usb_device *usb_dev; |
| 196 |
int retval; |
| 197 |
unsigned int in_pipe; |
| 198 |
struct ni_usb_urb_ctx *context = &ni_priv->context; |
| 199 |
|
| 200 |
*actual_data_length = 0; |
| 201 |
mutex_lock(&ni_priv->bulk_transfer_lock); |
| 202 |
if (!ni_priv->bus_interface) { |
| 203 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 204 |
return -ENODEV; |
| 205 |
} |
| 206 |
if (ni_priv->bulk_urb) { |
| 207 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 208 |
return -EAGAIN; |
| 209 |
} |
| 210 |
ni_priv->bulk_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 211 |
if (!ni_priv->bulk_urb) { |
| 212 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 213 |
return -ENOMEM; |
| 214 |
} |
| 215 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 216 |
in_pipe = usb_rcvbulkpipe(usb_dev, ni_priv->bulk_in_endpoint); |
| 217 |
init_completion(&context->complete); |
| 218 |
context->timed_out = 0; |
| 219 |
usb_fill_bulk_urb(ni_priv->bulk_urb, usb_dev, in_pipe, data, data_length, |
| 220 |
&ni_usb_bulk_complete, context); |
| 221 |
|
| 222 |
if (timeout_msecs) |
| 223 |
mod_timer(&ni_priv->bulk_timer, jiffies + msecs_to_jiffies(timeout_msecs)); |
| 224 |
|
| 225 |
retval = usb_submit_urb(ni_priv->bulk_urb, GFP_KERNEL); |
| 226 |
if (retval) { |
| 227 |
timer_delete_sync(&ni_priv->bulk_timer); |
| 228 |
usb_free_urb(ni_priv->bulk_urb); |
| 229 |
ni_priv->bulk_urb = NULL; |
| 230 |
dev_err(&usb_dev->dev, "failed to submit bulk in urb, retval=%i\n", retval); |
| 231 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 232 |
return retval; |
| 233 |
} |
| 234 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 235 |
if (interruptible) { |
| 236 |
if (wait_for_completion_interruptible(&context->complete)) { |
| 237 |
/* |
| 238 |
* If we got interrupted by a signal while |
| 239 |
* waiting for the usb gpib to respond, we |
| 240 |
* should send a stop command so it will |
| 241 |
* finish up with whatever it was doing and |
| 242 |
* send its response now. |
| 243 |
*/ |
| 244 |
ni_usb_stop(ni_priv); |
| 245 |
retval = -ERESTARTSYS; |
| 246 |
/* |
| 247 |
* now do an uninterruptible wait, it shouldn't take long |
| 248 |
* for the board to respond now. |
| 249 |
*/ |
| 250 |
wait_for_completion(&context->complete); |
| 251 |
} |
| 252 |
} else { |
| 253 |
wait_for_completion(&context->complete); |
| 254 |
} |
| 255 |
if (context->timed_out) { |
| 256 |
usb_kill_urb(ni_priv->bulk_urb); |
| 257 |
dev_err(&usb_dev->dev, "killed urb due to timeout\n"); |
| 258 |
retval = -ETIMEDOUT; |
| 259 |
} else { |
| 260 |
if (ni_priv->bulk_urb->status) |
| 261 |
retval = ni_priv->bulk_urb->status; |
| 262 |
} |
| 263 |
timer_delete_sync(&ni_priv->bulk_timer); |
| 264 |
*actual_data_length = ni_priv->bulk_urb->actual_length; |
| 265 |
mutex_lock(&ni_priv->bulk_transfer_lock); |
| 266 |
usb_free_urb(ni_priv->bulk_urb); |
| 267 |
ni_priv->bulk_urb = NULL; |
| 268 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 269 |
return retval; |
| 270 |
} |
| 271 |
|
| 272 |
static int ni_usb_receive_bulk_msg(struct ni_usb_priv *ni_priv, void *data, |
| 273 |
int data_length, int *actual_data_length, int timeout_msecs, |
| 274 |
int interruptible) |
| 275 |
{ |
| 276 |
int retval; |
| 277 |
int timeout_msecs_remaining = timeout_msecs; |
| 278 |
|
| 279 |
retval = ni_usb_nonblocking_receive_bulk_msg(ni_priv, data, data_length, |
| 280 |
actual_data_length, timeout_msecs_remaining, |
| 281 |
interruptible); |
| 282 |
while (retval == -EAGAIN && (timeout_msecs == 0 || timeout_msecs_remaining > 0)) { |
| 283 |
usleep_range(1000, 1500); |
| 284 |
retval = ni_usb_nonblocking_receive_bulk_msg(ni_priv, data, data_length, |
| 285 |
actual_data_length, |
| 286 |
timeout_msecs_remaining, |
| 287 |
interruptible); |
| 288 |
if (timeout_msecs != 0) |
| 289 |
--timeout_msecs_remaining; |
| 290 |
} |
| 291 |
if (timeout_msecs && timeout_msecs_remaining <= 0) |
| 292 |
return -ETIMEDOUT; |
| 293 |
return retval; |
| 294 |
} |
| 295 |
|
| 296 |
static int ni_usb_receive_control_msg(struct ni_usb_priv *ni_priv, __u8 request, |
| 297 |
__u8 requesttype, __u16 value, __u16 index, |
| 298 |
void *data, __u16 size, int timeout_msecs) |
| 299 |
{ |
| 300 |
struct usb_device *usb_dev; |
| 301 |
int retval; |
| 302 |
unsigned int in_pipe; |
| 303 |
|
| 304 |
mutex_lock(&ni_priv->control_transfer_lock); |
| 305 |
if (!ni_priv->bus_interface) { |
| 306 |
mutex_unlock(&ni_priv->control_transfer_lock); |
| 307 |
return -ENODEV; |
| 308 |
} |
| 309 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 310 |
in_pipe = usb_rcvctrlpipe(usb_dev, 0); |
| 311 |
retval = usb_control_msg(usb_dev, in_pipe, request, requesttype, value, index, data, |
| 312 |
size, timeout_msecs); |
| 313 |
mutex_unlock(&ni_priv->control_transfer_lock); |
| 314 |
return retval; |
| 315 |
} |
| 316 |
|
| 317 |
static void ni_usb_soft_update_status(struct gpib_board *board, unsigned int ni_usb_ibsta, |
| 318 |
unsigned int clear_mask) |
| 319 |
{ |
| 320 |
static const unsigned int ni_usb_ibsta_mask = SRQI | ATN | CIC | REM | LACS | TACS | LOK; |
| 321 |
|
| 322 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 323 |
struct usb_device *usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 324 |
unsigned int need_monitoring_bits = ni_usb_ibsta_monitor_mask; |
| 325 |
unsigned long flags; |
| 326 |
|
| 327 |
board->status &= ~clear_mask; |
| 328 |
board->status &= ~ni_usb_ibsta_mask; |
| 329 |
board->status |= ni_usb_ibsta & ni_usb_ibsta_mask; |
| 330 |
if (ni_usb_ibsta & DCAS) |
| 331 |
push_gpib_event(board, EVENT_DEV_CLR); |
| 332 |
if (ni_usb_ibsta & DTAS) |
| 333 |
push_gpib_event(board, EVENT_DEV_TRG); |
| 334 |
|
| 335 |
spin_lock_irqsave(&board->spinlock, flags); |
| 336 |
/* remove set status bits from monitored set why ?***/ |
| 337 |
ni_priv->monitored_ibsta_bits &= ~ni_usb_ibsta; |
| 338 |
need_monitoring_bits &= ~ni_priv->monitored_ibsta_bits; /* mm - monitored set */ |
| 339 |
spin_unlock_irqrestore(&board->spinlock, flags); |
| 340 |
dev_dbg(&usb_dev->dev, "need_monitoring_bits=0x%x\n", need_monitoring_bits); |
| 341 |
|
| 342 |
if (need_monitoring_bits & ~ni_usb_ibsta) |
| 343 |
ni_usb_set_interrupt_monitor(board, ni_usb_ibsta_monitor_mask); |
| 344 |
else if (need_monitoring_bits & ni_usb_ibsta) |
| 345 |
wake_up_interruptible(&board->wait); |
| 346 |
|
| 347 |
dev_dbg(&usb_dev->dev, "ibsta=0x%x\n", ni_usb_ibsta); |
| 348 |
} |
| 349 |
|
| 350 |
static int ni_usb_parse_status_block(const u8 *buffer, struct ni_usb_status_block *status) |
| 351 |
{ |
| 352 |
u16 count; |
| 353 |
|
| 354 |
status->id = buffer[0]; |
| 355 |
status->ibsta = (buffer[1] << 8) | buffer[2]; |
| 356 |
status->error_code = buffer[3]; |
| 357 |
count = buffer[4] | (buffer[5] << 8); |
| 358 |
count = ~count; |
| 359 |
count++; |
| 360 |
status->count = count; |
| 361 |
return 8; |
| 362 |
}; |
| 363 |
|
| 364 |
static void ni_usb_dump_raw_block(const u8 *raw_data, int length) |
| 365 |
{ |
| 366 |
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 8, 1, raw_data, length, true); |
| 367 |
} |
| 368 |
|
| 369 |
static int ni_usb_parse_register_read_block(const u8 *raw_data, unsigned int *results, |
| 370 |
int num_results) |
| 371 |
{ |
| 372 |
int i = 0; |
| 373 |
int j; |
| 374 |
int unexpected = 0; |
| 375 |
static const int results_per_chunk = 3; |
| 376 |
|
| 377 |
for (j = 0; j < num_results;) { |
| 378 |
int k; |
| 379 |
|
| 380 |
if (raw_data[i++] != NIUSB_REGISTER_READ_DATA_START_ID) { |
| 381 |
pr_err("parse error: wrong start id\n"); |
| 382 |
unexpected = 1; |
| 383 |
} |
| 384 |
for (k = 0; k < results_per_chunk && j < num_results; ++k) |
| 385 |
results[j++] = raw_data[i++]; |
| 386 |
} |
| 387 |
while (i % 4) |
| 388 |
i++; |
| 389 |
if (raw_data[i++] != NIUSB_REGISTER_READ_DATA_END_ID) { |
| 390 |
pr_err("parse error: wrong end id\n"); |
| 391 |
unexpected = 1; |
| 392 |
} |
| 393 |
if (raw_data[i++] % results_per_chunk != num_results % results_per_chunk) { |
| 394 |
pr_err("parse error: wrong count=%i for NIUSB_REGISTER_READ_DATA_END\n", |
| 395 |
(int)raw_data[i - 1]); |
| 396 |
unexpected = 1; |
| 397 |
} |
| 398 |
while (i % 4) { |
| 399 |
if (raw_data[i++] != 0) { |
| 400 |
pr_err("unexpected data: raw_data[%i]=0x%x, expected 0\n", |
| 401 |
i - 1, (int)raw_data[i - 1]); |
| 402 |
unexpected = 1; |
| 403 |
} |
| 404 |
} |
| 405 |
if (unexpected) |
| 406 |
ni_usb_dump_raw_block(raw_data, i); |
| 407 |
return i; |
| 408 |
} |
| 409 |
|
| 410 |
static int ni_usb_parse_termination_block(const u8 *buffer) |
| 411 |
{ |
| 412 |
int i = 0; |
| 413 |
|
| 414 |
if (buffer[i++] != NIUSB_TERM_ID || |
| 415 |
buffer[i++] != 0x0 || |
| 416 |
buffer[i++] != 0x0 || |
| 417 |
buffer[i++] != 0x0) { |
| 418 |
pr_err("received unexpected termination block\n"); |
| 419 |
pr_err(" expected: 0x%x 0x%x 0x%x 0x%x\n", NIUSB_TERM_ID, 0x0, 0x0, 0x0); |
| 420 |
pr_err(" received: 0x%x 0x%x 0x%x 0x%x\n", |
| 421 |
buffer[i - 4], buffer[i - 3], buffer[i - 2], buffer[i - 1]); |
| 422 |
} |
| 423 |
return i; |
| 424 |
}; |
| 425 |
|
| 426 |
static int parse_board_ibrd_readback(const u8 *raw_data, struct ni_usb_status_block *status, |
| 427 |
u8 *parsed_data, int parsed_data_length, |
| 428 |
int *actual_bytes_read) |
| 429 |
{ |
| 430 |
static const int ibrd_data_block_length = 0xf; |
| 431 |
static const int ibrd_extended_data_block_length = 0x1e; |
| 432 |
int data_block_length = 0; |
| 433 |
int i = 0; |
| 434 |
int j = 0; |
| 435 |
int k; |
| 436 |
int num_data_blocks = 0; |
| 437 |
struct ni_usb_status_block register_write_status; |
| 438 |
int unexpected = 0; |
| 439 |
|
| 440 |
while (raw_data[i] == NIUSB_IBRD_DATA_ID || raw_data[i] == NIUSB_IBRD_EXTENDED_DATA_ID) { |
| 441 |
if (raw_data[i] == NIUSB_IBRD_DATA_ID) { |
| 442 |
data_block_length = ibrd_data_block_length; |
| 443 |
} else if (raw_data[i] == NIUSB_IBRD_EXTENDED_DATA_ID) { |
| 444 |
data_block_length = ibrd_extended_data_block_length; |
| 445 |
if (raw_data[++i] != 0) { |
| 446 |
pr_err("unexpected data: raw_data[%i]=0x%x, expected 0\n", |
| 447 |
i, (int)raw_data[i]); |
| 448 |
unexpected = 1; |
| 449 |
} |
| 450 |
} else { |
| 451 |
pr_err("Unexpected NIUSB_IBRD ID\n"); |
| 452 |
return -EINVAL; |
| 453 |
} |
| 454 |
++i; |
| 455 |
for (k = 0; k < data_block_length; k++) { |
| 456 |
if (j < parsed_data_length) |
| 457 |
parsed_data[j++] = raw_data[i++]; |
| 458 |
else |
| 459 |
++i; |
| 460 |
} |
| 461 |
++num_data_blocks; |
| 462 |
} |
| 463 |
i += ni_usb_parse_status_block(&raw_data[i], status); |
| 464 |
if (status->id != NIUSB_IBRD_STATUS_ID) { |
| 465 |
pr_err("bug: status->id=%i, != ibrd_status_id\n", status->id); |
| 466 |
return -EIO; |
| 467 |
} |
| 468 |
i++; |
| 469 |
if (num_data_blocks) { |
| 470 |
*actual_bytes_read = (num_data_blocks - 1) * data_block_length + raw_data[i++]; |
| 471 |
} else { |
| 472 |
++i; |
| 473 |
*actual_bytes_read = 0; |
| 474 |
} |
| 475 |
if (*actual_bytes_read > j) |
| 476 |
pr_err("bug: discarded data. actual_bytes_read=%i, j=%i\n", *actual_bytes_read, j); |
| 477 |
for (k = 0; k < 2; k++) |
| 478 |
if (raw_data[i++] != 0) { |
| 479 |
pr_err("unexpected data: raw_data[%i]=0x%x, expected 0\n", |
| 480 |
i - 1, (int)raw_data[i - 1]); |
| 481 |
unexpected = 1; |
| 482 |
} |
| 483 |
i += ni_usb_parse_status_block(&raw_data[i], ®ister_write_status); |
| 484 |
if (register_write_status.id != NIUSB_REG_WRITE_ID) { |
| 485 |
pr_err("unexpected data: register write status id=0x%x, expected 0x%x\n", |
| 486 |
register_write_status.id, NIUSB_REG_WRITE_ID); |
| 487 |
unexpected = 1; |
| 488 |
} |
| 489 |
if (raw_data[i++] != 2) { |
| 490 |
pr_err("unexpected data: register write count=%i, expected 2\n", |
| 491 |
(int)raw_data[i - 1]); |
| 492 |
unexpected = 1; |
| 493 |
} |
| 494 |
for (k = 0; k < 3; k++) |
| 495 |
if (raw_data[i++] != 0) { |
| 496 |
pr_err("unexpected data: raw_data[%i]=0x%x, expected 0\n", |
| 497 |
i - 1, (int)raw_data[i - 1]); |
| 498 |
unexpected = 1; |
| 499 |
} |
| 500 |
i += ni_usb_parse_termination_block(&raw_data[i]); |
| 501 |
if (unexpected) |
| 502 |
ni_usb_dump_raw_block(raw_data, i); |
| 503 |
return i; |
| 504 |
} |
| 505 |
|
| 506 |
static int ni_usb_parse_reg_write_status_block(const u8 *raw_data, |
| 507 |
struct ni_usb_status_block *status, |
| 508 |
int *writes_completed) |
| 509 |
{ |
| 510 |
int i = 0; |
| 511 |
|
| 512 |
i += ni_usb_parse_status_block(raw_data, status); |
| 513 |
*writes_completed = raw_data[i++]; |
| 514 |
while (i % 4) |
| 515 |
i++; |
| 516 |
return i; |
| 517 |
} |
| 518 |
|
| 519 |
static int ni_usb_write_registers(struct ni_usb_priv *ni_priv, |
| 520 |
const struct ni_usb_register *writes, int num_writes, |
| 521 |
unsigned int *ibsta) |
| 522 |
{ |
| 523 |
struct usb_device *usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 524 |
int retval; |
| 525 |
u8 *out_data, *in_data; |
| 526 |
int out_data_length; |
| 527 |
static const int in_data_length = 0x20; |
| 528 |
int bytes_written = 0, bytes_read = 0; |
| 529 |
int i = 0; |
| 530 |
int j; |
| 531 |
struct ni_usb_status_block status; |
| 532 |
static const int bytes_per_write = 3; |
| 533 |
int reg_writes_completed; |
| 534 |
|
| 535 |
out_data_length = num_writes * bytes_per_write + 0x10; |
| 536 |
out_data = kmalloc(out_data_length, GFP_KERNEL); |
| 537 |
if (!out_data) |
| 538 |
return -ENOMEM; |
| 539 |
i += ni_usb_bulk_register_write_header(&out_data[i], num_writes); |
| 540 |
for (j = 0; j < num_writes; j++) |
| 541 |
i += ni_usb_bulk_register_write(&out_data[i], writes[j]); |
| 542 |
while (i % 4) |
| 543 |
out_data[i++] = 0x00; |
| 544 |
i += ni_usb_bulk_termination(&out_data[i]); |
| 545 |
|
| 546 |
mutex_lock(&ni_priv->addressed_transfer_lock); |
| 547 |
|
| 548 |
retval = ni_usb_send_bulk_msg(ni_priv, out_data, i, &bytes_written, 1000); |
| 549 |
kfree(out_data); |
| 550 |
if (retval) { |
| 551 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 552 |
dev_err(&usb_dev->dev, "send_bulk_msg returned %i, bytes_written=%i, i=%i\n", |
| 553 |
retval, bytes_written, i); |
| 554 |
return retval; |
| 555 |
} |
| 556 |
|
| 557 |
in_data = kmalloc(in_data_length, GFP_KERNEL); |
| 558 |
if (!in_data) { |
| 559 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 560 |
return -ENOMEM; |
| 561 |
} |
| 562 |
retval = ni_usb_receive_bulk_msg(ni_priv, in_data, in_data_length, &bytes_read, 1000, 0); |
| 563 |
if (retval || bytes_read != 16) { |
| 564 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 565 |
dev_err(&usb_dev->dev, "receive_bulk_msg returned %i, bytes_read=%i\n", |
| 566 |
retval, bytes_read); |
| 567 |
ni_usb_dump_raw_block(in_data, bytes_read); |
| 568 |
kfree(in_data); |
| 569 |
return retval ?: -EINVAL; |
| 570 |
} |
| 571 |
|
| 572 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 573 |
|
| 574 |
ni_usb_parse_reg_write_status_block(in_data, &status, ®_writes_completed); |
| 575 |
// FIXME parse extra 09 status bits and termination |
| 576 |
kfree(in_data); |
| 577 |
if (status.id != NIUSB_REG_WRITE_ID) { |
| 578 |
dev_err(&usb_dev->dev, "parse error, id=0x%x != NIUSB_REG_WRITE_ID\n", status.id); |
| 579 |
return -EIO; |
| 580 |
} |
| 581 |
if (status.error_code) { |
| 582 |
dev_err(&usb_dev->dev, "nonzero error code 0x%x\n", status.error_code); |
| 583 |
return -EIO; |
| 584 |
} |
| 585 |
if (reg_writes_completed != num_writes) { |
| 586 |
dev_err(&usb_dev->dev, "reg_writes_completed=%i, num_writes=%i\n", |
| 587 |
reg_writes_completed, num_writes); |
| 588 |
return -EIO; |
| 589 |
} |
| 590 |
if (ibsta) |
| 591 |
*ibsta = status.ibsta; |
| 592 |
return 0; |
| 593 |
} |
| 594 |
|
| 595 |
// interface functions |
| 596 |
static int ni_usb_read(struct gpib_board *board, u8 *buffer, size_t length, |
| 597 |
int *end, size_t *bytes_read) |
| 598 |
{ |
| 599 |
int retval, parse_retval; |
| 600 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 601 |
struct usb_device *usb_dev; |
| 602 |
u8 *out_data, *in_data; |
| 603 |
static const int out_data_length = 0x20; |
| 604 |
int in_data_length; |
| 605 |
int usb_bytes_written = 0, usb_bytes_read = 0; |
| 606 |
int i = 0; |
| 607 |
int complement_count; |
| 608 |
int actual_length; |
| 609 |
struct ni_usb_status_block status; |
| 610 |
static const int max_read_length = 0xffff; |
| 611 |
struct ni_usb_register reg; |
| 612 |
|
| 613 |
*bytes_read = 0; |
| 614 |
if (!ni_priv->bus_interface) |
| 615 |
return -ENODEV; |
| 616 |
if (length > max_read_length) |
| 617 |
return -EINVAL; |
| 618 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 619 |
out_data = kmalloc(out_data_length, GFP_KERNEL); |
| 620 |
if (!out_data) |
| 621 |
return -ENOMEM; |
| 622 |
out_data[i++] = 0x0a; |
| 623 |
out_data[i++] = ni_priv->eos_mode >> 8; |
| 624 |
out_data[i++] = ni_priv->eos_char; |
| 625 |
out_data[i++] = ni_usb_timeout_code(board->usec_timeout); |
| 626 |
complement_count = length - 1; |
| 627 |
complement_count = ~complement_count; |
| 628 |
out_data[i++] = complement_count & 0xff; |
| 629 |
out_data[i++] = (complement_count >> 8) & 0xff; |
| 630 |
out_data[i++] = 0x0; |
| 631 |
out_data[i++] = 0x0; |
| 632 |
i += ni_usb_bulk_register_write_header(&out_data[i], 2); |
| 633 |
reg.device = NIUSB_SUBDEV_TNT4882; |
| 634 |
reg.address = nec7210_to_tnt4882_offset(AUXMR); |
| 635 |
reg.value = AUX_HLDI; |
| 636 |
i += ni_usb_bulk_register_write(&out_data[i], reg); |
| 637 |
reg.value = AUX_CLEAR_END; |
| 638 |
i += ni_usb_bulk_register_write(&out_data[i], reg); |
| 639 |
while (i % 4) // pad with zeros to 4-byte boundary |
| 640 |
out_data[i++] = 0x0; |
| 641 |
i += ni_usb_bulk_termination(&out_data[i]); |
| 642 |
|
| 643 |
mutex_lock(&ni_priv->addressed_transfer_lock); |
| 644 |
|
| 645 |
retval = ni_usb_send_bulk_msg(ni_priv, out_data, i, &usb_bytes_written, 1000); |
| 646 |
kfree(out_data); |
| 647 |
if (retval || usb_bytes_written != i) { |
| 648 |
if (retval == 0) |
| 649 |
retval = -EIO; |
| 650 |
dev_err(&usb_dev->dev, "send_bulk_msg returned %i, usb_bytes_written=%i, i=%i\n", |
| 651 |
retval, usb_bytes_written, i); |
| 652 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 653 |
return retval; |
| 654 |
} |
| 655 |
|
| 656 |
in_data_length = (length / 30 + 1) * 0x20 + 0x20; |
| 657 |
in_data = kmalloc(in_data_length, GFP_KERNEL); |
| 658 |
if (!in_data) { |
| 659 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 660 |
return -ENOMEM; |
| 661 |
} |
| 662 |
retval = ni_usb_receive_bulk_msg(ni_priv, in_data, in_data_length, &usb_bytes_read, |
| 663 |
ni_usb_timeout_msecs(board->usec_timeout), 1); |
| 664 |
|
| 665 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 666 |
|
| 667 |
if (retval == -ERESTARTSYS) { |
| 668 |
} else if (retval) { |
| 669 |
dev_err(&usb_dev->dev, "receive_bulk_msg returned %i, usb_bytes_read=%i\n", |
| 670 |
retval, usb_bytes_read); |
| 671 |
kfree(in_data); |
| 672 |
return retval; |
| 673 |
} |
| 674 |
parse_retval = parse_board_ibrd_readback(in_data, &status, buffer, length, &actual_length); |
| 675 |
if (parse_retval != usb_bytes_read) { |
| 676 |
if (parse_retval >= 0) |
| 677 |
parse_retval = -EIO; |
| 678 |
dev_err(&usb_dev->dev, "retval=%i usb_bytes_read=%i\n", |
| 679 |
parse_retval, usb_bytes_read); |
| 680 |
kfree(in_data); |
| 681 |
return parse_retval; |
| 682 |
} |
| 683 |
if (actual_length != length - status.count) { |
| 684 |
dev_err(&usb_dev->dev, "actual_length=%i expected=%li\n", |
| 685 |
actual_length, (long)(length - status.count)); |
| 686 |
ni_usb_dump_raw_block(in_data, usb_bytes_read); |
| 687 |
} |
| 688 |
kfree(in_data); |
| 689 |
switch (status.error_code) { |
| 690 |
case NIUSB_NO_ERROR: |
| 691 |
retval = 0; |
| 692 |
break; |
| 693 |
case NIUSB_ABORTED_ERROR: |
| 694 |
/* |
| 695 |
* this is expected if ni_usb_receive_bulk_msg got |
| 696 |
* interrupted by a signal and returned -ERESTARTSYS |
| 697 |
*/ |
| 698 |
break; |
| 699 |
case NIUSB_ATN_STATE_ERROR: |
| 700 |
if (status.ibsta & DCAS) { |
| 701 |
retval = -EINTR; |
| 702 |
} else { |
| 703 |
retval = -EIO; |
| 704 |
dev_dbg(&usb_dev->dev, "read when ATN set stat: 0x%06x\n", status.ibsta); |
| 705 |
} |
| 706 |
break; |
| 707 |
case NIUSB_ADDRESSING_ERROR: |
| 708 |
retval = -EIO; |
| 709 |
break; |
| 710 |
case NIUSB_TIMEOUT_ERROR: |
| 711 |
retval = -ETIMEDOUT; |
| 712 |
break; |
| 713 |
case NIUSB_EOSMODE_ERROR: |
| 714 |
dev_err(&usb_dev->dev, "driver bug, we should have been able to avoid NIUSB_EOSMODE_ERROR.\n"); |
| 715 |
retval = -EINVAL; |
| 716 |
break; |
| 717 |
default: |
| 718 |
dev_err(&usb_dev->dev, "unknown error code=%i\n", status.error_code); |
| 719 |
retval = -EIO; |
| 720 |
break; |
| 721 |
} |
| 722 |
ni_usb_soft_update_status(board, status.ibsta, 0); |
| 723 |
if (status.ibsta & END) |
| 724 |
*end = 1; |
| 725 |
else |
| 726 |
*end = 0; |
| 727 |
*bytes_read = actual_length; |
| 728 |
return retval; |
| 729 |
} |
| 730 |
|
| 731 |
static int ni_usb_write(struct gpib_board *board, u8 *buffer, size_t length, |
| 732 |
int send_eoi, size_t *bytes_written) |
| 733 |
{ |
| 734 |
int retval; |
| 735 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 736 |
struct usb_device *usb_dev; |
| 737 |
u8 *out_data, *in_data; |
| 738 |
int out_data_length; |
| 739 |
static const int in_data_length = 0x10; |
| 740 |
int usb_bytes_written = 0, usb_bytes_read = 0; |
| 741 |
int i = 0, j; |
| 742 |
int complement_count; |
| 743 |
struct ni_usb_status_block status; |
| 744 |
static const int max_write_length = 0xffff; |
| 745 |
|
| 746 |
if (!ni_priv->bus_interface) |
| 747 |
return -ENODEV; |
| 748 |
if (length > max_write_length) |
| 749 |
return -EINVAL; |
| 750 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 751 |
out_data_length = length + 0x10; |
| 752 |
out_data = kmalloc(out_data_length, GFP_KERNEL); |
| 753 |
if (!out_data) |
| 754 |
return -ENOMEM; |
| 755 |
out_data[i++] = 0x0d; |
| 756 |
complement_count = length - 1; |
| 757 |
complement_count = ~complement_count; |
| 758 |
out_data[i++] = complement_count & 0xff; |
| 759 |
out_data[i++] = (complement_count >> 8) & 0xff; |
| 760 |
out_data[i++] = ni_usb_timeout_code(board->usec_timeout); |
| 761 |
out_data[i++] = 0x0; |
| 762 |
out_data[i++] = 0x0; |
| 763 |
if (send_eoi) |
| 764 |
out_data[i++] = 0x8; |
| 765 |
else |
| 766 |
out_data[i++] = 0x0; |
| 767 |
out_data[i++] = 0x0; |
| 768 |
for (j = 0; j < length; j++) |
| 769 |
out_data[i++] = buffer[j]; |
| 770 |
while (i % 4) // pad with zeros to 4-byte boundary |
| 771 |
out_data[i++] = 0x0; |
| 772 |
i += ni_usb_bulk_termination(&out_data[i]); |
| 773 |
|
| 774 |
mutex_lock(&ni_priv->addressed_transfer_lock); |
| 775 |
|
| 776 |
retval = ni_usb_send_bulk_msg(ni_priv, out_data, i, &usb_bytes_written, |
| 777 |
ni_usb_timeout_msecs(board->usec_timeout)); |
| 778 |
kfree(out_data); |
| 779 |
if (retval || usb_bytes_written != i) { |
| 780 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 781 |
dev_err(&usb_dev->dev, "send_bulk_msg returned %i, usb_bytes_written=%i, i=%i\n", |
| 782 |
retval, usb_bytes_written, i); |
| 783 |
return retval; |
| 784 |
} |
| 785 |
|
| 786 |
in_data = kmalloc(in_data_length, GFP_KERNEL); |
| 787 |
if (!in_data) { |
| 788 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 789 |
return -ENOMEM; |
| 790 |
} |
| 791 |
retval = ni_usb_receive_bulk_msg(ni_priv, in_data, in_data_length, &usb_bytes_read, |
| 792 |
ni_usb_timeout_msecs(board->usec_timeout), 1); |
| 793 |
|
| 794 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 795 |
|
| 796 |
if ((retval && retval != -ERESTARTSYS) || usb_bytes_read != 12) { |
| 797 |
dev_err(&usb_dev->dev, "receive_bulk_msg returned %i, usb_bytes_read=%i\n", |
| 798 |
retval, usb_bytes_read); |
| 799 |
kfree(in_data); |
| 800 |
return retval; |
| 801 |
} |
| 802 |
ni_usb_parse_status_block(in_data, &status); |
| 803 |
kfree(in_data); |
| 804 |
switch (status.error_code) { |
| 805 |
case NIUSB_NO_ERROR: |
| 806 |
retval = 0; |
| 807 |
break; |
| 808 |
case NIUSB_ABORTED_ERROR: |
| 809 |
/* |
| 810 |
* this is expected if ni_usb_receive_bulk_msg got |
| 811 |
* interrupted by a signal and returned -ERESTARTSYS |
| 812 |
*/ |
| 813 |
break; |
| 814 |
case NIUSB_ADDRESSING_ERROR: |
| 815 |
dev_err(&usb_dev->dev, "Addressing error retval %d error code=%i\n", |
| 816 |
retval, status.error_code); |
| 817 |
retval = -ENXIO; |
| 818 |
break; |
| 819 |
case NIUSB_NO_LISTENER_ERROR: |
| 820 |
retval = -ECOMM; |
| 821 |
break; |
| 822 |
case NIUSB_TIMEOUT_ERROR: |
| 823 |
retval = -ETIMEDOUT; |
| 824 |
break; |
| 825 |
default: |
| 826 |
dev_err(&usb_dev->dev, "unknown error code=%i\n", status.error_code); |
| 827 |
retval = -EPIPE; |
| 828 |
break; |
| 829 |
} |
| 830 |
ni_usb_soft_update_status(board, status.ibsta, 0); |
| 831 |
*bytes_written = length - status.count; |
| 832 |
return retval; |
| 833 |
} |
| 834 |
|
| 835 |
static int ni_usb_command_chunk(struct gpib_board *board, u8 *buffer, size_t length, |
| 836 |
size_t *command_bytes_written) |
| 837 |
{ |
| 838 |
int retval; |
| 839 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 840 |
struct usb_device *usb_dev; |
| 841 |
u8 *out_data, *in_data; |
| 842 |
int out_data_length; |
| 843 |
static const int in_data_length = 0x10; |
| 844 |
int bytes_written = 0, bytes_read = 0; |
| 845 |
int i = 0, j; |
| 846 |
unsigned int complement_count; |
| 847 |
struct ni_usb_status_block status; |
| 848 |
// usb-b gives error 4 if you try to send more than 16 command bytes at once |
| 849 |
static const int max_command_length = 0x10; |
| 850 |
|
| 851 |
*command_bytes_written = 0; |
| 852 |
if (!ni_priv->bus_interface) |
| 853 |
return -ENODEV; |
| 854 |
if (length > max_command_length) |
| 855 |
length = max_command_length; |
| 856 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 857 |
out_data_length = length + 0x10; |
| 858 |
out_data = kmalloc(out_data_length, GFP_KERNEL); |
| 859 |
if (!out_data) |
| 860 |
return -ENOMEM; |
| 861 |
out_data[i++] = 0x0c; |
| 862 |
complement_count = length - 1; |
| 863 |
complement_count = ~complement_count; |
| 864 |
out_data[i++] = complement_count; |
| 865 |
out_data[i++] = 0x0; |
| 866 |
out_data[i++] = ni_usb_timeout_code(board->usec_timeout); |
| 867 |
for (j = 0; j < length; j++) |
| 868 |
out_data[i++] = buffer[j]; |
| 869 |
while (i % 4) // pad with zeros to 4-byte boundary |
| 870 |
out_data[i++] = 0x0; |
| 871 |
i += ni_usb_bulk_termination(&out_data[i]); |
| 872 |
|
| 873 |
mutex_lock(&ni_priv->addressed_transfer_lock); |
| 874 |
|
| 875 |
retval = ni_usb_send_bulk_msg(ni_priv, out_data, i, &bytes_written, |
| 876 |
ni_usb_timeout_msecs(board->usec_timeout)); |
| 877 |
kfree(out_data); |
| 878 |
if (retval || bytes_written != i) { |
| 879 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 880 |
dev_err(&usb_dev->dev, "send_bulk_msg returned %i, bytes_written=%i, i=%i\n", |
| 881 |
retval, bytes_written, i); |
| 882 |
return retval; |
| 883 |
} |
| 884 |
|
| 885 |
in_data = kmalloc(in_data_length, GFP_KERNEL); |
| 886 |
if (!in_data) { |
| 887 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 888 |
return -ENOMEM; |
| 889 |
} |
| 890 |
|
| 891 |
retval = ni_usb_receive_bulk_msg(ni_priv, in_data, in_data_length, &bytes_read, |
| 892 |
ni_usb_timeout_msecs(board->usec_timeout), 1); |
| 893 |
|
| 894 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 895 |
|
| 896 |
if ((retval && retval != -ERESTARTSYS) || bytes_read != 12) { |
| 897 |
dev_err(&usb_dev->dev, "receive_bulk_msg returned %i, bytes_read=%i\n", |
| 898 |
retval, bytes_read); |
| 899 |
kfree(in_data); |
| 900 |
return retval; |
| 901 |
} |
| 902 |
ni_usb_parse_status_block(in_data, &status); |
| 903 |
kfree(in_data); |
| 904 |
*command_bytes_written = length - status.count; |
| 905 |
switch (status.error_code) { |
| 906 |
case NIUSB_NO_ERROR: |
| 907 |
break; |
| 908 |
case NIUSB_ABORTED_ERROR: |
| 909 |
/* |
| 910 |
* this is expected if ni_usb_receive_bulk_msg got |
| 911 |
* interrupted by a signal and returned -ERESTARTSYS |
| 912 |
*/ |
| 913 |
break; |
| 914 |
case NIUSB_NO_BUS_ERROR: |
| 915 |
return -ENOTCONN; |
| 916 |
case NIUSB_EOSMODE_ERROR: |
| 917 |
dev_err(&usb_dev->dev, "got eosmode error. Driver bug?\n"); |
| 918 |
return -EIO; |
| 919 |
case NIUSB_TIMEOUT_ERROR: |
| 920 |
return -ETIMEDOUT; |
| 921 |
default: |
| 922 |
dev_err(&usb_dev->dev, "unknown error code=%i\n", status.error_code); |
| 923 |
return -EIO; |
| 924 |
} |
| 925 |
ni_usb_soft_update_status(board, status.ibsta, 0); |
| 926 |
return 0; |
| 927 |
} |
| 928 |
|
| 929 |
static int ni_usb_command(struct gpib_board *board, u8 *buffer, size_t length, |
| 930 |
size_t *bytes_written) |
| 931 |
{ |
| 932 |
size_t count; |
| 933 |
int retval; |
| 934 |
|
| 935 |
*bytes_written = 0; |
| 936 |
while (*bytes_written < length) { |
| 937 |
retval = ni_usb_command_chunk(board, buffer + *bytes_written, |
| 938 |
length - *bytes_written, &count); |
| 939 |
*bytes_written += count; |
| 940 |
if (retval < 0) |
| 941 |
return retval; |
| 942 |
} |
| 943 |
return 0; |
| 944 |
} |
| 945 |
|
| 946 |
static int ni_usb_take_control(struct gpib_board *board, int synchronous) |
| 947 |
{ |
| 948 |
int retval; |
| 949 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 950 |
struct usb_device *usb_dev; |
| 951 |
u8 *out_data, *in_data; |
| 952 |
static const int out_data_length = 0x10; |
| 953 |
static const int in_data_length = 0x10; |
| 954 |
int bytes_written = 0, bytes_read = 0; |
| 955 |
int i = 0; |
| 956 |
struct ni_usb_status_block status; |
| 957 |
|
| 958 |
if (!ni_priv->bus_interface) |
| 959 |
return -ENODEV; |
| 960 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 961 |
out_data = kmalloc(out_data_length, GFP_KERNEL); |
| 962 |
if (!out_data) |
| 963 |
return -ENOMEM; |
| 964 |
out_data[i++] = NIUSB_IBCAC_ID; |
| 965 |
if (synchronous) |
| 966 |
out_data[i++] = 0x1; |
| 967 |
else |
| 968 |
out_data[i++] = 0x0; |
| 969 |
out_data[i++] = 0x0; |
| 970 |
out_data[i++] = 0x0; |
| 971 |
i += ni_usb_bulk_termination(&out_data[i]); |
| 972 |
|
| 973 |
mutex_lock(&ni_priv->addressed_transfer_lock); |
| 974 |
|
| 975 |
retval = ni_usb_send_bulk_msg(ni_priv, out_data, i, &bytes_written, 1000); |
| 976 |
kfree(out_data); |
| 977 |
if (retval || bytes_written != i) { |
| 978 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 979 |
dev_err(&usb_dev->dev, "send_bulk_msg returned %i, bytes_written=%i, i=%i\n", |
| 980 |
retval, bytes_written, i); |
| 981 |
return retval; |
| 982 |
} |
| 983 |
|
| 984 |
in_data = kmalloc(in_data_length, GFP_KERNEL); |
| 985 |
if (!in_data) { |
| 986 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 987 |
return -ENOMEM; |
| 988 |
} |
| 989 |
retval = ni_usb_receive_bulk_msg(ni_priv, in_data, in_data_length, &bytes_read, 1000, 1); |
| 990 |
|
| 991 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 992 |
|
| 993 |
if ((retval && retval != -ERESTARTSYS) || bytes_read != 12) { |
| 994 |
if (retval == 0) |
| 995 |
retval = -EIO; |
| 996 |
dev_err(&usb_dev->dev, "receive_bulk_msg returned %i, bytes_read=%i\n", |
| 997 |
retval, bytes_read); |
| 998 |
kfree(in_data); |
| 999 |
return retval; |
| 1000 |
} |
| 1001 |
ni_usb_parse_status_block(in_data, &status); |
| 1002 |
kfree(in_data); |
| 1003 |
ni_usb_soft_update_status(board, status.ibsta, 0); |
| 1004 |
return retval; |
| 1005 |
} |
| 1006 |
|
| 1007 |
static int ni_usb_go_to_standby(struct gpib_board *board) |
| 1008 |
{ |
| 1009 |
int retval; |
| 1010 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1011 |
struct usb_device *usb_dev; |
| 1012 |
u8 *out_data, *in_data; |
| 1013 |
static const int out_data_length = 0x10; |
| 1014 |
static const int in_data_length = 0x20; |
| 1015 |
int bytes_written = 0, bytes_read = 0; |
| 1016 |
int i = 0; |
| 1017 |
struct ni_usb_status_block status; |
| 1018 |
|
| 1019 |
if (!ni_priv->bus_interface) |
| 1020 |
return -ENODEV; |
| 1021 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1022 |
out_data = kmalloc(out_data_length, GFP_KERNEL); |
| 1023 |
if (!out_data) |
| 1024 |
return -ENOMEM; |
| 1025 |
|
| 1026 |
out_data[i++] = NIUSB_IBGTS_ID; |
| 1027 |
out_data[i++] = 0x0; |
| 1028 |
out_data[i++] = 0x0; |
| 1029 |
out_data[i++] = 0x0; |
| 1030 |
i += ni_usb_bulk_termination(&out_data[i]); |
| 1031 |
|
| 1032 |
mutex_lock(&ni_priv->addressed_transfer_lock); |
| 1033 |
|
| 1034 |
retval = ni_usb_send_bulk_msg(ni_priv, out_data, i, &bytes_written, 1000); |
| 1035 |
kfree(out_data); |
| 1036 |
if (retval || bytes_written != i) { |
| 1037 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 1038 |
dev_err(&usb_dev->dev, "send_bulk_msg returned %i, bytes_written=%i, i=%i\n", |
| 1039 |
retval, bytes_written, i); |
| 1040 |
return retval; |
| 1041 |
} |
| 1042 |
|
| 1043 |
in_data = kmalloc(in_data_length, GFP_KERNEL); |
| 1044 |
if (!in_data) { |
| 1045 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 1046 |
return -ENOMEM; |
| 1047 |
} |
| 1048 |
retval = ni_usb_receive_bulk_msg(ni_priv, in_data, in_data_length, &bytes_read, 1000, 0); |
| 1049 |
|
| 1050 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 1051 |
|
| 1052 |
if (retval || bytes_read != 12) { |
| 1053 |
dev_err(&usb_dev->dev, "receive_bulk_msg returned %i, bytes_read=%i\n", |
| 1054 |
retval, bytes_read); |
| 1055 |
kfree(in_data); |
| 1056 |
return retval; |
| 1057 |
} |
| 1058 |
ni_usb_parse_status_block(in_data, &status); |
| 1059 |
kfree(in_data); |
| 1060 |
if (status.id != NIUSB_IBGTS_ID) |
| 1061 |
dev_err(&usb_dev->dev, "bug: status.id 0x%x != INUSB_IBGTS_ID\n", status.id); |
| 1062 |
ni_usb_soft_update_status(board, status.ibsta, 0); |
| 1063 |
return 0; |
| 1064 |
} |
| 1065 |
|
| 1066 |
static int ni_usb_request_system_control(struct gpib_board *board, int request_control) |
| 1067 |
{ |
| 1068 |
int retval; |
| 1069 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1070 |
struct usb_device *usb_dev; |
| 1071 |
int i = 0; |
| 1072 |
struct ni_usb_register writes[4]; |
| 1073 |
unsigned int ibsta; |
| 1074 |
|
| 1075 |
if (!ni_priv->bus_interface) |
| 1076 |
return -ENODEV; |
| 1077 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1078 |
if (request_control) { |
| 1079 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1080 |
writes[i].address = CMDR; |
| 1081 |
writes[i].value = SETSC; |
| 1082 |
i++; |
| 1083 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1084 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1085 |
writes[i].value = AUX_CIFC; |
| 1086 |
i++; |
| 1087 |
} else { |
| 1088 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1089 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1090 |
writes[i].value = AUX_CREN; |
| 1091 |
i++; |
| 1092 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1093 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1094 |
writes[i].value = AUX_CIFC; |
| 1095 |
i++; |
| 1096 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1097 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1098 |
writes[i].value = AUX_DSC; |
| 1099 |
i++; |
| 1100 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1101 |
writes[i].address = CMDR; |
| 1102 |
writes[i].value = CLRSC; |
| 1103 |
i++; |
| 1104 |
} |
| 1105 |
retval = ni_usb_write_registers(ni_priv, writes, i, &ibsta); |
| 1106 |
if (retval < 0) { |
| 1107 |
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval); |
| 1108 |
return retval; |
| 1109 |
} |
| 1110 |
if (!request_control) |
| 1111 |
ni_priv->ren_state = 0; |
| 1112 |
ni_usb_soft_update_status(board, ibsta, 0); |
| 1113 |
return 0; |
| 1114 |
} |
| 1115 |
|
| 1116 |
// FIXME maybe the interface should have a "pulse interface clear" function that can return an error? |
| 1117 |
static void ni_usb_interface_clear(struct gpib_board *board, int assert) |
| 1118 |
{ |
| 1119 |
int retval; |
| 1120 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1121 |
struct usb_device *usb_dev; |
| 1122 |
u8 *out_data, *in_data; |
| 1123 |
static const int out_data_length = 0x10; |
| 1124 |
static const int in_data_length = 0x10; |
| 1125 |
int bytes_written = 0, bytes_read = 0; |
| 1126 |
int i = 0; |
| 1127 |
struct ni_usb_status_block status; |
| 1128 |
|
| 1129 |
if (!ni_priv->bus_interface) |
| 1130 |
return; // -ENODEV; |
| 1131 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1132 |
// FIXME: we are going to pulse when assert is true, and ignore otherwise |
| 1133 |
if (assert == 0) |
| 1134 |
return; |
| 1135 |
out_data = kmalloc(out_data_length, GFP_KERNEL); |
| 1136 |
if (!out_data) |
| 1137 |
return; |
| 1138 |
out_data[i++] = NIUSB_IBSIC_ID; |
| 1139 |
out_data[i++] = 0x0; |
| 1140 |
out_data[i++] = 0x0; |
| 1141 |
out_data[i++] = 0x0; |
| 1142 |
i += ni_usb_bulk_termination(&out_data[i]); |
| 1143 |
retval = ni_usb_send_bulk_msg(ni_priv, out_data, i, &bytes_written, 1000); |
| 1144 |
kfree(out_data); |
| 1145 |
if (retval || bytes_written != i) { |
| 1146 |
dev_err(&usb_dev->dev, "send_bulk_msg returned %i, bytes_written=%i, i=%i\n", |
| 1147 |
retval, bytes_written, i); |
| 1148 |
return; |
| 1149 |
} |
| 1150 |
in_data = kmalloc(in_data_length, GFP_KERNEL); |
| 1151 |
if (!in_data) |
| 1152 |
return; |
| 1153 |
|
| 1154 |
retval = ni_usb_receive_bulk_msg(ni_priv, in_data, in_data_length, &bytes_read, 1000, 0); |
| 1155 |
if (retval || bytes_read != 12) { |
| 1156 |
dev_err(&usb_dev->dev, "receive_bulk_msg returned %i, bytes_read=%i\n", |
| 1157 |
retval, bytes_read); |
| 1158 |
kfree(in_data); |
| 1159 |
return; |
| 1160 |
} |
| 1161 |
ni_usb_parse_status_block(in_data, &status); |
| 1162 |
kfree(in_data); |
| 1163 |
ni_usb_soft_update_status(board, status.ibsta, 0); |
| 1164 |
} |
| 1165 |
|
| 1166 |
static void ni_usb_remote_enable(struct gpib_board *board, int enable) |
| 1167 |
{ |
| 1168 |
int retval; |
| 1169 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1170 |
struct usb_device *usb_dev; |
| 1171 |
struct ni_usb_register reg; |
| 1172 |
unsigned int ibsta; |
| 1173 |
|
| 1174 |
if (!ni_priv->bus_interface) |
| 1175 |
return; // -ENODEV; |
| 1176 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1177 |
reg.device = NIUSB_SUBDEV_TNT4882; |
| 1178 |
reg.address = nec7210_to_tnt4882_offset(AUXMR); |
| 1179 |
if (enable) |
| 1180 |
reg.value = AUX_SREN; |
| 1181 |
else |
| 1182 |
reg.value = AUX_CREN; |
| 1183 |
retval = ni_usb_write_registers(ni_priv, ®, 1, &ibsta); |
| 1184 |
if (retval < 0) { |
| 1185 |
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval); |
| 1186 |
return; //retval; |
| 1187 |
} |
| 1188 |
ni_priv->ren_state = enable; |
| 1189 |
ni_usb_soft_update_status(board, ibsta, 0); |
| 1190 |
return;// 0; |
| 1191 |
} |
| 1192 |
|
| 1193 |
static int ni_usb_enable_eos(struct gpib_board *board, u8 eos_byte, int compare_8_bits) |
| 1194 |
{ |
| 1195 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1196 |
|
| 1197 |
ni_priv->eos_char = eos_byte; |
| 1198 |
ni_priv->eos_mode |= REOS; |
| 1199 |
if (compare_8_bits) |
| 1200 |
ni_priv->eos_mode |= BIN; |
| 1201 |
else |
| 1202 |
ni_priv->eos_mode &= ~BIN; |
| 1203 |
return 0; |
| 1204 |
} |
| 1205 |
|
| 1206 |
static void ni_usb_disable_eos(struct gpib_board *board) |
| 1207 |
{ |
| 1208 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1209 |
/* |
| 1210 |
* adapter gets unhappy if you don't zero all the bits |
| 1211 |
* for the eos mode and eos char (returns error 4 on reads). |
| 1212 |
*/ |
| 1213 |
ni_priv->eos_mode = 0; |
| 1214 |
ni_priv->eos_char = 0; |
| 1215 |
} |
| 1216 |
|
| 1217 |
static unsigned int ni_usb_update_status(struct gpib_board *board, unsigned int clear_mask) |
| 1218 |
{ |
| 1219 |
int retval; |
| 1220 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1221 |
struct usb_device *usb_dev; |
| 1222 |
static const int buffer_length = 8; |
| 1223 |
u8 *buffer; |
| 1224 |
struct ni_usb_status_block status; |
| 1225 |
|
| 1226 |
if (!ni_priv->bus_interface) |
| 1227 |
return -ENODEV; |
| 1228 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1229 |
buffer = kmalloc(buffer_length, GFP_KERNEL); |
| 1230 |
if (!buffer) |
| 1231 |
return board->status; |
| 1232 |
|
| 1233 |
retval = ni_usb_receive_control_msg(ni_priv, NI_USB_WAIT_REQUEST, USB_DIR_IN | |
| 1234 |
USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 1235 |
0x200, 0x0, buffer, buffer_length, 1000); |
| 1236 |
if (retval != buffer_length) { |
| 1237 |
dev_err(&usb_dev->dev, "usb_control_msg returned %i\n", retval); |
| 1238 |
kfree(buffer); |
| 1239 |
return board->status; |
| 1240 |
} |
| 1241 |
ni_usb_parse_status_block(buffer, &status); |
| 1242 |
kfree(buffer); |
| 1243 |
ni_usb_soft_update_status(board, status.ibsta, clear_mask); |
| 1244 |
return board->status; |
| 1245 |
} |
| 1246 |
|
| 1247 |
// tells ni-usb to immediately stop an ongoing i/o operation |
| 1248 |
static void ni_usb_stop(struct ni_usb_priv *ni_priv) |
| 1249 |
{ |
| 1250 |
struct usb_device *usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1251 |
int retval; |
| 1252 |
static const int buffer_length = 8; |
| 1253 |
u8 *buffer; |
| 1254 |
struct ni_usb_status_block status; |
| 1255 |
|
| 1256 |
buffer = kmalloc(buffer_length, GFP_KERNEL); |
| 1257 |
if (!buffer) |
| 1258 |
return; |
| 1259 |
|
| 1260 |
retval = ni_usb_receive_control_msg(ni_priv, NI_USB_STOP_REQUEST, USB_DIR_IN | |
| 1261 |
USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 1262 |
0x0, 0x0, buffer, buffer_length, 1000); |
| 1263 |
if (retval != buffer_length) { |
| 1264 |
dev_err(&usb_dev->dev, "usb_control_msg returned %i\n", retval); |
| 1265 |
kfree(buffer); |
| 1266 |
return; |
| 1267 |
} |
| 1268 |
ni_usb_parse_status_block(buffer, &status); |
| 1269 |
kfree(buffer); |
| 1270 |
} |
| 1271 |
|
| 1272 |
static int ni_usb_primary_address(struct gpib_board *board, unsigned int address) |
| 1273 |
{ |
| 1274 |
int retval; |
| 1275 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1276 |
struct usb_device *usb_dev; |
| 1277 |
int i = 0; |
| 1278 |
struct ni_usb_register writes[2]; |
| 1279 |
unsigned int ibsta; |
| 1280 |
|
| 1281 |
if (!ni_priv->bus_interface) |
| 1282 |
return -ENODEV; |
| 1283 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1284 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1285 |
writes[i].address = nec7210_to_tnt4882_offset(ADR); |
| 1286 |
writes[i].value = address; |
| 1287 |
i++; |
| 1288 |
writes[i].device = NIUSB_SUBDEV_UNKNOWN2; |
| 1289 |
writes[i].address = 0x0; |
| 1290 |
writes[i].value = address; |
| 1291 |
i++; |
| 1292 |
retval = ni_usb_write_registers(ni_priv, writes, i, &ibsta); |
| 1293 |
if (retval < 0) { |
| 1294 |
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval); |
| 1295 |
return retval; |
| 1296 |
} |
| 1297 |
ni_usb_soft_update_status(board, ibsta, 0); |
| 1298 |
return 0; |
| 1299 |
} |
| 1300 |
|
| 1301 |
static int ni_usb_write_sad(struct ni_usb_register *writes, int address, int enable) |
| 1302 |
{ |
| 1303 |
unsigned int adr_bits, admr_bits; |
| 1304 |
int i = 0; |
| 1305 |
|
| 1306 |
adr_bits = HR_ARS; |
| 1307 |
admr_bits = HR_TRM0 | HR_TRM1; |
| 1308 |
if (enable) { |
| 1309 |
adr_bits |= address; |
| 1310 |
admr_bits |= HR_ADM1; |
| 1311 |
} else { |
| 1312 |
adr_bits |= HR_DT | HR_DL; |
| 1313 |
admr_bits |= HR_ADM0; |
| 1314 |
} |
| 1315 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1316 |
writes[i].address = nec7210_to_tnt4882_offset(ADR); |
| 1317 |
writes[i].value = adr_bits; |
| 1318 |
i++; |
| 1319 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1320 |
writes[i].address = nec7210_to_tnt4882_offset(ADMR); |
| 1321 |
writes[i].value = admr_bits; |
| 1322 |
i++; |
| 1323 |
writes[i].device = NIUSB_SUBDEV_UNKNOWN2; |
| 1324 |
writes[i].address = 0x1; |
| 1325 |
writes[i].value = enable ? MSA(address) : 0x0; |
| 1326 |
i++; |
| 1327 |
return i; |
| 1328 |
} |
| 1329 |
|
| 1330 |
static int ni_usb_secondary_address(struct gpib_board *board, unsigned int address, int enable) |
| 1331 |
{ |
| 1332 |
int retval; |
| 1333 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1334 |
struct usb_device *usb_dev; |
| 1335 |
int i = 0; |
| 1336 |
struct ni_usb_register writes[3]; |
| 1337 |
unsigned int ibsta; |
| 1338 |
|
| 1339 |
if (!ni_priv->bus_interface) |
| 1340 |
return -ENODEV; |
| 1341 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1342 |
i += ni_usb_write_sad(writes, address, enable); |
| 1343 |
retval = ni_usb_write_registers(ni_priv, writes, i, &ibsta); |
| 1344 |
if (retval < 0) { |
| 1345 |
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval); |
| 1346 |
return retval; |
| 1347 |
} |
| 1348 |
ni_usb_soft_update_status(board, ibsta, 0); |
| 1349 |
return 0; |
| 1350 |
} |
| 1351 |
|
| 1352 |
static int ni_usb_parallel_poll(struct gpib_board *board, u8 *result) |
| 1353 |
{ |
| 1354 |
int retval; |
| 1355 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1356 |
struct usb_device *usb_dev; |
| 1357 |
u8 *out_data, *in_data; |
| 1358 |
static const int out_data_length = 0x10; |
| 1359 |
static const int in_data_length = 0x20; |
| 1360 |
int bytes_written = 0, bytes_read = 0; |
| 1361 |
int i = 0; |
| 1362 |
int j = 0; |
| 1363 |
struct ni_usb_status_block status; |
| 1364 |
|
| 1365 |
if (!ni_priv->bus_interface) |
| 1366 |
return -ENODEV; |
| 1367 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1368 |
out_data = kmalloc(out_data_length, GFP_KERNEL); |
| 1369 |
if (!out_data) |
| 1370 |
return -ENOMEM; |
| 1371 |
|
| 1372 |
out_data[i++] = NIUSB_IBRPP_ID; |
| 1373 |
out_data[i++] = 0xf0; // FIXME: this should be the parallel poll timeout code |
| 1374 |
out_data[i++] = 0x0; |
| 1375 |
out_data[i++] = 0x0; |
| 1376 |
i += ni_usb_bulk_termination(&out_data[i]); |
| 1377 |
/*FIXME: 1000 should use parallel poll timeout (not supported yet)*/ |
| 1378 |
retval = ni_usb_send_bulk_msg(ni_priv, out_data, i, &bytes_written, 1000); |
| 1379 |
|
| 1380 |
kfree(out_data); |
| 1381 |
if (retval || bytes_written != i) { |
| 1382 |
dev_err(&usb_dev->dev, "send_bulk_msg returned %i, bytes_written=%i, i=%i\n", |
| 1383 |
retval, bytes_written, i); |
| 1384 |
return retval; |
| 1385 |
} |
| 1386 |
in_data = kmalloc(in_data_length, GFP_KERNEL); |
| 1387 |
if (!in_data) |
| 1388 |
return -ENOMEM; |
| 1389 |
|
| 1390 |
/*FIXME: should use parallel poll timeout (not supported yet)*/ |
| 1391 |
retval = ni_usb_receive_bulk_msg(ni_priv, in_data, in_data_length, |
| 1392 |
&bytes_read, 1000, 1); |
| 1393 |
|
| 1394 |
if (retval && retval != -ERESTARTSYS) { |
| 1395 |
dev_err(&usb_dev->dev, "receive_bulk_msg returned %i, bytes_read=%i\n", |
| 1396 |
retval, bytes_read); |
| 1397 |
kfree(in_data); |
| 1398 |
return retval; |
| 1399 |
} |
| 1400 |
j += ni_usb_parse_status_block(in_data, &status); |
| 1401 |
*result = in_data[j++]; |
| 1402 |
kfree(in_data); |
| 1403 |
ni_usb_soft_update_status(board, status.ibsta, 0); |
| 1404 |
return retval; |
| 1405 |
} |
| 1406 |
|
| 1407 |
static void ni_usb_parallel_poll_configure(struct gpib_board *board, u8 config) |
| 1408 |
{ |
| 1409 |
int retval; |
| 1410 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1411 |
struct usb_device *usb_dev; |
| 1412 |
int i = 0; |
| 1413 |
struct ni_usb_register writes[1]; |
| 1414 |
unsigned int ibsta; |
| 1415 |
|
| 1416 |
if (!ni_priv->bus_interface) |
| 1417 |
return; // -ENODEV; |
| 1418 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1419 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1420 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1421 |
writes[i].value = PPR | config; |
| 1422 |
i++; |
| 1423 |
retval = ni_usb_write_registers(ni_priv, writes, i, &ibsta); |
| 1424 |
if (retval < 0) { |
| 1425 |
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval); |
| 1426 |
return;// retval; |
| 1427 |
} |
| 1428 |
ni_usb_soft_update_status(board, ibsta, 0); |
| 1429 |
return;// 0; |
| 1430 |
} |
| 1431 |
|
| 1432 |
static void ni_usb_parallel_poll_response(struct gpib_board *board, int ist) |
| 1433 |
{ |
| 1434 |
int retval; |
| 1435 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1436 |
struct usb_device *usb_dev; |
| 1437 |
int i = 0; |
| 1438 |
struct ni_usb_register writes[1]; |
| 1439 |
unsigned int ibsta; |
| 1440 |
|
| 1441 |
if (!ni_priv->bus_interface) |
| 1442 |
return; // -ENODEV; |
| 1443 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1444 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1445 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1446 |
if (ist) |
| 1447 |
writes[i].value = AUX_SPPF; |
| 1448 |
else |
| 1449 |
writes[i].value = AUX_CPPF; |
| 1450 |
i++; |
| 1451 |
retval = ni_usb_write_registers(ni_priv, writes, i, &ibsta); |
| 1452 |
if (retval < 0) { |
| 1453 |
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval); |
| 1454 |
return;// retval; |
| 1455 |
} |
| 1456 |
ni_usb_soft_update_status(board, ibsta, 0); |
| 1457 |
return;// 0; |
| 1458 |
} |
| 1459 |
|
| 1460 |
static void ni_usb_serial_poll_response(struct gpib_board *board, u8 status) |
| 1461 |
{ |
| 1462 |
int retval; |
| 1463 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1464 |
struct usb_device *usb_dev; |
| 1465 |
int i = 0; |
| 1466 |
struct ni_usb_register writes[1]; |
| 1467 |
unsigned int ibsta; |
| 1468 |
|
| 1469 |
if (!ni_priv->bus_interface) |
| 1470 |
return; // -ENODEV; |
| 1471 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1472 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1473 |
writes[i].address = nec7210_to_tnt4882_offset(SPMR); |
| 1474 |
writes[i].value = status; |
| 1475 |
i++; |
| 1476 |
retval = ni_usb_write_registers(ni_priv, writes, i, &ibsta); |
| 1477 |
if (retval < 0) { |
| 1478 |
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval); |
| 1479 |
return;// retval; |
| 1480 |
} |
| 1481 |
ni_usb_soft_update_status(board, ibsta, 0); |
| 1482 |
return;// 0; |
| 1483 |
} |
| 1484 |
|
| 1485 |
static u8 ni_usb_serial_poll_status(struct gpib_board *board) |
| 1486 |
{ |
| 1487 |
return 0; |
| 1488 |
} |
| 1489 |
|
| 1490 |
static void ni_usb_return_to_local(struct gpib_board *board) |
| 1491 |
{ |
| 1492 |
int retval; |
| 1493 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1494 |
struct usb_device *usb_dev; |
| 1495 |
int i = 0; |
| 1496 |
struct ni_usb_register writes[1]; |
| 1497 |
unsigned int ibsta; |
| 1498 |
|
| 1499 |
if (!ni_priv->bus_interface) |
| 1500 |
return; // -ENODEV; |
| 1501 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1502 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1503 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1504 |
writes[i].value = AUX_RTL; |
| 1505 |
i++; |
| 1506 |
retval = ni_usb_write_registers(ni_priv, writes, i, &ibsta); |
| 1507 |
if (retval < 0) { |
| 1508 |
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval); |
| 1509 |
return;// retval; |
| 1510 |
} |
| 1511 |
ni_usb_soft_update_status(board, ibsta, 0); |
| 1512 |
return;// 0; |
| 1513 |
} |
| 1514 |
|
| 1515 |
static int ni_usb_line_status(const struct gpib_board *board) |
| 1516 |
{ |
| 1517 |
int retval; |
| 1518 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1519 |
struct usb_device *usb_dev; |
| 1520 |
u8 *out_data, *in_data; |
| 1521 |
static const int out_data_length = 0x20; |
| 1522 |
static const int in_data_length = 0x20; |
| 1523 |
int bytes_written = 0, bytes_read = 0; |
| 1524 |
int i = 0; |
| 1525 |
unsigned int bsr_bits; |
| 1526 |
int line_status = VALID_ALL; |
| 1527 |
// NI windows driver reads 0xd(HSSEL), 0xc (ARD0), 0x1f (BSR) |
| 1528 |
|
| 1529 |
if (!ni_priv->bus_interface) |
| 1530 |
return -ENODEV; |
| 1531 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1532 |
out_data = kmalloc(out_data_length, GFP_KERNEL); |
| 1533 |
if (!out_data) |
| 1534 |
return -ENOMEM; |
| 1535 |
|
| 1536 |
/* line status gets called during ibwait */ |
| 1537 |
retval = mutex_trylock(&ni_priv->addressed_transfer_lock); |
| 1538 |
|
| 1539 |
if (retval == 0) { |
| 1540 |
kfree(out_data); |
| 1541 |
return -EBUSY; |
| 1542 |
} |
| 1543 |
i += ni_usb_bulk_register_read_header(&out_data[i], 1); |
| 1544 |
i += ni_usb_bulk_register_read(&out_data[i], NIUSB_SUBDEV_TNT4882, BSR); |
| 1545 |
while (i % 4) |
| 1546 |
out_data[i++] = 0x0; |
| 1547 |
i += ni_usb_bulk_termination(&out_data[i]); |
| 1548 |
retval = ni_usb_nonblocking_send_bulk_msg(ni_priv, out_data, i, &bytes_written, 1000); |
| 1549 |
kfree(out_data); |
| 1550 |
if (retval || bytes_written != i) { |
| 1551 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 1552 |
if (retval != -EAGAIN) |
| 1553 |
dev_err(&usb_dev->dev, "send_bulk_msg returned %i, bytes_written=%i, i=%i\n", |
| 1554 |
retval, bytes_written, i); |
| 1555 |
return retval; |
| 1556 |
} |
| 1557 |
|
| 1558 |
in_data = kmalloc(in_data_length, GFP_KERNEL); |
| 1559 |
if (!in_data) { |
| 1560 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 1561 |
return -ENOMEM; |
| 1562 |
} |
| 1563 |
retval = ni_usb_nonblocking_receive_bulk_msg(ni_priv, in_data, in_data_length, |
| 1564 |
&bytes_read, 1000, 0); |
| 1565 |
|
| 1566 |
mutex_unlock(&ni_priv->addressed_transfer_lock); |
| 1567 |
|
| 1568 |
if (retval) { |
| 1569 |
if (retval != -EAGAIN) |
| 1570 |
dev_err(&usb_dev->dev, "receive_bulk_msg returned %i, bytes_read=%i\n", |
| 1571 |
retval, bytes_read); |
| 1572 |
kfree(in_data); |
| 1573 |
return retval; |
| 1574 |
} |
| 1575 |
|
| 1576 |
ni_usb_parse_register_read_block(in_data, &bsr_bits, 1); |
| 1577 |
kfree(in_data); |
| 1578 |
if (bsr_bits & BCSR_REN_BIT) |
| 1579 |
line_status |= BUS_REN; |
| 1580 |
if (bsr_bits & BCSR_IFC_BIT) |
| 1581 |
line_status |= BUS_IFC; |
| 1582 |
if (bsr_bits & BCSR_SRQ_BIT) |
| 1583 |
line_status |= BUS_SRQ; |
| 1584 |
if (bsr_bits & BCSR_EOI_BIT) |
| 1585 |
line_status |= BUS_EOI; |
| 1586 |
if (bsr_bits & BCSR_NRFD_BIT) |
| 1587 |
line_status |= BUS_NRFD; |
| 1588 |
if (bsr_bits & BCSR_NDAC_BIT) |
| 1589 |
line_status |= BUS_NDAC; |
| 1590 |
if (bsr_bits & BCSR_DAV_BIT) |
| 1591 |
line_status |= BUS_DAV; |
| 1592 |
if (bsr_bits & BCSR_ATN_BIT) |
| 1593 |
line_status |= BUS_ATN; |
| 1594 |
return line_status; |
| 1595 |
} |
| 1596 |
|
| 1597 |
static int ni_usb_setup_t1_delay(struct ni_usb_register *reg, unsigned int nano_sec, |
| 1598 |
unsigned int *actual_ns) |
| 1599 |
{ |
| 1600 |
int i = 0; |
| 1601 |
|
| 1602 |
*actual_ns = 2000; |
| 1603 |
|
| 1604 |
reg[i].device = NIUSB_SUBDEV_TNT4882; |
| 1605 |
reg[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1606 |
if (nano_sec <= 1100) { |
| 1607 |
reg[i].value = AUXRI | USTD | SISB; |
| 1608 |
*actual_ns = 1100; |
| 1609 |
} else { |
| 1610 |
reg[i].value = AUXRI | SISB; |
| 1611 |
} |
| 1612 |
i++; |
| 1613 |
reg[i].device = NIUSB_SUBDEV_TNT4882; |
| 1614 |
reg[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1615 |
if (nano_sec <= 500) { |
| 1616 |
reg[i].value = AUXRB | HR_TRI; |
| 1617 |
*actual_ns = 500; |
| 1618 |
} else { |
| 1619 |
reg[i].value = AUXRB; |
| 1620 |
} |
| 1621 |
i++; |
| 1622 |
reg[i].device = NIUSB_SUBDEV_TNT4882; |
| 1623 |
reg[i].address = KEYREG; |
| 1624 |
if (nano_sec <= 350) { |
| 1625 |
reg[i].value = MSTD; |
| 1626 |
*actual_ns = 350; |
| 1627 |
} else { |
| 1628 |
reg[i].value = 0x0; |
| 1629 |
} |
| 1630 |
i++; |
| 1631 |
return i; |
| 1632 |
} |
| 1633 |
|
| 1634 |
static int ni_usb_t1_delay(struct gpib_board *board, unsigned int nano_sec) |
| 1635 |
{ |
| 1636 |
int retval; |
| 1637 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1638 |
struct usb_device *usb_dev; |
| 1639 |
struct ni_usb_register writes[3]; |
| 1640 |
unsigned int ibsta; |
| 1641 |
unsigned int actual_ns; |
| 1642 |
int i; |
| 1643 |
|
| 1644 |
if (!ni_priv->bus_interface) |
| 1645 |
return -ENODEV; |
| 1646 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1647 |
i = ni_usb_setup_t1_delay(writes, nano_sec, &actual_ns); |
| 1648 |
retval = ni_usb_write_registers(ni_priv, writes, i, &ibsta); |
| 1649 |
if (retval < 0) { |
| 1650 |
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval); |
| 1651 |
return retval; |
| 1652 |
} |
| 1653 |
board->t1_nano_sec = actual_ns; |
| 1654 |
ni_usb_soft_update_status(board, ibsta, 0); |
| 1655 |
return actual_ns; |
| 1656 |
} |
| 1657 |
|
| 1658 |
static int ni_usb_allocate_private(struct gpib_board *board) |
| 1659 |
{ |
| 1660 |
struct ni_usb_priv *ni_priv; |
| 1661 |
|
| 1662 |
board->private_data = kzalloc_obj(struct ni_usb_priv); |
| 1663 |
if (!board->private_data) |
| 1664 |
return -ENOMEM; |
| 1665 |
ni_priv = board->private_data; |
| 1666 |
mutex_init(&ni_priv->bulk_transfer_lock); |
| 1667 |
mutex_init(&ni_priv->control_transfer_lock); |
| 1668 |
mutex_init(&ni_priv->interrupt_transfer_lock); |
| 1669 |
mutex_init(&ni_priv->addressed_transfer_lock); |
| 1670 |
return 0; |
| 1671 |
} |
| 1672 |
|
| 1673 |
static void ni_usb_free_private(struct ni_usb_priv *ni_priv) |
| 1674 |
{ |
| 1675 |
usb_free_urb(ni_priv->interrupt_urb); |
| 1676 |
kfree(ni_priv); |
| 1677 |
} |
| 1678 |
|
| 1679 |
#define NUM_INIT_WRITES 26 |
| 1680 |
static int ni_usb_setup_init(struct gpib_board *board, struct ni_usb_register *writes) |
| 1681 |
{ |
| 1682 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1683 |
struct usb_device *usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1684 |
unsigned int mask, actual_ns; |
| 1685 |
int i = 0; |
| 1686 |
|
| 1687 |
writes[i].device = NIUSB_SUBDEV_UNKNOWN3; |
| 1688 |
writes[i].address = 0x10; |
| 1689 |
writes[i].value = 0x0; |
| 1690 |
i++; |
| 1691 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1692 |
writes[i].address = CMDR; |
| 1693 |
writes[i].value = SOFT_RESET; |
| 1694 |
i++; |
| 1695 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1696 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1697 |
mask = AUXRA | HR_HLDA; |
| 1698 |
if (ni_priv->eos_mode & BIN) |
| 1699 |
mask |= HR_BIN; |
| 1700 |
writes[i].value = mask; |
| 1701 |
i++; |
| 1702 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1703 |
writes[i].address = AUXCR; |
| 1704 |
writes[i].value = mask; |
| 1705 |
i++; |
| 1706 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1707 |
writes[i].address = HSSEL; |
| 1708 |
writes[i].value = TNT_ONE_CHIP_BIT; |
| 1709 |
i++; |
| 1710 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1711 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1712 |
writes[i].value = AUX_CR; |
| 1713 |
i++; |
| 1714 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1715 |
writes[i].address = IMR0; |
| 1716 |
writes[i].value = TNT_IMR0_ALWAYS_BITS; |
| 1717 |
i++; |
| 1718 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1719 |
writes[i].address = nec7210_to_tnt4882_offset(IMR1); |
| 1720 |
writes[i].value = 0x0; |
| 1721 |
i++; |
| 1722 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1723 |
writes[i].address = nec7210_to_tnt4882_offset(IMR2); |
| 1724 |
writes[i].value = 0x0; |
| 1725 |
i++; |
| 1726 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1727 |
writes[i].address = IMR3; |
| 1728 |
writes[i].value = 0x0; |
| 1729 |
i++; |
| 1730 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1731 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1732 |
writes[i].value = AUX_HLDI; |
| 1733 |
i++; |
| 1734 |
|
| 1735 |
i += ni_usb_setup_t1_delay(&writes[i], board->t1_nano_sec, &actual_ns); |
| 1736 |
|
| 1737 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1738 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1739 |
writes[i].value = AUXRG | NTNL_BIT; |
| 1740 |
i++; |
| 1741 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1742 |
writes[i].address = CMDR; |
| 1743 |
if (board->master) |
| 1744 |
mask = SETSC; // set system controller |
| 1745 |
else |
| 1746 |
mask = CLRSC; // clear system controller |
| 1747 |
writes[i].value = mask; |
| 1748 |
i++; |
| 1749 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1750 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1751 |
writes[i].value = AUX_CIFC; |
| 1752 |
i++; |
| 1753 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1754 |
writes[i].address = nec7210_to_tnt4882_offset(ADR); |
| 1755 |
writes[i].value = board->pad; |
| 1756 |
i++; |
| 1757 |
writes[i].device = NIUSB_SUBDEV_UNKNOWN2; |
| 1758 |
writes[i].address = 0x0; |
| 1759 |
writes[i].value = board->pad; |
| 1760 |
i++; |
| 1761 |
|
| 1762 |
i += ni_usb_write_sad(&writes[i], board->sad, board->sad >= 0); |
| 1763 |
|
| 1764 |
writes[i].device = NIUSB_SUBDEV_UNKNOWN2; |
| 1765 |
writes[i].address = 0x2; // could this be a timeout ? |
| 1766 |
writes[i].value = 0xfd; |
| 1767 |
i++; |
| 1768 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1769 |
writes[i].address = 0xf; // undocumented address |
| 1770 |
writes[i].value = 0x11; |
| 1771 |
i++; |
| 1772 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1773 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1774 |
writes[i].value = AUX_PON; |
| 1775 |
i++; |
| 1776 |
writes[i].device = NIUSB_SUBDEV_TNT4882; |
| 1777 |
writes[i].address = nec7210_to_tnt4882_offset(AUXMR); |
| 1778 |
writes[i].value = AUX_CPPF; |
| 1779 |
i++; |
| 1780 |
if (i > NUM_INIT_WRITES) { |
| 1781 |
dev_err(&usb_dev->dev, "bug!, buffer overrun, i=%i\n", i); |
| 1782 |
return -EINVAL; |
| 1783 |
} |
| 1784 |
return i; |
| 1785 |
} |
| 1786 |
|
| 1787 |
static int ni_usb_init(struct gpib_board *board) |
| 1788 |
{ |
| 1789 |
int retval; |
| 1790 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1791 |
struct usb_device *usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1792 |
struct ni_usb_register *writes; |
| 1793 |
unsigned int ibsta; |
| 1794 |
int writes_len; |
| 1795 |
|
| 1796 |
writes = kmalloc_objs(*writes, NUM_INIT_WRITES); |
| 1797 |
if (!writes) |
| 1798 |
return -ENOMEM; |
| 1799 |
|
| 1800 |
writes_len = ni_usb_setup_init(board, writes); |
| 1801 |
if (writes_len < 0) { |
| 1802 |
kfree(writes); |
| 1803 |
return writes_len; |
| 1804 |
} |
| 1805 |
|
| 1806 |
retval = ni_usb_write_registers(ni_priv, writes, writes_len, &ibsta); |
| 1807 |
kfree(writes); |
| 1808 |
if (retval) { |
| 1809 |
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval); |
| 1810 |
return retval; |
| 1811 |
} |
| 1812 |
ni_usb_soft_update_status(board, ibsta, 0); |
| 1813 |
return 0; |
| 1814 |
} |
| 1815 |
|
| 1816 |
static void ni_usb_interrupt_complete(struct urb *urb) |
| 1817 |
{ |
| 1818 |
struct gpib_board *board = urb->context; |
| 1819 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1820 |
struct usb_device *usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1821 |
int retval; |
| 1822 |
struct ni_usb_status_block status; |
| 1823 |
unsigned long flags; |
| 1824 |
|
| 1825 |
switch (urb->status) { |
| 1826 |
/* success */ |
| 1827 |
case 0: |
| 1828 |
break; |
| 1829 |
/* unlinked, don't resubmit */ |
| 1830 |
case -ECONNRESET: |
| 1831 |
case -ENOENT: |
| 1832 |
case -ESHUTDOWN: |
| 1833 |
return; |
| 1834 |
default: /* other error, resubmit */ |
| 1835 |
retval = usb_submit_urb(ni_priv->interrupt_urb, GFP_ATOMIC); |
| 1836 |
if (retval) |
| 1837 |
dev_err(&usb_dev->dev, "failed to resubmit interrupt urb\n"); |
| 1838 |
return; |
| 1839 |
} |
| 1840 |
|
| 1841 |
ni_usb_parse_status_block(urb->transfer_buffer, &status); |
| 1842 |
|
| 1843 |
spin_lock_irqsave(&board->spinlock, flags); |
| 1844 |
ni_priv->monitored_ibsta_bits &= ~status.ibsta; |
| 1845 |
spin_unlock_irqrestore(&board->spinlock, flags); |
| 1846 |
|
| 1847 |
wake_up_interruptible(&board->wait); |
| 1848 |
|
| 1849 |
retval = usb_submit_urb(ni_priv->interrupt_urb, GFP_ATOMIC); |
| 1850 |
if (retval) |
| 1851 |
dev_err(&usb_dev->dev, "failed to resubmit interrupt urb\n"); |
| 1852 |
} |
| 1853 |
|
| 1854 |
static int ni_usb_set_interrupt_monitor(struct gpib_board *board, unsigned int monitored_bits) |
| 1855 |
{ |
| 1856 |
int retval; |
| 1857 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1858 |
struct usb_device *usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1859 |
static const int buffer_length = 8; |
| 1860 |
u8 *buffer; |
| 1861 |
struct ni_usb_status_block status; |
| 1862 |
unsigned long flags; |
| 1863 |
|
| 1864 |
buffer = kmalloc(buffer_length, GFP_KERNEL); |
| 1865 |
if (!buffer) |
| 1866 |
return -ENOMEM; |
| 1867 |
|
| 1868 |
spin_lock_irqsave(&board->spinlock, flags); |
| 1869 |
ni_priv->monitored_ibsta_bits = ni_usb_ibsta_monitor_mask & monitored_bits; |
| 1870 |
spin_unlock_irqrestore(&board->spinlock, flags); |
| 1871 |
retval = ni_usb_receive_control_msg(ni_priv, NI_USB_WAIT_REQUEST, USB_DIR_IN | |
| 1872 |
USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 1873 |
0x300, ni_usb_ibsta_monitor_mask & monitored_bits, |
| 1874 |
buffer, buffer_length, 1000); |
| 1875 |
if (retval != buffer_length) { |
| 1876 |
dev_err(&usb_dev->dev, "usb_control_msg returned %i\n", retval); |
| 1877 |
kfree(buffer); |
| 1878 |
return -1; |
| 1879 |
} |
| 1880 |
ni_usb_parse_status_block(buffer, &status); |
| 1881 |
kfree(buffer); |
| 1882 |
return 0; |
| 1883 |
} |
| 1884 |
|
| 1885 |
static int ni_usb_setup_urbs(struct gpib_board *board) |
| 1886 |
{ |
| 1887 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 1888 |
struct usb_device *usb_dev; |
| 1889 |
int int_pipe; |
| 1890 |
int retval; |
| 1891 |
|
| 1892 |
if (ni_priv->interrupt_in_endpoint < 0) |
| 1893 |
return 0; |
| 1894 |
|
| 1895 |
mutex_lock(&ni_priv->interrupt_transfer_lock); |
| 1896 |
if (!ni_priv->bus_interface) { |
| 1897 |
mutex_unlock(&ni_priv->interrupt_transfer_lock); |
| 1898 |
return -ENODEV; |
| 1899 |
} |
| 1900 |
ni_priv->interrupt_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1901 |
if (!ni_priv->interrupt_urb) { |
| 1902 |
mutex_unlock(&ni_priv->interrupt_transfer_lock); |
| 1903 |
return -ENOMEM; |
| 1904 |
} |
| 1905 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1906 |
int_pipe = usb_rcvintpipe(usb_dev, ni_priv->interrupt_in_endpoint); |
| 1907 |
usb_fill_int_urb(ni_priv->interrupt_urb, usb_dev, int_pipe, ni_priv->interrupt_buffer, |
| 1908 |
sizeof(ni_priv->interrupt_buffer), &ni_usb_interrupt_complete, board, 1); |
| 1909 |
retval = usb_submit_urb(ni_priv->interrupt_urb, GFP_KERNEL); |
| 1910 |
mutex_unlock(&ni_priv->interrupt_transfer_lock); |
| 1911 |
if (retval) { |
| 1912 |
dev_err(&usb_dev->dev, "failed to submit first interrupt urb, retval=%i\n", retval); |
| 1913 |
return retval; |
| 1914 |
} |
| 1915 |
return 0; |
| 1916 |
} |
| 1917 |
|
| 1918 |
static void ni_usb_cleanup_urbs(struct ni_usb_priv *ni_priv) |
| 1919 |
{ |
| 1920 |
if (ni_priv && ni_priv->bus_interface) { |
| 1921 |
if (ni_priv->interrupt_urb) |
| 1922 |
usb_kill_urb(ni_priv->interrupt_urb); |
| 1923 |
if (ni_priv->bulk_urb) |
| 1924 |
usb_kill_urb(ni_priv->bulk_urb); |
| 1925 |
} |
| 1926 |
} |
| 1927 |
|
| 1928 |
static int ni_usb_b_read_serial_number(struct ni_usb_priv *ni_priv) |
| 1929 |
{ |
| 1930 |
struct usb_device *usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1931 |
int retval; |
| 1932 |
u8 *out_data; |
| 1933 |
u8 *in_data; |
| 1934 |
static const int out_data_length = 0x20; |
| 1935 |
static const int in_data_length = 0x20; |
| 1936 |
int bytes_written = 0, bytes_read = 0; |
| 1937 |
int i = 0; |
| 1938 |
static const int num_reads = 4; |
| 1939 |
unsigned int results[4]; |
| 1940 |
int j; |
| 1941 |
unsigned int serial_number; |
| 1942 |
|
| 1943 |
in_data = kmalloc(in_data_length, GFP_KERNEL); |
| 1944 |
if (!in_data) |
| 1945 |
return -ENOMEM; |
| 1946 |
|
| 1947 |
out_data = kmalloc(out_data_length, GFP_KERNEL); |
| 1948 |
if (!out_data) { |
| 1949 |
kfree(in_data); |
| 1950 |
return -ENOMEM; |
| 1951 |
} |
| 1952 |
i += ni_usb_bulk_register_read_header(&out_data[i], num_reads); |
| 1953 |
i += ni_usb_bulk_register_read(&out_data[i], NIUSB_SUBDEV_UNKNOWN3, SERIAL_NUMBER_1_REG); |
| 1954 |
i += ni_usb_bulk_register_read(&out_data[i], NIUSB_SUBDEV_UNKNOWN3, SERIAL_NUMBER_2_REG); |
| 1955 |
i += ni_usb_bulk_register_read(&out_data[i], NIUSB_SUBDEV_UNKNOWN3, SERIAL_NUMBER_3_REG); |
| 1956 |
i += ni_usb_bulk_register_read(&out_data[i], NIUSB_SUBDEV_UNKNOWN3, SERIAL_NUMBER_4_REG); |
| 1957 |
while (i % 4) |
| 1958 |
out_data[i++] = 0x0; |
| 1959 |
i += ni_usb_bulk_termination(&out_data[i]); |
| 1960 |
retval = ni_usb_send_bulk_msg(ni_priv, out_data, out_data_length, &bytes_written, 1000); |
| 1961 |
if (retval) { |
| 1962 |
dev_err(&usb_dev->dev, "send_bulk_msg returned %i, bytes_written=%i, i=%li\n", |
| 1963 |
retval, bytes_written, (long)out_data_length); |
| 1964 |
goto serial_out; |
| 1965 |
} |
| 1966 |
retval = ni_usb_receive_bulk_msg(ni_priv, in_data, in_data_length, &bytes_read, 1000, 0); |
| 1967 |
if (retval) { |
| 1968 |
dev_err(&usb_dev->dev, "receive_bulk_msg returned %i, bytes_read=%i\n", |
| 1969 |
retval, bytes_read); |
| 1970 |
ni_usb_dump_raw_block(in_data, bytes_read); |
| 1971 |
goto serial_out; |
| 1972 |
} |
| 1973 |
if (ARRAY_SIZE(results) < num_reads) { |
| 1974 |
dev_err(&usb_dev->dev, "serial number eetup bug\n"); |
| 1975 |
retval = -EINVAL; |
| 1976 |
goto serial_out; |
| 1977 |
} |
| 1978 |
ni_usb_parse_register_read_block(in_data, results, num_reads); |
| 1979 |
serial_number = 0; |
| 1980 |
for (j = 0; j < num_reads; ++j) |
| 1981 |
serial_number |= (results[j] & 0xff) << (8 * j); |
| 1982 |
dev_dbg(&usb_dev->dev, "board serial number is 0x%x\n", serial_number); |
| 1983 |
retval = 0; |
| 1984 |
serial_out: |
| 1985 |
kfree(in_data); |
| 1986 |
kfree(out_data); |
| 1987 |
return retval; |
| 1988 |
} |
| 1989 |
|
| 1990 |
static int ni_usb_hs_wait_for_ready(struct ni_usb_priv *ni_priv) |
| 1991 |
{ |
| 1992 |
struct usb_device *usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 1993 |
static const int buffer_size = 0x10; |
| 1994 |
static const int timeout = 50; |
| 1995 |
static const int msec_sleep_duration = 100; |
| 1996 |
int i; int retval; |
| 1997 |
int j; |
| 1998 |
int unexpected = 0; |
| 1999 |
unsigned int serial_number; |
| 2000 |
u8 *buffer; |
| 2001 |
|
| 2002 |
buffer = kmalloc(buffer_size, GFP_KERNEL); |
| 2003 |
if (!buffer) |
| 2004 |
return -ENOMEM; |
| 2005 |
|
| 2006 |
retval = ni_usb_receive_control_msg(ni_priv, NI_USB_SERIAL_NUMBER_REQUEST, |
| 2007 |
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 2008 |
0x0, 0x0, buffer, buffer_size, 1000); |
| 2009 |
if (retval < 0) { |
| 2010 |
dev_err(&usb_dev->dev, "usb_control_msg request 0x%x returned %i\n", |
| 2011 |
NI_USB_SERIAL_NUMBER_REQUEST, retval); |
| 2012 |
goto ready_out; |
| 2013 |
} |
| 2014 |
j = 0; |
| 2015 |
if (buffer[j] != NI_USB_SERIAL_NUMBER_REQUEST) { |
| 2016 |
dev_err(&usb_dev->dev, "unexpected data: buffer[%i]=0x%x, expected 0x%x\n", |
| 2017 |
j, (int)buffer[j], NI_USB_SERIAL_NUMBER_REQUEST); |
| 2018 |
unexpected = 1; |
| 2019 |
} |
| 2020 |
if (unexpected) |
| 2021 |
ni_usb_dump_raw_block(buffer, retval); |
| 2022 |
// NI-USB-HS+ pads the serial with 0x0 to make 16 bytes |
| 2023 |
if (retval != 5 && retval != 16) { |
| 2024 |
dev_err(&usb_dev->dev, "received unexpected number of bytes = %i, expected 5 or 16\n", |
| 2025 |
retval); |
| 2026 |
ni_usb_dump_raw_block(buffer, retval); |
| 2027 |
} |
| 2028 |
serial_number = 0; |
| 2029 |
serial_number |= buffer[++j]; |
| 2030 |
serial_number |= (buffer[++j] << 8); |
| 2031 |
serial_number |= (buffer[++j] << 16); |
| 2032 |
serial_number |= (buffer[++j] << 24); |
| 2033 |
dev_dbg(&usb_dev->dev, "board serial number is 0x%x\n", serial_number); |
| 2034 |
for (i = 0; i < timeout; ++i) { |
| 2035 |
int ready = 0; |
| 2036 |
|
| 2037 |
retval = ni_usb_receive_control_msg(ni_priv, NI_USB_POLL_READY_REQUEST, |
| 2038 |
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 2039 |
0x0, 0x0, buffer, buffer_size, 100); |
| 2040 |
if (retval < 0) { |
| 2041 |
dev_err(&usb_dev->dev, "usb_control_msg request 0x%x returned %i\n", |
| 2042 |
NI_USB_POLL_READY_REQUEST, retval); |
| 2043 |
goto ready_out; |
| 2044 |
} |
| 2045 |
j = 0; |
| 2046 |
unexpected = 0; |
| 2047 |
if (buffer[j] != NI_USB_POLL_READY_REQUEST) { // [0] |
| 2048 |
dev_err(&usb_dev->dev, "unexpected data: buffer[%i]=0x%x, expected 0x%x\n", |
| 2049 |
j, (int)buffer[j], NI_USB_POLL_READY_REQUEST); |
| 2050 |
unexpected = 1; |
| 2051 |
} |
| 2052 |
++j; |
| 2053 |
if (buffer[j] != 0x1 && buffer[j] != 0x0) { // [1] HS+ sends 0x0 |
| 2054 |
dev_err(&usb_dev->dev, "unexpected data: buffer[%i]=0x%x, expected 0x1 or 0x0\n", |
| 2055 |
j, (int)buffer[j]); |
| 2056 |
unexpected = 1; |
| 2057 |
} |
| 2058 |
if (buffer[++j] != 0x0) { // [2] |
| 2059 |
dev_err(&usb_dev->dev, "unexpected data: buffer[%i]=0x%x, expected 0x%x\n", |
| 2060 |
j, (int)buffer[j], 0x0); |
| 2061 |
unexpected = 1; |
| 2062 |
} |
| 2063 |
++j; |
| 2064 |
/* |
| 2065 |
* MC usb-488 (and sometimes NI-USB-HS?) sends 0x8 here; MC usb-488A sends 0x7 here |
| 2066 |
* NI-USB-HS+ sends 0x0 |
| 2067 |
*/ |
| 2068 |
if (buffer[j] != 0x1 && buffer[j] != 0x8 && buffer[j] != 0x7 && buffer[j] != 0x0) { |
| 2069 |
// [3] |
| 2070 |
dev_err(&usb_dev->dev, "unexpected data: buffer[%i]=0x%x, expected 0x0, 0x1, 0x7 or 0x8\n", |
| 2071 |
j, (int)buffer[j]); |
| 2072 |
unexpected = 1; |
| 2073 |
} |
| 2074 |
++j; |
| 2075 |
// NI-USB-HS+ sends 0 here |
| 2076 |
if (buffer[j] != 0x30 && buffer[j] != 0x0) { // [4] |
| 2077 |
dev_err(&usb_dev->dev, "unexpected data: buffer[%i]=0x%x, expected 0x0 or 0x30\n", |
| 2078 |
j, (int)buffer[j]); |
| 2079 |
unexpected = 1; |
| 2080 |
} |
| 2081 |
++j; |
| 2082 |
// MC usb-488 (and sometimes NI-USB-HS?) and NI-USB-HS+ sends 0x0 here |
| 2083 |
if (buffer[j] != 0x1 && buffer[j] != 0x0) { // [5] |
| 2084 |
dev_err(&usb_dev->dev, "unexpected data: buffer[%i]=0x%x, expected 0x1 or 0x0\n", |
| 2085 |
j, (int)buffer[j]); |
| 2086 |
unexpected = 1; |
| 2087 |
} |
| 2088 |
if (buffer[++j] != 0x0) { // [6] |
| 2089 |
ready = 1; |
| 2090 |
// NI-USB-HS+ sends 0xf or 0x19 here |
| 2091 |
if (buffer[j] != 0x2 && buffer[j] != 0xe && buffer[j] != 0xf && |
| 2092 |
buffer[j] != 0x16 && buffer[j] != 0x19) { |
| 2093 |
dev_err(&usb_dev->dev, "unexpected data: buffer[%i]=0x%x, expected 0x2, 0xe, 0xf, 0x16 or 0x19\n", |
| 2094 |
j, (int)buffer[j]); |
| 2095 |
unexpected = 1; |
| 2096 |
} |
| 2097 |
} |
| 2098 |
if (buffer[++j] != 0x0) { // [7] |
| 2099 |
ready = 1; |
| 2100 |
// MC usb-488 sends 0x5 here; MC usb-488A sends 0x6 here |
| 2101 |
if (buffer[j] != 0x3 && buffer[j] != 0x5 && buffer[j] != 0x6 && |
| 2102 |
buffer[j] != 0x8) { |
| 2103 |
dev_err(&usb_dev->dev, "unexpected data: buffer[%i]=0x%x, expected 0x3 or 0x5, 0x6 or 0x08\n", |
| 2104 |
j, (int)buffer[j]); |
| 2105 |
unexpected = 1; |
| 2106 |
} |
| 2107 |
} |
| 2108 |
++j; |
| 2109 |
if (buffer[j] != 0x0 && buffer[j] != 0x2) { // [8] MC usb-488 sends 0x2 here |
| 2110 |
dev_err(&usb_dev->dev, " unexpected data: buffer[%i]=0x%x, expected 0x0 or 0x2\n", |
| 2111 |
j, (int)buffer[j]); |
| 2112 |
unexpected = 1; |
| 2113 |
} |
| 2114 |
++j; |
| 2115 |
// MC usb-488A and NI-USB-HS sends 0x3 here; NI-USB-HS+ sends 0x30 here |
| 2116 |
if (buffer[j] != 0x0 && buffer[j] != 0x3 && buffer[j] != 0x30) { // [9] |
| 2117 |
dev_err(&usb_dev->dev, "unexpected data: buffer[%i]=0x%x, expected 0x0, 0x3 or 0x30\n", |
| 2118 |
j, (int)buffer[j]); |
| 2119 |
unexpected = 1; |
| 2120 |
} |
| 2121 |
if (buffer[++j] != 0x0) { // [10] MC usb-488 sends 0x7 here, new HS+ sends 0x59 |
| 2122 |
ready = 1; |
| 2123 |
if (buffer[j] != 0x96 && buffer[j] != 0x7 && buffer[j] != 0x6e && |
| 2124 |
buffer[j] != 0x59) { |
| 2125 |
dev_err(&usb_dev->dev, "unexpected data: buffer[%i]=0x%x, expected 0x96, 0x07, 0x6e or 0x59\n", |
| 2126 |
j, (int)buffer[j]); |
| 2127 |
unexpected = 1; |
| 2128 |
} |
| 2129 |
} |
| 2130 |
if (unexpected) |
| 2131 |
ni_usb_dump_raw_block(buffer, retval); |
| 2132 |
if (ready) |
| 2133 |
break; |
| 2134 |
retval = msleep_interruptible(msec_sleep_duration); |
| 2135 |
if (retval) { |
| 2136 |
retval = -ERESTARTSYS; |
| 2137 |
goto ready_out; |
| 2138 |
} |
| 2139 |
} |
| 2140 |
retval = 0; |
| 2141 |
|
| 2142 |
ready_out: |
| 2143 |
kfree(buffer); |
| 2144 |
dev_dbg(&usb_dev->dev, "exit retval=%d\n", retval); |
| 2145 |
return retval; |
| 2146 |
} |
| 2147 |
|
| 2148 |
/* |
| 2149 |
* This does some extra init for HS+ models, as observed on Windows. One of the |
| 2150 |
* control requests causes the LED to stop blinking. |
| 2151 |
* I'm not sure what the other 2 requests do. None of these requests are actually required |
| 2152 |
* for the adapter to work, maybe they do some init for the analyzer interface |
| 2153 |
* (which we don't use). |
| 2154 |
*/ |
| 2155 |
static int ni_usb_hs_plus_extra_init(struct ni_usb_priv *ni_priv) |
| 2156 |
{ |
| 2157 |
struct usb_device *usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 2158 |
int retval; |
| 2159 |
u8 *buffer; |
| 2160 |
static const int buffer_size = 16; |
| 2161 |
int transfer_size; |
| 2162 |
|
| 2163 |
buffer = kmalloc(buffer_size, GFP_KERNEL); |
| 2164 |
if (!buffer) |
| 2165 |
return -ENOMEM; |
| 2166 |
do { |
| 2167 |
transfer_size = 16; |
| 2168 |
|
| 2169 |
retval = ni_usb_receive_control_msg(ni_priv, NI_USB_HS_PLUS_0x48_REQUEST, |
| 2170 |
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 2171 |
0x0, 0x0, buffer, transfer_size, 1000); |
| 2172 |
if (retval < 0) { |
| 2173 |
dev_err(&usb_dev->dev, "usb_control_msg request 0x%x returned %i\n", |
| 2174 |
NI_USB_HS_PLUS_0x48_REQUEST, retval); |
| 2175 |
break; |
| 2176 |
} |
| 2177 |
// expected response data: 48 f3 30 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 2178 |
if (buffer[0] != NI_USB_HS_PLUS_0x48_REQUEST) |
| 2179 |
dev_err(&usb_dev->dev, "unexpected data: buffer[0]=0x%x, expected 0x%x\n", |
| 2180 |
(int)buffer[0], NI_USB_HS_PLUS_0x48_REQUEST); |
| 2181 |
|
| 2182 |
transfer_size = 2; |
| 2183 |
|
| 2184 |
retval = ni_usb_receive_control_msg(ni_priv, NI_USB_HS_PLUS_LED_REQUEST, |
| 2185 |
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 2186 |
0x1, 0x0, buffer, transfer_size, 1000); |
| 2187 |
if (retval < 0) { |
| 2188 |
dev_err(&usb_dev->dev, "usb_control_msg request 0x%x returned %i\n", |
| 2189 |
NI_USB_HS_PLUS_LED_REQUEST, retval); |
| 2190 |
break; |
| 2191 |
} |
| 2192 |
// expected response data: 4b 00 |
| 2193 |
if (buffer[0] != NI_USB_HS_PLUS_LED_REQUEST) |
| 2194 |
dev_err(&usb_dev->dev, "unexpected data: buffer[0]=0x%x, expected 0x%x\n", |
| 2195 |
(int)buffer[0], NI_USB_HS_PLUS_LED_REQUEST); |
| 2196 |
|
| 2197 |
transfer_size = 9; |
| 2198 |
|
| 2199 |
retval = ni_usb_receive_control_msg(ni_priv, NI_USB_HS_PLUS_0xf8_REQUEST, |
| 2200 |
USB_DIR_IN | USB_TYPE_VENDOR | |
| 2201 |
USB_RECIP_INTERFACE, |
| 2202 |
0x0, 0x1, buffer, transfer_size, 1000); |
| 2203 |
if (retval < 0) { |
| 2204 |
dev_err(&usb_dev->dev, "usb_control_msg request 0x%x returned %i\n", |
| 2205 |
NI_USB_HS_PLUS_0xf8_REQUEST, retval); |
| 2206 |
break; |
| 2207 |
} |
| 2208 |
// expected response data: f8 01 00 00 00 01 00 00 00 |
| 2209 |
if (buffer[0] != NI_USB_HS_PLUS_0xf8_REQUEST) |
| 2210 |
dev_err(&usb_dev->dev, "unexpected data: buffer[0]=0x%x, expected 0x%x\n", |
| 2211 |
(int)buffer[0], NI_USB_HS_PLUS_0xf8_REQUEST); |
| 2212 |
} while (0); |
| 2213 |
|
| 2214 |
// cleanup |
| 2215 |
kfree(buffer); |
| 2216 |
return retval; |
| 2217 |
} |
| 2218 |
|
| 2219 |
static inline int ni_usb_device_match(struct usb_interface *interface, |
| 2220 |
const struct gpib_board_config *config) |
| 2221 |
{ |
| 2222 |
if (gpib_match_device_path(&interface->dev, config->device_path) == 0) |
| 2223 |
return 0; |
| 2224 |
return 1; |
| 2225 |
} |
| 2226 |
|
| 2227 |
static int ni_usb_attach(struct gpib_board *board, const struct gpib_board_config *config) |
| 2228 |
{ |
| 2229 |
int retval; |
| 2230 |
int i, index; |
| 2231 |
struct ni_usb_priv *ni_priv; |
| 2232 |
int product_id; |
| 2233 |
struct usb_device *usb_dev; |
| 2234 |
|
| 2235 |
mutex_lock(&ni_usb_hotplug_lock); |
| 2236 |
retval = ni_usb_allocate_private(board); |
| 2237 |
if (retval) { |
| 2238 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2239 |
return retval; |
| 2240 |
} |
| 2241 |
ni_priv = board->private_data; |
| 2242 |
for (i = 0; i < MAX_NUM_NI_USB_INTERFACES; i++) { |
| 2243 |
if (ni_usb_driver_interfaces[i] && |
| 2244 |
!usb_get_intfdata(ni_usb_driver_interfaces[i]) && |
| 2245 |
ni_usb_device_match(ni_usb_driver_interfaces[i], config)) { |
| 2246 |
ni_priv->bus_interface = ni_usb_driver_interfaces[i]; |
| 2247 |
usb_set_intfdata(ni_usb_driver_interfaces[i], board); |
| 2248 |
usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 2249 |
index = i; |
| 2250 |
break; |
| 2251 |
} |
| 2252 |
} |
| 2253 |
if (i == MAX_NUM_NI_USB_INTERFACES) { |
| 2254 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2255 |
dev_err(board->gpib_dev, "No supported adapters found, have you loaded its firmware?\n"); |
| 2256 |
return -ENODEV; |
| 2257 |
} |
| 2258 |
if (usb_reset_configuration(interface_to_usbdev(ni_priv->bus_interface))) |
| 2259 |
dev_err(&usb_dev->dev, "usb_reset_configuration() failed.\n"); |
| 2260 |
|
| 2261 |
product_id = le16_to_cpu(usb_dev->descriptor.idProduct); |
| 2262 |
ni_priv->product_id = product_id; |
| 2263 |
|
| 2264 |
timer_setup(&ni_priv->bulk_timer, ni_usb_timeout_handler, 0); |
| 2265 |
|
| 2266 |
switch (product_id) { |
| 2267 |
case USB_DEVICE_ID_NI_USB_B: |
| 2268 |
ni_priv->bulk_out_endpoint = NIUSB_B_BULK_OUT_ENDPOINT; |
| 2269 |
ni_priv->bulk_in_endpoint = NIUSB_B_BULK_IN_ENDPOINT; |
| 2270 |
ni_priv->interrupt_in_endpoint = NIUSB_B_INTERRUPT_IN_ENDPOINT; |
| 2271 |
ni_usb_b_read_serial_number(ni_priv); |
| 2272 |
break; |
| 2273 |
case USB_DEVICE_ID_NI_USB_HS: |
| 2274 |
case USB_DEVICE_ID_MC_USB_488: |
| 2275 |
case USB_DEVICE_ID_KUSB_488A: |
| 2276 |
ni_priv->bulk_out_endpoint = NIUSB_HS_BULK_OUT_ENDPOINT; |
| 2277 |
ni_priv->bulk_in_endpoint = NIUSB_HS_BULK_IN_ENDPOINT; |
| 2278 |
ni_priv->interrupt_in_endpoint = NIUSB_HS_INTERRUPT_IN_ENDPOINT; |
| 2279 |
retval = ni_usb_hs_wait_for_ready(ni_priv); |
| 2280 |
if (retval < 0) { |
| 2281 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2282 |
return retval; |
| 2283 |
} |
| 2284 |
break; |
| 2285 |
case USB_DEVICE_ID_NI_USB_HS_PLUS: |
| 2286 |
ni_priv->bulk_out_endpoint = NIUSB_HS_PLUS_BULK_OUT_ENDPOINT; |
| 2287 |
ni_priv->bulk_in_endpoint = NIUSB_HS_PLUS_BULK_IN_ENDPOINT; |
| 2288 |
ni_priv->interrupt_in_endpoint = NIUSB_HS_PLUS_INTERRUPT_IN_ENDPOINT; |
| 2289 |
retval = ni_usb_hs_wait_for_ready(ni_priv); |
| 2290 |
if (retval < 0) { |
| 2291 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2292 |
return retval; |
| 2293 |
} |
| 2294 |
retval = ni_usb_hs_plus_extra_init(ni_priv); |
| 2295 |
if (retval < 0) { |
| 2296 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2297 |
return retval; |
| 2298 |
} |
| 2299 |
break; |
| 2300 |
default: |
| 2301 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2302 |
dev_err(&usb_dev->dev, "\tDriver bug: unknown endpoints for usb device id %x\n", |
| 2303 |
product_id); |
| 2304 |
return -EINVAL; |
| 2305 |
} |
| 2306 |
|
| 2307 |
retval = ni_usb_setup_urbs(board); |
| 2308 |
if (retval < 0) { |
| 2309 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2310 |
return retval; |
| 2311 |
} |
| 2312 |
retval = ni_usb_set_interrupt_monitor(board, 0); |
| 2313 |
if (retval < 0) { |
| 2314 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2315 |
return retval; |
| 2316 |
} |
| 2317 |
|
| 2318 |
board->t1_nano_sec = 500; |
| 2319 |
|
| 2320 |
retval = ni_usb_init(board); |
| 2321 |
if (retval < 0) { |
| 2322 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2323 |
return retval; |
| 2324 |
} |
| 2325 |
retval = ni_usb_set_interrupt_monitor(board, ni_usb_ibsta_monitor_mask); |
| 2326 |
if (retval < 0) { |
| 2327 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2328 |
return retval; |
| 2329 |
} |
| 2330 |
|
| 2331 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2332 |
dev_info(&usb_dev->dev, |
| 2333 |
"bus %d dev num %d attached to gpib%d, intf %i\n", |
| 2334 |
usb_dev->bus->busnum, usb_dev->devnum, board->minor, index); |
| 2335 |
return retval; |
| 2336 |
} |
| 2337 |
|
| 2338 |
static int ni_usb_shutdown_hardware(struct ni_usb_priv *ni_priv) |
| 2339 |
{ |
| 2340 |
struct usb_device *usb_dev = interface_to_usbdev(ni_priv->bus_interface); |
| 2341 |
int retval; |
| 2342 |
struct ni_usb_register writes[2]; |
| 2343 |
static const int writes_length = ARRAY_SIZE(writes); |
| 2344 |
unsigned int ibsta; |
| 2345 |
|
| 2346 |
writes[0].device = NIUSB_SUBDEV_TNT4882; |
| 2347 |
writes[0].address = nec7210_to_tnt4882_offset(AUXMR); |
| 2348 |
writes[0].value = AUX_CR; |
| 2349 |
writes[1].device = NIUSB_SUBDEV_UNKNOWN3; |
| 2350 |
writes[1].address = 0x10; |
| 2351 |
writes[1].value = 0x0; |
| 2352 |
retval = ni_usb_write_registers(ni_priv, writes, writes_length, &ibsta); |
| 2353 |
if (retval) { |
| 2354 |
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval); |
| 2355 |
return retval; |
| 2356 |
} |
| 2357 |
return 0; |
| 2358 |
} |
| 2359 |
|
| 2360 |
static void ni_usb_detach(struct gpib_board *board) |
| 2361 |
{ |
| 2362 |
struct ni_usb_priv *ni_priv; |
| 2363 |
|
| 2364 |
mutex_lock(&ni_usb_hotplug_lock); |
| 2365 |
/* |
| 2366 |
* under windows, software unplug does chip_reset nec7210 aux command, |
| 2367 |
* then writes 0x0 to address 0x10 of device 3 |
| 2368 |
*/ |
| 2369 |
ni_priv = board->private_data; |
| 2370 |
if (ni_priv) { |
| 2371 |
if (ni_priv->bus_interface) { |
| 2372 |
ni_usb_set_interrupt_monitor(board, 0); |
| 2373 |
ni_usb_shutdown_hardware(ni_priv); |
| 2374 |
usb_set_intfdata(ni_priv->bus_interface, NULL); |
| 2375 |
} |
| 2376 |
mutex_lock(&ni_priv->bulk_transfer_lock); |
| 2377 |
mutex_lock(&ni_priv->control_transfer_lock); |
| 2378 |
mutex_lock(&ni_priv->interrupt_transfer_lock); |
| 2379 |
ni_usb_cleanup_urbs(ni_priv); |
| 2380 |
ni_usb_free_private(ni_priv); |
| 2381 |
} |
| 2382 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2383 |
} |
| 2384 |
|
| 2385 |
static struct gpib_interface ni_usb_gpib_interface = { |
| 2386 |
.name = "ni_usb_b", |
| 2387 |
.attach = ni_usb_attach, |
| 2388 |
.detach = ni_usb_detach, |
| 2389 |
.read = ni_usb_read, |
| 2390 |
.write = ni_usb_write, |
| 2391 |
.command = ni_usb_command, |
| 2392 |
.take_control = ni_usb_take_control, |
| 2393 |
.go_to_standby = ni_usb_go_to_standby, |
| 2394 |
.request_system_control = ni_usb_request_system_control, |
| 2395 |
.interface_clear = ni_usb_interface_clear, |
| 2396 |
.remote_enable = ni_usb_remote_enable, |
| 2397 |
.enable_eos = ni_usb_enable_eos, |
| 2398 |
.disable_eos = ni_usb_disable_eos, |
| 2399 |
.parallel_poll = ni_usb_parallel_poll, |
| 2400 |
.parallel_poll_configure = ni_usb_parallel_poll_configure, |
| 2401 |
.parallel_poll_response = ni_usb_parallel_poll_response, |
| 2402 |
.local_parallel_poll_mode = NULL, // XXX |
| 2403 |
.line_status = ni_usb_line_status, |
| 2404 |
.update_status = ni_usb_update_status, |
| 2405 |
.primary_address = ni_usb_primary_address, |
| 2406 |
.secondary_address = ni_usb_secondary_address, |
| 2407 |
.serial_poll_response = ni_usb_serial_poll_response, |
| 2408 |
.serial_poll_status = ni_usb_serial_poll_status, |
| 2409 |
.t1_delay = ni_usb_t1_delay, |
| 2410 |
.return_to_local = ni_usb_return_to_local, |
| 2411 |
.skip_check_for_command_acceptors = 1 |
| 2412 |
}; |
| 2413 |
|
| 2414 |
// Table with the USB-devices: just now only testing IDs |
| 2415 |
static struct usb_device_id ni_usb_driver_device_table[] = { |
| 2416 |
{USB_DEVICE(USB_VENDOR_ID_NI, USB_DEVICE_ID_NI_USB_B)}, |
| 2417 |
{USB_DEVICE(USB_VENDOR_ID_NI, USB_DEVICE_ID_NI_USB_HS)}, |
| 2418 |
// gpib-usb-hs+ has a second interface for the analyzer, which we ignore |
| 2419 |
{USB_DEVICE_INTERFACE_NUMBER(USB_VENDOR_ID_NI, USB_DEVICE_ID_NI_USB_HS_PLUS, 0)}, |
| 2420 |
{USB_DEVICE(USB_VENDOR_ID_NI, USB_DEVICE_ID_KUSB_488A)}, |
| 2421 |
{USB_DEVICE(USB_VENDOR_ID_NI, USB_DEVICE_ID_MC_USB_488)}, |
| 2422 |
{} /* Terminating entry */ |
| 2423 |
}; |
| 2424 |
MODULE_DEVICE_TABLE(usb, ni_usb_driver_device_table); |
| 2425 |
|
| 2426 |
static int ni_usb_driver_probe(struct usb_interface *interface, const struct usb_device_id *id) |
| 2427 |
{ |
| 2428 |
struct usb_device *usb_dev = interface_to_usbdev(interface); |
| 2429 |
int i; |
| 2430 |
char *path; |
| 2431 |
static const int path_length = 1024; |
| 2432 |
|
| 2433 |
mutex_lock(&ni_usb_hotplug_lock); |
| 2434 |
usb_get_dev(usb_dev); |
| 2435 |
for (i = 0; i < MAX_NUM_NI_USB_INTERFACES; i++) { |
| 2436 |
if (!ni_usb_driver_interfaces[i]) { |
| 2437 |
ni_usb_driver_interfaces[i] = interface; |
| 2438 |
usb_set_intfdata(interface, NULL); |
| 2439 |
break; |
| 2440 |
} |
| 2441 |
} |
| 2442 |
if (i == MAX_NUM_NI_USB_INTERFACES) { |
| 2443 |
usb_put_dev(usb_dev); |
| 2444 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2445 |
dev_err(&usb_dev->dev, "ni_usb_driver_interfaces[] full\n"); |
| 2446 |
return -1; |
| 2447 |
} |
| 2448 |
path = kmalloc(path_length, GFP_KERNEL); |
| 2449 |
if (!path) { |
| 2450 |
usb_put_dev(usb_dev); |
| 2451 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2452 |
return -ENOMEM; |
| 2453 |
} |
| 2454 |
usb_make_path(usb_dev, path, path_length); |
| 2455 |
dev_info(&usb_dev->dev, "probe succeeded for path: %s\n", path); |
| 2456 |
kfree(path); |
| 2457 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2458 |
return 0; |
| 2459 |
} |
| 2460 |
|
| 2461 |
static void ni_usb_driver_disconnect(struct usb_interface *interface) |
| 2462 |
{ |
| 2463 |
struct usb_device *usb_dev = interface_to_usbdev(interface); |
| 2464 |
int i; |
| 2465 |
|
| 2466 |
mutex_lock(&ni_usb_hotplug_lock); |
| 2467 |
for (i = 0; i < MAX_NUM_NI_USB_INTERFACES; i++) { |
| 2468 |
if (ni_usb_driver_interfaces[i] == interface) { |
| 2469 |
struct gpib_board *board = usb_get_intfdata(interface); |
| 2470 |
|
| 2471 |
if (board) { |
| 2472 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 2473 |
|
| 2474 |
if (ni_priv) { |
| 2475 |
mutex_lock(&ni_priv->bulk_transfer_lock); |
| 2476 |
mutex_lock(&ni_priv->control_transfer_lock); |
| 2477 |
mutex_lock(&ni_priv->interrupt_transfer_lock); |
| 2478 |
ni_usb_cleanup_urbs(ni_priv); |
| 2479 |
ni_priv->bus_interface = NULL; |
| 2480 |
mutex_unlock(&ni_priv->interrupt_transfer_lock); |
| 2481 |
mutex_unlock(&ni_priv->control_transfer_lock); |
| 2482 |
mutex_unlock(&ni_priv->bulk_transfer_lock); |
| 2483 |
} |
| 2484 |
} |
| 2485 |
ni_usb_driver_interfaces[i] = NULL; |
| 2486 |
break; |
| 2487 |
} |
| 2488 |
} |
| 2489 |
if (i == MAX_NUM_NI_USB_INTERFACES) |
| 2490 |
dev_err(&usb_dev->dev, "unable to find interface bug?\n"); |
| 2491 |
usb_put_dev(usb_dev); |
| 2492 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2493 |
} |
| 2494 |
|
| 2495 |
static int ni_usb_driver_suspend(struct usb_interface *interface, pm_message_t message) |
| 2496 |
{ |
| 2497 |
struct usb_device *usb_dev = interface_to_usbdev(interface); |
| 2498 |
struct gpib_board *board; |
| 2499 |
int i, retval; |
| 2500 |
|
| 2501 |
mutex_lock(&ni_usb_hotplug_lock); |
| 2502 |
|
| 2503 |
for (i = 0; i < MAX_NUM_NI_USB_INTERFACES; i++) { |
| 2504 |
if (ni_usb_driver_interfaces[i] == interface) { |
| 2505 |
board = usb_get_intfdata(interface); |
| 2506 |
if (board) |
| 2507 |
break; |
| 2508 |
} |
| 2509 |
} |
| 2510 |
if (i == MAX_NUM_NI_USB_INTERFACES) { |
| 2511 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2512 |
return 0; |
| 2513 |
} |
| 2514 |
|
| 2515 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 2516 |
|
| 2517 |
if (ni_priv) { |
| 2518 |
ni_usb_set_interrupt_monitor(board, 0); |
| 2519 |
retval = ni_usb_shutdown_hardware(ni_priv); |
| 2520 |
if (retval) { |
| 2521 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2522 |
return retval; |
| 2523 |
} |
| 2524 |
if (ni_priv->interrupt_urb) { |
| 2525 |
mutex_lock(&ni_priv->interrupt_transfer_lock); |
| 2526 |
ni_usb_cleanup_urbs(ni_priv); |
| 2527 |
mutex_unlock(&ni_priv->interrupt_transfer_lock); |
| 2528 |
} |
| 2529 |
dev_dbg(&usb_dev->dev, |
| 2530 |
"bus %d dev num %d gpib%d, interface %i suspended\n", |
| 2531 |
usb_dev->bus->busnum, usb_dev->devnum, board->minor, i); |
| 2532 |
} |
| 2533 |
|
| 2534 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2535 |
return 0; |
| 2536 |
} |
| 2537 |
|
| 2538 |
static int ni_usb_driver_resume(struct usb_interface *interface) |
| 2539 |
{ |
| 2540 |
struct usb_device *usb_dev = interface_to_usbdev(interface); |
| 2541 |
|
| 2542 |
struct gpib_board *board; |
| 2543 |
int i, retval; |
| 2544 |
|
| 2545 |
mutex_lock(&ni_usb_hotplug_lock); |
| 2546 |
|
| 2547 |
for (i = 0; i < MAX_NUM_NI_USB_INTERFACES; i++) { |
| 2548 |
if (ni_usb_driver_interfaces[i] == interface) { |
| 2549 |
board = usb_get_intfdata(interface); |
| 2550 |
if (board) |
| 2551 |
break; |
| 2552 |
} |
| 2553 |
} |
| 2554 |
if (i == MAX_NUM_NI_USB_INTERFACES) { |
| 2555 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2556 |
return 0; |
| 2557 |
} |
| 2558 |
|
| 2559 |
struct ni_usb_priv *ni_priv = board->private_data; |
| 2560 |
|
| 2561 |
if (ni_priv) { |
| 2562 |
if (ni_priv->interrupt_urb) { |
| 2563 |
mutex_lock(&ni_priv->interrupt_transfer_lock); |
| 2564 |
retval = usb_submit_urb(ni_priv->interrupt_urb, GFP_KERNEL); |
| 2565 |
if (retval) { |
| 2566 |
dev_err(&usb_dev->dev, "resume failed to resubmit interrupt urb, retval=%i\n", |
| 2567 |
retval); |
| 2568 |
mutex_unlock(&ni_priv->interrupt_transfer_lock); |
| 2569 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2570 |
return retval; |
| 2571 |
} |
| 2572 |
mutex_unlock(&ni_priv->interrupt_transfer_lock); |
| 2573 |
} else { |
| 2574 |
dev_err(&usb_dev->dev, "bug! resume int urb not set up\n"); |
| 2575 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2576 |
return -EINVAL; |
| 2577 |
} |
| 2578 |
|
| 2579 |
switch (ni_priv->product_id) { |
| 2580 |
case USB_DEVICE_ID_NI_USB_B: |
| 2581 |
ni_usb_b_read_serial_number(ni_priv); |
| 2582 |
break; |
| 2583 |
case USB_DEVICE_ID_NI_USB_HS: |
| 2584 |
case USB_DEVICE_ID_MC_USB_488: |
| 2585 |
case USB_DEVICE_ID_KUSB_488A: |
| 2586 |
retval = ni_usb_hs_wait_for_ready(ni_priv); |
| 2587 |
if (retval < 0) { |
| 2588 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2589 |
return retval; |
| 2590 |
} |
| 2591 |
break; |
| 2592 |
case USB_DEVICE_ID_NI_USB_HS_PLUS: |
| 2593 |
retval = ni_usb_hs_wait_for_ready(ni_priv); |
| 2594 |
if (retval < 0) { |
| 2595 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2596 |
return retval; |
| 2597 |
} |
| 2598 |
retval = ni_usb_hs_plus_extra_init(ni_priv); |
| 2599 |
if (retval < 0) { |
| 2600 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2601 |
return retval; |
| 2602 |
} |
| 2603 |
break; |
| 2604 |
default: |
| 2605 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2606 |
dev_err(&usb_dev->dev, "\tDriver bug: unknown endpoints for usb device id\n"); |
| 2607 |
return -EINVAL; |
| 2608 |
} |
| 2609 |
|
| 2610 |
retval = ni_usb_set_interrupt_monitor(board, 0); |
| 2611 |
if (retval < 0) { |
| 2612 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2613 |
return retval; |
| 2614 |
} |
| 2615 |
|
| 2616 |
retval = ni_usb_init(board); |
| 2617 |
if (retval < 0) { |
| 2618 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2619 |
return retval; |
| 2620 |
} |
| 2621 |
retval = ni_usb_set_interrupt_monitor(board, ni_usb_ibsta_monitor_mask); |
| 2622 |
if (retval < 0) { |
| 2623 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2624 |
return retval; |
| 2625 |
} |
| 2626 |
if (board->master) |
| 2627 |
ni_usb_interface_clear(board, 1); // this is a pulsed action |
| 2628 |
if (ni_priv->ren_state) |
| 2629 |
ni_usb_remote_enable(board, 1); |
| 2630 |
|
| 2631 |
dev_dbg(&usb_dev->dev, |
| 2632 |
"bus %d dev num %d gpib%d, interface %i resumed\n", |
| 2633 |
usb_dev->bus->busnum, usb_dev->devnum, board->minor, i); |
| 2634 |
} |
| 2635 |
|
| 2636 |
mutex_unlock(&ni_usb_hotplug_lock); |
| 2637 |
return 0; |
| 2638 |
} |
| 2639 |
|
| 2640 |
static struct usb_driver ni_usb_bus_driver = { |
| 2641 |
.name = DRV_NAME, |
| 2642 |
.probe = ni_usb_driver_probe, |
| 2643 |
.disconnect = ni_usb_driver_disconnect, |
| 2644 |
.suspend = ni_usb_driver_suspend, |
| 2645 |
.resume = ni_usb_driver_resume, |
| 2646 |
.id_table = ni_usb_driver_device_table, |
| 2647 |
}; |
| 2648 |
|
| 2649 |
static int __init ni_usb_init_module(void) |
| 2650 |
{ |
| 2651 |
int i; |
| 2652 |
int ret; |
| 2653 |
|
| 2654 |
for (i = 0; i < MAX_NUM_NI_USB_INTERFACES; i++) |
| 2655 |
ni_usb_driver_interfaces[i] = NULL; |
| 2656 |
|
| 2657 |
ret = usb_register(&ni_usb_bus_driver); |
| 2658 |
if (ret) { |
| 2659 |
pr_err("usb_register failed: error = %d\n", ret); |
| 2660 |
return ret; |
| 2661 |
} |
| 2662 |
|
| 2663 |
ret = gpib_register_driver(&ni_usb_gpib_interface, THIS_MODULE); |
| 2664 |
if (ret) { |
| 2665 |
pr_err("gpib_register_driver failed: error = %d\n", ret); |
| 2666 |
return ret; |
| 2667 |
} |
| 2668 |
|
| 2669 |
return 0; |
| 2670 |
} |
| 2671 |
|
| 2672 |
static void __exit ni_usb_exit_module(void) |
| 2673 |
{ |
| 2674 |
gpib_unregister_driver(&ni_usb_gpib_interface); |
| 2675 |
usb_deregister(&ni_usb_bus_driver); |
| 2676 |
} |
| 2677 |
|
| 2678 |
module_init(ni_usb_init_module); |
| 2679 |
module_exit(ni_usb_exit_module); |