| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* copyright : (C) 2001, 2004 by Frank Mori Hess |
| 5 |
*************************************************************************** |
| 6 |
*/ |
| 7 |
|
| 8 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 |
#define dev_fmt pr_fmt |
| 10 |
|
| 11 |
#include "ibsys.h" |
| 12 |
#include <linux/module.h> |
| 13 |
#include <linux/wait.h> |
| 14 |
#include <linux/list.h> |
| 15 |
#include <linux/fs.h> |
| 16 |
#include <linux/pci.h> |
| 17 |
#include <linux/device.h> |
| 18 |
#include <linux/init.h> |
| 19 |
#include <linux/string.h> |
| 20 |
#include <linux/vmalloc.h> |
| 21 |
#include <linux/fcntl.h> |
| 22 |
#include <linux/kmod.h> |
| 23 |
#include <linux/uaccess.h> |
| 24 |
|
| 25 |
MODULE_LICENSE("GPL"); |
| 26 |
MODULE_DESCRIPTION("GPIB base support"); |
| 27 |
MODULE_ALIAS_CHARDEV_MAJOR(GPIB_CODE); |
| 28 |
|
| 29 |
static int board_type_ioctl(struct gpib_file_private *file_priv, |
| 30 |
struct gpib_board *board, unsigned long arg); |
| 31 |
static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, |
| 32 |
unsigned long arg); |
| 33 |
static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, |
| 34 |
unsigned long arg); |
| 35 |
static int command_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, |
| 36 |
unsigned long arg); |
| 37 |
static int open_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg); |
| 38 |
static int close_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg); |
| 39 |
static int serial_poll_ioctl(struct gpib_board *board, unsigned long arg); |
| 40 |
static int wait_ioctl(struct gpib_file_private *file_priv, |
| 41 |
struct gpib_board *board, unsigned long arg); |
| 42 |
static int parallel_poll_ioctl(struct gpib_board *board, unsigned long arg); |
| 43 |
static int online_ioctl(struct gpib_board *board, unsigned long arg); |
| 44 |
static int remote_enable_ioctl(struct gpib_board *board, unsigned long arg); |
| 45 |
static int take_control_ioctl(struct gpib_board *board, unsigned long arg); |
| 46 |
static int line_status_ioctl(struct gpib_board *board, unsigned long arg); |
| 47 |
static int pad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, |
| 48 |
unsigned long arg); |
| 49 |
static int sad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, |
| 50 |
unsigned long arg); |
| 51 |
static int eos_ioctl(struct gpib_board *board, unsigned long arg); |
| 52 |
static int request_service_ioctl(struct gpib_board *board, unsigned long arg); |
| 53 |
static int request_service2_ioctl(struct gpib_board *board, unsigned long arg); |
| 54 |
static int iobase_ioctl(struct gpib_board_config *config, unsigned long arg); |
| 55 |
static int irq_ioctl(struct gpib_board_config *config, unsigned long arg); |
| 56 |
static int dma_ioctl(struct gpib_board_config *config, unsigned long arg); |
| 57 |
static int autospoll_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, |
| 58 |
unsigned long arg); |
| 59 |
static int mutex_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, |
| 60 |
unsigned long arg); |
| 61 |
static int timeout_ioctl(struct gpib_board *board, unsigned long arg); |
| 62 |
static int status_bytes_ioctl(struct gpib_board *board, unsigned long arg); |
| 63 |
static int board_info_ioctl(const struct gpib_board *board, unsigned long arg); |
| 64 |
static int ppc_ioctl(struct gpib_board *board, unsigned long arg); |
| 65 |
static int set_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg); |
| 66 |
static int get_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg); |
| 67 |
static int query_board_rsv_ioctl(struct gpib_board *board, unsigned long arg); |
| 68 |
static int interface_clear_ioctl(struct gpib_board *board, unsigned long arg); |
| 69 |
static int select_pci_ioctl(struct gpib_board_config *config, unsigned long arg); |
| 70 |
static int select_device_path_ioctl(struct gpib_board_config *config, unsigned long arg); |
| 71 |
static int event_ioctl(struct gpib_board *board, unsigned long arg); |
| 72 |
static int request_system_control_ioctl(struct gpib_board *board, unsigned long arg); |
| 73 |
static int t1_delay_ioctl(struct gpib_board *board, unsigned long arg); |
| 74 |
|
| 75 |
static int cleanup_open_devices(struct gpib_file_private *file_priv, struct gpib_board *board); |
| 76 |
|
| 77 |
static int pop_gpib_event_nolock(struct gpib_board *board, |
| 78 |
struct gpib_event_queue *queue, short *event_type); |
| 79 |
|
| 80 |
/* |
| 81 |
* Timer functions |
| 82 |
*/ |
| 83 |
|
| 84 |
/* Watchdog timeout routine */ |
| 85 |
|
| 86 |
static void watchdog_timeout(struct timer_list *t) |
| 87 |
{ |
| 88 |
struct gpib_board *board = timer_container_of(board, t, timer); |
| 89 |
|
| 90 |
set_bit(TIMO_NUM, &board->status); |
| 91 |
wake_up_interruptible(&board->wait); |
| 92 |
} |
| 93 |
|
| 94 |
/* install timer interrupt handler */ |
| 95 |
void os_start_timer(struct gpib_board *board, unsigned int usec_timeout) |
| 96 |
/* Starts the timeout task */ |
| 97 |
{ |
| 98 |
if (timer_pending(&board->timer)) { |
| 99 |
dev_err(board->gpib_dev, "bug! timer already running?\n"); |
| 100 |
return; |
| 101 |
} |
| 102 |
clear_bit(TIMO_NUM, &board->status); |
| 103 |
|
| 104 |
if (usec_timeout > 0) { |
| 105 |
board->timer.function = watchdog_timeout; |
| 106 |
/* set number of ticks */ |
| 107 |
mod_timer(&board->timer, jiffies + usec_to_jiffies(usec_timeout)); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void os_remove_timer(struct gpib_board *board) |
| 112 |
/* Removes the timeout task */ |
| 113 |
{ |
| 114 |
if (timer_pending(&board->timer)) |
| 115 |
timer_delete_sync(&board->timer); |
| 116 |
} |
| 117 |
|
| 118 |
int io_timed_out(struct gpib_board *board) |
| 119 |
{ |
| 120 |
if (test_bit(TIMO_NUM, &board->status)) |
| 121 |
return 1; |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
/* |
| 126 |
* this is a function instead of a constant because of Suse |
| 127 |
* defining HZ to be a function call to get_hz() |
| 128 |
*/ |
| 129 |
static inline int pseudo_irq_period(void) |
| 130 |
{ |
| 131 |
return (HZ + 99) / 100; |
| 132 |
} |
| 133 |
|
| 134 |
static void pseudo_irq_handler(struct timer_list *t) |
| 135 |
{ |
| 136 |
struct gpib_pseudo_irq *pseudo_irq = timer_container_of(pseudo_irq, t, |
| 137 |
timer); |
| 138 |
|
| 139 |
if (pseudo_irq->handler) |
| 140 |
pseudo_irq->handler(0, pseudo_irq->board); |
| 141 |
else |
| 142 |
pr_err("gpib: bug! pseudo_irq.handler is NULL\n"); |
| 143 |
|
| 144 |
if (atomic_read(&pseudo_irq->active)) |
| 145 |
mod_timer(&pseudo_irq->timer, jiffies + pseudo_irq_period()); |
| 146 |
} |
| 147 |
|
| 148 |
int gpib_request_pseudo_irq(struct gpib_board *board, irqreturn_t (*handler)(int, void *)) |
| 149 |
{ |
| 150 |
if (timer_pending(&board->pseudo_irq.timer) || board->pseudo_irq.handler) { |
| 151 |
dev_err(board->gpib_dev, "only one pseudo interrupt per board allowed\n"); |
| 152 |
return -1; |
| 153 |
} |
| 154 |
|
| 155 |
board->pseudo_irq.handler = handler; |
| 156 |
board->pseudo_irq.timer.function = pseudo_irq_handler; |
| 157 |
board->pseudo_irq.board = board; |
| 158 |
|
| 159 |
atomic_set(&board->pseudo_irq.active, 1); |
| 160 |
|
| 161 |
mod_timer(&board->pseudo_irq.timer, jiffies + pseudo_irq_period()); |
| 162 |
|
| 163 |
return 0; |
| 164 |
} |
| 165 |
EXPORT_SYMBOL(gpib_request_pseudo_irq); |
| 166 |
|
| 167 |
void gpib_free_pseudo_irq(struct gpib_board *board) |
| 168 |
{ |
| 169 |
atomic_set(&board->pseudo_irq.active, 0); |
| 170 |
|
| 171 |
timer_delete_sync(&board->pseudo_irq.timer); |
| 172 |
board->pseudo_irq.handler = NULL; |
| 173 |
} |
| 174 |
EXPORT_SYMBOL(gpib_free_pseudo_irq); |
| 175 |
|
| 176 |
static const unsigned int serial_timeout = 1000000; |
| 177 |
|
| 178 |
unsigned int num_status_bytes(const struct gpib_status_queue *dev) |
| 179 |
{ |
| 180 |
if (!dev) |
| 181 |
return 0; |
| 182 |
return dev->num_status_bytes; |
| 183 |
} |
| 184 |
|
| 185 |
// push status byte onto back of status byte fifo |
| 186 |
int push_status_byte(struct gpib_board *board, struct gpib_status_queue *device, u8 poll_byte) |
| 187 |
{ |
| 188 |
struct list_head *head = &device->status_bytes; |
| 189 |
struct gpib_status_byte *status; |
| 190 |
static const unsigned int max_num_status_bytes = 1024; |
| 191 |
int retval; |
| 192 |
|
| 193 |
if (num_status_bytes(device) >= max_num_status_bytes) { |
| 194 |
u8 lost_byte; |
| 195 |
|
| 196 |
device->dropped_byte = 1; |
| 197 |
retval = pop_status_byte(board, device, &lost_byte); |
| 198 |
if (retval < 0) |
| 199 |
return retval; |
| 200 |
} |
| 201 |
|
| 202 |
status = kmalloc_obj(*status); |
| 203 |
if (!status) |
| 204 |
return -ENOMEM; |
| 205 |
|
| 206 |
INIT_LIST_HEAD(&status->list); |
| 207 |
status->poll_byte = poll_byte; |
| 208 |
|
| 209 |
list_add_tail(&status->list, head); |
| 210 |
|
| 211 |
device->num_status_bytes++; |
| 212 |
|
| 213 |
dev_dbg(board->gpib_dev, "pushed status byte 0x%x, %i in queue\n", |
| 214 |
(int)poll_byte, num_status_bytes(device)); |
| 215 |
|
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
// pop status byte from front of status byte fifo |
| 220 |
int pop_status_byte(struct gpib_board *board, struct gpib_status_queue *device, u8 *poll_byte) |
| 221 |
{ |
| 222 |
struct list_head *head = &device->status_bytes; |
| 223 |
struct list_head *front = head->next; |
| 224 |
struct gpib_status_byte *status; |
| 225 |
|
| 226 |
if (num_status_bytes(device) == 0) |
| 227 |
return -EIO; |
| 228 |
|
| 229 |
if (front == head) |
| 230 |
return -EIO; |
| 231 |
|
| 232 |
if (device->dropped_byte) { |
| 233 |
device->dropped_byte = 0; |
| 234 |
return -EPIPE; |
| 235 |
} |
| 236 |
|
| 237 |
status = list_entry(front, struct gpib_status_byte, list); |
| 238 |
*poll_byte = status->poll_byte; |
| 239 |
|
| 240 |
list_del(front); |
| 241 |
kfree(status); |
| 242 |
|
| 243 |
device->num_status_bytes--; |
| 244 |
|
| 245 |
dev_dbg(board->gpib_dev, "popped status byte 0x%x, %i in queue\n", |
| 246 |
(int)*poll_byte, num_status_bytes(device)); |
| 247 |
|
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
struct gpib_status_queue *get_gpib_status_queue(struct gpib_board *board, unsigned int pad, int sad) |
| 252 |
{ |
| 253 |
struct gpib_status_queue *device; |
| 254 |
struct list_head *list_ptr; |
| 255 |
const struct list_head *head = &board->device_list; |
| 256 |
|
| 257 |
for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) { |
| 258 |
device = list_entry(list_ptr, struct gpib_status_queue, list); |
| 259 |
if (gpib_address_equal(device->pad, device->sad, pad, sad)) |
| 260 |
return device; |
| 261 |
} |
| 262 |
|
| 263 |
return NULL; |
| 264 |
} |
| 265 |
|
| 266 |
int get_serial_poll_byte(struct gpib_board *board, unsigned int pad, int sad, |
| 267 |
unsigned int usec_timeout, u8 *poll_byte) |
| 268 |
{ |
| 269 |
struct gpib_status_queue *device; |
| 270 |
|
| 271 |
device = get_gpib_status_queue(board, pad, sad); |
| 272 |
if (num_status_bytes(device)) |
| 273 |
return pop_status_byte(board, device, poll_byte); |
| 274 |
else |
| 275 |
return dvrsp(board, pad, sad, usec_timeout, poll_byte); |
| 276 |
} |
| 277 |
|
| 278 |
int autopoll_all_devices(struct gpib_board *board) |
| 279 |
{ |
| 280 |
int retval; |
| 281 |
|
| 282 |
if (mutex_lock_interruptible(&board->user_mutex)) |
| 283 |
return -ERESTARTSYS; |
| 284 |
if (mutex_lock_interruptible(&board->big_gpib_mutex)) { |
| 285 |
mutex_unlock(&board->user_mutex); |
| 286 |
return -ERESTARTSYS; |
| 287 |
} |
| 288 |
|
| 289 |
dev_dbg(board->gpib_dev, "autopoll has board lock\n"); |
| 290 |
|
| 291 |
retval = serial_poll_all(board, serial_timeout); |
| 292 |
if (retval < 0) { |
| 293 |
mutex_unlock(&board->big_gpib_mutex); |
| 294 |
mutex_unlock(&board->user_mutex); |
| 295 |
return retval; |
| 296 |
} |
| 297 |
|
| 298 |
dev_dbg(board->gpib_dev, "complete\n"); |
| 299 |
/* |
| 300 |
* need to wake wait queue in case someone is |
| 301 |
* waiting on RQS |
| 302 |
*/ |
| 303 |
wake_up_interruptible(&board->wait); |
| 304 |
mutex_unlock(&board->big_gpib_mutex); |
| 305 |
mutex_unlock(&board->user_mutex); |
| 306 |
|
| 307 |
return retval; |
| 308 |
} |
| 309 |
|
| 310 |
static int setup_serial_poll(struct gpib_board *board, unsigned int usec_timeout) |
| 311 |
{ |
| 312 |
u8 cmd_string[8]; |
| 313 |
int i; |
| 314 |
size_t bytes_written; |
| 315 |
int ret; |
| 316 |
|
| 317 |
os_start_timer(board, usec_timeout); |
| 318 |
ret = ibcac(board, 1, 1); |
| 319 |
if (ret < 0) { |
| 320 |
os_remove_timer(board); |
| 321 |
return ret; |
| 322 |
} |
| 323 |
|
| 324 |
i = 0; |
| 325 |
cmd_string[i++] = UNL; |
| 326 |
cmd_string[i++] = MLA(board->pad); /* controller's listen address */ |
| 327 |
if (board->sad >= 0) |
| 328 |
cmd_string[i++] = MSA(board->sad); |
| 329 |
cmd_string[i++] = SPE; // serial poll enable |
| 330 |
|
| 331 |
ret = board->interface->command(board, cmd_string, i, &bytes_written); |
| 332 |
if (ret < 0 || bytes_written < i) { |
| 333 |
dev_dbg(board->gpib_dev, "failed to setup serial poll\n"); |
| 334 |
os_remove_timer(board); |
| 335 |
return -EIO; |
| 336 |
} |
| 337 |
os_remove_timer(board); |
| 338 |
|
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
static int read_serial_poll_byte(struct gpib_board *board, unsigned int pad, |
| 343 |
int sad, unsigned int usec_timeout, u8 *result) |
| 344 |
{ |
| 345 |
u8 cmd_string[8]; |
| 346 |
int end_flag; |
| 347 |
int ret; |
| 348 |
int i; |
| 349 |
size_t nbytes; |
| 350 |
|
| 351 |
dev_dbg(board->gpib_dev, "entering pad=%i sad=%i\n", pad, sad); |
| 352 |
|
| 353 |
os_start_timer(board, usec_timeout); |
| 354 |
ret = ibcac(board, 1, 1); |
| 355 |
if (ret < 0) { |
| 356 |
os_remove_timer(board); |
| 357 |
return ret; |
| 358 |
} |
| 359 |
|
| 360 |
i = 0; |
| 361 |
// send talk address |
| 362 |
cmd_string[i++] = MTA(pad); |
| 363 |
if (sad >= 0) |
| 364 |
cmd_string[i++] = MSA(sad); |
| 365 |
|
| 366 |
ret = board->interface->command(board, cmd_string, i, &nbytes); |
| 367 |
if (ret < 0 || nbytes < i) { |
| 368 |
dev_err(board->gpib_dev, "failed to setup serial poll\n"); |
| 369 |
os_remove_timer(board); |
| 370 |
return -EIO; |
| 371 |
} |
| 372 |
|
| 373 |
ibgts(board); |
| 374 |
|
| 375 |
// read poll result |
| 376 |
ret = board->interface->read(board, result, 1, &end_flag, &nbytes); |
| 377 |
if (ret < 0 || nbytes < 1) { |
| 378 |
dev_err(board->gpib_dev, "serial poll failed\n"); |
| 379 |
os_remove_timer(board); |
| 380 |
return -EIO; |
| 381 |
} |
| 382 |
os_remove_timer(board); |
| 383 |
|
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
static int cleanup_serial_poll(struct gpib_board *board, unsigned int usec_timeout) |
| 388 |
{ |
| 389 |
u8 cmd_string[8]; |
| 390 |
int ret; |
| 391 |
size_t bytes_written; |
| 392 |
|
| 393 |
os_start_timer(board, usec_timeout); |
| 394 |
ret = ibcac(board, 1, 1); |
| 395 |
if (ret < 0) { |
| 396 |
os_remove_timer(board); |
| 397 |
return ret; |
| 398 |
} |
| 399 |
|
| 400 |
cmd_string[0] = SPD; /* disable serial poll bytes */ |
| 401 |
cmd_string[1] = UNT; |
| 402 |
ret = board->interface->command(board, cmd_string, 2, &bytes_written); |
| 403 |
if (ret < 0 || bytes_written < 2) { |
| 404 |
dev_err(board->gpib_dev, "failed to disable serial poll\n"); |
| 405 |
os_remove_timer(board); |
| 406 |
return -EIO; |
| 407 |
} |
| 408 |
os_remove_timer(board); |
| 409 |
|
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
static int serial_poll_single(struct gpib_board *board, unsigned int pad, int sad, |
| 414 |
unsigned int usec_timeout, u8 *result) |
| 415 |
{ |
| 416 |
int retval, cleanup_retval; |
| 417 |
|
| 418 |
retval = setup_serial_poll(board, usec_timeout); |
| 419 |
if (retval < 0) |
| 420 |
return retval; |
| 421 |
retval = read_serial_poll_byte(board, pad, sad, usec_timeout, result); |
| 422 |
cleanup_retval = cleanup_serial_poll(board, usec_timeout); |
| 423 |
if (retval < 0) |
| 424 |
return retval; |
| 425 |
if (cleanup_retval < 0) |
| 426 |
return retval; |
| 427 |
|
| 428 |
return 0; |
| 429 |
} |
| 430 |
|
| 431 |
int serial_poll_all(struct gpib_board *board, unsigned int usec_timeout) |
| 432 |
{ |
| 433 |
int retval = 0; |
| 434 |
struct list_head *cur; |
| 435 |
const struct list_head *head = NULL; |
| 436 |
struct gpib_status_queue *device; |
| 437 |
u8 result; |
| 438 |
unsigned int num_bytes = 0; |
| 439 |
|
| 440 |
head = &board->device_list; |
| 441 |
if (head->next == head) |
| 442 |
return 0; |
| 443 |
|
| 444 |
retval = setup_serial_poll(board, usec_timeout); |
| 445 |
if (retval < 0) |
| 446 |
return retval; |
| 447 |
|
| 448 |
for (cur = head->next; cur != head; cur = cur->next) { |
| 449 |
device = list_entry(cur, struct gpib_status_queue, list); |
| 450 |
retval = read_serial_poll_byte(board, |
| 451 |
device->pad, device->sad, usec_timeout, &result); |
| 452 |
if (retval < 0) |
| 453 |
continue; |
| 454 |
if (result & request_service_bit) { |
| 455 |
retval = push_status_byte(board, device, result); |
| 456 |
if (retval < 0) |
| 457 |
continue; |
| 458 |
num_bytes++; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
retval = cleanup_serial_poll(board, usec_timeout); |
| 463 |
if (retval < 0) |
| 464 |
return retval; |
| 465 |
|
| 466 |
return num_bytes; |
| 467 |
} |
| 468 |
|
| 469 |
/* |
| 470 |
* DVRSP |
| 471 |
* This function performs a serial poll of the device with primary |
| 472 |
* address pad and secondary address sad. If the device has no |
| 473 |
* secondary address, pass a negative number in for this argument. At the |
| 474 |
* end of a successful serial poll the response is returned in result. |
| 475 |
* SPD and UNT are sent at the completion of the poll. |
| 476 |
*/ |
| 477 |
|
| 478 |
int dvrsp(struct gpib_board *board, unsigned int pad, int sad, |
| 479 |
unsigned int usec_timeout, u8 *result) |
| 480 |
{ |
| 481 |
int status = ibstatus(board); |
| 482 |
int retval; |
| 483 |
|
| 484 |
if ((status & CIC) == 0) { |
| 485 |
dev_err(board->gpib_dev, "not CIC during serial poll\n"); |
| 486 |
return -1; |
| 487 |
} |
| 488 |
|
| 489 |
if (pad > MAX_GPIB_PRIMARY_ADDRESS || sad > MAX_GPIB_SECONDARY_ADDRESS || sad < -1) { |
| 490 |
dev_err(board->gpib_dev, "bad address for serial poll"); |
| 491 |
return -1; |
| 492 |
} |
| 493 |
|
| 494 |
retval = serial_poll_single(board, pad, sad, usec_timeout, result); |
| 495 |
if (io_timed_out(board)) |
| 496 |
retval = -ETIMEDOUT; |
| 497 |
|
| 498 |
return retval; |
| 499 |
} |
| 500 |
|
| 501 |
static struct gpib_descriptor *handle_to_descriptor(const struct gpib_file_private *file_priv, |
| 502 |
int handle) |
| 503 |
{ |
| 504 |
if (handle < 0 || handle >= GPIB_MAX_NUM_DESCRIPTORS) { |
| 505 |
pr_err("gpib: invalid handle %i\n", handle); |
| 506 |
return NULL; |
| 507 |
} |
| 508 |
|
| 509 |
return file_priv->descriptors[handle]; |
| 510 |
} |
| 511 |
|
| 512 |
static int init_gpib_file_private(struct gpib_file_private *priv) |
| 513 |
{ |
| 514 |
memset(priv, 0, sizeof(*priv)); |
| 515 |
atomic_set(&priv->holding_mutex, 0); |
| 516 |
priv->descriptors[0] = kmalloc_obj(struct gpib_descriptor); |
| 517 |
if (!priv->descriptors[0]) { |
| 518 |
pr_err("gpib: failed to allocate default board descriptor\n"); |
| 519 |
return -ENOMEM; |
| 520 |
} |
| 521 |
init_gpib_descriptor(priv->descriptors[0]); |
| 522 |
priv->descriptors[0]->is_board = 1; |
| 523 |
mutex_init(&priv->descriptors_mutex); |
| 524 |
return 0; |
| 525 |
} |
| 526 |
|
| 527 |
int ibopen(struct inode *inode, struct file *filep) |
| 528 |
{ |
| 529 |
unsigned int minor = iminor(inode); |
| 530 |
struct gpib_board *board; |
| 531 |
struct gpib_file_private *priv; |
| 532 |
|
| 533 |
if (minor >= GPIB_MAX_NUM_BOARDS) { |
| 534 |
pr_err("gpib: invalid minor number of device file\n"); |
| 535 |
return -ENXIO; |
| 536 |
} |
| 537 |
|
| 538 |
board = &board_array[minor]; |
| 539 |
|
| 540 |
filep->private_data = kmalloc_obj(struct gpib_file_private); |
| 541 |
if (!filep->private_data) |
| 542 |
return -ENOMEM; |
| 543 |
|
| 544 |
priv = filep->private_data; |
| 545 |
init_gpib_file_private((struct gpib_file_private *)filep->private_data); |
| 546 |
|
| 547 |
if (board->use_count == 0) { |
| 548 |
int retval; |
| 549 |
|
| 550 |
retval = request_module("gpib%i", minor); |
| 551 |
if (retval) |
| 552 |
dev_dbg(board->gpib_dev, "request module returned %i\n", retval); |
| 553 |
} |
| 554 |
if (board->interface) { |
| 555 |
if (!try_module_get(board->provider_module)) { |
| 556 |
dev_err(board->gpib_dev, "try_module_get() failed\n"); |
| 557 |
return -EIO; |
| 558 |
} |
| 559 |
board->use_count++; |
| 560 |
priv->got_module = 1; |
| 561 |
} |
| 562 |
return 0; |
| 563 |
} |
| 564 |
|
| 565 |
int ibclose(struct inode *inode, struct file *filep) |
| 566 |
{ |
| 567 |
unsigned int minor = iminor(inode); |
| 568 |
struct gpib_board *board; |
| 569 |
struct gpib_file_private *priv = filep->private_data; |
| 570 |
struct gpib_descriptor *desc; |
| 571 |
|
| 572 |
if (minor >= GPIB_MAX_NUM_BOARDS) { |
| 573 |
pr_err("gpib: invalid minor number of device file\n"); |
| 574 |
return -ENODEV; |
| 575 |
} |
| 576 |
|
| 577 |
board = &board_array[minor]; |
| 578 |
|
| 579 |
if (priv) { |
| 580 |
desc = handle_to_descriptor(priv, 0); |
| 581 |
if (desc) { |
| 582 |
if (desc->autopoll_enabled) { |
| 583 |
dev_dbg(board->gpib_dev, "decrementing autospollers\n"); |
| 584 |
if (board->autospollers > 0) |
| 585 |
board->autospollers--; |
| 586 |
else |
| 587 |
dev_err(board->gpib_dev, |
| 588 |
"Attempt to decrement zero autospollers\n"); |
| 589 |
} |
| 590 |
} else { |
| 591 |
dev_err(board->gpib_dev, "Unexpected null gpib_descriptor\n"); |
| 592 |
} |
| 593 |
|
| 594 |
cleanup_open_devices(priv, board); |
| 595 |
|
| 596 |
if (atomic_read(&priv->holding_mutex)) |
| 597 |
mutex_unlock(&board->user_mutex); |
| 598 |
|
| 599 |
if (priv->got_module && board->use_count) { |
| 600 |
module_put(board->provider_module); |
| 601 |
--board->use_count; |
| 602 |
} |
| 603 |
|
| 604 |
kfree(filep->private_data); |
| 605 |
filep->private_data = NULL; |
| 606 |
} |
| 607 |
|
| 608 |
return 0; |
| 609 |
} |
| 610 |
|
| 611 |
long ibioctl(struct file *filep, unsigned int cmd, unsigned long arg) |
| 612 |
{ |
| 613 |
unsigned int minor = iminor(file_inode(filep)); |
| 614 |
struct gpib_board *board; |
| 615 |
struct gpib_file_private *file_priv = filep->private_data; |
| 616 |
long retval = -ENOTTY; |
| 617 |
|
| 618 |
if (minor >= GPIB_MAX_NUM_BOARDS) { |
| 619 |
pr_err("gpib: invalid minor number of device file\n"); |
| 620 |
return -ENODEV; |
| 621 |
} |
| 622 |
board = &board_array[minor]; |
| 623 |
|
| 624 |
if (mutex_lock_interruptible(&board->big_gpib_mutex)) |
| 625 |
return -ERESTARTSYS; |
| 626 |
|
| 627 |
dev_dbg(board->gpib_dev, "ioctl %d, interface=%s, use=%d, onl=%d\n", |
| 628 |
cmd & 0xff, |
| 629 |
board->interface ? board->interface->name : "", |
| 630 |
board->use_count, |
| 631 |
board->online); |
| 632 |
|
| 633 |
switch (cmd) { |
| 634 |
case CFCBOARDTYPE: |
| 635 |
retval = board_type_ioctl(file_priv, board, arg); |
| 636 |
goto done; |
| 637 |
case IBONL: |
| 638 |
retval = online_ioctl(board, arg); |
| 639 |
goto done; |
| 640 |
default: |
| 641 |
break; |
| 642 |
} |
| 643 |
if (!board->interface) { |
| 644 |
dev_err(board->gpib_dev, "no gpib board configured\n"); |
| 645 |
retval = -ENODEV; |
| 646 |
goto done; |
| 647 |
} |
| 648 |
if (file_priv->got_module == 0) { |
| 649 |
if (!try_module_get(board->provider_module)) { |
| 650 |
dev_err(board->gpib_dev, "try_module_get() failed\n"); |
| 651 |
retval = -EIO; |
| 652 |
goto done; |
| 653 |
} |
| 654 |
file_priv->got_module = 1; |
| 655 |
board->use_count++; |
| 656 |
} |
| 657 |
switch (cmd) { |
| 658 |
case CFCBASE: |
| 659 |
retval = iobase_ioctl(&board->config, arg); |
| 660 |
goto done; |
| 661 |
case CFCIRQ: |
| 662 |
retval = irq_ioctl(&board->config, arg); |
| 663 |
goto done; |
| 664 |
case CFCDMA: |
| 665 |
retval = dma_ioctl(&board->config, arg); |
| 666 |
goto done; |
| 667 |
case IBAUTOSPOLL: |
| 668 |
retval = autospoll_ioctl(board, file_priv, arg); |
| 669 |
goto done; |
| 670 |
case IBBOARD_INFO: |
| 671 |
retval = board_info_ioctl(board, arg); |
| 672 |
goto done; |
| 673 |
case IBMUTEX: |
| 674 |
/* |
| 675 |
* Need to unlock board->big_gpib_mutex before potentially locking board->user_mutex |
| 676 |
* to maintain consistent locking order |
| 677 |
*/ |
| 678 |
mutex_unlock(&board->big_gpib_mutex); |
| 679 |
return mutex_ioctl(board, file_priv, arg); |
| 680 |
case IBPAD: |
| 681 |
retval = pad_ioctl(board, file_priv, arg); |
| 682 |
goto done; |
| 683 |
case IBSAD: |
| 684 |
retval = sad_ioctl(board, file_priv, arg); |
| 685 |
goto done; |
| 686 |
case IBSELECT_PCI: |
| 687 |
retval = select_pci_ioctl(&board->config, arg); |
| 688 |
goto done; |
| 689 |
case IBSELECT_DEVICE_PATH: |
| 690 |
retval = select_device_path_ioctl(&board->config, arg); |
| 691 |
goto done; |
| 692 |
default: |
| 693 |
break; |
| 694 |
} |
| 695 |
|
| 696 |
if (!board->online) { |
| 697 |
retval = -EINVAL; |
| 698 |
goto done; |
| 699 |
} |
| 700 |
|
| 701 |
switch (cmd) { |
| 702 |
case IBEVENT: |
| 703 |
retval = event_ioctl(board, arg); |
| 704 |
goto done; |
| 705 |
case IBCLOSEDEV: |
| 706 |
retval = close_dev_ioctl(filep, board, arg); |
| 707 |
goto done; |
| 708 |
case IBOPENDEV: |
| 709 |
retval = open_dev_ioctl(filep, board, arg); |
| 710 |
goto done; |
| 711 |
case IBSPOLL_BYTES: |
| 712 |
retval = status_bytes_ioctl(board, arg); |
| 713 |
goto done; |
| 714 |
case IBWAIT: |
| 715 |
retval = wait_ioctl(file_priv, board, arg); |
| 716 |
if (retval == -ERESTARTSYS) |
| 717 |
return retval; |
| 718 |
goto done; |
| 719 |
case IBLINES: |
| 720 |
retval = line_status_ioctl(board, arg); |
| 721 |
goto done; |
| 722 |
case IBLOC: |
| 723 |
board->interface->return_to_local(board); |
| 724 |
retval = 0; |
| 725 |
goto done; |
| 726 |
default: |
| 727 |
break; |
| 728 |
} |
| 729 |
|
| 730 |
spin_lock(&board->locking_pid_spinlock); |
| 731 |
if (current->pid != board->locking_pid) { |
| 732 |
spin_unlock(&board->locking_pid_spinlock); |
| 733 |
retval = -EPERM; |
| 734 |
goto done; |
| 735 |
} |
| 736 |
spin_unlock(&board->locking_pid_spinlock); |
| 737 |
|
| 738 |
switch (cmd) { |
| 739 |
case IB_T1_DELAY: |
| 740 |
retval = t1_delay_ioctl(board, arg); |
| 741 |
goto done; |
| 742 |
case IBCAC: |
| 743 |
retval = take_control_ioctl(board, arg); |
| 744 |
goto done; |
| 745 |
case IBCMD: |
| 746 |
/* |
| 747 |
* IO ioctls can take a long time, we need to unlock board->big_gpib_mutex |
| 748 |
* before we call them. |
| 749 |
*/ |
| 750 |
mutex_unlock(&board->big_gpib_mutex); |
| 751 |
return command_ioctl(file_priv, board, arg); |
| 752 |
case IBEOS: |
| 753 |
retval = eos_ioctl(board, arg); |
| 754 |
goto done; |
| 755 |
case IBGTS: |
| 756 |
retval = ibgts(board); |
| 757 |
goto done; |
| 758 |
case IBPPC: |
| 759 |
retval = ppc_ioctl(board, arg); |
| 760 |
goto done; |
| 761 |
case IBPP2_SET: |
| 762 |
retval = set_local_ppoll_mode_ioctl(board, arg); |
| 763 |
goto done; |
| 764 |
case IBPP2_GET: |
| 765 |
retval = get_local_ppoll_mode_ioctl(board, arg); |
| 766 |
goto done; |
| 767 |
case IBQUERY_BOARD_RSV: |
| 768 |
retval = query_board_rsv_ioctl(board, arg); |
| 769 |
goto done; |
| 770 |
case IBRD: |
| 771 |
/* |
| 772 |
* IO ioctls can take a long time, we need to unlock board->big_gpib_mutex |
| 773 |
* before we call them. |
| 774 |
*/ |
| 775 |
mutex_unlock(&board->big_gpib_mutex); |
| 776 |
return read_ioctl(file_priv, board, arg); |
| 777 |
case IBRPP: |
| 778 |
retval = parallel_poll_ioctl(board, arg); |
| 779 |
goto done; |
| 780 |
case IBRSC: |
| 781 |
retval = request_system_control_ioctl(board, arg); |
| 782 |
goto done; |
| 783 |
case IBRSP: |
| 784 |
retval = serial_poll_ioctl(board, arg); |
| 785 |
goto done; |
| 786 |
case IBRSV: |
| 787 |
retval = request_service_ioctl(board, arg); |
| 788 |
goto done; |
| 789 |
case IBRSV2: |
| 790 |
retval = request_service2_ioctl(board, arg); |
| 791 |
goto done; |
| 792 |
case IBSIC: |
| 793 |
retval = interface_clear_ioctl(board, arg); |
| 794 |
goto done; |
| 795 |
case IBSRE: |
| 796 |
retval = remote_enable_ioctl(board, arg); |
| 797 |
goto done; |
| 798 |
case IBTMO: |
| 799 |
retval = timeout_ioctl(board, arg); |
| 800 |
goto done; |
| 801 |
case IBWRT: |
| 802 |
/* |
| 803 |
* IO ioctls can take a long time, we need to unlock board->big_gpib_mutex |
| 804 |
* before we call them. |
| 805 |
*/ |
| 806 |
mutex_unlock(&board->big_gpib_mutex); |
| 807 |
return write_ioctl(file_priv, board, arg); |
| 808 |
default: |
| 809 |
retval = -ENOTTY; |
| 810 |
goto done; |
| 811 |
} |
| 812 |
|
| 813 |
done: |
| 814 |
mutex_unlock(&board->big_gpib_mutex); |
| 815 |
dev_dbg(board->gpib_dev, "ioctl done status = 0x%lx\n", board->status); |
| 816 |
return retval; |
| 817 |
} |
| 818 |
|
| 819 |
static int board_type_ioctl(struct gpib_file_private *file_priv, |
| 820 |
struct gpib_board *board, unsigned long arg) |
| 821 |
{ |
| 822 |
struct list_head *list_ptr; |
| 823 |
struct gpib_board_type_ioctl cmd; |
| 824 |
int retval; |
| 825 |
|
| 826 |
if (!capable(CAP_SYS_ADMIN)) |
| 827 |
return -EPERM; |
| 828 |
if (board->online) |
| 829 |
return -EBUSY; |
| 830 |
|
| 831 |
retval = copy_from_user(&cmd, (void __user *)arg, |
| 832 |
sizeof(struct gpib_board_type_ioctl)); |
| 833 |
if (retval) |
| 834 |
return -EFAULT; |
| 835 |
|
| 836 |
for (list_ptr = registered_drivers.next; list_ptr != ®istered_drivers; |
| 837 |
list_ptr = list_ptr->next) { |
| 838 |
struct gpib_interface_list *entry; |
| 839 |
|
| 840 |
entry = list_entry(list_ptr, struct gpib_interface_list, list); |
| 841 |
if (strcmp(entry->interface->name, cmd.name) == 0) { |
| 842 |
int i; |
| 843 |
int had_module = file_priv->got_module; |
| 844 |
|
| 845 |
if (board->use_count) { |
| 846 |
for (i = 0; i < board->use_count; ++i) |
| 847 |
module_put(board->provider_module); |
| 848 |
board->interface = NULL; |
| 849 |
file_priv->got_module = 0; |
| 850 |
} |
| 851 |
board->interface = entry->interface; |
| 852 |
board->provider_module = entry->module; |
| 853 |
for (i = 0; i < board->use_count; ++i) { |
| 854 |
if (!try_module_get(entry->module)) { |
| 855 |
board->use_count = i; |
| 856 |
return -EIO; |
| 857 |
} |
| 858 |
} |
| 859 |
if (had_module == 0) { |
| 860 |
if (!try_module_get(entry->module)) |
| 861 |
return -EIO; |
| 862 |
++board->use_count; |
| 863 |
} |
| 864 |
file_priv->got_module = 1; |
| 865 |
return 0; |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
return -EINVAL; |
| 870 |
} |
| 871 |
|
| 872 |
static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, |
| 873 |
unsigned long arg) |
| 874 |
{ |
| 875 |
struct gpib_read_write_ioctl read_cmd; |
| 876 |
u8 __user *userbuf; |
| 877 |
unsigned long remain; |
| 878 |
int end_flag = 0; |
| 879 |
int retval; |
| 880 |
ssize_t read_ret = 0; |
| 881 |
struct gpib_descriptor *desc; |
| 882 |
size_t nbytes; |
| 883 |
|
| 884 |
retval = copy_from_user(&read_cmd, (void __user *)arg, sizeof(read_cmd)); |
| 885 |
if (retval) |
| 886 |
return -EFAULT; |
| 887 |
|
| 888 |
if (read_cmd.completed_transfer_count > read_cmd.requested_transfer_count) |
| 889 |
return -EINVAL; |
| 890 |
|
| 891 |
if (WARN_ON_ONCE(sizeof(userbuf) > sizeof(read_cmd.buffer_ptr))) |
| 892 |
return -EFAULT; |
| 893 |
|
| 894 |
userbuf = (u8 __user *)(unsigned long)read_cmd.buffer_ptr; |
| 895 |
userbuf += read_cmd.completed_transfer_count; |
| 896 |
|
| 897 |
remain = read_cmd.requested_transfer_count - read_cmd.completed_transfer_count; |
| 898 |
|
| 899 |
/* Check write access to buffer */ |
| 900 |
if (!access_ok(userbuf, remain)) |
| 901 |
return -EFAULT; |
| 902 |
|
| 903 |
/* Lock descriptors to prevent concurrent close from freeing descriptor */ |
| 904 |
if (mutex_lock_interruptible(&file_priv->descriptors_mutex)) |
| 905 |
return -ERESTARTSYS; |
| 906 |
desc = handle_to_descriptor(file_priv, read_cmd.handle); |
| 907 |
if (!desc) { |
| 908 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 909 |
return -EINVAL; |
| 910 |
} |
| 911 |
atomic_inc(&desc->descriptor_busy); |
| 912 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 913 |
|
| 914 |
atomic_set(&desc->io_in_progress, 1); |
| 915 |
|
| 916 |
/* Read buffer loads till we fill the user supplied buffer */ |
| 917 |
while (remain > 0 && end_flag == 0) { |
| 918 |
nbytes = 0; |
| 919 |
read_ret = ibrd(board, board->buffer, (board->buffer_length < remain) ? |
| 920 |
board->buffer_length : remain, &end_flag, &nbytes); |
| 921 |
if (nbytes == 0) |
| 922 |
break; |
| 923 |
retval = copy_to_user(userbuf, board->buffer, nbytes); |
| 924 |
if (retval) { |
| 925 |
retval = -EFAULT; |
| 926 |
break; |
| 927 |
} |
| 928 |
remain -= nbytes; |
| 929 |
userbuf += nbytes; |
| 930 |
if (read_ret < 0) |
| 931 |
break; |
| 932 |
} |
| 933 |
read_cmd.completed_transfer_count = read_cmd.requested_transfer_count - remain; |
| 934 |
read_cmd.end = end_flag; |
| 935 |
/* |
| 936 |
* suppress errors (for example due to timeout or interruption by device clear) |
| 937 |
* if all bytes got sent. This prevents races that can occur in the various drivers |
| 938 |
* if a device receives a device clear immediately after a transfer completes and |
| 939 |
* the driver code wasn't careful enough to handle that case. |
| 940 |
*/ |
| 941 |
if (remain == 0 || end_flag) |
| 942 |
read_ret = 0; |
| 943 |
if (retval == 0) |
| 944 |
retval = copy_to_user((void __user *)arg, &read_cmd, sizeof(read_cmd)); |
| 945 |
|
| 946 |
atomic_set(&desc->io_in_progress, 0); |
| 947 |
atomic_dec(&desc->descriptor_busy); |
| 948 |
|
| 949 |
wake_up_interruptible(&board->wait); |
| 950 |
if (retval) |
| 951 |
return -EFAULT; |
| 952 |
|
| 953 |
return read_ret; |
| 954 |
} |
| 955 |
|
| 956 |
static int command_ioctl(struct gpib_file_private *file_priv, |
| 957 |
struct gpib_board *board, unsigned long arg) |
| 958 |
{ |
| 959 |
struct gpib_read_write_ioctl cmd; |
| 960 |
u8 __user *userbuf; |
| 961 |
unsigned long remain; |
| 962 |
int retval; |
| 963 |
int fault = 0; |
| 964 |
struct gpib_descriptor *desc; |
| 965 |
size_t bytes_written; |
| 966 |
int no_clear_io_in_prog; |
| 967 |
|
| 968 |
retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); |
| 969 |
if (retval) |
| 970 |
return -EFAULT; |
| 971 |
|
| 972 |
if (cmd.completed_transfer_count > cmd.requested_transfer_count) |
| 973 |
return -EINVAL; |
| 974 |
|
| 975 |
userbuf = (u8 __user *)(unsigned long)cmd.buffer_ptr; |
| 976 |
userbuf += cmd.completed_transfer_count; |
| 977 |
|
| 978 |
no_clear_io_in_prog = cmd.end; |
| 979 |
cmd.end = 0; |
| 980 |
|
| 981 |
remain = cmd.requested_transfer_count - cmd.completed_transfer_count; |
| 982 |
|
| 983 |
/* Check read access to buffer */ |
| 984 |
if (!access_ok(userbuf, remain)) |
| 985 |
return -EFAULT; |
| 986 |
|
| 987 |
/* Lock descriptors to prevent concurrent close from freeing descriptor */ |
| 988 |
if (mutex_lock_interruptible(&file_priv->descriptors_mutex)) |
| 989 |
return -ERESTARTSYS; |
| 990 |
desc = handle_to_descriptor(file_priv, cmd.handle); |
| 991 |
if (!desc) { |
| 992 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 993 |
return -EINVAL; |
| 994 |
} |
| 995 |
atomic_inc(&desc->descriptor_busy); |
| 996 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 997 |
|
| 998 |
/* |
| 999 |
* Write buffer loads till we empty the user supplied buffer. |
| 1000 |
* Call drivers at least once, even if remain is zero, in |
| 1001 |
* order to allow them to insure previous commands were |
| 1002 |
* completely finished, in the case of a restarted ioctl. |
| 1003 |
*/ |
| 1004 |
|
| 1005 |
atomic_set(&desc->io_in_progress, 1); |
| 1006 |
|
| 1007 |
do { |
| 1008 |
fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ? |
| 1009 |
board->buffer_length : remain); |
| 1010 |
if (fault) { |
| 1011 |
retval = -EFAULT; |
| 1012 |
bytes_written = 0; |
| 1013 |
} else { |
| 1014 |
retval = ibcmd(board, board->buffer, (board->buffer_length < remain) ? |
| 1015 |
board->buffer_length : remain, &bytes_written); |
| 1016 |
} |
| 1017 |
remain -= bytes_written; |
| 1018 |
userbuf += bytes_written; |
| 1019 |
if (retval < 0) { |
| 1020 |
atomic_set(&desc->io_in_progress, 0); |
| 1021 |
atomic_dec(&desc->descriptor_busy); |
| 1022 |
|
| 1023 |
wake_up_interruptible(&board->wait); |
| 1024 |
break; |
| 1025 |
} |
| 1026 |
} while (remain > 0); |
| 1027 |
|
| 1028 |
cmd.completed_transfer_count = cmd.requested_transfer_count - remain; |
| 1029 |
|
| 1030 |
if (fault == 0) |
| 1031 |
fault = copy_to_user((void __user *)arg, &cmd, sizeof(cmd)); |
| 1032 |
|
| 1033 |
/* |
| 1034 |
* no_clear_io_in_prog (cmd.end) is true when io_in_progress should |
| 1035 |
* not be set to zero because the cmd in progress is the address setup |
| 1036 |
* operation for an async read or write. This causes CMPL not to be set |
| 1037 |
* in general_ibstatus until the async read or write completes. |
| 1038 |
*/ |
| 1039 |
if (!no_clear_io_in_prog || fault) |
| 1040 |
atomic_set(&desc->io_in_progress, 0); |
| 1041 |
atomic_dec(&desc->descriptor_busy); |
| 1042 |
|
| 1043 |
wake_up_interruptible(&board->wait); |
| 1044 |
if (fault) |
| 1045 |
return -EFAULT; |
| 1046 |
|
| 1047 |
return retval; |
| 1048 |
} |
| 1049 |
|
| 1050 |
static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, |
| 1051 |
unsigned long arg) |
| 1052 |
{ |
| 1053 |
struct gpib_read_write_ioctl write_cmd; |
| 1054 |
u8 __user *userbuf; |
| 1055 |
unsigned long remain; |
| 1056 |
int retval = 0; |
| 1057 |
int fault; |
| 1058 |
struct gpib_descriptor *desc; |
| 1059 |
|
| 1060 |
fault = copy_from_user(&write_cmd, (void __user *)arg, sizeof(write_cmd)); |
| 1061 |
if (fault) |
| 1062 |
return -EFAULT; |
| 1063 |
|
| 1064 |
if (write_cmd.completed_transfer_count > write_cmd.requested_transfer_count) |
| 1065 |
return -EINVAL; |
| 1066 |
|
| 1067 |
userbuf = (u8 __user *)(unsigned long)write_cmd.buffer_ptr; |
| 1068 |
userbuf += write_cmd.completed_transfer_count; |
| 1069 |
|
| 1070 |
remain = write_cmd.requested_transfer_count - write_cmd.completed_transfer_count; |
| 1071 |
|
| 1072 |
/* Check read access to buffer */ |
| 1073 |
if (!access_ok(userbuf, remain)) |
| 1074 |
return -EFAULT; |
| 1075 |
|
| 1076 |
/* Lock descriptors to prevent concurrent close from freeing descriptor */ |
| 1077 |
if (mutex_lock_interruptible(&file_priv->descriptors_mutex)) |
| 1078 |
return -ERESTARTSYS; |
| 1079 |
desc = handle_to_descriptor(file_priv, write_cmd.handle); |
| 1080 |
if (!desc) { |
| 1081 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 1082 |
return -EINVAL; |
| 1083 |
} |
| 1084 |
atomic_inc(&desc->descriptor_busy); |
| 1085 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 1086 |
|
| 1087 |
atomic_set(&desc->io_in_progress, 1); |
| 1088 |
|
| 1089 |
/* Write buffer loads till we empty the user supplied buffer */ |
| 1090 |
while (remain > 0) { |
| 1091 |
int send_eoi; |
| 1092 |
size_t bytes_written = 0; |
| 1093 |
|
| 1094 |
send_eoi = remain <= board->buffer_length && write_cmd.end; |
| 1095 |
fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ? |
| 1096 |
board->buffer_length : remain); |
| 1097 |
if (fault) { |
| 1098 |
retval = -EFAULT; |
| 1099 |
break; |
| 1100 |
} |
| 1101 |
retval = ibwrt(board, board->buffer, (board->buffer_length < remain) ? |
| 1102 |
board->buffer_length : remain, send_eoi, &bytes_written); |
| 1103 |
remain -= bytes_written; |
| 1104 |
userbuf += bytes_written; |
| 1105 |
if (retval < 0) |
| 1106 |
break; |
| 1107 |
} |
| 1108 |
write_cmd.completed_transfer_count = write_cmd.requested_transfer_count - remain; |
| 1109 |
/* |
| 1110 |
* suppress errors (for example due to timeout or interruption by device clear) |
| 1111 |
* if all bytes got sent. This prevents races that can occur in the various drivers |
| 1112 |
* if a device receives a device clear immediately after a transfer completes and |
| 1113 |
* the driver code wasn't careful enough to handle that case. |
| 1114 |
*/ |
| 1115 |
if (remain == 0) |
| 1116 |
retval = 0; |
| 1117 |
if (fault == 0) |
| 1118 |
fault = copy_to_user((void __user *)arg, &write_cmd, sizeof(write_cmd)); |
| 1119 |
|
| 1120 |
atomic_set(&desc->io_in_progress, 0); |
| 1121 |
atomic_dec(&desc->descriptor_busy); |
| 1122 |
|
| 1123 |
wake_up_interruptible(&board->wait); |
| 1124 |
if (fault) |
| 1125 |
return -EFAULT; |
| 1126 |
|
| 1127 |
return retval; |
| 1128 |
} |
| 1129 |
|
| 1130 |
static int status_bytes_ioctl(struct gpib_board *board, unsigned long arg) |
| 1131 |
{ |
| 1132 |
struct gpib_status_queue *device; |
| 1133 |
struct gpib_spoll_bytes_ioctl cmd; |
| 1134 |
int retval; |
| 1135 |
|
| 1136 |
retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); |
| 1137 |
if (retval) |
| 1138 |
return -EFAULT; |
| 1139 |
|
| 1140 |
device = get_gpib_status_queue(board, cmd.pad, cmd.sad); |
| 1141 |
if (!device) |
| 1142 |
cmd.num_bytes = 0; |
| 1143 |
else |
| 1144 |
cmd.num_bytes = num_status_bytes(device); |
| 1145 |
|
| 1146 |
retval = copy_to_user((void __user *)arg, &cmd, sizeof(cmd)); |
| 1147 |
if (retval) |
| 1148 |
return -EFAULT; |
| 1149 |
|
| 1150 |
return 0; |
| 1151 |
} |
| 1152 |
|
| 1153 |
static int increment_open_device_count(struct gpib_board *board, struct list_head *head, |
| 1154 |
unsigned int pad, int sad) |
| 1155 |
{ |
| 1156 |
struct list_head *list_ptr; |
| 1157 |
struct gpib_status_queue *device; |
| 1158 |
|
| 1159 |
/* |
| 1160 |
* first see if address has already been opened, then increment |
| 1161 |
* open count |
| 1162 |
*/ |
| 1163 |
for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) { |
| 1164 |
device = list_entry(list_ptr, struct gpib_status_queue, list); |
| 1165 |
if (gpib_address_equal(device->pad, device->sad, pad, sad)) { |
| 1166 |
dev_dbg(board->gpib_dev, "incrementing open count for pad %i, sad %i\n", |
| 1167 |
device->pad, device->sad); |
| 1168 |
device->reference_count++; |
| 1169 |
return 0; |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
/* otherwise we need to allocate a new struct gpib_status_queue */ |
| 1174 |
device = kmalloc_obj(struct gpib_status_queue, GFP_ATOMIC); |
| 1175 |
if (!device) |
| 1176 |
return -ENOMEM; |
| 1177 |
init_gpib_status_queue(device); |
| 1178 |
device->pad = pad; |
| 1179 |
device->sad = sad; |
| 1180 |
device->reference_count = 1; |
| 1181 |
|
| 1182 |
list_add(&device->list, head); |
| 1183 |
|
| 1184 |
dev_dbg(board->gpib_dev, "opened pad %i, sad %i\n", device->pad, device->sad); |
| 1185 |
|
| 1186 |
return 0; |
| 1187 |
} |
| 1188 |
|
| 1189 |
static int subtract_open_device_count(struct gpib_board *board, struct list_head *head, |
| 1190 |
unsigned int pad, int sad, unsigned int count) |
| 1191 |
{ |
| 1192 |
struct gpib_status_queue *device; |
| 1193 |
struct list_head *list_ptr; |
| 1194 |
|
| 1195 |
for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) { |
| 1196 |
device = list_entry(list_ptr, struct gpib_status_queue, list); |
| 1197 |
if (gpib_address_equal(device->pad, device->sad, pad, sad)) { |
| 1198 |
dev_dbg(board->gpib_dev, "decrementing open count for pad %i, sad %i\n", |
| 1199 |
device->pad, device->sad); |
| 1200 |
if (count > device->reference_count) { |
| 1201 |
dev_err(board->gpib_dev, "bug! in %s()\n", __func__); |
| 1202 |
return -EINVAL; |
| 1203 |
} |
| 1204 |
device->reference_count -= count; |
| 1205 |
if (device->reference_count == 0) { |
| 1206 |
dev_dbg(board->gpib_dev, "closing pad %i, sad %i\n", |
| 1207 |
device->pad, device->sad); |
| 1208 |
list_del(list_ptr); |
| 1209 |
kfree(device); |
| 1210 |
} |
| 1211 |
return 0; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
dev_err(board->gpib_dev, "bug! tried to close address that was never opened!\n"); |
| 1215 |
return -EINVAL; |
| 1216 |
} |
| 1217 |
|
| 1218 |
static inline int decrement_open_device_count(struct gpib_board *board, struct list_head *head, |
| 1219 |
unsigned int pad, int sad) |
| 1220 |
{ |
| 1221 |
return subtract_open_device_count(board, head, pad, sad, 1); |
| 1222 |
} |
| 1223 |
|
| 1224 |
static int cleanup_open_devices(struct gpib_file_private *file_priv, struct gpib_board *board) |
| 1225 |
{ |
| 1226 |
int retval = 0; |
| 1227 |
int i; |
| 1228 |
|
| 1229 |
for (i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++) { |
| 1230 |
struct gpib_descriptor *desc; |
| 1231 |
|
| 1232 |
desc = file_priv->descriptors[i]; |
| 1233 |
if (!desc) |
| 1234 |
continue; |
| 1235 |
|
| 1236 |
if (desc->is_board == 0) { |
| 1237 |
retval = decrement_open_device_count(board, &board->device_list, desc->pad, |
| 1238 |
desc->sad); |
| 1239 |
if (retval < 0) |
| 1240 |
return retval; |
| 1241 |
} |
| 1242 |
kfree(desc); |
| 1243 |
file_priv->descriptors[i] = NULL; |
| 1244 |
} |
| 1245 |
|
| 1246 |
return 0; |
| 1247 |
} |
| 1248 |
|
| 1249 |
static int open_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg) |
| 1250 |
{ |
| 1251 |
struct gpib_open_dev_ioctl open_dev_cmd; |
| 1252 |
int retval; |
| 1253 |
struct gpib_file_private *file_priv = filep->private_data; |
| 1254 |
int i; |
| 1255 |
|
| 1256 |
retval = copy_from_user(&open_dev_cmd, (void __user *)arg, sizeof(open_dev_cmd)); |
| 1257 |
if (retval) |
| 1258 |
return -EFAULT; |
| 1259 |
|
| 1260 |
if (mutex_lock_interruptible(&file_priv->descriptors_mutex)) |
| 1261 |
return -ERESTARTSYS; |
| 1262 |
for (i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++) |
| 1263 |
if (!file_priv->descriptors[i]) |
| 1264 |
break; |
| 1265 |
if (i == GPIB_MAX_NUM_DESCRIPTORS) { |
| 1266 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 1267 |
return -ERANGE; |
| 1268 |
} |
| 1269 |
file_priv->descriptors[i] = kmalloc_obj(struct gpib_descriptor); |
| 1270 |
if (!file_priv->descriptors[i]) { |
| 1271 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 1272 |
return -ENOMEM; |
| 1273 |
} |
| 1274 |
init_gpib_descriptor(file_priv->descriptors[i]); |
| 1275 |
|
| 1276 |
file_priv->descriptors[i]->pad = open_dev_cmd.pad; |
| 1277 |
file_priv->descriptors[i]->sad = open_dev_cmd.sad; |
| 1278 |
file_priv->descriptors[i]->is_board = open_dev_cmd.is_board; |
| 1279 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 1280 |
|
| 1281 |
retval = increment_open_device_count(board, &board->device_list, open_dev_cmd.pad, |
| 1282 |
open_dev_cmd.sad); |
| 1283 |
if (retval < 0) |
| 1284 |
return retval; |
| 1285 |
|
| 1286 |
/* |
| 1287 |
* clear stuck srq state, since we may be able to find service request on |
| 1288 |
* the new device |
| 1289 |
*/ |
| 1290 |
atomic_set(&board->stuck_srq, 0); |
| 1291 |
|
| 1292 |
open_dev_cmd.handle = i; |
| 1293 |
retval = copy_to_user((void __user *)arg, &open_dev_cmd, sizeof(open_dev_cmd)); |
| 1294 |
if (retval) |
| 1295 |
return -EFAULT; |
| 1296 |
|
| 1297 |
return 0; |
| 1298 |
} |
| 1299 |
|
| 1300 |
static int close_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg) |
| 1301 |
{ |
| 1302 |
struct gpib_close_dev_ioctl cmd; |
| 1303 |
struct gpib_file_private *file_priv = filep->private_data; |
| 1304 |
struct gpib_descriptor *desc; |
| 1305 |
unsigned int pad; |
| 1306 |
int sad; |
| 1307 |
int retval; |
| 1308 |
|
| 1309 |
retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); |
| 1310 |
if (retval) |
| 1311 |
return -EFAULT; |
| 1312 |
|
| 1313 |
if (cmd.handle >= GPIB_MAX_NUM_DESCRIPTORS) |
| 1314 |
return -EINVAL; |
| 1315 |
|
| 1316 |
mutex_lock(&file_priv->descriptors_mutex); |
| 1317 |
desc = file_priv->descriptors[cmd.handle]; |
| 1318 |
if (!desc) { |
| 1319 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 1320 |
return -EINVAL; |
| 1321 |
} |
| 1322 |
if (atomic_read(&desc->descriptor_busy)) { |
| 1323 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 1324 |
return -EBUSY; |
| 1325 |
} |
| 1326 |
/* Remove from table while holding lock to prevent new IO from starting */ |
| 1327 |
file_priv->descriptors[cmd.handle] = NULL; |
| 1328 |
pad = desc->pad; |
| 1329 |
sad = desc->sad; |
| 1330 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 1331 |
|
| 1332 |
retval = decrement_open_device_count(board, &board->device_list, pad, sad); |
| 1333 |
|
| 1334 |
kfree(desc); |
| 1335 |
return retval; |
| 1336 |
} |
| 1337 |
|
| 1338 |
static int serial_poll_ioctl(struct gpib_board *board, unsigned long arg) |
| 1339 |
{ |
| 1340 |
struct gpib_serial_poll_ioctl serial_cmd; |
| 1341 |
int retval; |
| 1342 |
|
| 1343 |
retval = copy_from_user(&serial_cmd, (void __user *)arg, sizeof(serial_cmd)); |
| 1344 |
if (retval) |
| 1345 |
return -EFAULT; |
| 1346 |
|
| 1347 |
retval = get_serial_poll_byte(board, serial_cmd.pad, serial_cmd.sad, board->usec_timeout, |
| 1348 |
&serial_cmd.status_byte); |
| 1349 |
if (retval < 0) |
| 1350 |
return retval; |
| 1351 |
|
| 1352 |
retval = copy_to_user((void __user *)arg, &serial_cmd, sizeof(serial_cmd)); |
| 1353 |
if (retval) |
| 1354 |
return -EFAULT; |
| 1355 |
|
| 1356 |
return 0; |
| 1357 |
} |
| 1358 |
|
| 1359 |
static int wait_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, |
| 1360 |
unsigned long arg) |
| 1361 |
{ |
| 1362 |
struct gpib_wait_ioctl wait_cmd; |
| 1363 |
int retval; |
| 1364 |
struct gpib_descriptor *desc; |
| 1365 |
|
| 1366 |
retval = copy_from_user(&wait_cmd, (void __user *)arg, sizeof(wait_cmd)); |
| 1367 |
if (retval) |
| 1368 |
return -EFAULT; |
| 1369 |
|
| 1370 |
/* |
| 1371 |
* Lock descriptors to prevent concurrent close from freeing |
| 1372 |
* descriptor. ibwait() releases big_gpib_mutex when wait_mask |
| 1373 |
* is non-zero, so desc must be pinned with descriptor_busy. |
| 1374 |
*/ |
| 1375 |
mutex_lock(&file_priv->descriptors_mutex); |
| 1376 |
desc = handle_to_descriptor(file_priv, wait_cmd.handle); |
| 1377 |
if (!desc) { |
| 1378 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 1379 |
return -EINVAL; |
| 1380 |
} |
| 1381 |
atomic_inc(&desc->descriptor_busy); |
| 1382 |
mutex_unlock(&file_priv->descriptors_mutex); |
| 1383 |
|
| 1384 |
retval = ibwait(board, wait_cmd.wait_mask, wait_cmd.clear_mask, |
| 1385 |
wait_cmd.set_mask, &wait_cmd.ibsta, wait_cmd.usec_timeout, desc); |
| 1386 |
|
| 1387 |
atomic_dec(&desc->descriptor_busy); |
| 1388 |
|
| 1389 |
if (retval < 0) |
| 1390 |
return retval; |
| 1391 |
|
| 1392 |
retval = copy_to_user((void __user *)arg, &wait_cmd, sizeof(wait_cmd)); |
| 1393 |
if (retval) |
| 1394 |
return -EFAULT; |
| 1395 |
|
| 1396 |
return 0; |
| 1397 |
} |
| 1398 |
|
| 1399 |
static int parallel_poll_ioctl(struct gpib_board *board, unsigned long arg) |
| 1400 |
{ |
| 1401 |
u8 poll_byte; |
| 1402 |
int retval; |
| 1403 |
|
| 1404 |
retval = ibrpp(board, &poll_byte); |
| 1405 |
if (retval < 0) |
| 1406 |
return retval; |
| 1407 |
|
| 1408 |
retval = copy_to_user((void __user *)arg, &poll_byte, sizeof(poll_byte)); |
| 1409 |
if (retval) |
| 1410 |
return -EFAULT; |
| 1411 |
|
| 1412 |
return 0; |
| 1413 |
} |
| 1414 |
|
| 1415 |
static int online_ioctl(struct gpib_board *board, unsigned long arg) |
| 1416 |
{ |
| 1417 |
struct gpib_online_ioctl online_cmd; |
| 1418 |
int retval; |
| 1419 |
void __user *init_data = NULL; |
| 1420 |
|
| 1421 |
board->config.init_data = NULL; |
| 1422 |
|
| 1423 |
if (!capable(CAP_SYS_ADMIN)) |
| 1424 |
return -EPERM; |
| 1425 |
|
| 1426 |
retval = copy_from_user(&online_cmd, (void __user *)arg, sizeof(online_cmd)); |
| 1427 |
if (retval) |
| 1428 |
return -EFAULT; |
| 1429 |
if (online_cmd.init_data_length > 0) { |
| 1430 |
board->config.init_data = vmalloc(online_cmd.init_data_length); |
| 1431 |
if (!board->config.init_data) |
| 1432 |
return -ENOMEM; |
| 1433 |
if (WARN_ON_ONCE(sizeof(init_data) > sizeof(online_cmd.init_data_ptr))) |
| 1434 |
return -EFAULT; |
| 1435 |
init_data = (void __user *)(unsigned long)(online_cmd.init_data_ptr); |
| 1436 |
retval = copy_from_user(board->config.init_data, init_data, |
| 1437 |
online_cmd.init_data_length); |
| 1438 |
if (retval) { |
| 1439 |
vfree(board->config.init_data); |
| 1440 |
return -EFAULT; |
| 1441 |
} |
| 1442 |
board->config.init_data_length = online_cmd.init_data_length; |
| 1443 |
} else { |
| 1444 |
board->config.init_data = NULL; |
| 1445 |
board->config.init_data_length = 0; |
| 1446 |
} |
| 1447 |
if (online_cmd.online) |
| 1448 |
retval = ibonline(board); |
| 1449 |
else |
| 1450 |
retval = iboffline(board); |
| 1451 |
if (board->config.init_data) { |
| 1452 |
vfree(board->config.init_data); |
| 1453 |
board->config.init_data = NULL; |
| 1454 |
board->config.init_data_length = 0; |
| 1455 |
} |
| 1456 |
return retval; |
| 1457 |
} |
| 1458 |
|
| 1459 |
static int remote_enable_ioctl(struct gpib_board *board, unsigned long arg) |
| 1460 |
{ |
| 1461 |
int enable; |
| 1462 |
int retval; |
| 1463 |
|
| 1464 |
retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable)); |
| 1465 |
if (retval) |
| 1466 |
return -EFAULT; |
| 1467 |
|
| 1468 |
return ibsre(board, enable); |
| 1469 |
} |
| 1470 |
|
| 1471 |
static int take_control_ioctl(struct gpib_board *board, unsigned long arg) |
| 1472 |
{ |
| 1473 |
int synchronous; |
| 1474 |
int retval; |
| 1475 |
|
| 1476 |
retval = copy_from_user(&synchronous, (void __user *)arg, sizeof(synchronous)); |
| 1477 |
if (retval) |
| 1478 |
return -EFAULT; |
| 1479 |
|
| 1480 |
return ibcac(board, synchronous, 1); |
| 1481 |
} |
| 1482 |
|
| 1483 |
static int line_status_ioctl(struct gpib_board *board, unsigned long arg) |
| 1484 |
{ |
| 1485 |
short lines; |
| 1486 |
int retval; |
| 1487 |
|
| 1488 |
retval = iblines(board, &lines); |
| 1489 |
if (retval < 0) |
| 1490 |
return retval; |
| 1491 |
|
| 1492 |
retval = copy_to_user((void __user *)arg, &lines, sizeof(lines)); |
| 1493 |
if (retval) |
| 1494 |
return -EFAULT; |
| 1495 |
|
| 1496 |
return 0; |
| 1497 |
} |
| 1498 |
|
| 1499 |
static int pad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, |
| 1500 |
unsigned long arg) |
| 1501 |
{ |
| 1502 |
struct gpib_pad_ioctl cmd; |
| 1503 |
int retval; |
| 1504 |
struct gpib_descriptor *desc; |
| 1505 |
|
| 1506 |
retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); |
| 1507 |
if (retval) |
| 1508 |
return -EFAULT; |
| 1509 |
|
| 1510 |
desc = handle_to_descriptor(file_priv, cmd.handle); |
| 1511 |
if (!desc) |
| 1512 |
return -EINVAL; |
| 1513 |
|
| 1514 |
if (desc->is_board) { |
| 1515 |
retval = ibpad(board, cmd.pad); |
| 1516 |
if (retval < 0) |
| 1517 |
return retval; |
| 1518 |
} else { |
| 1519 |
retval = decrement_open_device_count(board, &board->device_list, desc->pad, |
| 1520 |
desc->sad); |
| 1521 |
if (retval < 0) |
| 1522 |
return retval; |
| 1523 |
|
| 1524 |
desc->pad = cmd.pad; |
| 1525 |
|
| 1526 |
retval = increment_open_device_count(board, &board->device_list, desc->pad, |
| 1527 |
desc->sad); |
| 1528 |
if (retval < 0) |
| 1529 |
return retval; |
| 1530 |
} |
| 1531 |
|
| 1532 |
return 0; |
| 1533 |
} |
| 1534 |
|
| 1535 |
static int sad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, |
| 1536 |
unsigned long arg) |
| 1537 |
{ |
| 1538 |
struct gpib_sad_ioctl cmd; |
| 1539 |
int retval; |
| 1540 |
struct gpib_descriptor *desc; |
| 1541 |
|
| 1542 |
retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); |
| 1543 |
if (retval) |
| 1544 |
return -EFAULT; |
| 1545 |
|
| 1546 |
desc = handle_to_descriptor(file_priv, cmd.handle); |
| 1547 |
if (!desc) |
| 1548 |
return -EINVAL; |
| 1549 |
|
| 1550 |
if (desc->is_board) { |
| 1551 |
retval = ibsad(board, cmd.sad); |
| 1552 |
if (retval < 0) |
| 1553 |
return retval; |
| 1554 |
} else { |
| 1555 |
retval = decrement_open_device_count(board, &board->device_list, desc->pad, |
| 1556 |
desc->sad); |
| 1557 |
if (retval < 0) |
| 1558 |
return retval; |
| 1559 |
|
| 1560 |
desc->sad = cmd.sad; |
| 1561 |
|
| 1562 |
retval = increment_open_device_count(board, &board->device_list, desc->pad, |
| 1563 |
desc->sad); |
| 1564 |
if (retval < 0) |
| 1565 |
return retval; |
| 1566 |
} |
| 1567 |
return 0; |
| 1568 |
} |
| 1569 |
|
| 1570 |
static int eos_ioctl(struct gpib_board *board, unsigned long arg) |
| 1571 |
{ |
| 1572 |
struct gpib_eos_ioctl eos_cmd; |
| 1573 |
int retval; |
| 1574 |
|
| 1575 |
retval = copy_from_user(&eos_cmd, (void __user *)arg, sizeof(eos_cmd)); |
| 1576 |
if (retval) |
| 1577 |
return -EFAULT; |
| 1578 |
|
| 1579 |
return ibeos(board, eos_cmd.eos, eos_cmd.eos_flags); |
| 1580 |
} |
| 1581 |
|
| 1582 |
static int request_service_ioctl(struct gpib_board *board, unsigned long arg) |
| 1583 |
{ |
| 1584 |
u8 status_byte; |
| 1585 |
int retval; |
| 1586 |
|
| 1587 |
retval = copy_from_user(&status_byte, (void __user *)arg, sizeof(status_byte)); |
| 1588 |
if (retval) |
| 1589 |
return -EFAULT; |
| 1590 |
|
| 1591 |
return ibrsv2(board, status_byte, status_byte & request_service_bit); |
| 1592 |
} |
| 1593 |
|
| 1594 |
static int request_service2_ioctl(struct gpib_board *board, unsigned long arg) |
| 1595 |
{ |
| 1596 |
struct gpib_request_service2 request_service2_cmd; |
| 1597 |
int retval; |
| 1598 |
|
| 1599 |
retval = copy_from_user(&request_service2_cmd, (void __user *)arg, |
| 1600 |
sizeof(struct gpib_request_service2)); |
| 1601 |
if (retval) |
| 1602 |
return -EFAULT; |
| 1603 |
|
| 1604 |
return ibrsv2(board, request_service2_cmd.status_byte, |
| 1605 |
request_service2_cmd.new_reason_for_service); |
| 1606 |
} |
| 1607 |
|
| 1608 |
static int iobase_ioctl(struct gpib_board_config *config, unsigned long arg) |
| 1609 |
{ |
| 1610 |
u64 base_addr; |
| 1611 |
int retval; |
| 1612 |
|
| 1613 |
if (!capable(CAP_SYS_ADMIN)) |
| 1614 |
return -EPERM; |
| 1615 |
|
| 1616 |
retval = copy_from_user(&base_addr, (void __user *)arg, sizeof(base_addr)); |
| 1617 |
if (retval) |
| 1618 |
return -EFAULT; |
| 1619 |
|
| 1620 |
if (WARN_ON_ONCE(sizeof(void *) > sizeof(base_addr))) |
| 1621 |
return -EFAULT; |
| 1622 |
config->ibbase = base_addr; |
| 1623 |
|
| 1624 |
return 0; |
| 1625 |
} |
| 1626 |
|
| 1627 |
static int irq_ioctl(struct gpib_board_config *config, unsigned long arg) |
| 1628 |
{ |
| 1629 |
unsigned int irq; |
| 1630 |
int retval; |
| 1631 |
|
| 1632 |
if (!capable(CAP_SYS_ADMIN)) |
| 1633 |
return -EPERM; |
| 1634 |
|
| 1635 |
retval = copy_from_user(&irq, (void __user *)arg, sizeof(irq)); |
| 1636 |
if (retval) |
| 1637 |
return -EFAULT; |
| 1638 |
|
| 1639 |
config->ibirq = irq; |
| 1640 |
|
| 1641 |
return 0; |
| 1642 |
} |
| 1643 |
|
| 1644 |
static int dma_ioctl(struct gpib_board_config *config, unsigned long arg) |
| 1645 |
{ |
| 1646 |
unsigned int dma_channel; |
| 1647 |
int retval; |
| 1648 |
|
| 1649 |
if (!capable(CAP_SYS_ADMIN)) |
| 1650 |
return -EPERM; |
| 1651 |
|
| 1652 |
retval = copy_from_user(&dma_channel, (void __user *)arg, sizeof(dma_channel)); |
| 1653 |
if (retval) |
| 1654 |
return -EFAULT; |
| 1655 |
|
| 1656 |
config->ibdma = dma_channel; |
| 1657 |
|
| 1658 |
return 0; |
| 1659 |
} |
| 1660 |
|
| 1661 |
static int autospoll_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, |
| 1662 |
unsigned long arg) |
| 1663 |
{ |
| 1664 |
short enable; |
| 1665 |
int retval; |
| 1666 |
struct gpib_descriptor *desc; |
| 1667 |
|
| 1668 |
retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable)); |
| 1669 |
if (retval) |
| 1670 |
return -EFAULT; |
| 1671 |
|
| 1672 |
desc = handle_to_descriptor(file_priv, 0); /* board handle is 0 */ |
| 1673 |
|
| 1674 |
if (enable) { |
| 1675 |
if (!desc->autopoll_enabled) { |
| 1676 |
board->autospollers++; |
| 1677 |
desc->autopoll_enabled = 1; |
| 1678 |
} |
| 1679 |
retval = 0; |
| 1680 |
} else { |
| 1681 |
if (desc->autopoll_enabled) { |
| 1682 |
desc->autopoll_enabled = 0; |
| 1683 |
if (board->autospollers > 0) { |
| 1684 |
board->autospollers--; |
| 1685 |
retval = 0; |
| 1686 |
} else { |
| 1687 |
dev_err(board->gpib_dev, |
| 1688 |
"tried to set number of autospollers negative\n"); |
| 1689 |
retval = -EINVAL; |
| 1690 |
} |
| 1691 |
} else { |
| 1692 |
dev_err(board->gpib_dev, "autopoll disable requested before enable\n"); |
| 1693 |
retval = -EINVAL; |
| 1694 |
} |
| 1695 |
} |
| 1696 |
return retval; |
| 1697 |
} |
| 1698 |
|
| 1699 |
static int mutex_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, |
| 1700 |
unsigned long arg) |
| 1701 |
{ |
| 1702 |
int retval, lock_mutex; |
| 1703 |
|
| 1704 |
retval = copy_from_user(&lock_mutex, (void __user *)arg, sizeof(lock_mutex)); |
| 1705 |
if (retval) |
| 1706 |
return -EFAULT; |
| 1707 |
|
| 1708 |
if (lock_mutex) { |
| 1709 |
retval = mutex_lock_interruptible(&board->user_mutex); |
| 1710 |
if (retval) |
| 1711 |
return -ERESTARTSYS; |
| 1712 |
|
| 1713 |
spin_lock(&board->locking_pid_spinlock); |
| 1714 |
board->locking_pid = current->pid; |
| 1715 |
spin_unlock(&board->locking_pid_spinlock); |
| 1716 |
|
| 1717 |
atomic_set(&file_priv->holding_mutex, 1); |
| 1718 |
|
| 1719 |
dev_dbg(board->gpib_dev, "locked board mutex\n"); |
| 1720 |
} else { |
| 1721 |
spin_lock(&board->locking_pid_spinlock); |
| 1722 |
if (current->pid != board->locking_pid) { |
| 1723 |
dev_err(board->gpib_dev, "bug! pid %i tried to release mutex held by pid %i\n", |
| 1724 |
current->pid, board->locking_pid); |
| 1725 |
spin_unlock(&board->locking_pid_spinlock); |
| 1726 |
return -EPERM; |
| 1727 |
} |
| 1728 |
board->locking_pid = 0; |
| 1729 |
spin_unlock(&board->locking_pid_spinlock); |
| 1730 |
|
| 1731 |
atomic_set(&file_priv->holding_mutex, 0); |
| 1732 |
|
| 1733 |
mutex_unlock(&board->user_mutex); |
| 1734 |
dev_dbg(board->gpib_dev, "unlocked board mutex\n"); |
| 1735 |
} |
| 1736 |
return 0; |
| 1737 |
} |
| 1738 |
|
| 1739 |
static int timeout_ioctl(struct gpib_board *board, unsigned long arg) |
| 1740 |
{ |
| 1741 |
unsigned int timeout; |
| 1742 |
int retval; |
| 1743 |
|
| 1744 |
retval = copy_from_user(&timeout, (void __user *)arg, sizeof(timeout)); |
| 1745 |
if (retval) |
| 1746 |
return -EFAULT; |
| 1747 |
|
| 1748 |
board->usec_timeout = timeout; |
| 1749 |
dev_dbg(board->gpib_dev, "timeout set to %i usec\n", timeout); |
| 1750 |
|
| 1751 |
return 0; |
| 1752 |
} |
| 1753 |
|
| 1754 |
static int ppc_ioctl(struct gpib_board *board, unsigned long arg) |
| 1755 |
{ |
| 1756 |
struct gpib_ppoll_config_ioctl cmd; |
| 1757 |
int retval; |
| 1758 |
|
| 1759 |
retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); |
| 1760 |
if (retval) |
| 1761 |
return -EFAULT; |
| 1762 |
|
| 1763 |
if (cmd.set_ist) { |
| 1764 |
board->ist = 1; |
| 1765 |
board->interface->parallel_poll_response(board, board->ist); |
| 1766 |
} else if (cmd.clear_ist) { |
| 1767 |
board->ist = 0; |
| 1768 |
board->interface->parallel_poll_response(board, board->ist); |
| 1769 |
} |
| 1770 |
|
| 1771 |
if (cmd.config) { |
| 1772 |
retval = ibppc(board, cmd.config); |
| 1773 |
if (retval < 0) |
| 1774 |
return retval; |
| 1775 |
} |
| 1776 |
|
| 1777 |
return 0; |
| 1778 |
} |
| 1779 |
|
| 1780 |
static int set_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg) |
| 1781 |
{ |
| 1782 |
short cmd; |
| 1783 |
int retval; |
| 1784 |
|
| 1785 |
retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); |
| 1786 |
if (retval) |
| 1787 |
return -EFAULT; |
| 1788 |
|
| 1789 |
if (!board->interface->local_parallel_poll_mode) |
| 1790 |
return -ENOENT; |
| 1791 |
board->local_ppoll_mode = cmd != 0; |
| 1792 |
board->interface->local_parallel_poll_mode(board, board->local_ppoll_mode); |
| 1793 |
|
| 1794 |
return 0; |
| 1795 |
} |
| 1796 |
|
| 1797 |
static int get_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg) |
| 1798 |
{ |
| 1799 |
short cmd; |
| 1800 |
int retval; |
| 1801 |
|
| 1802 |
cmd = board->local_ppoll_mode; |
| 1803 |
retval = copy_to_user((void __user *)arg, &cmd, sizeof(cmd)); |
| 1804 |
if (retval) |
| 1805 |
return -EFAULT; |
| 1806 |
|
| 1807 |
return 0; |
| 1808 |
} |
| 1809 |
|
| 1810 |
static int query_board_rsv_ioctl(struct gpib_board *board, unsigned long arg) |
| 1811 |
{ |
| 1812 |
int status; |
| 1813 |
int retval; |
| 1814 |
|
| 1815 |
status = board->interface->serial_poll_status(board); |
| 1816 |
|
| 1817 |
retval = copy_to_user((void __user *)arg, &status, sizeof(status)); |
| 1818 |
if (retval) |
| 1819 |
return -EFAULT; |
| 1820 |
|
| 1821 |
return 0; |
| 1822 |
} |
| 1823 |
|
| 1824 |
static int board_info_ioctl(const struct gpib_board *board, unsigned long arg) |
| 1825 |
{ |
| 1826 |
struct gpib_board_info_ioctl info = { }; |
| 1827 |
int retval; |
| 1828 |
|
| 1829 |
info.pad = board->pad; |
| 1830 |
info.sad = board->sad; |
| 1831 |
info.parallel_poll_configuration = board->parallel_poll_configuration; |
| 1832 |
info.is_system_controller = board->master; |
| 1833 |
if (board->autospollers) |
| 1834 |
info.autopolling = 1; |
| 1835 |
else |
| 1836 |
info.autopolling = 0; |
| 1837 |
info.t1_delay = board->t1_nano_sec; |
| 1838 |
info.ist = board->ist; |
| 1839 |
info.no_7_bit_eos = board->interface->no_7_bit_eos; |
| 1840 |
retval = copy_to_user((void __user *)arg, &info, sizeof(info)); |
| 1841 |
if (retval) |
| 1842 |
return -EFAULT; |
| 1843 |
|
| 1844 |
return 0; |
| 1845 |
} |
| 1846 |
|
| 1847 |
static int interface_clear_ioctl(struct gpib_board *board, unsigned long arg) |
| 1848 |
{ |
| 1849 |
unsigned int usec_duration; |
| 1850 |
int retval; |
| 1851 |
|
| 1852 |
retval = copy_from_user(&usec_duration, (void __user *)arg, sizeof(usec_duration)); |
| 1853 |
if (retval) |
| 1854 |
return -EFAULT; |
| 1855 |
|
| 1856 |
return ibsic(board, usec_duration); |
| 1857 |
} |
| 1858 |
|
| 1859 |
static int select_pci_ioctl(struct gpib_board_config *config, unsigned long arg) |
| 1860 |
{ |
| 1861 |
struct gpib_select_pci_ioctl selection; |
| 1862 |
int retval; |
| 1863 |
|
| 1864 |
if (!capable(CAP_SYS_ADMIN)) |
| 1865 |
return -EPERM; |
| 1866 |
|
| 1867 |
retval = copy_from_user(&selection, (void __user *)arg, sizeof(selection)); |
| 1868 |
if (retval) |
| 1869 |
return -EFAULT; |
| 1870 |
|
| 1871 |
config->pci_bus = selection.pci_bus; |
| 1872 |
config->pci_slot = selection.pci_slot; |
| 1873 |
|
| 1874 |
return 0; |
| 1875 |
} |
| 1876 |
|
| 1877 |
static int select_device_path_ioctl(struct gpib_board_config *config, unsigned long arg) |
| 1878 |
{ |
| 1879 |
struct gpib_select_device_path_ioctl *selection; |
| 1880 |
int retval; |
| 1881 |
|
| 1882 |
if (!capable(CAP_SYS_ADMIN)) |
| 1883 |
return -EPERM; |
| 1884 |
|
| 1885 |
selection = vmalloc(sizeof(struct gpib_select_device_path_ioctl)); |
| 1886 |
if (!selection) |
| 1887 |
return -ENOMEM; |
| 1888 |
|
| 1889 |
retval = copy_from_user(selection, (void __user *)arg, |
| 1890 |
sizeof(struct gpib_select_device_path_ioctl)); |
| 1891 |
if (retval) { |
| 1892 |
vfree(selection); |
| 1893 |
return -EFAULT; |
| 1894 |
} |
| 1895 |
|
| 1896 |
selection->device_path[sizeof(selection->device_path) - 1] = '\0'; |
| 1897 |
kfree(config->device_path); |
| 1898 |
config->device_path = NULL; |
| 1899 |
if (strlen(selection->device_path) > 0) |
| 1900 |
config->device_path = kstrdup(selection->device_path, GFP_KERNEL); |
| 1901 |
|
| 1902 |
vfree(selection); |
| 1903 |
return 0; |
| 1904 |
} |
| 1905 |
|
| 1906 |
unsigned int num_gpib_events(const struct gpib_event_queue *queue) |
| 1907 |
{ |
| 1908 |
return queue->num_events; |
| 1909 |
} |
| 1910 |
|
| 1911 |
static int push_gpib_event_nolock(struct gpib_board *board, short event_type) |
| 1912 |
{ |
| 1913 |
struct gpib_event_queue *queue = &board->event_queue; |
| 1914 |
struct list_head *head = &queue->event_head; |
| 1915 |
struct gpib_event *event; |
| 1916 |
static const unsigned int max_num_events = 1024; |
| 1917 |
int retval; |
| 1918 |
|
| 1919 |
if (num_gpib_events(queue) >= max_num_events) { |
| 1920 |
short lost_event; |
| 1921 |
|
| 1922 |
queue->dropped_event = 1; |
| 1923 |
retval = pop_gpib_event_nolock(board, queue, &lost_event); |
| 1924 |
if (retval < 0) |
| 1925 |
return retval; |
| 1926 |
} |
| 1927 |
|
| 1928 |
event = kmalloc_obj(struct gpib_event, GFP_ATOMIC); |
| 1929 |
if (!event) { |
| 1930 |
queue->dropped_event = 1; |
| 1931 |
dev_err(board->gpib_dev, "failed to allocate memory for event\n"); |
| 1932 |
return -ENOMEM; |
| 1933 |
} |
| 1934 |
|
| 1935 |
INIT_LIST_HEAD(&event->list); |
| 1936 |
event->event_type = event_type; |
| 1937 |
|
| 1938 |
list_add_tail(&event->list, head); |
| 1939 |
|
| 1940 |
queue->num_events++; |
| 1941 |
|
| 1942 |
dev_dbg(board->gpib_dev, "pushed event %i, %i in queue\n", |
| 1943 |
(int)event_type, num_gpib_events(queue)); |
| 1944 |
|
| 1945 |
return 0; |
| 1946 |
} |
| 1947 |
|
| 1948 |
// push event onto back of event queue |
| 1949 |
int push_gpib_event(struct gpib_board *board, short event_type) |
| 1950 |
{ |
| 1951 |
unsigned long flags; |
| 1952 |
int retval; |
| 1953 |
|
| 1954 |
spin_lock_irqsave(&board->event_queue.lock, flags); |
| 1955 |
retval = push_gpib_event_nolock(board, event_type); |
| 1956 |
spin_unlock_irqrestore(&board->event_queue.lock, flags); |
| 1957 |
|
| 1958 |
if (event_type == EVENT_DEV_TRG) |
| 1959 |
board->status |= DTAS; |
| 1960 |
if (event_type == EVENT_DEV_CLR) |
| 1961 |
board->status |= DCAS; |
| 1962 |
|
| 1963 |
return retval; |
| 1964 |
} |
| 1965 |
EXPORT_SYMBOL(push_gpib_event); |
| 1966 |
|
| 1967 |
static int pop_gpib_event_nolock(struct gpib_board *board, |
| 1968 |
struct gpib_event_queue *queue, short *event_type) |
| 1969 |
{ |
| 1970 |
struct list_head *head = &queue->event_head; |
| 1971 |
struct list_head *front = head->next; |
| 1972 |
struct gpib_event *event; |
| 1973 |
|
| 1974 |
if (num_gpib_events(queue) == 0) { |
| 1975 |
*event_type = EVENT_NONE; |
| 1976 |
return 0; |
| 1977 |
} |
| 1978 |
|
| 1979 |
if (front == head) |
| 1980 |
return -EIO; |
| 1981 |
|
| 1982 |
if (queue->dropped_event) { |
| 1983 |
queue->dropped_event = 0; |
| 1984 |
return -EPIPE; |
| 1985 |
} |
| 1986 |
|
| 1987 |
event = list_entry(front, struct gpib_event, list); |
| 1988 |
*event_type = event->event_type; |
| 1989 |
|
| 1990 |
list_del(front); |
| 1991 |
kfree(event); |
| 1992 |
|
| 1993 |
queue->num_events--; |
| 1994 |
|
| 1995 |
dev_dbg(board->gpib_dev, "popped event %i, %i in queue\n", |
| 1996 |
(int)*event_type, num_gpib_events(queue)); |
| 1997 |
|
| 1998 |
return 0; |
| 1999 |
} |
| 2000 |
|
| 2001 |
// pop event from front of event queue |
| 2002 |
int pop_gpib_event(struct gpib_board *board, struct gpib_event_queue *queue, short *event_type) |
| 2003 |
{ |
| 2004 |
unsigned long flags; |
| 2005 |
int retval; |
| 2006 |
|
| 2007 |
spin_lock_irqsave(&queue->lock, flags); |
| 2008 |
retval = pop_gpib_event_nolock(board, queue, event_type); |
| 2009 |
spin_unlock_irqrestore(&queue->lock, flags); |
| 2010 |
return retval; |
| 2011 |
} |
| 2012 |
|
| 2013 |
static int event_ioctl(struct gpib_board *board, unsigned long arg) |
| 2014 |
{ |
| 2015 |
short user_event; |
| 2016 |
int retval; |
| 2017 |
short event; |
| 2018 |
|
| 2019 |
retval = pop_gpib_event(board, &board->event_queue, &event); |
| 2020 |
if (retval < 0) |
| 2021 |
return retval; |
| 2022 |
|
| 2023 |
user_event = event; |
| 2024 |
|
| 2025 |
retval = copy_to_user((void __user *)arg, &user_event, sizeof(user_event)); |
| 2026 |
if (retval) |
| 2027 |
return -EFAULT; |
| 2028 |
|
| 2029 |
return 0; |
| 2030 |
} |
| 2031 |
|
| 2032 |
static int request_system_control_ioctl(struct gpib_board *board, unsigned long arg) |
| 2033 |
{ |
| 2034 |
int request_control; |
| 2035 |
int retval; |
| 2036 |
|
| 2037 |
retval = copy_from_user(&request_control, (void __user *)arg, sizeof(request_control)); |
| 2038 |
if (retval) |
| 2039 |
return -EFAULT; |
| 2040 |
|
| 2041 |
return ibrsc(board, request_control); |
| 2042 |
} |
| 2043 |
|
| 2044 |
static int t1_delay_ioctl(struct gpib_board *board, unsigned long arg) |
| 2045 |
{ |
| 2046 |
unsigned int cmd; |
| 2047 |
unsigned int delay; |
| 2048 |
int retval; |
| 2049 |
|
| 2050 |
if (!board->interface->t1_delay) |
| 2051 |
return -ENOENT; |
| 2052 |
|
| 2053 |
retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); |
| 2054 |
if (retval) |
| 2055 |
return -EFAULT; |
| 2056 |
|
| 2057 |
delay = cmd; |
| 2058 |
|
| 2059 |
retval = board->interface->t1_delay(board, delay); |
| 2060 |
if (retval < 0) |
| 2061 |
return retval; |
| 2062 |
|
| 2063 |
board->t1_nano_sec = retval; |
| 2064 |
return 0; |
| 2065 |
} |
| 2066 |
|
| 2067 |
static const struct file_operations ib_fops = { |
| 2068 |
.owner = THIS_MODULE, |
| 2069 |
.llseek = NULL, |
| 2070 |
.unlocked_ioctl = &ibioctl, |
| 2071 |
.compat_ioctl = &ibioctl, |
| 2072 |
.open = &ibopen, |
| 2073 |
.release = &ibclose, |
| 2074 |
}; |
| 2075 |
|
| 2076 |
struct gpib_board board_array[GPIB_MAX_NUM_BOARDS]; |
| 2077 |
|
| 2078 |
LIST_HEAD(registered_drivers); |
| 2079 |
|
| 2080 |
void init_gpib_descriptor(struct gpib_descriptor *desc) |
| 2081 |
{ |
| 2082 |
desc->pad = 0; |
| 2083 |
desc->sad = -1; |
| 2084 |
desc->is_board = 0; |
| 2085 |
desc->autopoll_enabled = 0; |
| 2086 |
atomic_set(&desc->io_in_progress, 0); |
| 2087 |
atomic_set(&desc->descriptor_busy, 0); |
| 2088 |
} |
| 2089 |
|
| 2090 |
int gpib_register_driver(struct gpib_interface *interface, struct module *provider_module) |
| 2091 |
{ |
| 2092 |
struct gpib_interface_list *entry; |
| 2093 |
|
| 2094 |
entry = kmalloc_obj(*entry); |
| 2095 |
if (!entry) |
| 2096 |
return -ENOMEM; |
| 2097 |
|
| 2098 |
entry->interface = interface; |
| 2099 |
entry->module = provider_module; |
| 2100 |
list_add(&entry->list, ®istered_drivers); |
| 2101 |
|
| 2102 |
return 0; |
| 2103 |
} |
| 2104 |
EXPORT_SYMBOL(gpib_register_driver); |
| 2105 |
|
| 2106 |
void gpib_unregister_driver(struct gpib_interface *interface) |
| 2107 |
{ |
| 2108 |
int i; |
| 2109 |
struct list_head *list_ptr; |
| 2110 |
|
| 2111 |
for (i = 0; i < GPIB_MAX_NUM_BOARDS; i++) { |
| 2112 |
struct gpib_board *board = &board_array[i]; |
| 2113 |
|
| 2114 |
if (board->interface == interface) { |
| 2115 |
if (board->use_count > 0) |
| 2116 |
pr_warn("gpib: Warning: deregistered interface %s in use\n", |
| 2117 |
interface->name); |
| 2118 |
iboffline(board); |
| 2119 |
board->interface = NULL; |
| 2120 |
} |
| 2121 |
} |
| 2122 |
for (list_ptr = registered_drivers.next; list_ptr != ®istered_drivers;) { |
| 2123 |
struct gpib_interface_list *entry; |
| 2124 |
|
| 2125 |
entry = list_entry(list_ptr, struct gpib_interface_list, list); |
| 2126 |
list_ptr = list_ptr->next; |
| 2127 |
if (entry->interface == interface) { |
| 2128 |
list_del(&entry->list); |
| 2129 |
kfree(entry); |
| 2130 |
} |
| 2131 |
} |
| 2132 |
} |
| 2133 |
EXPORT_SYMBOL(gpib_unregister_driver); |
| 2134 |
|
| 2135 |
static void init_gpib_board_config(struct gpib_board_config *config) |
| 2136 |
{ |
| 2137 |
memset(config, 0, sizeof(struct gpib_board_config)); |
| 2138 |
config->pci_bus = -1; |
| 2139 |
config->pci_slot = -1; |
| 2140 |
} |
| 2141 |
|
| 2142 |
void init_gpib_board(struct gpib_board *board) |
| 2143 |
{ |
| 2144 |
board->interface = NULL; |
| 2145 |
board->provider_module = NULL; |
| 2146 |
board->buffer = NULL; |
| 2147 |
board->buffer_length = 0; |
| 2148 |
board->status = 0; |
| 2149 |
init_waitqueue_head(&board->wait); |
| 2150 |
mutex_init(&board->user_mutex); |
| 2151 |
mutex_init(&board->big_gpib_mutex); |
| 2152 |
board->locking_pid = 0; |
| 2153 |
spin_lock_init(&board->locking_pid_spinlock); |
| 2154 |
spin_lock_init(&board->spinlock); |
| 2155 |
timer_setup(&board->timer, NULL, 0); |
| 2156 |
board->dev = NULL; |
| 2157 |
board->gpib_dev = NULL; |
| 2158 |
init_gpib_board_config(&board->config); |
| 2159 |
board->private_data = NULL; |
| 2160 |
board->use_count = 0; |
| 2161 |
INIT_LIST_HEAD(&board->device_list); |
| 2162 |
board->pad = 0; |
| 2163 |
board->sad = -1; |
| 2164 |
board->usec_timeout = 3000000; |
| 2165 |
board->parallel_poll_configuration = 0; |
| 2166 |
board->online = 0; |
| 2167 |
board->autospollers = 0; |
| 2168 |
board->autospoll_task = NULL; |
| 2169 |
init_event_queue(&board->event_queue); |
| 2170 |
board->minor = -1; |
| 2171 |
init_gpib_pseudo_irq(&board->pseudo_irq); |
| 2172 |
board->master = 1; |
| 2173 |
atomic_set(&board->stuck_srq, 0); |
| 2174 |
board->local_ppoll_mode = 0; |
| 2175 |
} |
| 2176 |
|
| 2177 |
int gpib_allocate_board(struct gpib_board *board) |
| 2178 |
{ |
| 2179 |
if (!board->buffer) { |
| 2180 |
board->buffer_length = 0x4000; |
| 2181 |
board->buffer = vmalloc(board->buffer_length); |
| 2182 |
if (!board->buffer) { |
| 2183 |
board->buffer_length = 0; |
| 2184 |
return -ENOMEM; |
| 2185 |
} |
| 2186 |
} |
| 2187 |
return 0; |
| 2188 |
} |
| 2189 |
|
| 2190 |
void gpib_deallocate_board(struct gpib_board *board) |
| 2191 |
{ |
| 2192 |
short dummy; |
| 2193 |
|
| 2194 |
if (board->buffer) { |
| 2195 |
vfree(board->buffer); |
| 2196 |
board->buffer = NULL; |
| 2197 |
board->buffer_length = 0; |
| 2198 |
} |
| 2199 |
while (num_gpib_events(&board->event_queue)) |
| 2200 |
pop_gpib_event(board, &board->event_queue, &dummy); |
| 2201 |
} |
| 2202 |
|
| 2203 |
static void init_board_array(struct gpib_board *board_array, unsigned int length) |
| 2204 |
{ |
| 2205 |
int i; |
| 2206 |
|
| 2207 |
for (i = 0; i < length; i++) { |
| 2208 |
init_gpib_board(&board_array[i]); |
| 2209 |
board_array[i].minor = i; |
| 2210 |
} |
| 2211 |
} |
| 2212 |
|
| 2213 |
void init_gpib_status_queue(struct gpib_status_queue *device) |
| 2214 |
{ |
| 2215 |
INIT_LIST_HEAD(&device->list); |
| 2216 |
INIT_LIST_HEAD(&device->status_bytes); |
| 2217 |
device->num_status_bytes = 0; |
| 2218 |
device->reference_count = 0; |
| 2219 |
device->dropped_byte = 0; |
| 2220 |
} |
| 2221 |
|
| 2222 |
static struct class *gpib_class; |
| 2223 |
|
| 2224 |
static int __init gpib_common_init_module(void) |
| 2225 |
{ |
| 2226 |
int i; |
| 2227 |
|
| 2228 |
pr_info("GPIB core driver\n"); |
| 2229 |
init_board_array(board_array, GPIB_MAX_NUM_BOARDS); |
| 2230 |
if (register_chrdev(GPIB_CODE, "gpib", &ib_fops)) { |
| 2231 |
pr_err("gpib: can't get major %d\n", GPIB_CODE); |
| 2232 |
return -EIO; |
| 2233 |
} |
| 2234 |
gpib_class = class_create("gpib_common"); |
| 2235 |
if (IS_ERR(gpib_class)) { |
| 2236 |
pr_err("gpib: failed to create gpib class\n"); |
| 2237 |
unregister_chrdev(GPIB_CODE, "gpib"); |
| 2238 |
return PTR_ERR(gpib_class); |
| 2239 |
} |
| 2240 |
for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i) |
| 2241 |
board_array[i].gpib_dev = device_create(gpib_class, NULL, |
| 2242 |
MKDEV(GPIB_CODE, i), NULL, "gpib%i", i); |
| 2243 |
|
| 2244 |
return 0; |
| 2245 |
} |
| 2246 |
|
| 2247 |
static void __exit gpib_common_exit_module(void) |
| 2248 |
{ |
| 2249 |
int i; |
| 2250 |
|
| 2251 |
for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i) |
| 2252 |
device_destroy(gpib_class, MKDEV(GPIB_CODE, i)); |
| 2253 |
|
| 2254 |
class_destroy(gpib_class); |
| 2255 |
unregister_chrdev(GPIB_CODE, "gpib"); |
| 2256 |
} |
| 2257 |
|
| 2258 |
int gpib_match_device_path(struct device *dev, const char *device_path_in) |
| 2259 |
{ |
| 2260 |
if (device_path_in) { |
| 2261 |
char *device_path; |
| 2262 |
|
| 2263 |
device_path = kobject_get_path(&dev->kobj, GFP_KERNEL); |
| 2264 |
if (!device_path) { |
| 2265 |
dev_err(dev, "kobject_get_path returned NULL."); |
| 2266 |
return 0; |
| 2267 |
} |
| 2268 |
if (strcmp(device_path_in, device_path) != 0) { |
| 2269 |
kfree(device_path); |
| 2270 |
return 0; |
| 2271 |
} |
| 2272 |
kfree(device_path); |
| 2273 |
} |
| 2274 |
return 1; |
| 2275 |
} |
| 2276 |
EXPORT_SYMBOL(gpib_match_device_path); |
| 2277 |
|
| 2278 |
struct pci_dev *gpib_pci_get_device(const struct gpib_board_config *config, unsigned int vendor_id, |
| 2279 |
unsigned int device_id, struct pci_dev *from) |
| 2280 |
{ |
| 2281 |
struct pci_dev *pci_device = from; |
| 2282 |
|
| 2283 |
while ((pci_device = pci_get_device(vendor_id, device_id, pci_device))) { |
| 2284 |
if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number) |
| 2285 |
continue; |
| 2286 |
if (config->pci_slot >= 0 && config->pci_slot != |
| 2287 |
PCI_SLOT(pci_device->devfn)) |
| 2288 |
continue; |
| 2289 |
if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0) |
| 2290 |
continue; |
| 2291 |
return pci_device; |
| 2292 |
} |
| 2293 |
return NULL; |
| 2294 |
} |
| 2295 |
EXPORT_SYMBOL(gpib_pci_get_device); |
| 2296 |
|
| 2297 |
struct pci_dev *gpib_pci_get_subsys(const struct gpib_board_config *config, unsigned int vendor_id, |
| 2298 |
unsigned int device_id, unsigned int ss_vendor, |
| 2299 |
unsigned int ss_device, |
| 2300 |
struct pci_dev *from) |
| 2301 |
{ |
| 2302 |
struct pci_dev *pci_device = from; |
| 2303 |
|
| 2304 |
while ((pci_device = pci_get_subsys(vendor_id, device_id, |
| 2305 |
ss_vendor, ss_device, pci_device))) { |
| 2306 |
if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number) |
| 2307 |
continue; |
| 2308 |
if (config->pci_slot >= 0 && config->pci_slot != |
| 2309 |
PCI_SLOT(pci_device->devfn)) |
| 2310 |
continue; |
| 2311 |
if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0) |
| 2312 |
continue; |
| 2313 |
return pci_device; |
| 2314 |
} |
| 2315 |
return NULL; |
| 2316 |
} |
| 2317 |
EXPORT_SYMBOL(gpib_pci_get_subsys); |
| 2318 |
|
| 2319 |
module_init(gpib_common_init_module); |
| 2320 |
module_exit(gpib_common_exit_module); |
| 2321 |
|