xref: /linux/drivers/gpib/common/gpib_os.c (revision c4faab452b3c1ada003d49c477609dd80523b9bf)
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->interface) {
548 		if (!try_module_get(board->provider_module)) {
549 			dev_err(board->gpib_dev, "try_module_get() failed\n");
550 			return -EIO;
551 		}
552 		board->use_count++;
553 		priv->got_module = 1;
554 	}
555 	return 0;
556 }
557 
558 int ibclose(struct inode *inode, struct file *filep)
559 {
560 	unsigned int minor = iminor(inode);
561 	struct gpib_board *board;
562 	struct gpib_file_private *priv = filep->private_data;
563 	struct gpib_descriptor *desc;
564 
565 	if (minor >= GPIB_MAX_NUM_BOARDS) {
566 		pr_err("gpib: invalid minor number of device file\n");
567 		return -ENODEV;
568 	}
569 
570 	board = &board_array[minor];
571 
572 	if (priv) {
573 		desc = handle_to_descriptor(priv, 0);
574 		if (desc) {
575 			if (desc->autopoll_enabled) {
576 				dev_dbg(board->gpib_dev, "decrementing autospollers\n");
577 				if (board->autospollers > 0)
578 					board->autospollers--;
579 				else
580 					dev_err(board->gpib_dev,
581 						"Attempt to decrement zero autospollers\n");
582 			}
583 		} else {
584 			dev_err(board->gpib_dev, "Unexpected null gpib_descriptor\n");
585 		}
586 
587 		cleanup_open_devices(priv, board);
588 
589 		if (atomic_read(&priv->holding_mutex))
590 			mutex_unlock(&board->user_mutex);
591 
592 		if (priv->got_module && board->use_count) {
593 			module_put(board->provider_module);
594 			--board->use_count;
595 		}
596 
597 		kfree(filep->private_data);
598 		filep->private_data = NULL;
599 	}
600 
601 	return 0;
602 }
603 
604 long ibioctl(struct file *filep, unsigned int cmd, unsigned long arg)
605 {
606 	unsigned int minor = iminor(file_inode(filep));
607 	struct gpib_board *board;
608 	struct gpib_file_private *file_priv = filep->private_data;
609 	long retval = -EBADRQC;
610 
611 	if (minor >= GPIB_MAX_NUM_BOARDS) {
612 		pr_err("gpib: invalid minor number of device file\n");
613 		return -ENODEV;
614 	}
615 	board = &board_array[minor];
616 
617 	if (mutex_lock_interruptible(&board->big_gpib_mutex))
618 		return -ERESTARTSYS;
619 
620 	dev_dbg(board->gpib_dev, "ioctl %d, interface=%s, use=%d, onl=%d\n",
621 		cmd & 0xff,
622 		board->interface ? board->interface->name : "",
623 		board->use_count,
624 		board->online);
625 
626 	switch (cmd) {
627 	case CFCBOARDTYPE:
628 		retval = board_type_ioctl(file_priv, board, arg);
629 		goto done;
630 	case IBONL:
631 		retval = online_ioctl(board, arg);
632 		goto done;
633 	default:
634 		break;
635 	}
636 	if (!board->interface) {
637 		dev_err(board->gpib_dev, "no gpib board configured\n");
638 		retval = -ENODEV;
639 		goto done;
640 	}
641 	if (file_priv->got_module == 0)	{
642 		if (!try_module_get(board->provider_module)) {
643 			dev_err(board->gpib_dev, "try_module_get() failed\n");
644 			retval = -EIO;
645 			goto done;
646 		}
647 		file_priv->got_module = 1;
648 		board->use_count++;
649 	}
650 	switch (cmd) {
651 	case CFCBASE:
652 		retval = iobase_ioctl(&board->config, arg);
653 		goto done;
654 	case CFCIRQ:
655 		retval = irq_ioctl(&board->config, arg);
656 		goto done;
657 	case CFCDMA:
658 		retval = dma_ioctl(&board->config, arg);
659 		goto done;
660 	case IBAUTOSPOLL:
661 		retval = autospoll_ioctl(board, file_priv, arg);
662 		goto done;
663 	case IBBOARD_INFO:
664 		retval = board_info_ioctl(board, arg);
665 		goto done;
666 	case IBMUTEX:
667 		/*
668 		 * Need to unlock board->big_gpib_mutex before potentially locking board->user_mutex
669 		 * to maintain consistent locking order
670 		 */
671 		mutex_unlock(&board->big_gpib_mutex);
672 		return mutex_ioctl(board, file_priv, arg);
673 	case IBPAD:
674 		retval = pad_ioctl(board, file_priv, arg);
675 		goto done;
676 	case IBSAD:
677 		retval = sad_ioctl(board, file_priv, arg);
678 		goto done;
679 	case IBSELECT_PCI:
680 		retval = select_pci_ioctl(&board->config, arg);
681 		goto done;
682 	case IBSELECT_DEVICE_PATH:
683 		retval = select_device_path_ioctl(&board->config, arg);
684 		goto done;
685 	default:
686 		break;
687 	}
688 
689 	if (!board->online) {
690 		retval = -EINVAL;
691 		goto done;
692 	}
693 
694 	switch (cmd) {
695 	case IBEVENT:
696 		retval = event_ioctl(board, arg);
697 		goto done;
698 	case IBCLOSEDEV:
699 		retval = close_dev_ioctl(filep, board, arg);
700 		goto done;
701 	case IBOPENDEV:
702 		retval = open_dev_ioctl(filep, board, arg);
703 		goto done;
704 	case IBSPOLL_BYTES:
705 		retval = status_bytes_ioctl(board, arg);
706 		goto done;
707 	case IBWAIT:
708 		retval = wait_ioctl(file_priv, board, arg);
709 		if (retval == -ERESTARTSYS)
710 			return retval;
711 		goto done;
712 	case IBLINES:
713 		retval = line_status_ioctl(board, arg);
714 		goto done;
715 	case IBLOC:
716 		board->interface->return_to_local(board);
717 		retval = 0;
718 		goto done;
719 	default:
720 		break;
721 	}
722 
723 	spin_lock(&board->locking_pid_spinlock);
724 	if (current->pid != board->locking_pid)	{
725 		spin_unlock(&board->locking_pid_spinlock);
726 		retval = -EPERM;
727 		goto done;
728 	}
729 	spin_unlock(&board->locking_pid_spinlock);
730 
731 	switch (cmd) {
732 	case IB_T1_DELAY:
733 		retval = t1_delay_ioctl(board, arg);
734 		goto done;
735 	case IBCAC:
736 		retval = take_control_ioctl(board, arg);
737 		goto done;
738 	case IBCMD:
739 		/*
740 		 * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex
741 		 * before we call them.
742 		 */
743 		mutex_unlock(&board->big_gpib_mutex);
744 		return command_ioctl(file_priv, board, arg);
745 	case IBEOS:
746 		retval = eos_ioctl(board, arg);
747 		goto done;
748 	case IBGTS:
749 		retval = ibgts(board);
750 		goto done;
751 	case IBPPC:
752 		retval = ppc_ioctl(board, arg);
753 		goto done;
754 	case IBPP2_SET:
755 		retval = set_local_ppoll_mode_ioctl(board, arg);
756 		goto done;
757 	case IBPP2_GET:
758 		retval = get_local_ppoll_mode_ioctl(board, arg);
759 		goto done;
760 	case IBQUERY_BOARD_RSV:
761 		retval = query_board_rsv_ioctl(board, arg);
762 		goto done;
763 	case IBRD:
764 		/*
765 		 * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex
766 		 * before we call them.
767 		 */
768 		mutex_unlock(&board->big_gpib_mutex);
769 		return read_ioctl(file_priv, board, arg);
770 	case IBRPP:
771 		retval = parallel_poll_ioctl(board, arg);
772 		goto done;
773 	case IBRSC:
774 		retval = request_system_control_ioctl(board, arg);
775 		goto done;
776 	case IBRSP:
777 		retval = serial_poll_ioctl(board, arg);
778 		goto done;
779 	case IBRSV:
780 		retval = request_service_ioctl(board, arg);
781 		goto done;
782 	case IBRSV2:
783 		retval = request_service2_ioctl(board, arg);
784 		goto done;
785 	case IBSIC:
786 		retval = interface_clear_ioctl(board, arg);
787 		goto done;
788 	case IBSRE:
789 		retval = remote_enable_ioctl(board, arg);
790 		goto done;
791 	case IBTMO:
792 		retval = timeout_ioctl(board, arg);
793 		goto done;
794 	case IBWRT:
795 		/*
796 		 * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex
797 		 * before we call them.
798 		 */
799 		mutex_unlock(&board->big_gpib_mutex);
800 		return write_ioctl(file_priv, board, arg);
801 	default:
802 		goto done;
803 	}
804 
805 done:
806 	mutex_unlock(&board->big_gpib_mutex);
807 	dev_dbg(board->gpib_dev, "ioctl done status = 0x%lx\n", board->status);
808 	return retval;
809 }
810 
811 static int board_type_ioctl(struct gpib_file_private *file_priv,
812 			    struct gpib_board *board, unsigned long arg)
813 {
814 	struct list_head *list_ptr;
815 	struct gpib_board_type_ioctl cmd;
816 	int retval;
817 
818 	if (!capable(CAP_SYS_ADMIN))
819 		return -EPERM;
820 	if (board->online)
821 		return -EBUSY;
822 
823 	retval = copy_from_user(&cmd, (void __user *)arg,
824 				sizeof(struct gpib_board_type_ioctl));
825 	if (retval)
826 		return -EFAULT;
827 
828 	for (list_ptr = registered_drivers.next; list_ptr != &registered_drivers;
829 	     list_ptr = list_ptr->next) {
830 		struct gpib_interface_list *entry;
831 
832 		entry = list_entry(list_ptr, struct gpib_interface_list, list);
833 		if (strcmp(entry->interface->name, cmd.name) == 0) {
834 			int i;
835 			int had_module = file_priv->got_module;
836 
837 			if (board->use_count) {
838 				for (i = 0; i < board->use_count; ++i)
839 					module_put(board->provider_module);
840 				board->interface = NULL;
841 				file_priv->got_module = 0;
842 			}
843 			board->interface = entry->interface;
844 			board->provider_module = entry->module;
845 			for (i = 0; i < board->use_count; ++i) {
846 				if (!try_module_get(entry->module)) {
847 					board->use_count = i;
848 					return -EIO;
849 				}
850 			}
851 			if (had_module == 0) {
852 				if (!try_module_get(entry->module))
853 					return -EIO;
854 				++board->use_count;
855 			}
856 			file_priv->got_module = 1;
857 			return 0;
858 		}
859 	}
860 
861 	return -EINVAL;
862 }
863 
864 static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board,
865 		      unsigned long arg)
866 {
867 	struct gpib_read_write_ioctl read_cmd;
868 	u8 __user *userbuf;
869 	unsigned long remain;
870 	int end_flag = 0;
871 	int retval;
872 	ssize_t read_ret = 0;
873 	struct gpib_descriptor *desc;
874 	size_t nbytes;
875 
876 	retval = copy_from_user(&read_cmd, (void __user *)arg, sizeof(read_cmd));
877 	if (retval)
878 		return -EFAULT;
879 
880 	if (read_cmd.completed_transfer_count > read_cmd.requested_transfer_count)
881 		return -EINVAL;
882 
883 	if (WARN_ON_ONCE(sizeof(userbuf) > sizeof(read_cmd.buffer_ptr)))
884 		return -EFAULT;
885 
886 	userbuf = (u8 __user *)(unsigned long)read_cmd.buffer_ptr;
887 	userbuf += read_cmd.completed_transfer_count;
888 
889 	remain = read_cmd.requested_transfer_count - read_cmd.completed_transfer_count;
890 
891 	/* Check write access to buffer */
892 	if (!access_ok(userbuf, remain))
893 		return -EFAULT;
894 
895 	/* Lock descriptors to prevent concurrent close from freeing descriptor */
896 	if (mutex_lock_interruptible(&file_priv->descriptors_mutex))
897 		return -ERESTARTSYS;
898 	desc = handle_to_descriptor(file_priv, read_cmd.handle);
899 	if (!desc) {
900 		mutex_unlock(&file_priv->descriptors_mutex);
901 		return -EINVAL;
902 	}
903 	atomic_inc(&desc->descriptor_busy);
904 	mutex_unlock(&file_priv->descriptors_mutex);
905 
906 	atomic_set(&desc->io_in_progress, 1);
907 
908 	/* Read buffer loads till we fill the user supplied buffer */
909 	while (remain > 0 && end_flag == 0) {
910 		nbytes = 0;
911 		read_ret = ibrd(board, board->buffer, (board->buffer_length < remain) ?
912 				board->buffer_length : remain, &end_flag, &nbytes);
913 		if (nbytes == 0)
914 			break;
915 		retval = copy_to_user(userbuf, board->buffer, nbytes);
916 		if (retval) {
917 			retval = -EFAULT;
918 			break;
919 		}
920 		remain -= nbytes;
921 		userbuf += nbytes;
922 		if (read_ret < 0)
923 			break;
924 	}
925 	read_cmd.completed_transfer_count = read_cmd.requested_transfer_count - remain;
926 	read_cmd.end = end_flag;
927 	/*
928 	 * suppress errors (for example due to timeout or interruption by device clear)
929 	 * if all bytes got sent.  This prevents races that can occur in the various drivers
930 	 * if a device receives a device clear immediately after a transfer completes and
931 	 * the driver code wasn't careful enough to handle that case.
932 	 */
933 	if (remain == 0 || end_flag)
934 		read_ret = 0;
935 	if (retval == 0)
936 		retval = copy_to_user((void __user *)arg, &read_cmd, sizeof(read_cmd));
937 
938 	atomic_set(&desc->io_in_progress, 0);
939 	atomic_dec(&desc->descriptor_busy);
940 
941 	wake_up_interruptible(&board->wait);
942 	if (retval)
943 		return -EFAULT;
944 
945 	return read_ret;
946 }
947 
948 static int command_ioctl(struct gpib_file_private *file_priv,
949 			 struct gpib_board *board, unsigned long arg)
950 {
951 	struct gpib_read_write_ioctl cmd;
952 	u8 __user *userbuf;
953 	unsigned long remain;
954 	int retval;
955 	int fault = 0;
956 	struct gpib_descriptor *desc;
957 	size_t bytes_written;
958 	int no_clear_io_in_prog;
959 
960 	retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
961 	if (retval)
962 		return -EFAULT;
963 
964 	if (cmd.completed_transfer_count > cmd.requested_transfer_count)
965 		return -EINVAL;
966 
967 	userbuf = (u8 __user *)(unsigned long)cmd.buffer_ptr;
968 	userbuf += cmd.completed_transfer_count;
969 
970 	no_clear_io_in_prog = cmd.end;
971 	cmd.end = 0;
972 
973 	remain = cmd.requested_transfer_count - cmd.completed_transfer_count;
974 
975 	/* Check read access to buffer */
976 	if (!access_ok(userbuf, remain))
977 		return -EFAULT;
978 
979 	/* Lock descriptors to prevent concurrent close from freeing descriptor */
980 	if (mutex_lock_interruptible(&file_priv->descriptors_mutex))
981 		return -ERESTARTSYS;
982 	desc = handle_to_descriptor(file_priv, cmd.handle);
983 	if (!desc) {
984 		mutex_unlock(&file_priv->descriptors_mutex);
985 		return -EINVAL;
986 	}
987 	atomic_inc(&desc->descriptor_busy);
988 	mutex_unlock(&file_priv->descriptors_mutex);
989 
990 	/*
991 	 * Write buffer loads till we empty the user supplied buffer.
992 	 * Call drivers at least once, even if remain is zero, in
993 	 * order to allow them to insure previous commands were
994 	 * completely finished, in the case of a restarted ioctl.
995 	 */
996 
997 	atomic_set(&desc->io_in_progress, 1);
998 
999 	do {
1000 		fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ?
1001 				       board->buffer_length : remain);
1002 		if (fault) {
1003 			retval = -EFAULT;
1004 			bytes_written = 0;
1005 		} else {
1006 			retval = ibcmd(board, board->buffer, (board->buffer_length < remain) ?
1007 				       board->buffer_length : remain, &bytes_written);
1008 		}
1009 		remain -= bytes_written;
1010 		userbuf += bytes_written;
1011 		if (retval < 0) {
1012 			atomic_set(&desc->io_in_progress, 0);
1013 
1014 			wake_up_interruptible(&board->wait);
1015 			break;
1016 		}
1017 	} while (remain > 0);
1018 
1019 	cmd.completed_transfer_count = cmd.requested_transfer_count - remain;
1020 
1021 	if (fault == 0)
1022 		fault = copy_to_user((void __user *)arg, &cmd, sizeof(cmd));
1023 
1024 	/*
1025 	 * no_clear_io_in_prog (cmd.end) is true when io_in_progress should
1026 	 * not be set to zero because the cmd in progress is the address setup
1027 	 * operation for an async read or write. This causes CMPL not to be set
1028 	 * in general_ibstatus until the async read or write completes.
1029 	 */
1030 	if (!no_clear_io_in_prog || fault)
1031 		atomic_set(&desc->io_in_progress, 0);
1032 	atomic_dec(&desc->descriptor_busy);
1033 
1034 	wake_up_interruptible(&board->wait);
1035 	if (fault)
1036 		return -EFAULT;
1037 
1038 	return retval;
1039 }
1040 
1041 static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board,
1042 		       unsigned long arg)
1043 {
1044 	struct gpib_read_write_ioctl write_cmd;
1045 	u8 __user *userbuf;
1046 	unsigned long remain;
1047 	int retval = 0;
1048 	int fault;
1049 	struct gpib_descriptor *desc;
1050 
1051 	fault = copy_from_user(&write_cmd, (void __user *)arg, sizeof(write_cmd));
1052 	if (fault)
1053 		return -EFAULT;
1054 
1055 	if (write_cmd.completed_transfer_count > write_cmd.requested_transfer_count)
1056 		return -EINVAL;
1057 
1058 	userbuf = (u8 __user *)(unsigned long)write_cmd.buffer_ptr;
1059 	userbuf += write_cmd.completed_transfer_count;
1060 
1061 	remain = write_cmd.requested_transfer_count - write_cmd.completed_transfer_count;
1062 
1063 	/* Check read access to buffer */
1064 	if (!access_ok(userbuf, remain))
1065 		return -EFAULT;
1066 
1067 	/* Lock descriptors to prevent concurrent close from freeing descriptor */
1068 	if (mutex_lock_interruptible(&file_priv->descriptors_mutex))
1069 		return -ERESTARTSYS;
1070 	desc = handle_to_descriptor(file_priv, write_cmd.handle);
1071 	if (!desc) {
1072 		mutex_unlock(&file_priv->descriptors_mutex);
1073 		return -EINVAL;
1074 	}
1075 	atomic_inc(&desc->descriptor_busy);
1076 	mutex_unlock(&file_priv->descriptors_mutex);
1077 
1078 	atomic_set(&desc->io_in_progress, 1);
1079 
1080 	/* Write buffer loads till we empty the user supplied buffer */
1081 	while (remain > 0) {
1082 		int send_eoi;
1083 		size_t bytes_written = 0;
1084 
1085 		send_eoi = remain <= board->buffer_length && write_cmd.end;
1086 		fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ?
1087 				       board->buffer_length : remain);
1088 		if (fault) {
1089 			retval = -EFAULT;
1090 			break;
1091 		}
1092 		retval = ibwrt(board, board->buffer, (board->buffer_length < remain) ?
1093 			       board->buffer_length : remain, send_eoi, &bytes_written);
1094 		remain -= bytes_written;
1095 		userbuf += bytes_written;
1096 		if (retval < 0)
1097 			break;
1098 	}
1099 	write_cmd.completed_transfer_count = write_cmd.requested_transfer_count - remain;
1100 	/*
1101 	 * suppress errors (for example due to timeout or interruption by device clear)
1102 	 * if all bytes got sent.  This prevents races that can occur in the various drivers
1103 	 * if a device receives a device clear immediately after a transfer completes and
1104 	 * the driver code wasn't careful enough to handle that case.
1105 	 */
1106 	if (remain == 0)
1107 		retval = 0;
1108 	if (fault == 0)
1109 		fault = copy_to_user((void __user *)arg, &write_cmd, sizeof(write_cmd));
1110 
1111 	atomic_set(&desc->io_in_progress, 0);
1112 	atomic_dec(&desc->descriptor_busy);
1113 
1114 	wake_up_interruptible(&board->wait);
1115 	if (fault)
1116 		return -EFAULT;
1117 
1118 	return retval;
1119 }
1120 
1121 static int status_bytes_ioctl(struct gpib_board *board, unsigned long arg)
1122 {
1123 	struct gpib_status_queue *device;
1124 	struct gpib_spoll_bytes_ioctl cmd;
1125 	int retval;
1126 
1127 	retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
1128 	if (retval)
1129 		return -EFAULT;
1130 
1131 	device = get_gpib_status_queue(board, cmd.pad, cmd.sad);
1132 	if (!device)
1133 		cmd.num_bytes = 0;
1134 	else
1135 		cmd.num_bytes = num_status_bytes(device);
1136 
1137 	retval = copy_to_user((void __user *)arg, &cmd, sizeof(cmd));
1138 	if (retval)
1139 		return -EFAULT;
1140 
1141 	return 0;
1142 }
1143 
1144 static int increment_open_device_count(struct gpib_board *board, struct list_head *head,
1145 				       unsigned int pad, int sad)
1146 {
1147 	struct list_head *list_ptr;
1148 	struct gpib_status_queue *device;
1149 
1150 	/*
1151 	 * first see if address has already been opened, then increment
1152 	 * open count
1153 	 */
1154 	for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) {
1155 		device = list_entry(list_ptr, struct gpib_status_queue, list);
1156 		if (gpib_address_equal(device->pad, device->sad, pad, sad)) {
1157 			dev_dbg(board->gpib_dev, "incrementing open count for pad %i, sad %i\n",
1158 				device->pad, device->sad);
1159 			device->reference_count++;
1160 			return 0;
1161 		}
1162 	}
1163 
1164 	/* otherwise we need to allocate a new struct gpib_status_queue */
1165 	device = kmalloc_obj(struct gpib_status_queue, GFP_ATOMIC);
1166 	if (!device)
1167 		return -ENOMEM;
1168 	init_gpib_status_queue(device);
1169 	device->pad = pad;
1170 	device->sad = sad;
1171 	device->reference_count = 1;
1172 
1173 	list_add(&device->list, head);
1174 
1175 	dev_dbg(board->gpib_dev, "opened pad %i, sad %i\n", device->pad, device->sad);
1176 
1177 	return 0;
1178 }
1179 
1180 static int subtract_open_device_count(struct gpib_board *board, struct list_head *head,
1181 				      unsigned int pad, int sad, unsigned int count)
1182 {
1183 	struct gpib_status_queue *device;
1184 	struct list_head *list_ptr;
1185 
1186 	for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) {
1187 		device = list_entry(list_ptr, struct gpib_status_queue, list);
1188 		if (gpib_address_equal(device->pad, device->sad, pad, sad)) {
1189 			dev_dbg(board->gpib_dev, "decrementing open count for pad %i, sad %i\n",
1190 				device->pad, device->sad);
1191 			if (count > device->reference_count) {
1192 				dev_err(board->gpib_dev, "bug! in %s()\n", __func__);
1193 				return -EINVAL;
1194 			}
1195 			device->reference_count -= count;
1196 			if (device->reference_count == 0) {
1197 				dev_dbg(board->gpib_dev, "closing pad %i, sad %i\n",
1198 					device->pad, device->sad);
1199 				list_del(list_ptr);
1200 				kfree(device);
1201 			}
1202 			return 0;
1203 		}
1204 	}
1205 	dev_err(board->gpib_dev, "bug! tried to close address that was never opened!\n");
1206 	return -EINVAL;
1207 }
1208 
1209 static inline int decrement_open_device_count(struct gpib_board *board, struct list_head *head,
1210 					      unsigned int pad, int sad)
1211 {
1212 	return subtract_open_device_count(board, head, pad, sad, 1);
1213 }
1214 
1215 static int cleanup_open_devices(struct gpib_file_private *file_priv, struct gpib_board *board)
1216 {
1217 	int retval = 0;
1218 	int i;
1219 
1220 	for (i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++) {
1221 		struct gpib_descriptor *desc;
1222 
1223 		desc = file_priv->descriptors[i];
1224 		if (!desc)
1225 			continue;
1226 
1227 		if (desc->is_board == 0) {
1228 			retval = decrement_open_device_count(board, &board->device_list, desc->pad,
1229 							     desc->sad);
1230 			if (retval < 0)
1231 				return retval;
1232 		}
1233 		kfree(desc);
1234 		file_priv->descriptors[i] = NULL;
1235 	}
1236 
1237 	return 0;
1238 }
1239 
1240 static int open_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg)
1241 {
1242 	struct gpib_open_dev_ioctl open_dev_cmd;
1243 	int retval;
1244 	struct gpib_file_private *file_priv = filep->private_data;
1245 	int i;
1246 
1247 	retval = copy_from_user(&open_dev_cmd, (void __user *)arg, sizeof(open_dev_cmd));
1248 	if (retval)
1249 		return -EFAULT;
1250 
1251 	if (mutex_lock_interruptible(&file_priv->descriptors_mutex))
1252 		return -ERESTARTSYS;
1253 	for (i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++)
1254 		if (!file_priv->descriptors[i])
1255 			break;
1256 	if (i == GPIB_MAX_NUM_DESCRIPTORS) {
1257 		mutex_unlock(&file_priv->descriptors_mutex);
1258 		return -ERANGE;
1259 	}
1260 	file_priv->descriptors[i] = kmalloc_obj(struct gpib_descriptor);
1261 	if (!file_priv->descriptors[i]) {
1262 		mutex_unlock(&file_priv->descriptors_mutex);
1263 		return -ENOMEM;
1264 	}
1265 	init_gpib_descriptor(file_priv->descriptors[i]);
1266 
1267 	file_priv->descriptors[i]->pad = open_dev_cmd.pad;
1268 	file_priv->descriptors[i]->sad = open_dev_cmd.sad;
1269 	file_priv->descriptors[i]->is_board = open_dev_cmd.is_board;
1270 	mutex_unlock(&file_priv->descriptors_mutex);
1271 
1272 	retval = increment_open_device_count(board, &board->device_list, open_dev_cmd.pad,
1273 					     open_dev_cmd.sad);
1274 	if (retval < 0)
1275 		return retval;
1276 
1277 	/*
1278 	 * clear stuck srq state, since we may be able to find service request on
1279 	 * the new device
1280 	 */
1281 	atomic_set(&board->stuck_srq, 0);
1282 
1283 	open_dev_cmd.handle = i;
1284 	retval = copy_to_user((void __user *)arg, &open_dev_cmd, sizeof(open_dev_cmd));
1285 	if (retval)
1286 		return -EFAULT;
1287 
1288 	return 0;
1289 }
1290 
1291 static int close_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg)
1292 {
1293 	struct gpib_close_dev_ioctl cmd;
1294 	struct gpib_file_private *file_priv = filep->private_data;
1295 	struct gpib_descriptor *desc;
1296 	unsigned int pad;
1297 	int sad;
1298 	int retval;
1299 
1300 	retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
1301 	if (retval)
1302 		return -EFAULT;
1303 
1304 	if (cmd.handle >= GPIB_MAX_NUM_DESCRIPTORS)
1305 		return -EINVAL;
1306 
1307 	mutex_lock(&file_priv->descriptors_mutex);
1308 	desc = file_priv->descriptors[cmd.handle];
1309 	if (!desc) {
1310 		mutex_unlock(&file_priv->descriptors_mutex);
1311 		return -EINVAL;
1312 	}
1313 	if (atomic_read(&desc->descriptor_busy)) {
1314 		mutex_unlock(&file_priv->descriptors_mutex);
1315 		return -EBUSY;
1316 	}
1317 	/* Remove from table while holding lock to prevent new IO from starting */
1318 	file_priv->descriptors[cmd.handle] = NULL;
1319 	pad = desc->pad;
1320 	sad = desc->sad;
1321 	mutex_unlock(&file_priv->descriptors_mutex);
1322 
1323 	retval = decrement_open_device_count(board, &board->device_list, pad, sad);
1324 
1325 	kfree(desc);
1326 	return retval;
1327 }
1328 
1329 static int serial_poll_ioctl(struct gpib_board *board, unsigned long arg)
1330 {
1331 	struct gpib_serial_poll_ioctl serial_cmd;
1332 	int retval;
1333 
1334 	retval = copy_from_user(&serial_cmd, (void __user *)arg, sizeof(serial_cmd));
1335 	if (retval)
1336 		return -EFAULT;
1337 
1338 	retval = get_serial_poll_byte(board, serial_cmd.pad, serial_cmd.sad, board->usec_timeout,
1339 				      &serial_cmd.status_byte);
1340 	if (retval < 0)
1341 		return retval;
1342 
1343 	retval = copy_to_user((void __user *)arg, &serial_cmd, sizeof(serial_cmd));
1344 	if (retval)
1345 		return -EFAULT;
1346 
1347 	return 0;
1348 }
1349 
1350 static int wait_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board,
1351 		      unsigned long arg)
1352 {
1353 	struct gpib_wait_ioctl wait_cmd;
1354 	int retval;
1355 	struct gpib_descriptor *desc;
1356 
1357 	retval = copy_from_user(&wait_cmd, (void __user *)arg, sizeof(wait_cmd));
1358 	if (retval)
1359 		return -EFAULT;
1360 
1361 	/*
1362 	 * Lock descriptors to prevent concurrent close from freeing
1363 	 * descriptor.  ibwait() releases big_gpib_mutex when wait_mask
1364 	 * is non-zero, so desc must be pinned with descriptor_busy.
1365 	 */
1366 	mutex_lock(&file_priv->descriptors_mutex);
1367 	desc = handle_to_descriptor(file_priv, wait_cmd.handle);
1368 	if (!desc) {
1369 		mutex_unlock(&file_priv->descriptors_mutex);
1370 		return -EINVAL;
1371 	}
1372 	atomic_inc(&desc->descriptor_busy);
1373 	mutex_unlock(&file_priv->descriptors_mutex);
1374 
1375 	retval = ibwait(board, wait_cmd.wait_mask, wait_cmd.clear_mask,
1376 			wait_cmd.set_mask, &wait_cmd.ibsta, wait_cmd.usec_timeout, desc);
1377 
1378 	atomic_dec(&desc->descriptor_busy);
1379 
1380 	if (retval < 0)
1381 		return retval;
1382 
1383 	retval = copy_to_user((void __user *)arg, &wait_cmd, sizeof(wait_cmd));
1384 	if (retval)
1385 		return -EFAULT;
1386 
1387 	return 0;
1388 }
1389 
1390 static int parallel_poll_ioctl(struct gpib_board *board, unsigned long arg)
1391 {
1392 	u8 poll_byte;
1393 	int retval;
1394 
1395 	retval = ibrpp(board, &poll_byte);
1396 	if (retval < 0)
1397 		return retval;
1398 
1399 	retval = copy_to_user((void __user *)arg, &poll_byte, sizeof(poll_byte));
1400 	if (retval)
1401 		return -EFAULT;
1402 
1403 	return 0;
1404 }
1405 
1406 static int online_ioctl(struct gpib_board *board, unsigned long arg)
1407 {
1408 	struct gpib_online_ioctl online_cmd;
1409 	int retval;
1410 	void __user *init_data = NULL;
1411 
1412 	board->config.init_data = NULL;
1413 
1414 	if (!capable(CAP_SYS_ADMIN))
1415 		return -EPERM;
1416 
1417 	retval = copy_from_user(&online_cmd, (void __user *)arg, sizeof(online_cmd));
1418 	if (retval)
1419 		return -EFAULT;
1420 	if (online_cmd.init_data_length > 0) {
1421 		board->config.init_data = vmalloc(online_cmd.init_data_length);
1422 		if (!board->config.init_data)
1423 			return -ENOMEM;
1424 		if (WARN_ON_ONCE(sizeof(init_data) > sizeof(online_cmd.init_data_ptr)))
1425 			return -EFAULT;
1426 		init_data = (void __user *)(unsigned long)(online_cmd.init_data_ptr);
1427 		retval = copy_from_user(board->config.init_data, init_data,
1428 					online_cmd.init_data_length);
1429 		if (retval) {
1430 			vfree(board->config.init_data);
1431 			return -EFAULT;
1432 		}
1433 		board->config.init_data_length = online_cmd.init_data_length;
1434 	} else {
1435 		board->config.init_data = NULL;
1436 		board->config.init_data_length = 0;
1437 	}
1438 	if (online_cmd.online)
1439 		retval = ibonline(board);
1440 	else
1441 		retval = iboffline(board);
1442 	if (board->config.init_data) {
1443 		vfree(board->config.init_data);
1444 		board->config.init_data = NULL;
1445 		board->config.init_data_length = 0;
1446 	}
1447 	return retval;
1448 }
1449 
1450 static int remote_enable_ioctl(struct gpib_board *board, unsigned long arg)
1451 {
1452 	int enable;
1453 	int retval;
1454 
1455 	retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable));
1456 	if (retval)
1457 		return -EFAULT;
1458 
1459 	return ibsre(board, enable);
1460 }
1461 
1462 static int take_control_ioctl(struct gpib_board *board, unsigned long arg)
1463 {
1464 	int synchronous;
1465 	int retval;
1466 
1467 	retval = copy_from_user(&synchronous, (void __user *)arg, sizeof(synchronous));
1468 	if (retval)
1469 		return -EFAULT;
1470 
1471 	return ibcac(board, synchronous, 1);
1472 }
1473 
1474 static int line_status_ioctl(struct gpib_board *board, unsigned long arg)
1475 {
1476 	short lines;
1477 	int retval;
1478 
1479 	retval = iblines(board, &lines);
1480 	if (retval < 0)
1481 		return retval;
1482 
1483 	retval = copy_to_user((void __user *)arg, &lines, sizeof(lines));
1484 	if (retval)
1485 		return -EFAULT;
1486 
1487 	return 0;
1488 }
1489 
1490 static int pad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
1491 		     unsigned long arg)
1492 {
1493 	struct gpib_pad_ioctl cmd;
1494 	int retval;
1495 	struct gpib_descriptor *desc;
1496 
1497 	retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
1498 	if (retval)
1499 		return -EFAULT;
1500 
1501 	desc = handle_to_descriptor(file_priv, cmd.handle);
1502 	if (!desc)
1503 		return -EINVAL;
1504 
1505 	if (desc->is_board) {
1506 		retval = ibpad(board, cmd.pad);
1507 		if (retval < 0)
1508 			return retval;
1509 	} else {
1510 		retval = decrement_open_device_count(board, &board->device_list, desc->pad,
1511 						     desc->sad);
1512 		if (retval < 0)
1513 			return retval;
1514 
1515 		desc->pad = cmd.pad;
1516 
1517 		retval = increment_open_device_count(board, &board->device_list, desc->pad,
1518 						     desc->sad);
1519 		if (retval < 0)
1520 			return retval;
1521 	}
1522 
1523 	return 0;
1524 }
1525 
1526 static int sad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
1527 		     unsigned long arg)
1528 {
1529 	struct gpib_sad_ioctl cmd;
1530 	int retval;
1531 	struct gpib_descriptor *desc;
1532 
1533 	retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
1534 	if (retval)
1535 		return -EFAULT;
1536 
1537 	desc = handle_to_descriptor(file_priv, cmd.handle);
1538 	if (!desc)
1539 		return -EINVAL;
1540 
1541 	if (desc->is_board) {
1542 		retval = ibsad(board, cmd.sad);
1543 		if (retval < 0)
1544 			return retval;
1545 	} else {
1546 		retval = decrement_open_device_count(board, &board->device_list, desc->pad,
1547 						     desc->sad);
1548 		if (retval < 0)
1549 			return retval;
1550 
1551 		desc->sad = cmd.sad;
1552 
1553 		retval = increment_open_device_count(board, &board->device_list, desc->pad,
1554 						     desc->sad);
1555 		if (retval < 0)
1556 			return retval;
1557 	}
1558 	return 0;
1559 }
1560 
1561 static int eos_ioctl(struct gpib_board *board, unsigned long arg)
1562 {
1563 	struct gpib_eos_ioctl eos_cmd;
1564 	int retval;
1565 
1566 	retval = copy_from_user(&eos_cmd, (void __user *)arg, sizeof(eos_cmd));
1567 	if (retval)
1568 		return -EFAULT;
1569 
1570 	return ibeos(board, eos_cmd.eos, eos_cmd.eos_flags);
1571 }
1572 
1573 static int request_service_ioctl(struct gpib_board *board, unsigned long arg)
1574 {
1575 	u8 status_byte;
1576 	int retval;
1577 
1578 	retval = copy_from_user(&status_byte, (void __user *)arg, sizeof(status_byte));
1579 	if (retval)
1580 		return -EFAULT;
1581 
1582 	return ibrsv2(board, status_byte, status_byte & request_service_bit);
1583 }
1584 
1585 static int request_service2_ioctl(struct gpib_board *board, unsigned long arg)
1586 {
1587 	struct gpib_request_service2 request_service2_cmd;
1588 	int retval;
1589 
1590 	retval = copy_from_user(&request_service2_cmd, (void __user *)arg,
1591 				sizeof(struct gpib_request_service2));
1592 	if (retval)
1593 		return -EFAULT;
1594 
1595 	return ibrsv2(board, request_service2_cmd.status_byte,
1596 		      request_service2_cmd.new_reason_for_service);
1597 }
1598 
1599 static int iobase_ioctl(struct gpib_board_config *config, unsigned long arg)
1600 {
1601 	u64 base_addr;
1602 	int retval;
1603 
1604 	if (!capable(CAP_SYS_ADMIN))
1605 		return -EPERM;
1606 
1607 	retval = copy_from_user(&base_addr, (void __user *)arg, sizeof(base_addr));
1608 	if (retval)
1609 		return -EFAULT;
1610 
1611 	if (WARN_ON_ONCE(sizeof(void *) > sizeof(base_addr)))
1612 		return -EFAULT;
1613 	config->ibbase = base_addr;
1614 
1615 	return 0;
1616 }
1617 
1618 static int irq_ioctl(struct gpib_board_config *config, unsigned long arg)
1619 {
1620 	unsigned int irq;
1621 	int retval;
1622 
1623 	if (!capable(CAP_SYS_ADMIN))
1624 		return -EPERM;
1625 
1626 	retval = copy_from_user(&irq, (void __user *)arg, sizeof(irq));
1627 	if (retval)
1628 		return -EFAULT;
1629 
1630 	config->ibirq = irq;
1631 
1632 	return 0;
1633 }
1634 
1635 static int dma_ioctl(struct gpib_board_config *config, unsigned long arg)
1636 {
1637 	unsigned int dma_channel;
1638 	int retval;
1639 
1640 	if (!capable(CAP_SYS_ADMIN))
1641 		return -EPERM;
1642 
1643 	retval = copy_from_user(&dma_channel, (void __user *)arg, sizeof(dma_channel));
1644 	if (retval)
1645 		return -EFAULT;
1646 
1647 	config->ibdma = dma_channel;
1648 
1649 	return 0;
1650 }
1651 
1652 static int autospoll_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
1653 			   unsigned long arg)
1654 {
1655 	short enable;
1656 	int retval;
1657 	struct gpib_descriptor *desc;
1658 
1659 	retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable));
1660 	if (retval)
1661 		return -EFAULT;
1662 
1663 	desc = handle_to_descriptor(file_priv, 0); /* board handle is 0 */
1664 
1665 	if (enable) {
1666 		if (!desc->autopoll_enabled) {
1667 			board->autospollers++;
1668 			desc->autopoll_enabled = 1;
1669 		}
1670 		retval = 0;
1671 	} else {
1672 		if (desc->autopoll_enabled) {
1673 			desc->autopoll_enabled = 0;
1674 			if (board->autospollers > 0) {
1675 				board->autospollers--;
1676 				retval = 0;
1677 			} else {
1678 				dev_err(board->gpib_dev,
1679 					"tried to set number of autospollers negative\n");
1680 				retval = -EINVAL;
1681 			}
1682 		} else {
1683 			dev_err(board->gpib_dev, "autopoll disable requested before enable\n");
1684 			retval = -EINVAL;
1685 		}
1686 	}
1687 	return retval;
1688 }
1689 
1690 static int mutex_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
1691 		       unsigned long arg)
1692 {
1693 	int retval, lock_mutex;
1694 
1695 	retval = copy_from_user(&lock_mutex, (void __user *)arg, sizeof(lock_mutex));
1696 	if (retval)
1697 		return -EFAULT;
1698 
1699 	if (lock_mutex)	{
1700 		retval = mutex_lock_interruptible(&board->user_mutex);
1701 		if (retval)
1702 			return -ERESTARTSYS;
1703 
1704 		spin_lock(&board->locking_pid_spinlock);
1705 		board->locking_pid = current->pid;
1706 		spin_unlock(&board->locking_pid_spinlock);
1707 
1708 		atomic_set(&file_priv->holding_mutex, 1);
1709 
1710 		dev_dbg(board->gpib_dev, "locked board mutex\n");
1711 	} else {
1712 		spin_lock(&board->locking_pid_spinlock);
1713 		if (current->pid != board->locking_pid) {
1714 			dev_err(board->gpib_dev, "bug! pid %i tried to release mutex held by pid %i\n",
1715 				current->pid, board->locking_pid);
1716 			spin_unlock(&board->locking_pid_spinlock);
1717 			return -EPERM;
1718 		}
1719 		board->locking_pid = 0;
1720 		spin_unlock(&board->locking_pid_spinlock);
1721 
1722 		atomic_set(&file_priv->holding_mutex, 0);
1723 
1724 		mutex_unlock(&board->user_mutex);
1725 		dev_dbg(board->gpib_dev, "unlocked board mutex\n");
1726 	}
1727 	return 0;
1728 }
1729 
1730 static int timeout_ioctl(struct gpib_board *board, unsigned long arg)
1731 {
1732 	unsigned int timeout;
1733 	int retval;
1734 
1735 	retval = copy_from_user(&timeout, (void __user *)arg, sizeof(timeout));
1736 	if (retval)
1737 		return -EFAULT;
1738 
1739 	board->usec_timeout = timeout;
1740 	dev_dbg(board->gpib_dev, "timeout set to %i usec\n", timeout);
1741 
1742 	return 0;
1743 }
1744 
1745 static int ppc_ioctl(struct gpib_board *board, unsigned long arg)
1746 {
1747 	struct gpib_ppoll_config_ioctl cmd;
1748 	int retval;
1749 
1750 	retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
1751 	if (retval)
1752 		return -EFAULT;
1753 
1754 	if (cmd.set_ist) {
1755 		board->ist = 1;
1756 		board->interface->parallel_poll_response(board, board->ist);
1757 	} else if (cmd.clear_ist) {
1758 		board->ist = 0;
1759 		board->interface->parallel_poll_response(board, board->ist);
1760 	}
1761 
1762 	if (cmd.config)	{
1763 		retval = ibppc(board, cmd.config);
1764 		if (retval < 0)
1765 			return retval;
1766 	}
1767 
1768 	return 0;
1769 }
1770 
1771 static int set_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg)
1772 {
1773 	short cmd;
1774 	int retval;
1775 
1776 	retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
1777 	if (retval)
1778 		return -EFAULT;
1779 
1780 	if (!board->interface->local_parallel_poll_mode)
1781 		return -ENOENT;
1782 	board->local_ppoll_mode = cmd != 0;
1783 	board->interface->local_parallel_poll_mode(board, board->local_ppoll_mode);
1784 
1785 	return 0;
1786 }
1787 
1788 static int get_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg)
1789 {
1790 	short cmd;
1791 	int retval;
1792 
1793 	cmd = board->local_ppoll_mode;
1794 	retval = copy_to_user((void __user *)arg, &cmd, sizeof(cmd));
1795 	if (retval)
1796 		return -EFAULT;
1797 
1798 	return 0;
1799 }
1800 
1801 static int query_board_rsv_ioctl(struct gpib_board *board, unsigned long arg)
1802 {
1803 	int status;
1804 	int retval;
1805 
1806 	status = board->interface->serial_poll_status(board);
1807 
1808 	retval = copy_to_user((void __user *)arg, &status, sizeof(status));
1809 	if (retval)
1810 		return -EFAULT;
1811 
1812 	return 0;
1813 }
1814 
1815 static int board_info_ioctl(const struct gpib_board *board, unsigned long arg)
1816 {
1817 	struct gpib_board_info_ioctl info = { };
1818 	int retval;
1819 
1820 	info.pad = board->pad;
1821 	info.sad = board->sad;
1822 	info.parallel_poll_configuration = board->parallel_poll_configuration;
1823 	info.is_system_controller = board->master;
1824 	if (board->autospollers)
1825 		info.autopolling = 1;
1826 	else
1827 		info.autopolling = 0;
1828 	info.t1_delay = board->t1_nano_sec;
1829 	info.ist = board->ist;
1830 	info.no_7_bit_eos = board->interface->no_7_bit_eos;
1831 	retval = copy_to_user((void __user *)arg, &info, sizeof(info));
1832 	if (retval)
1833 		return -EFAULT;
1834 
1835 	return 0;
1836 }
1837 
1838 static int interface_clear_ioctl(struct gpib_board *board, unsigned long arg)
1839 {
1840 	unsigned int usec_duration;
1841 	int retval;
1842 
1843 	retval = copy_from_user(&usec_duration, (void __user *)arg, sizeof(usec_duration));
1844 	if (retval)
1845 		return -EFAULT;
1846 
1847 	return ibsic(board, usec_duration);
1848 }
1849 
1850 static int select_pci_ioctl(struct gpib_board_config *config, unsigned long arg)
1851 {
1852 	struct gpib_select_pci_ioctl selection;
1853 	int retval;
1854 
1855 	if (!capable(CAP_SYS_ADMIN))
1856 		return -EPERM;
1857 
1858 	retval = copy_from_user(&selection, (void __user *)arg, sizeof(selection));
1859 	if (retval)
1860 		return -EFAULT;
1861 
1862 	config->pci_bus = selection.pci_bus;
1863 	config->pci_slot = selection.pci_slot;
1864 
1865 	return 0;
1866 }
1867 
1868 static int select_device_path_ioctl(struct gpib_board_config *config, unsigned long arg)
1869 {
1870 	struct gpib_select_device_path_ioctl *selection;
1871 	int retval;
1872 
1873 	if (!capable(CAP_SYS_ADMIN))
1874 		return -EPERM;
1875 
1876 	selection = vmalloc(sizeof(struct gpib_select_device_path_ioctl));
1877 	if (!selection)
1878 		return -ENOMEM;
1879 
1880 	retval = copy_from_user(selection, (void __user *)arg,
1881 				sizeof(struct gpib_select_device_path_ioctl));
1882 	if (retval) {
1883 		vfree(selection);
1884 		return -EFAULT;
1885 	}
1886 
1887 	selection->device_path[sizeof(selection->device_path) - 1] = '\0';
1888 	kfree(config->device_path);
1889 	config->device_path = NULL;
1890 	if (strlen(selection->device_path) > 0)
1891 		config->device_path = kstrdup(selection->device_path, GFP_KERNEL);
1892 
1893 	vfree(selection);
1894 	return 0;
1895 }
1896 
1897 unsigned int num_gpib_events(const struct gpib_event_queue *queue)
1898 {
1899 	return queue->num_events;
1900 }
1901 
1902 static int push_gpib_event_nolock(struct gpib_board *board, short event_type)
1903 {
1904 	struct gpib_event_queue *queue = &board->event_queue;
1905 	struct list_head *head = &queue->event_head;
1906 	struct gpib_event *event;
1907 	static const unsigned int max_num_events = 1024;
1908 	int retval;
1909 
1910 	if (num_gpib_events(queue) >= max_num_events) {
1911 		short lost_event;
1912 
1913 		queue->dropped_event = 1;
1914 		retval = pop_gpib_event_nolock(board, queue, &lost_event);
1915 		if (retval < 0)
1916 			return retval;
1917 	}
1918 
1919 	event = kmalloc_obj(struct gpib_event, GFP_ATOMIC);
1920 	if (!event) {
1921 		queue->dropped_event = 1;
1922 		dev_err(board->gpib_dev, "failed to allocate memory for event\n");
1923 		return -ENOMEM;
1924 	}
1925 
1926 	INIT_LIST_HEAD(&event->list);
1927 	event->event_type = event_type;
1928 
1929 	list_add_tail(&event->list, head);
1930 
1931 	queue->num_events++;
1932 
1933 	dev_dbg(board->gpib_dev, "pushed event %i, %i in queue\n",
1934 		(int)event_type, num_gpib_events(queue));
1935 
1936 	return 0;
1937 }
1938 
1939 // push event onto back of event queue
1940 int push_gpib_event(struct gpib_board *board, short event_type)
1941 {
1942 	unsigned long flags;
1943 	int retval;
1944 
1945 	spin_lock_irqsave(&board->event_queue.lock, flags);
1946 	retval = push_gpib_event_nolock(board, event_type);
1947 	spin_unlock_irqrestore(&board->event_queue.lock, flags);
1948 
1949 	if (event_type == EVENT_DEV_TRG)
1950 		board->status |= DTAS;
1951 	if (event_type == EVENT_DEV_CLR)
1952 		board->status |= DCAS;
1953 
1954 	return retval;
1955 }
1956 EXPORT_SYMBOL(push_gpib_event);
1957 
1958 static int pop_gpib_event_nolock(struct gpib_board *board,
1959 				 struct gpib_event_queue *queue, short *event_type)
1960 {
1961 	struct list_head *head = &queue->event_head;
1962 	struct list_head *front = head->next;
1963 	struct gpib_event *event;
1964 
1965 	if (num_gpib_events(queue) == 0) {
1966 		*event_type = EVENT_NONE;
1967 		return 0;
1968 	}
1969 
1970 	if (front == head)
1971 		return -EIO;
1972 
1973 	if (queue->dropped_event) {
1974 		queue->dropped_event = 0;
1975 		return -EPIPE;
1976 	}
1977 
1978 	event = list_entry(front, struct gpib_event, list);
1979 	*event_type = event->event_type;
1980 
1981 	list_del(front);
1982 	kfree(event);
1983 
1984 	queue->num_events--;
1985 
1986 	dev_dbg(board->gpib_dev, "popped event %i, %i in queue\n",
1987 		(int)*event_type, num_gpib_events(queue));
1988 
1989 	return 0;
1990 }
1991 
1992 // pop event from front of event queue
1993 int pop_gpib_event(struct gpib_board *board, struct gpib_event_queue *queue, short *event_type)
1994 {
1995 	unsigned long flags;
1996 	int retval;
1997 
1998 	spin_lock_irqsave(&queue->lock, flags);
1999 	retval = pop_gpib_event_nolock(board, queue, event_type);
2000 	spin_unlock_irqrestore(&queue->lock, flags);
2001 	return retval;
2002 }
2003 
2004 static int event_ioctl(struct gpib_board *board, unsigned long arg)
2005 {
2006 	short user_event;
2007 	int retval;
2008 	short event;
2009 
2010 	retval = pop_gpib_event(board, &board->event_queue, &event);
2011 	if (retval < 0)
2012 		return retval;
2013 
2014 	user_event = event;
2015 
2016 	retval = copy_to_user((void __user *)arg, &user_event, sizeof(user_event));
2017 	if (retval)
2018 		return -EFAULT;
2019 
2020 	return 0;
2021 }
2022 
2023 static int request_system_control_ioctl(struct gpib_board *board, unsigned long arg)
2024 {
2025 	int request_control;
2026 	int retval;
2027 
2028 	retval = copy_from_user(&request_control, (void __user *)arg, sizeof(request_control));
2029 	if (retval)
2030 		return -EFAULT;
2031 
2032 	return ibrsc(board, request_control);
2033 }
2034 
2035 static int t1_delay_ioctl(struct gpib_board *board, unsigned long arg)
2036 {
2037 	unsigned int cmd;
2038 	unsigned int delay;
2039 	int retval;
2040 
2041 	if (!board->interface->t1_delay)
2042 		return -ENOENT;
2043 
2044 	retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
2045 	if (retval)
2046 		return -EFAULT;
2047 
2048 	delay = cmd;
2049 
2050 	retval = board->interface->t1_delay(board, delay);
2051 	if (retval < 0)
2052 		return retval;
2053 
2054 	board->t1_nano_sec = retval;
2055 	return 0;
2056 }
2057 
2058 static const struct file_operations ib_fops = {
2059 	.owner = THIS_MODULE,
2060 	.llseek = NULL,
2061 	.unlocked_ioctl = &ibioctl,
2062 	.compat_ioctl = &ibioctl,
2063 	.open = &ibopen,
2064 	.release = &ibclose,
2065 };
2066 
2067 struct gpib_board board_array[GPIB_MAX_NUM_BOARDS];
2068 
2069 LIST_HEAD(registered_drivers);
2070 
2071 void init_gpib_descriptor(struct gpib_descriptor *desc)
2072 {
2073 	desc->pad = 0;
2074 	desc->sad = -1;
2075 	desc->is_board = 0;
2076 	desc->autopoll_enabled = 0;
2077 	atomic_set(&desc->io_in_progress, 0);
2078 	atomic_set(&desc->descriptor_busy, 0);
2079 }
2080 
2081 int gpib_register_driver(struct gpib_interface *interface, struct module *provider_module)
2082 {
2083 	struct gpib_interface_list *entry;
2084 
2085 	entry = kmalloc_obj(*entry);
2086 	if (!entry)
2087 		return -ENOMEM;
2088 
2089 	entry->interface = interface;
2090 	entry->module = provider_module;
2091 	list_add(&entry->list, &registered_drivers);
2092 
2093 	return 0;
2094 }
2095 EXPORT_SYMBOL(gpib_register_driver);
2096 
2097 void gpib_unregister_driver(struct gpib_interface *interface)
2098 {
2099 	int i;
2100 	struct list_head *list_ptr;
2101 
2102 	for (i = 0; i < GPIB_MAX_NUM_BOARDS; i++) {
2103 		struct gpib_board *board = &board_array[i];
2104 
2105 		if (board->interface == interface) {
2106 			if (board->use_count > 0)
2107 				pr_warn("gpib: Warning: deregistered interface %s in use\n",
2108 					interface->name);
2109 			iboffline(board);
2110 			board->interface = NULL;
2111 		}
2112 	}
2113 	for (list_ptr = registered_drivers.next; list_ptr != &registered_drivers;) {
2114 		struct gpib_interface_list *entry;
2115 
2116 		entry = list_entry(list_ptr, struct gpib_interface_list, list);
2117 		list_ptr = list_ptr->next;
2118 		if (entry->interface == interface) {
2119 			list_del(&entry->list);
2120 			kfree(entry);
2121 		}
2122 	}
2123 }
2124 EXPORT_SYMBOL(gpib_unregister_driver);
2125 
2126 static void init_gpib_board_config(struct gpib_board_config *config)
2127 {
2128 	memset(config, 0, sizeof(struct gpib_board_config));
2129 	config->pci_bus = -1;
2130 	config->pci_slot = -1;
2131 }
2132 
2133 void init_gpib_board(struct gpib_board *board)
2134 {
2135 	board->interface = NULL;
2136 	board->provider_module = NULL;
2137 	board->buffer = NULL;
2138 	board->buffer_length = 0;
2139 	board->status = 0;
2140 	init_waitqueue_head(&board->wait);
2141 	mutex_init(&board->user_mutex);
2142 	mutex_init(&board->big_gpib_mutex);
2143 	board->locking_pid = 0;
2144 	spin_lock_init(&board->locking_pid_spinlock);
2145 	spin_lock_init(&board->spinlock);
2146 	timer_setup(&board->timer, NULL, 0);
2147 	board->dev = NULL;
2148 	board->gpib_dev = NULL;
2149 	init_gpib_board_config(&board->config);
2150 	board->private_data = NULL;
2151 	board->use_count = 0;
2152 	INIT_LIST_HEAD(&board->device_list);
2153 	board->pad = 0;
2154 	board->sad = -1;
2155 	board->usec_timeout = 3000000;
2156 	board->parallel_poll_configuration = 0;
2157 	board->online = 0;
2158 	board->autospollers = 0;
2159 	board->autospoll_task = NULL;
2160 	init_event_queue(&board->event_queue);
2161 	board->minor = -1;
2162 	init_gpib_pseudo_irq(&board->pseudo_irq);
2163 	board->master = 1;
2164 	atomic_set(&board->stuck_srq, 0);
2165 	board->local_ppoll_mode = 0;
2166 }
2167 
2168 int gpib_allocate_board(struct gpib_board *board)
2169 {
2170 	if (!board->buffer) {
2171 		board->buffer_length = 0x4000;
2172 		board->buffer = vmalloc(board->buffer_length);
2173 		if (!board->buffer) {
2174 			board->buffer_length = 0;
2175 			return -ENOMEM;
2176 		}
2177 	}
2178 	return 0;
2179 }
2180 
2181 void gpib_deallocate_board(struct gpib_board *board)
2182 {
2183 	short dummy;
2184 
2185 	if (board->buffer) {
2186 		vfree(board->buffer);
2187 		board->buffer = NULL;
2188 		board->buffer_length = 0;
2189 	}
2190 	while (num_gpib_events(&board->event_queue))
2191 		pop_gpib_event(board, &board->event_queue, &dummy);
2192 }
2193 
2194 static void init_board_array(struct gpib_board *board_array, unsigned int length)
2195 {
2196 	int i;
2197 
2198 	for (i = 0; i < length; i++) {
2199 		init_gpib_board(&board_array[i]);
2200 		board_array[i].minor = i;
2201 	}
2202 }
2203 
2204 void init_gpib_status_queue(struct gpib_status_queue *device)
2205 {
2206 	INIT_LIST_HEAD(&device->list);
2207 	INIT_LIST_HEAD(&device->status_bytes);
2208 	device->num_status_bytes = 0;
2209 	device->reference_count = 0;
2210 	device->dropped_byte = 0;
2211 }
2212 
2213 static const struct class gpib_class = {
2214 	.name	= "gpib_common",
2215 };
2216 
2217 static int __init gpib_common_init_module(void)
2218 {
2219 	int err;
2220 	int i;
2221 
2222 	pr_info("GPIB core driver\n");
2223 	init_board_array(board_array, GPIB_MAX_NUM_BOARDS);
2224 	if (register_chrdev(GPIB_CODE, "gpib", &ib_fops)) {
2225 		pr_err("gpib: can't get major %d\n", GPIB_CODE);
2226 		return -EIO;
2227 	}
2228 	err = class_register(&gpib_class);
2229 	if (err) {
2230 		pr_err("gpib: failed to create gpib class\n");
2231 		unregister_chrdev(GPIB_CODE, "gpib");
2232 		return err;
2233 	}
2234 	for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i)
2235 		board_array[i].gpib_dev = device_create(&gpib_class, NULL,
2236 							MKDEV(GPIB_CODE, i), NULL, "gpib%i", i);
2237 
2238 	return 0;
2239 }
2240 
2241 static void __exit gpib_common_exit_module(void)
2242 {
2243 	int i;
2244 
2245 	for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i)
2246 		device_destroy(&gpib_class, MKDEV(GPIB_CODE, i));
2247 
2248 	class_unregister(&gpib_class);
2249 	unregister_chrdev(GPIB_CODE, "gpib");
2250 }
2251 
2252 int gpib_match_device_path(struct device *dev, const char *device_path_in)
2253 {
2254 	if (device_path_in) {
2255 		char *device_path;
2256 
2257 		device_path = kobject_get_path(&dev->kobj, GFP_KERNEL);
2258 		if (!device_path) {
2259 			dev_err(dev, "kobject_get_path returned NULL.");
2260 			return 0;
2261 		}
2262 		if (strcmp(device_path_in, device_path) != 0) {
2263 			kfree(device_path);
2264 			return 0;
2265 		}
2266 		kfree(device_path);
2267 	}
2268 	return 1;
2269 }
2270 EXPORT_SYMBOL(gpib_match_device_path);
2271 
2272 struct pci_dev *gpib_pci_get_device(const struct gpib_board_config *config, unsigned int vendor_id,
2273 				    unsigned int device_id, struct pci_dev *from)
2274 {
2275 	struct pci_dev *pci_device = from;
2276 
2277 	while ((pci_device = pci_get_device(vendor_id, device_id, pci_device)))	{
2278 		if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number)
2279 			continue;
2280 		if (config->pci_slot >= 0 && config->pci_slot !=
2281 		    PCI_SLOT(pci_device->devfn))
2282 			continue;
2283 		if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0)
2284 			continue;
2285 		return pci_device;
2286 	}
2287 	return NULL;
2288 }
2289 EXPORT_SYMBOL(gpib_pci_get_device);
2290 
2291 struct pci_dev *gpib_pci_get_subsys(const struct gpib_board_config *config, unsigned int vendor_id,
2292 				    unsigned int device_id, unsigned int ss_vendor,
2293 				    unsigned int ss_device,
2294 				    struct pci_dev *from)
2295 {
2296 	struct pci_dev *pci_device = from;
2297 
2298 	while ((pci_device = pci_get_subsys(vendor_id, device_id,
2299 					    ss_vendor, ss_device, pci_device))) {
2300 		if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number)
2301 			continue;
2302 		if (config->pci_slot >= 0 && config->pci_slot !=
2303 		    PCI_SLOT(pci_device->devfn))
2304 			continue;
2305 		if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0)
2306 			continue;
2307 		return pci_device;
2308 	}
2309 	return NULL;
2310 }
2311 EXPORT_SYMBOL(gpib_pci_get_subsys);
2312 
2313 module_init(gpib_common_init_module);
2314 module_exit(gpib_common_exit_module);
2315 
2316