rio500.c 12.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* -*- linux-c -*- */

/* 
 * Driver for USB Rio 500
 *
 * Cesar Miquel (miquel@df.uba.ar)
 * 
 * based on hp_scanner.c by David E. Nelson (dnelson@jump.net)
 *
 * Based upon mouse.c (Brad Keryan) and printer.c (Michael Gee).
 *
 * Changelog:
 * 30/05/2003  replaced lock/unlock kernel with up/down
 *             Daniele Bellucci  bellucda@tiscali.it
 * */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/signal.h>
21
#include <linux/sched/signal.h>
22
#include <linux/mutex.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <linux/errno.h>
#include <linux/random.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/usb.h>
#include <linux/wait.h>

#include "rio500_usb.h"

#define DRIVER_AUTHOR "Cesar Miquel <miquel@df.uba.ar>"
#define DRIVER_DESC "USB Rio 500 driver"

#define RIO_MINOR	64

/* stall/wait timeout for rio */
#define NAK_TIMEOUT (HZ)

#define IBUF_SIZE 0x1000

/* Size of the rio buffer */
#define OBUF_SIZE 0x10000

struct rio_usb_data {
        struct usb_device *rio_dev;     /* init: probe_rio */
        unsigned int ifnum;             /* Interface number of the USB device */
        int isopen;                     /* nz if open */
        int present;                    /* Device is present on the bus */
        char *obuf, *ibuf;              /* transfer buffers */
        char bulk_in_ep, bulk_out_ep;   /* Endpoint assignments */
        wait_queue_head_t wait_q;       /* for timeouts */
};

56
static DEFINE_MUTEX(rio500_mutex);
L
Linus Torvalds 已提交
57 58 59 60 61
static struct rio_usb_data rio_instance;

static int open_rio(struct inode *inode, struct file *file)
{
	struct rio_usb_data *rio = &rio_instance;
O
Oliver Neukum 已提交
62 63

	/* against disconnect() */
64
	mutex_lock(&rio500_mutex);
L
Linus Torvalds 已提交
65 66

	if (rio->isopen || !rio->present) {
67
		mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
68 69 70 71 72 73 74
		return -EBUSY;
	}
	rio->isopen = 1;

	init_waitqueue_head(&rio->wait_q);


75
	dev_info(&rio->rio_dev->dev, "Rio opened.\n");
76
	mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84

	return 0;
}

static int close_rio(struct inode *inode, struct file *file)
{
	struct rio_usb_data *rio = &rio_instance;

85 86
	/* against disconnect() */
	mutex_lock(&rio500_mutex);
L
Linus Torvalds 已提交
87

88 89 90 91 92 93 94 95 96 97 98
	rio->isopen = 0;
	if (!rio->present) {
		/* cleanup has been delayed */
		kfree(rio->ibuf);
		kfree(rio->obuf);
		rio->ibuf = NULL;
		rio->obuf = NULL;
	} else {
		dev_info(&rio->rio_dev->dev, "Rio closed.\n");
	}
	mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
99 100 101
	return 0;
}

A
Alan Cox 已提交
102
static long ioctl_rio(struct file *file, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
103 104 105 106 107 108 109 110 111
{
	struct RioCommand rio_cmd;
	struct rio_usb_data *rio = &rio_instance;
	void __user *data;
	unsigned char *buffer;
	int result, requesttype;
	int retries;
	int retval=0;

O
Oliver Neukum 已提交
112
	mutex_lock(&rio500_mutex);
L
Linus Torvalds 已提交
113
        /* Sanity check to make sure rio is connected, powered, etc */
A
Adrian Bunk 已提交
114
        if (rio->present == 0 || rio->rio_dev == NULL) {
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
		retval = -ENODEV;
		goto err_out;
	}

	switch (cmd) {
	case RIO_RECV_COMMAND:
		data = (void __user *) arg;
		if (data == NULL)
			break;
		if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) {
			retval = -EFAULT;
			goto err_out;
		}
		if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) {
			retval = -EINVAL;
			goto err_out;
		}
		buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
		if (buffer == NULL) {
			retval = -ENOMEM;
			goto err_out;
		}
		if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
			retval = -EFAULT;
			free_page((unsigned long) buffer);
			goto err_out;
		}

		requesttype = rio_cmd.requesttype | USB_DIR_IN |
		    USB_TYPE_VENDOR | USB_RECIP_DEVICE;
145 146 147 148
		dev_dbg(&rio->rio_dev->dev,
			"sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
			requesttype, rio_cmd.request, rio_cmd.value,
			rio_cmd.index, rio_cmd.length);
L
Linus Torvalds 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162
		/* Send rio control message */
		retries = 3;
		while (retries) {
			result = usb_control_msg(rio->rio_dev,
						 usb_rcvctrlpipe(rio-> rio_dev, 0),
						 rio_cmd.request,
						 requesttype,
						 rio_cmd.value,
						 rio_cmd.index, buffer,
						 rio_cmd.length,
						 jiffies_to_msecs(rio_cmd.timeout));
			if (result == -ETIMEDOUT)
				retries--;
			else if (result < 0) {
163 164 165
				dev_err(&rio->rio_dev->dev,
					"Error executing ioctrl. code = %d\n",
					result);
L
Linus Torvalds 已提交
166 167
				retries = 0;
			} else {
168 169 170
				dev_dbg(&rio->rio_dev->dev,
					"Executed ioctl. Result = %d (data=%02x)\n",
					result, buffer[0]);
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
				if (copy_to_user(rio_cmd.buffer, buffer,
						 rio_cmd.length)) {
					free_page((unsigned long) buffer);
					retval = -EFAULT;
					goto err_out;
				}
				retries = 0;
			}

			/* rio_cmd.buffer contains a raw stream of single byte
			   data which has been returned from rio.  Data is
			   interpreted at application level.  For data that
			   will be cast to data types longer than 1 byte, data
			   will be little_endian and will potentially need to
			   be swapped at the app level */

		}
		free_page((unsigned long) buffer);
		break;

	case RIO_SEND_COMMAND:
		data = (void __user *) arg;
		if (data == NULL)
			break;
		if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) {
			retval = -EFAULT;
			goto err_out;
		}
		if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) {
			retval = -EINVAL;
			goto err_out;
		}
		buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
		if (buffer == NULL) {
			retval = -ENOMEM;
			goto err_out;
		}
		if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
			free_page((unsigned long)buffer);
			retval = -EFAULT;
			goto err_out;
		}

		requesttype = rio_cmd.requesttype | USB_DIR_OUT |
		    USB_TYPE_VENDOR | USB_RECIP_DEVICE;
216 217 218 219
		dev_dbg(&rio->rio_dev->dev,
			"sending command: reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
			requesttype, rio_cmd.request, rio_cmd.value,
			rio_cmd.index, rio_cmd.length);
L
Linus Torvalds 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233
		/* Send rio control message */
		retries = 3;
		while (retries) {
			result = usb_control_msg(rio->rio_dev,
						 usb_sndctrlpipe(rio-> rio_dev, 0),
						 rio_cmd.request,
						 requesttype,
						 rio_cmd.value,
						 rio_cmd.index, buffer,
						 rio_cmd.length,
						 jiffies_to_msecs(rio_cmd.timeout));
			if (result == -ETIMEDOUT)
				retries--;
			else if (result < 0) {
234 235 236
				dev_err(&rio->rio_dev->dev,
					"Error executing ioctrl. code = %d\n",
					result);
L
Linus Torvalds 已提交
237 238
				retries = 0;
			} else {
239 240
				dev_dbg(&rio->rio_dev->dev,
					"Executed ioctl. Result = %d\n", result);
L
Linus Torvalds 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
				retries = 0;

			}

		}
		free_page((unsigned long) buffer);
		break;

	default:
		retval = -ENOTTY;
		break;
	}


err_out:
O
Oliver Neukum 已提交
256
	mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	return retval;
}

static ssize_t
write_rio(struct file *file, const char __user *buffer,
	  size_t count, loff_t * ppos)
{
	DEFINE_WAIT(wait);
	struct rio_usb_data *rio = &rio_instance;

	unsigned long copy_size;
	unsigned long bytes_written = 0;
	unsigned int partial;

	int result = 0;
	int maxretry;
	int errn = 0;
O
Oliver Neukum 已提交
274
	int intr;
L
Linus Torvalds 已提交
275

O
Oliver Neukum 已提交
276
	intr = mutex_lock_interruptible(&rio500_mutex);
O
Oliver Neukum 已提交
277 278
	if (intr)
		return -EINTR;
L
Linus Torvalds 已提交
279
        /* Sanity check to make sure rio is connected, powered, etc */
A
Adrian Bunk 已提交
280
        if (rio->present == 0 || rio->rio_dev == NULL) {
O
Oliver Neukum 已提交
281
		mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
		return -ENODEV;
	}



	do {
		unsigned long thistime;
		char *obuf = rio->obuf;

		thistime = copy_size =
		    (count >= OBUF_SIZE) ? OBUF_SIZE : count;
		if (copy_from_user(rio->obuf, buffer, copy_size)) {
			errn = -EFAULT;
			goto error;
		}
		maxretry = 5;
		while (thistime) {
			if (!rio->rio_dev) {
				errn = -ENODEV;
				goto error;
			}
			if (signal_pending(current)) {
O
Oliver Neukum 已提交
304
				mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
305 306 307 308 309 310 311
				return bytes_written ? bytes_written : -EINTR;
			}

			result = usb_bulk_msg(rio->rio_dev,
					 usb_sndbulkpipe(rio->rio_dev, 2),
					 obuf, thistime, &partial, 5000);

312 313 314
			dev_dbg(&rio->rio_dev->dev,
				"write stats: result:%d thistime:%lu partial:%u\n",
				result, thistime, partial);
L
Linus Torvalds 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

			if (result == -ETIMEDOUT) {	/* NAK - so hold for a while */
				if (!maxretry--) {
					errn = -ETIME;
					goto error;
				}
				prepare_to_wait(&rio->wait_q, &wait, TASK_INTERRUPTIBLE);
				schedule_timeout(NAK_TIMEOUT);
				finish_wait(&rio->wait_q, &wait);
				continue;
			} else if (!result && partial) {
				obuf += partial;
				thistime -= partial;
			} else
				break;
330
		}
L
Linus Torvalds 已提交
331
		if (result) {
332 333
			dev_err(&rio->rio_dev->dev, "Write Whoops - %x\n",
				result);
L
Linus Torvalds 已提交
334 335 336 337 338 339 340 341
			errn = -EIO;
			goto error;
		}
		bytes_written += copy_size;
		count -= copy_size;
		buffer += copy_size;
	} while (count > 0);

O
Oliver Neukum 已提交
342
	mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
343 344 345 346

	return bytes_written ? bytes_written : -EIO;

error:
O
Oliver Neukum 已提交
347
	mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361
	return errn;
}

static ssize_t
read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
{
	DEFINE_WAIT(wait);
	struct rio_usb_data *rio = &rio_instance;
	ssize_t read_count;
	unsigned int partial;
	int this_read;
	int result;
	int maxretry = 10;
	char *ibuf;
O
Oliver Neukum 已提交
362
	int intr;
L
Linus Torvalds 已提交
363

O
Oliver Neukum 已提交
364
	intr = mutex_lock_interruptible(&rio500_mutex);
O
Oliver Neukum 已提交
365 366
	if (intr)
		return -EINTR;
L
Linus Torvalds 已提交
367
	/* Sanity check to make sure rio is connected, powered, etc */
A
Adrian Bunk 已提交
368
        if (rio->present == 0 || rio->rio_dev == NULL) {
O
Oliver Neukum 已提交
369
		mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
370 371 372 373 374 375 376 377 378 379
		return -ENODEV;
	}

	ibuf = rio->ibuf;

	read_count = 0;


	while (count > 0) {
		if (signal_pending(current)) {
O
Oliver Neukum 已提交
380
			mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
381 382 383
			return read_count ? read_count : -EINTR;
		}
		if (!rio->rio_dev) {
O
Oliver Neukum 已提交
384
			mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
385 386 387 388 389 390 391 392 393
			return -ENODEV;
		}
		this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;

		result = usb_bulk_msg(rio->rio_dev,
				      usb_rcvbulkpipe(rio->rio_dev, 1),
				      ibuf, this_read, &partial,
				      8000);

394 395 396
		dev_dbg(&rio->rio_dev->dev,
			"read stats: result:%d this_read:%u partial:%u\n",
			result, this_read, partial);
L
Linus Torvalds 已提交
397 398 399 400 401

		if (partial) {
			count = this_read = partial;
		} else if (result == -ETIMEDOUT || result == 15) {	/* FIXME: 15 ??? */
			if (!maxretry--) {
O
Oliver Neukum 已提交
402
				mutex_unlock(&rio500_mutex);
403 404
				dev_err(&rio->rio_dev->dev,
					"read_rio: maxretry timeout\n");
L
Linus Torvalds 已提交
405 406 407 408 409 410 411
				return -ETIME;
			}
			prepare_to_wait(&rio->wait_q, &wait, TASK_INTERRUPTIBLE);
			schedule_timeout(NAK_TIMEOUT);
			finish_wait(&rio->wait_q, &wait);
			continue;
		} else if (result != -EREMOTEIO) {
O
Oliver Neukum 已提交
412
			mutex_unlock(&rio500_mutex);
413
			dev_err(&rio->rio_dev->dev,
414
				"Read Whoops - result:%d partial:%u this_read:%u\n",
415
				result, partial, this_read);
L
Linus Torvalds 已提交
416 417
			return -EIO;
		} else {
O
Oliver Neukum 已提交
418
			mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
419 420 421 422 423
			return (0);
		}

		if (this_read) {
			if (copy_to_user(buffer, ibuf, this_read)) {
O
Oliver Neukum 已提交
424
				mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
425 426 427 428 429 430 431
				return -EFAULT;
			}
			count -= this_read;
			read_count += this_read;
			buffer += this_read;
		}
	}
O
Oliver Neukum 已提交
432
	mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
433 434 435
	return read_count;
}

436
static const struct file_operations usb_rio_fops = {
L
Linus Torvalds 已提交
437 438 439
	.owner =	THIS_MODULE,
	.read =		read_rio,
	.write =	write_rio,
A
Alan Cox 已提交
440
	.unlocked_ioctl = ioctl_rio,
L
Linus Torvalds 已提交
441 442
	.open =		open_rio,
	.release =	close_rio,
443
	.llseek =	noop_llseek,
L
Linus Torvalds 已提交
444 445 446
};

static struct usb_class_driver usb_rio_class = {
447
	.name =		"rio500%d",
L
Linus Torvalds 已提交
448 449 450 451 452 453 454 455 456
	.fops =		&usb_rio_fops,
	.minor_base =	RIO_MINOR,
};

static int probe_rio(struct usb_interface *intf,
		     const struct usb_device_id *id)
{
	struct usb_device *dev = interface_to_usbdev(intf);
	struct rio_usb_data *rio = &rio_instance;
457
	int retval = 0;
L
Linus Torvalds 已提交
458

459 460 461 462 463 464 465 466
	mutex_lock(&rio500_mutex);
	if (rio->present) {
		dev_info(&intf->dev, "Second USB Rio at address %d refused\n", dev->devnum);
		retval = -EBUSY;
		goto bail_out;
	} else {
		dev_info(&intf->dev, "USB Rio found at address %d\n", dev->devnum);
	}
L
Linus Torvalds 已提交
467 468 469

	retval = usb_register_dev(intf, &usb_rio_class);
	if (retval) {
470 471
		dev_err(&dev->dev,
			"Not able to get a minor for this device.\n");
472 473
		retval = -ENOMEM;
		goto bail_out;
L
Linus Torvalds 已提交
474 475 476 477
	}

	rio->rio_dev = dev;

478
	if (!(rio->obuf = kmalloc(OBUF_SIZE, GFP_KERNEL))) {
479 480
		dev_err(&dev->dev,
			"probe_rio: Not enough memory for the output buffer\n");
L
Linus Torvalds 已提交
481
		usb_deregister_dev(intf, &usb_rio_class);
482 483
		retval = -ENOMEM;
		goto bail_out;
L
Linus Torvalds 已提交
484
	}
485
	dev_dbg(&intf->dev, "obuf address:%p\n", rio->obuf);
L
Linus Torvalds 已提交
486

487
	if (!(rio->ibuf = kmalloc(IBUF_SIZE, GFP_KERNEL))) {
488 489
		dev_err(&dev->dev,
			"probe_rio: Not enough memory for the input buffer\n");
L
Linus Torvalds 已提交
490 491
		usb_deregister_dev(intf, &usb_rio_class);
		kfree(rio->obuf);
492 493
		retval = -ENOMEM;
		goto bail_out;
L
Linus Torvalds 已提交
494
	}
495
	dev_dbg(&intf->dev, "ibuf address:%p\n", rio->ibuf);
L
Linus Torvalds 已提交
496 497 498

	usb_set_intfdata (intf, rio);
	rio->present = 1;
499 500
bail_out:
	mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
501

502
	return retval;
L
Linus Torvalds 已提交
503 504 505 506 507 508 509
}

static void disconnect_rio(struct usb_interface *intf)
{
	struct rio_usb_data *rio = usb_get_intfdata (intf);

	usb_set_intfdata (intf, NULL);
510
	mutex_lock(&rio500_mutex);
L
Linus Torvalds 已提交
511 512 513 514 515 516 517
	if (rio) {
		usb_deregister_dev(intf, &usb_rio_class);

		if (rio->isopen) {
			rio->isopen = 0;
			/* better let it finish - the release will do whats needed */
			rio->rio_dev = NULL;
518
			mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
519 520 521 522 523
			return;
		}
		kfree(rio->ibuf);
		kfree(rio->obuf);

524
		dev_info(&intf->dev, "USB Rio disconnected.\n");
L
Linus Torvalds 已提交
525 526 527

		rio->present = 0;
	}
528
	mutex_unlock(&rio500_mutex);
L
Linus Torvalds 已提交
529 530
}

531
static const struct usb_device_id rio_table[] = {
L
Linus Torvalds 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544
	{ USB_DEVICE(0x0841, 1) }, 		/* Rio 500 */
	{ }					/* Terminating entry */
};

MODULE_DEVICE_TABLE (usb, rio_table);

static struct usb_driver rio_driver = {
	.name =		"rio500",
	.probe =	probe_rio,
	.disconnect =	disconnect_rio,
	.id_table =	rio_table,
};

545
module_usb_driver(rio_driver);
L
Linus Torvalds 已提交
546 547 548 549 550

MODULE_AUTHOR( DRIVER_AUTHOR );
MODULE_DESCRIPTION( DRIVER_DESC );
MODULE_LICENSE("GPL");