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