Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.14-rc2 549 lines 14 kB view raw
1/* -*- linux-c -*- */ 2 3/* 4 * Driver for USB Rio 500 5 * 6 * Cesar Miquel (miquel@df.uba.ar) 7 * 8 * based on hp_scanner.c by David E. Nelson (dnelson@jump.net) 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License as 12 * published by the Free Software Foundation; either version 2 of the 13 * License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, but 16 * WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 23 * 24 * Based upon mouse.c (Brad Keryan) and printer.c (Michael Gee). 25 * 26 * Changelog: 27 * 30/05/2003 replaced lock/unlock kernel with up/down 28 * Daniele Bellucci bellucda@tiscali.it 29 * */ 30 31#include <linux/module.h> 32#include <linux/kernel.h> 33#include <linux/signal.h> 34#include <linux/sched/signal.h> 35#include <linux/mutex.h> 36#include <linux/errno.h> 37#include <linux/random.h> 38#include <linux/poll.h> 39#include <linux/slab.h> 40#include <linux/spinlock.h> 41#include <linux/usb.h> 42#include <linux/wait.h> 43 44#include "rio500_usb.h" 45 46#define DRIVER_AUTHOR "Cesar Miquel <miquel@df.uba.ar>" 47#define DRIVER_DESC "USB Rio 500 driver" 48 49#define RIO_MINOR 64 50 51/* stall/wait timeout for rio */ 52#define NAK_TIMEOUT (HZ) 53 54#define IBUF_SIZE 0x1000 55 56/* Size of the rio buffer */ 57#define OBUF_SIZE 0x10000 58 59struct rio_usb_data { 60 struct usb_device *rio_dev; /* init: probe_rio */ 61 unsigned int ifnum; /* Interface number of the USB device */ 62 int isopen; /* nz if open */ 63 int present; /* Device is present on the bus */ 64 char *obuf, *ibuf; /* transfer buffers */ 65 char bulk_in_ep, bulk_out_ep; /* Endpoint assignments */ 66 wait_queue_head_t wait_q; /* for timeouts */ 67 struct mutex lock; /* general race avoidance */ 68}; 69 70static DEFINE_MUTEX(rio500_mutex); 71static struct rio_usb_data rio_instance; 72 73static int open_rio(struct inode *inode, struct file *file) 74{ 75 struct rio_usb_data *rio = &rio_instance; 76 77 /* against disconnect() */ 78 mutex_lock(&rio500_mutex); 79 mutex_lock(&(rio->lock)); 80 81 if (rio->isopen || !rio->present) { 82 mutex_unlock(&(rio->lock)); 83 mutex_unlock(&rio500_mutex); 84 return -EBUSY; 85 } 86 rio->isopen = 1; 87 88 init_waitqueue_head(&rio->wait_q); 89 90 mutex_unlock(&(rio->lock)); 91 92 dev_info(&rio->rio_dev->dev, "Rio opened.\n"); 93 mutex_unlock(&rio500_mutex); 94 95 return 0; 96} 97 98static int close_rio(struct inode *inode, struct file *file) 99{ 100 struct rio_usb_data *rio = &rio_instance; 101 102 rio->isopen = 0; 103 104 dev_info(&rio->rio_dev->dev, "Rio closed.\n"); 105 return 0; 106} 107 108static long ioctl_rio(struct file *file, unsigned int cmd, unsigned long arg) 109{ 110 struct RioCommand rio_cmd; 111 struct rio_usb_data *rio = &rio_instance; 112 void __user *data; 113 unsigned char *buffer; 114 int result, requesttype; 115 int retries; 116 int retval=0; 117 118 mutex_lock(&(rio->lock)); 119 /* Sanity check to make sure rio is connected, powered, etc */ 120 if (rio->present == 0 || rio->rio_dev == NULL) { 121 retval = -ENODEV; 122 goto err_out; 123 } 124 125 switch (cmd) { 126 case RIO_RECV_COMMAND: 127 data = (void __user *) arg; 128 if (data == NULL) 129 break; 130 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) { 131 retval = -EFAULT; 132 goto err_out; 133 } 134 if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) { 135 retval = -EINVAL; 136 goto err_out; 137 } 138 buffer = (unsigned char *) __get_free_page(GFP_KERNEL); 139 if (buffer == NULL) { 140 retval = -ENOMEM; 141 goto err_out; 142 } 143 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) { 144 retval = -EFAULT; 145 free_page((unsigned long) buffer); 146 goto err_out; 147 } 148 149 requesttype = rio_cmd.requesttype | USB_DIR_IN | 150 USB_TYPE_VENDOR | USB_RECIP_DEVICE; 151 dev_dbg(&rio->rio_dev->dev, 152 "sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n", 153 requesttype, rio_cmd.request, rio_cmd.value, 154 rio_cmd.index, rio_cmd.length); 155 /* Send rio control message */ 156 retries = 3; 157 while (retries) { 158 result = usb_control_msg(rio->rio_dev, 159 usb_rcvctrlpipe(rio-> rio_dev, 0), 160 rio_cmd.request, 161 requesttype, 162 rio_cmd.value, 163 rio_cmd.index, buffer, 164 rio_cmd.length, 165 jiffies_to_msecs(rio_cmd.timeout)); 166 if (result == -ETIMEDOUT) 167 retries--; 168 else if (result < 0) { 169 dev_err(&rio->rio_dev->dev, 170 "Error executing ioctrl. code = %d\n", 171 result); 172 retries = 0; 173 } else { 174 dev_dbg(&rio->rio_dev->dev, 175 "Executed ioctl. Result = %d (data=%02x)\n", 176 result, buffer[0]); 177 if (copy_to_user(rio_cmd.buffer, buffer, 178 rio_cmd.length)) { 179 free_page((unsigned long) buffer); 180 retval = -EFAULT; 181 goto err_out; 182 } 183 retries = 0; 184 } 185 186 /* rio_cmd.buffer contains a raw stream of single byte 187 data which has been returned from rio. Data is 188 interpreted at application level. For data that 189 will be cast to data types longer than 1 byte, data 190 will be little_endian and will potentially need to 191 be swapped at the app level */ 192 193 } 194 free_page((unsigned long) buffer); 195 break; 196 197 case RIO_SEND_COMMAND: 198 data = (void __user *) arg; 199 if (data == NULL) 200 break; 201 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) { 202 retval = -EFAULT; 203 goto err_out; 204 } 205 if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) { 206 retval = -EINVAL; 207 goto err_out; 208 } 209 buffer = (unsigned char *) __get_free_page(GFP_KERNEL); 210 if (buffer == NULL) { 211 retval = -ENOMEM; 212 goto err_out; 213 } 214 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) { 215 free_page((unsigned long)buffer); 216 retval = -EFAULT; 217 goto err_out; 218 } 219 220 requesttype = rio_cmd.requesttype | USB_DIR_OUT | 221 USB_TYPE_VENDOR | USB_RECIP_DEVICE; 222 dev_dbg(&rio->rio_dev->dev, 223 "sending command: reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n", 224 requesttype, rio_cmd.request, rio_cmd.value, 225 rio_cmd.index, rio_cmd.length); 226 /* Send rio control message */ 227 retries = 3; 228 while (retries) { 229 result = usb_control_msg(rio->rio_dev, 230 usb_sndctrlpipe(rio-> rio_dev, 0), 231 rio_cmd.request, 232 requesttype, 233 rio_cmd.value, 234 rio_cmd.index, buffer, 235 rio_cmd.length, 236 jiffies_to_msecs(rio_cmd.timeout)); 237 if (result == -ETIMEDOUT) 238 retries--; 239 else if (result < 0) { 240 dev_err(&rio->rio_dev->dev, 241 "Error executing ioctrl. code = %d\n", 242 result); 243 retries = 0; 244 } else { 245 dev_dbg(&rio->rio_dev->dev, 246 "Executed ioctl. Result = %d\n", result); 247 retries = 0; 248 249 } 250 251 } 252 free_page((unsigned long) buffer); 253 break; 254 255 default: 256 retval = -ENOTTY; 257 break; 258 } 259 260 261err_out: 262 mutex_unlock(&(rio->lock)); 263 return retval; 264} 265 266static ssize_t 267write_rio(struct file *file, const char __user *buffer, 268 size_t count, loff_t * ppos) 269{ 270 DEFINE_WAIT(wait); 271 struct rio_usb_data *rio = &rio_instance; 272 273 unsigned long copy_size; 274 unsigned long bytes_written = 0; 275 unsigned int partial; 276 277 int result = 0; 278 int maxretry; 279 int errn = 0; 280 int intr; 281 282 intr = mutex_lock_interruptible(&(rio->lock)); 283 if (intr) 284 return -EINTR; 285 /* Sanity check to make sure rio is connected, powered, etc */ 286 if (rio->present == 0 || rio->rio_dev == NULL) { 287 mutex_unlock(&(rio->lock)); 288 return -ENODEV; 289 } 290 291 292 293 do { 294 unsigned long thistime; 295 char *obuf = rio->obuf; 296 297 thistime = copy_size = 298 (count >= OBUF_SIZE) ? OBUF_SIZE : count; 299 if (copy_from_user(rio->obuf, buffer, copy_size)) { 300 errn = -EFAULT; 301 goto error; 302 } 303 maxretry = 5; 304 while (thistime) { 305 if (!rio->rio_dev) { 306 errn = -ENODEV; 307 goto error; 308 } 309 if (signal_pending(current)) { 310 mutex_unlock(&(rio->lock)); 311 return bytes_written ? bytes_written : -EINTR; 312 } 313 314 result = usb_bulk_msg(rio->rio_dev, 315 usb_sndbulkpipe(rio->rio_dev, 2), 316 obuf, thistime, &partial, 5000); 317 318 dev_dbg(&rio->rio_dev->dev, 319 "write stats: result:%d thistime:%lu partial:%u\n", 320 result, thistime, partial); 321 322 if (result == -ETIMEDOUT) { /* NAK - so hold for a while */ 323 if (!maxretry--) { 324 errn = -ETIME; 325 goto error; 326 } 327 prepare_to_wait(&rio->wait_q, &wait, TASK_INTERRUPTIBLE); 328 schedule_timeout(NAK_TIMEOUT); 329 finish_wait(&rio->wait_q, &wait); 330 continue; 331 } else if (!result && partial) { 332 obuf += partial; 333 thistime -= partial; 334 } else 335 break; 336 } 337 if (result) { 338 dev_err(&rio->rio_dev->dev, "Write Whoops - %x\n", 339 result); 340 errn = -EIO; 341 goto error; 342 } 343 bytes_written += copy_size; 344 count -= copy_size; 345 buffer += copy_size; 346 } while (count > 0); 347 348 mutex_unlock(&(rio->lock)); 349 350 return bytes_written ? bytes_written : -EIO; 351 352error: 353 mutex_unlock(&(rio->lock)); 354 return errn; 355} 356 357static ssize_t 358read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos) 359{ 360 DEFINE_WAIT(wait); 361 struct rio_usb_data *rio = &rio_instance; 362 ssize_t read_count; 363 unsigned int partial; 364 int this_read; 365 int result; 366 int maxretry = 10; 367 char *ibuf; 368 int intr; 369 370 intr = mutex_lock_interruptible(&(rio->lock)); 371 if (intr) 372 return -EINTR; 373 /* Sanity check to make sure rio is connected, powered, etc */ 374 if (rio->present == 0 || rio->rio_dev == NULL) { 375 mutex_unlock(&(rio->lock)); 376 return -ENODEV; 377 } 378 379 ibuf = rio->ibuf; 380 381 read_count = 0; 382 383 384 while (count > 0) { 385 if (signal_pending(current)) { 386 mutex_unlock(&(rio->lock)); 387 return read_count ? read_count : -EINTR; 388 } 389 if (!rio->rio_dev) { 390 mutex_unlock(&(rio->lock)); 391 return -ENODEV; 392 } 393 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count; 394 395 result = usb_bulk_msg(rio->rio_dev, 396 usb_rcvbulkpipe(rio->rio_dev, 1), 397 ibuf, this_read, &partial, 398 8000); 399 400 dev_dbg(&rio->rio_dev->dev, 401 "read stats: result:%d this_read:%u partial:%u\n", 402 result, this_read, partial); 403 404 if (partial) { 405 count = this_read = partial; 406 } else if (result == -ETIMEDOUT || result == 15) { /* FIXME: 15 ??? */ 407 if (!maxretry--) { 408 mutex_unlock(&(rio->lock)); 409 dev_err(&rio->rio_dev->dev, 410 "read_rio: maxretry timeout\n"); 411 return -ETIME; 412 } 413 prepare_to_wait(&rio->wait_q, &wait, TASK_INTERRUPTIBLE); 414 schedule_timeout(NAK_TIMEOUT); 415 finish_wait(&rio->wait_q, &wait); 416 continue; 417 } else if (result != -EREMOTEIO) { 418 mutex_unlock(&(rio->lock)); 419 dev_err(&rio->rio_dev->dev, 420 "Read Whoops - result:%d partial:%u this_read:%u\n", 421 result, partial, this_read); 422 return -EIO; 423 } else { 424 mutex_unlock(&(rio->lock)); 425 return (0); 426 } 427 428 if (this_read) { 429 if (copy_to_user(buffer, ibuf, this_read)) { 430 mutex_unlock(&(rio->lock)); 431 return -EFAULT; 432 } 433 count -= this_read; 434 read_count += this_read; 435 buffer += this_read; 436 } 437 } 438 mutex_unlock(&(rio->lock)); 439 return read_count; 440} 441 442static const struct file_operations usb_rio_fops = { 443 .owner = THIS_MODULE, 444 .read = read_rio, 445 .write = write_rio, 446 .unlocked_ioctl = ioctl_rio, 447 .open = open_rio, 448 .release = close_rio, 449 .llseek = noop_llseek, 450}; 451 452static struct usb_class_driver usb_rio_class = { 453 .name = "rio500%d", 454 .fops = &usb_rio_fops, 455 .minor_base = RIO_MINOR, 456}; 457 458static int probe_rio(struct usb_interface *intf, 459 const struct usb_device_id *id) 460{ 461 struct usb_device *dev = interface_to_usbdev(intf); 462 struct rio_usb_data *rio = &rio_instance; 463 int retval; 464 465 dev_info(&intf->dev, "USB Rio found at address %d\n", dev->devnum); 466 467 retval = usb_register_dev(intf, &usb_rio_class); 468 if (retval) { 469 dev_err(&dev->dev, 470 "Not able to get a minor for this device.\n"); 471 return -ENOMEM; 472 } 473 474 rio->rio_dev = dev; 475 476 if (!(rio->obuf = kmalloc(OBUF_SIZE, GFP_KERNEL))) { 477 dev_err(&dev->dev, 478 "probe_rio: Not enough memory for the output buffer\n"); 479 usb_deregister_dev(intf, &usb_rio_class); 480 return -ENOMEM; 481 } 482 dev_dbg(&intf->dev, "obuf address:%p\n", rio->obuf); 483 484 if (!(rio->ibuf = kmalloc(IBUF_SIZE, GFP_KERNEL))) { 485 dev_err(&dev->dev, 486 "probe_rio: Not enough memory for the input buffer\n"); 487 usb_deregister_dev(intf, &usb_rio_class); 488 kfree(rio->obuf); 489 return -ENOMEM; 490 } 491 dev_dbg(&intf->dev, "ibuf address:%p\n", rio->ibuf); 492 493 mutex_init(&(rio->lock)); 494 495 usb_set_intfdata (intf, rio); 496 rio->present = 1; 497 498 return 0; 499} 500 501static void disconnect_rio(struct usb_interface *intf) 502{ 503 struct rio_usb_data *rio = usb_get_intfdata (intf); 504 505 usb_set_intfdata (intf, NULL); 506 mutex_lock(&rio500_mutex); 507 if (rio) { 508 usb_deregister_dev(intf, &usb_rio_class); 509 510 mutex_lock(&(rio->lock)); 511 if (rio->isopen) { 512 rio->isopen = 0; 513 /* better let it finish - the release will do whats needed */ 514 rio->rio_dev = NULL; 515 mutex_unlock(&(rio->lock)); 516 mutex_unlock(&rio500_mutex); 517 return; 518 } 519 kfree(rio->ibuf); 520 kfree(rio->obuf); 521 522 dev_info(&intf->dev, "USB Rio disconnected.\n"); 523 524 rio->present = 0; 525 mutex_unlock(&(rio->lock)); 526 } 527 mutex_unlock(&rio500_mutex); 528} 529 530static const struct usb_device_id rio_table[] = { 531 { USB_DEVICE(0x0841, 1) }, /* Rio 500 */ 532 { } /* Terminating entry */ 533}; 534 535MODULE_DEVICE_TABLE (usb, rio_table); 536 537static struct usb_driver rio_driver = { 538 .name = "rio500", 539 .probe = probe_rio, 540 .disconnect = disconnect_rio, 541 .id_table = rio_table, 542}; 543 544module_usb_driver(rio_driver); 545 546MODULE_AUTHOR( DRIVER_AUTHOR ); 547MODULE_DESCRIPTION( DRIVER_DESC ); 548MODULE_LICENSE("GPL"); 549