1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * f_printer.c - USB printer function driver 4 * 5 * Copied from drivers/usb/gadget/legacy/printer.c, 6 * which was: 7 * 8 * printer.c -- Printer gadget driver 9 * 10 * Copyright (C) 2003-2005 David Brownell 11 * Copyright (C) 2006 Craig W. Nadler 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License as published by 15 * the Free Software Foundation; either version 2 of the License, or 16 * (at your option) any later version. 17 */ 18 19 #include <linux/module.h> 20 #include <linux/kernel.h> 21 #include <linux/delay.h> 22 #include <linux/ioport.h> 23 #include <linux/sched.h> 24 #include <linux/slab.h> 25 #include <linux/mutex.h> 26 #include <linux/errno.h> 27 #include <linux/init.h> 28 #include <linux/idr.h> 29 #include <linux/timer.h> 30 #include <linux/list.h> 31 #include <linux/interrupt.h> 32 #include <linux/device.h> 33 #include <linux/moduleparam.h> 34 #include <linux/fs.h> 35 #include <linux/poll.h> 36 #include <linux/types.h> 37 #include <linux/ctype.h> 38 #include <linux/cdev.h> 39 40 #include <asm/byteorder.h> 41 #include <linux/io.h> 42 #include <linux/irq.h> 43 #include <linux/uaccess.h> 44 #include <asm/unaligned.h> 45 46 #include <linux/usb/ch9.h> 47 #include <linux/usb/composite.h> 48 #include <linux/usb/gadget.h> 49 #include <linux/usb/g_printer.h> 50 51 #include "u_printer.h" 52 53 #define PRINTER_MINORS 4 54 #define GET_DEVICE_ID 0 55 #define GET_PORT_STATUS 1 56 #define SOFT_RESET 2 57 58 static int major, minors; 59 static struct class *usb_gadget_class; 60 static DEFINE_IDA(printer_ida); 61 static DEFINE_MUTEX(printer_ida_lock); /* protects access do printer_ida */ 62 63 /*-------------------------------------------------------------------------*/ 64 65 struct printer_dev { 66 spinlock_t lock; /* lock this structure */ 67 /* lock buffer lists during read/write calls */ 68 struct mutex lock_printer_io; 69 struct usb_gadget *gadget; 70 s8 interface; 71 struct usb_ep *in_ep, *out_ep; 72 73 struct list_head rx_reqs; /* List of free RX structs */ 74 struct list_head rx_reqs_active; /* List of Active RX xfers */ 75 struct list_head rx_buffers; /* List of completed xfers */ 76 /* wait until there is data to be read. */ 77 wait_queue_head_t rx_wait; 78 struct list_head tx_reqs; /* List of free TX structs */ 79 struct list_head tx_reqs_active; /* List of Active TX xfers */ 80 /* Wait until there are write buffers available to use. */ 81 wait_queue_head_t tx_wait; 82 /* Wait until all write buffers have been sent. */ 83 wait_queue_head_t tx_flush_wait; 84 struct usb_request *current_rx_req; 85 size_t current_rx_bytes; 86 u8 *current_rx_buf; 87 u8 printer_status; 88 u8 reset_printer; 89 int minor; 90 struct cdev printer_cdev; 91 u8 printer_cdev_open; 92 wait_queue_head_t wait; 93 unsigned q_len; 94 char *pnp_string; /* We don't own memory! */ 95 struct usb_function function; 96 }; 97 98 static inline struct printer_dev *func_to_printer(struct usb_function *f) 99 { 100 return container_of(f, struct printer_dev, function); 101 } 102 103 /*-------------------------------------------------------------------------*/ 104 105 /* 106 * DESCRIPTORS ... most are static, but strings and (full) configuration 107 * descriptors are built on demand. 108 */ 109 110 /* holds our biggest descriptor */ 111 #define USB_DESC_BUFSIZE 256 112 #define USB_BUFSIZE 8192 113 114 static struct usb_interface_descriptor intf_desc = { 115 .bLength = sizeof(intf_desc), 116 .bDescriptorType = USB_DT_INTERFACE, 117 .bNumEndpoints = 2, 118 .bInterfaceClass = USB_CLASS_PRINTER, 119 .bInterfaceSubClass = 1, /* Printer Sub-Class */ 120 .bInterfaceProtocol = 2, /* Bi-Directional */ 121 .iInterface = 0 122 }; 123 124 static struct usb_endpoint_descriptor fs_ep_in_desc = { 125 .bLength = USB_DT_ENDPOINT_SIZE, 126 .bDescriptorType = USB_DT_ENDPOINT, 127 .bEndpointAddress = USB_DIR_IN, 128 .bmAttributes = USB_ENDPOINT_XFER_BULK 129 }; 130 131 static struct usb_endpoint_descriptor fs_ep_out_desc = { 132 .bLength = USB_DT_ENDPOINT_SIZE, 133 .bDescriptorType = USB_DT_ENDPOINT, 134 .bEndpointAddress = USB_DIR_OUT, 135 .bmAttributes = USB_ENDPOINT_XFER_BULK 136 }; 137 138 static struct usb_descriptor_header *fs_printer_function[] = { 139 (struct usb_descriptor_header *) &intf_desc, 140 (struct usb_descriptor_header *) &fs_ep_in_desc, 141 (struct usb_descriptor_header *) &fs_ep_out_desc, 142 NULL 143 }; 144 145 /* 146 * usb 2.0 devices need to expose both high speed and full speed 147 * descriptors, unless they only run at full speed. 148 */ 149 150 static struct usb_endpoint_descriptor hs_ep_in_desc = { 151 .bLength = USB_DT_ENDPOINT_SIZE, 152 .bDescriptorType = USB_DT_ENDPOINT, 153 .bmAttributes = USB_ENDPOINT_XFER_BULK, 154 .wMaxPacketSize = cpu_to_le16(512) 155 }; 156 157 static struct usb_endpoint_descriptor hs_ep_out_desc = { 158 .bLength = USB_DT_ENDPOINT_SIZE, 159 .bDescriptorType = USB_DT_ENDPOINT, 160 .bmAttributes = USB_ENDPOINT_XFER_BULK, 161 .wMaxPacketSize = cpu_to_le16(512) 162 }; 163 164 static struct usb_descriptor_header *hs_printer_function[] = { 165 (struct usb_descriptor_header *) &intf_desc, 166 (struct usb_descriptor_header *) &hs_ep_in_desc, 167 (struct usb_descriptor_header *) &hs_ep_out_desc, 168 NULL 169 }; 170 171 /* 172 * Added endpoint descriptors for 3.0 devices 173 */ 174 175 static struct usb_endpoint_descriptor ss_ep_in_desc = { 176 .bLength = USB_DT_ENDPOINT_SIZE, 177 .bDescriptorType = USB_DT_ENDPOINT, 178 .bmAttributes = USB_ENDPOINT_XFER_BULK, 179 .wMaxPacketSize = cpu_to_le16(1024), 180 }; 181 182 static struct usb_ss_ep_comp_descriptor ss_ep_in_comp_desc = { 183 .bLength = sizeof(ss_ep_in_comp_desc), 184 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 185 }; 186 187 static struct usb_endpoint_descriptor ss_ep_out_desc = { 188 .bLength = USB_DT_ENDPOINT_SIZE, 189 .bDescriptorType = USB_DT_ENDPOINT, 190 .bmAttributes = USB_ENDPOINT_XFER_BULK, 191 .wMaxPacketSize = cpu_to_le16(1024), 192 }; 193 194 static struct usb_ss_ep_comp_descriptor ss_ep_out_comp_desc = { 195 .bLength = sizeof(ss_ep_out_comp_desc), 196 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 197 }; 198 199 static struct usb_descriptor_header *ss_printer_function[] = { 200 (struct usb_descriptor_header *) &intf_desc, 201 (struct usb_descriptor_header *) &ss_ep_in_desc, 202 (struct usb_descriptor_header *) &ss_ep_in_comp_desc, 203 (struct usb_descriptor_header *) &ss_ep_out_desc, 204 (struct usb_descriptor_header *) &ss_ep_out_comp_desc, 205 NULL 206 }; 207 208 /* maxpacket and other transfer characteristics vary by speed. */ 209 static inline struct usb_endpoint_descriptor *ep_desc(struct usb_gadget *gadget, 210 struct usb_endpoint_descriptor *fs, 211 struct usb_endpoint_descriptor *hs, 212 struct usb_endpoint_descriptor *ss) 213 { 214 switch (gadget->speed) { 215 case USB_SPEED_SUPER: 216 return ss; 217 case USB_SPEED_HIGH: 218 return hs; 219 default: 220 return fs; 221 } 222 } 223 224 /*-------------------------------------------------------------------------*/ 225 226 static struct usb_request * 227 printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags) 228 { 229 struct usb_request *req; 230 231 req = usb_ep_alloc_request(ep, gfp_flags); 232 233 if (req != NULL) { 234 req->length = len; 235 req->buf = kmalloc(len, gfp_flags); 236 if (req->buf == NULL) { 237 usb_ep_free_request(ep, req); 238 return NULL; 239 } 240 } 241 242 return req; 243 } 244 245 static void 246 printer_req_free(struct usb_ep *ep, struct usb_request *req) 247 { 248 if (ep != NULL && req != NULL) { 249 kfree(req->buf); 250 usb_ep_free_request(ep, req); 251 } 252 } 253 254 /*-------------------------------------------------------------------------*/ 255 256 static void rx_complete(struct usb_ep *ep, struct usb_request *req) 257 { 258 struct printer_dev *dev = ep->driver_data; 259 int status = req->status; 260 unsigned long flags; 261 262 spin_lock_irqsave(&dev->lock, flags); 263 264 list_del_init(&req->list); /* Remode from Active List */ 265 266 switch (status) { 267 268 /* normal completion */ 269 case 0: 270 if (req->actual > 0) { 271 list_add_tail(&req->list, &dev->rx_buffers); 272 DBG(dev, "G_Printer : rx length %d\n", req->actual); 273 } else { 274 list_add(&req->list, &dev->rx_reqs); 275 } 276 break; 277 278 /* software-driven interface shutdown */ 279 case -ECONNRESET: /* unlink */ 280 case -ESHUTDOWN: /* disconnect etc */ 281 VDBG(dev, "rx shutdown, code %d\n", status); 282 list_add(&req->list, &dev->rx_reqs); 283 break; 284 285 /* for hardware automagic (such as pxa) */ 286 case -ECONNABORTED: /* endpoint reset */ 287 DBG(dev, "rx %s reset\n", ep->name); 288 list_add(&req->list, &dev->rx_reqs); 289 break; 290 291 /* data overrun */ 292 case -EOVERFLOW: 293 /* FALLTHROUGH */ 294 295 default: 296 DBG(dev, "rx status %d\n", status); 297 list_add(&req->list, &dev->rx_reqs); 298 break; 299 } 300 301 wake_up_interruptible(&dev->rx_wait); 302 spin_unlock_irqrestore(&dev->lock, flags); 303 } 304 305 static void tx_complete(struct usb_ep *ep, struct usb_request *req) 306 { 307 struct printer_dev *dev = ep->driver_data; 308 309 switch (req->status) { 310 default: 311 VDBG(dev, "tx err %d\n", req->status); 312 /* FALLTHROUGH */ 313 case -ECONNRESET: /* unlink */ 314 case -ESHUTDOWN: /* disconnect etc */ 315 break; 316 case 0: 317 break; 318 } 319 320 spin_lock(&dev->lock); 321 /* Take the request struct off the active list and put it on the 322 * free list. 323 */ 324 list_del_init(&req->list); 325 list_add(&req->list, &dev->tx_reqs); 326 wake_up_interruptible(&dev->tx_wait); 327 if (likely(list_empty(&dev->tx_reqs_active))) 328 wake_up_interruptible(&dev->tx_flush_wait); 329 330 spin_unlock(&dev->lock); 331 } 332 333 /*-------------------------------------------------------------------------*/ 334 335 static int 336 printer_open(struct inode *inode, struct file *fd) 337 { 338 struct printer_dev *dev; 339 unsigned long flags; 340 int ret = -EBUSY; 341 342 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev); 343 344 spin_lock_irqsave(&dev->lock, flags); 345 346 if (!dev->printer_cdev_open) { 347 dev->printer_cdev_open = 1; 348 fd->private_data = dev; 349 ret = 0; 350 /* Change the printer status to show that it's on-line. */ 351 dev->printer_status |= PRINTER_SELECTED; 352 } 353 354 spin_unlock_irqrestore(&dev->lock, flags); 355 356 DBG(dev, "printer_open returned %x\n", ret); 357 return ret; 358 } 359 360 static int 361 printer_close(struct inode *inode, struct file *fd) 362 { 363 struct printer_dev *dev = fd->private_data; 364 unsigned long flags; 365 366 spin_lock_irqsave(&dev->lock, flags); 367 dev->printer_cdev_open = 0; 368 fd->private_data = NULL; 369 /* Change printer status to show that the printer is off-line. */ 370 dev->printer_status &= ~PRINTER_SELECTED; 371 spin_unlock_irqrestore(&dev->lock, flags); 372 373 DBG(dev, "printer_close\n"); 374 375 return 0; 376 } 377 378 /* This function must be called with interrupts turned off. */ 379 static void 380 setup_rx_reqs(struct printer_dev *dev) 381 { 382 struct usb_request *req; 383 384 while (likely(!list_empty(&dev->rx_reqs))) { 385 int error; 386 387 req = container_of(dev->rx_reqs.next, 388 struct usb_request, list); 389 list_del_init(&req->list); 390 391 /* The USB Host sends us whatever amount of data it wants to 392 * so we always set the length field to the full USB_BUFSIZE. 393 * If the amount of data is more than the read() caller asked 394 * for it will be stored in the request buffer until it is 395 * asked for by read(). 396 */ 397 req->length = USB_BUFSIZE; 398 req->complete = rx_complete; 399 400 /* here, we unlock, and only unlock, to avoid deadlock. */ 401 spin_unlock(&dev->lock); 402 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC); 403 spin_lock(&dev->lock); 404 if (error) { 405 DBG(dev, "rx submit --> %d\n", error); 406 list_add(&req->list, &dev->rx_reqs); 407 break; 408 } 409 /* if the req is empty, then add it into dev->rx_reqs_active. */ 410 else if (list_empty(&req->list)) 411 list_add(&req->list, &dev->rx_reqs_active); 412 } 413 } 414 415 static ssize_t 416 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr) 417 { 418 struct printer_dev *dev = fd->private_data; 419 unsigned long flags; 420 size_t size; 421 size_t bytes_copied; 422 struct usb_request *req; 423 /* This is a pointer to the current USB rx request. */ 424 struct usb_request *current_rx_req; 425 /* This is the number of bytes in the current rx buffer. */ 426 size_t current_rx_bytes; 427 /* This is a pointer to the current rx buffer. */ 428 u8 *current_rx_buf; 429 430 if (len == 0) 431 return -EINVAL; 432 433 DBG(dev, "printer_read trying to read %d bytes\n", (int)len); 434 435 mutex_lock(&dev->lock_printer_io); 436 spin_lock_irqsave(&dev->lock, flags); 437 438 /* We will use this flag later to check if a printer reset happened 439 * after we turn interrupts back on. 440 */ 441 dev->reset_printer = 0; 442 443 setup_rx_reqs(dev); 444 445 bytes_copied = 0; 446 current_rx_req = dev->current_rx_req; 447 current_rx_bytes = dev->current_rx_bytes; 448 current_rx_buf = dev->current_rx_buf; 449 dev->current_rx_req = NULL; 450 dev->current_rx_bytes = 0; 451 dev->current_rx_buf = NULL; 452 453 /* Check if there is any data in the read buffers. Please note that 454 * current_rx_bytes is the number of bytes in the current rx buffer. 455 * If it is zero then check if there are any other rx_buffers that 456 * are on the completed list. We are only out of data if all rx 457 * buffers are empty. 458 */ 459 if ((current_rx_bytes == 0) && 460 (likely(list_empty(&dev->rx_buffers)))) { 461 /* Turn interrupts back on before sleeping. */ 462 spin_unlock_irqrestore(&dev->lock, flags); 463 464 /* 465 * If no data is available check if this is a NON-Blocking 466 * call or not. 467 */ 468 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) { 469 mutex_unlock(&dev->lock_printer_io); 470 return -EAGAIN; 471 } 472 473 /* Sleep until data is available */ 474 wait_event_interruptible(dev->rx_wait, 475 (likely(!list_empty(&dev->rx_buffers)))); 476 spin_lock_irqsave(&dev->lock, flags); 477 } 478 479 /* We have data to return then copy it to the caller's buffer.*/ 480 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers))) 481 && len) { 482 if (current_rx_bytes == 0) { 483 req = container_of(dev->rx_buffers.next, 484 struct usb_request, list); 485 list_del_init(&req->list); 486 487 if (req->actual && req->buf) { 488 current_rx_req = req; 489 current_rx_bytes = req->actual; 490 current_rx_buf = req->buf; 491 } else { 492 list_add(&req->list, &dev->rx_reqs); 493 continue; 494 } 495 } 496 497 /* Don't leave irqs off while doing memory copies */ 498 spin_unlock_irqrestore(&dev->lock, flags); 499 500 if (len > current_rx_bytes) 501 size = current_rx_bytes; 502 else 503 size = len; 504 505 size -= copy_to_user(buf, current_rx_buf, size); 506 bytes_copied += size; 507 len -= size; 508 buf += size; 509 510 spin_lock_irqsave(&dev->lock, flags); 511 512 /* We've disconnected or reset so return. */ 513 if (dev->reset_printer) { 514 list_add(¤t_rx_req->list, &dev->rx_reqs); 515 spin_unlock_irqrestore(&dev->lock, flags); 516 mutex_unlock(&dev->lock_printer_io); 517 return -EAGAIN; 518 } 519 520 /* If we not returning all the data left in this RX request 521 * buffer then adjust the amount of data left in the buffer. 522 * Othewise if we are done with this RX request buffer then 523 * requeue it to get any incoming data from the USB host. 524 */ 525 if (size < current_rx_bytes) { 526 current_rx_bytes -= size; 527 current_rx_buf += size; 528 } else { 529 list_add(¤t_rx_req->list, &dev->rx_reqs); 530 current_rx_bytes = 0; 531 current_rx_buf = NULL; 532 current_rx_req = NULL; 533 } 534 } 535 536 dev->current_rx_req = current_rx_req; 537 dev->current_rx_bytes = current_rx_bytes; 538 dev->current_rx_buf = current_rx_buf; 539 540 spin_unlock_irqrestore(&dev->lock, flags); 541 mutex_unlock(&dev->lock_printer_io); 542 543 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied); 544 545 if (bytes_copied) 546 return bytes_copied; 547 else 548 return -EAGAIN; 549 } 550 551 static ssize_t 552 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr) 553 { 554 struct printer_dev *dev = fd->private_data; 555 unsigned long flags; 556 size_t size; /* Amount of data in a TX request. */ 557 size_t bytes_copied = 0; 558 struct usb_request *req; 559 int value; 560 561 DBG(dev, "printer_write trying to send %d bytes\n", (int)len); 562 563 if (len == 0) 564 return -EINVAL; 565 566 mutex_lock(&dev->lock_printer_io); 567 spin_lock_irqsave(&dev->lock, flags); 568 569 /* Check if a printer reset happens while we have interrupts on */ 570 dev->reset_printer = 0; 571 572 /* Check if there is any available write buffers */ 573 if (likely(list_empty(&dev->tx_reqs))) { 574 /* Turn interrupts back on before sleeping. */ 575 spin_unlock_irqrestore(&dev->lock, flags); 576 577 /* 578 * If write buffers are available check if this is 579 * a NON-Blocking call or not. 580 */ 581 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) { 582 mutex_unlock(&dev->lock_printer_io); 583 return -EAGAIN; 584 } 585 586 /* Sleep until a write buffer is available */ 587 wait_event_interruptible(dev->tx_wait, 588 (likely(!list_empty(&dev->tx_reqs)))); 589 spin_lock_irqsave(&dev->lock, flags); 590 } 591 592 while (likely(!list_empty(&dev->tx_reqs)) && len) { 593 594 if (len > USB_BUFSIZE) 595 size = USB_BUFSIZE; 596 else 597 size = len; 598 599 req = container_of(dev->tx_reqs.next, struct usb_request, 600 list); 601 list_del_init(&req->list); 602 603 req->complete = tx_complete; 604 req->length = size; 605 606 /* Check if we need to send a zero length packet. */ 607 if (len > size) 608 /* They will be more TX requests so no yet. */ 609 req->zero = 0; 610 else 611 /* If the data amount is not a multiple of the 612 * maxpacket size then send a zero length packet. 613 */ 614 req->zero = ((len % dev->in_ep->maxpacket) == 0); 615 616 /* Don't leave irqs off while doing memory copies */ 617 spin_unlock_irqrestore(&dev->lock, flags); 618 619 if (copy_from_user(req->buf, buf, size)) { 620 list_add(&req->list, &dev->tx_reqs); 621 mutex_unlock(&dev->lock_printer_io); 622 return bytes_copied; 623 } 624 625 bytes_copied += size; 626 len -= size; 627 buf += size; 628 629 spin_lock_irqsave(&dev->lock, flags); 630 631 /* We've disconnected or reset so free the req and buffer */ 632 if (dev->reset_printer) { 633 list_add(&req->list, &dev->tx_reqs); 634 spin_unlock_irqrestore(&dev->lock, flags); 635 mutex_unlock(&dev->lock_printer_io); 636 return -EAGAIN; 637 } 638 639 /* here, we unlock, and only unlock, to avoid deadlock. */ 640 spin_unlock(&dev->lock); 641 value = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC); 642 spin_lock(&dev->lock); 643 if (value) { 644 list_add(&req->list, &dev->tx_reqs); 645 spin_unlock_irqrestore(&dev->lock, flags); 646 mutex_unlock(&dev->lock_printer_io); 647 return -EAGAIN; 648 } 649 650 list_add(&req->list, &dev->tx_reqs_active); 651 652 } 653 654 spin_unlock_irqrestore(&dev->lock, flags); 655 mutex_unlock(&dev->lock_printer_io); 656 657 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied); 658 659 if (bytes_copied) 660 return bytes_copied; 661 else 662 return -EAGAIN; 663 } 664 665 static int 666 printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync) 667 { 668 struct printer_dev *dev = fd->private_data; 669 struct inode *inode = file_inode(fd); 670 unsigned long flags; 671 int tx_list_empty; 672 673 inode_lock(inode); 674 spin_lock_irqsave(&dev->lock, flags); 675 tx_list_empty = (likely(list_empty(&dev->tx_reqs))); 676 spin_unlock_irqrestore(&dev->lock, flags); 677 678 if (!tx_list_empty) { 679 /* Sleep until all data has been sent */ 680 wait_event_interruptible(dev->tx_flush_wait, 681 (likely(list_empty(&dev->tx_reqs_active)))); 682 } 683 inode_unlock(inode); 684 685 return 0; 686 } 687 688 static unsigned int 689 printer_poll(struct file *fd, poll_table *wait) 690 { 691 struct printer_dev *dev = fd->private_data; 692 unsigned long flags; 693 int status = 0; 694 695 mutex_lock(&dev->lock_printer_io); 696 spin_lock_irqsave(&dev->lock, flags); 697 setup_rx_reqs(dev); 698 spin_unlock_irqrestore(&dev->lock, flags); 699 mutex_unlock(&dev->lock_printer_io); 700 701 poll_wait(fd, &dev->rx_wait, wait); 702 poll_wait(fd, &dev->tx_wait, wait); 703 704 spin_lock_irqsave(&dev->lock, flags); 705 if (likely(!list_empty(&dev->tx_reqs))) 706 status |= POLLOUT | POLLWRNORM; 707 708 if (likely(dev->current_rx_bytes) || 709 likely(!list_empty(&dev->rx_buffers))) 710 status |= POLLIN | POLLRDNORM; 711 712 spin_unlock_irqrestore(&dev->lock, flags); 713 714 return status; 715 } 716 717 static long 718 printer_ioctl(struct file *fd, unsigned int code, unsigned long arg) 719 { 720 struct printer_dev *dev = fd->private_data; 721 unsigned long flags; 722 int status = 0; 723 724 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg); 725 726 /* handle ioctls */ 727 728 spin_lock_irqsave(&dev->lock, flags); 729 730 switch (code) { 731 case GADGET_GET_PRINTER_STATUS: 732 status = (int)dev->printer_status; 733 break; 734 case GADGET_SET_PRINTER_STATUS: 735 dev->printer_status = (u8)arg; 736 break; 737 default: 738 /* could not handle ioctl */ 739 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n", 740 code); 741 status = -ENOTTY; 742 } 743 744 spin_unlock_irqrestore(&dev->lock, flags); 745 746 return status; 747 } 748 749 /* used after endpoint configuration */ 750 static const struct file_operations printer_io_operations = { 751 .owner = THIS_MODULE, 752 .open = printer_open, 753 .read = printer_read, 754 .write = printer_write, 755 .fsync = printer_fsync, 756 .poll = printer_poll, 757 .unlocked_ioctl = printer_ioctl, 758 .release = printer_close, 759 .llseek = noop_llseek, 760 }; 761 762 /*-------------------------------------------------------------------------*/ 763 764 static int 765 set_printer_interface(struct printer_dev *dev) 766 { 767 int result = 0; 768 769 dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc, 770 &ss_ep_in_desc); 771 dev->in_ep->driver_data = dev; 772 773 dev->out_ep->desc = ep_desc(dev->gadget, &fs_ep_out_desc, 774 &hs_ep_out_desc, &ss_ep_out_desc); 775 dev->out_ep->driver_data = dev; 776 777 result = usb_ep_enable(dev->in_ep); 778 if (result != 0) { 779 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result); 780 goto done; 781 } 782 783 result = usb_ep_enable(dev->out_ep); 784 if (result != 0) { 785 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result); 786 goto done; 787 } 788 789 done: 790 /* on error, disable any endpoints */ 791 if (result != 0) { 792 (void) usb_ep_disable(dev->in_ep); 793 (void) usb_ep_disable(dev->out_ep); 794 dev->in_ep->desc = NULL; 795 dev->out_ep->desc = NULL; 796 } 797 798 /* caller is responsible for cleanup on error */ 799 return result; 800 } 801 802 static void printer_reset_interface(struct printer_dev *dev) 803 { 804 unsigned long flags; 805 806 if (dev->interface < 0) 807 return; 808 809 DBG(dev, "%s\n", __func__); 810 811 if (dev->in_ep->desc) 812 usb_ep_disable(dev->in_ep); 813 814 if (dev->out_ep->desc) 815 usb_ep_disable(dev->out_ep); 816 817 spin_lock_irqsave(&dev->lock, flags); 818 dev->in_ep->desc = NULL; 819 dev->out_ep->desc = NULL; 820 dev->interface = -1; 821 spin_unlock_irqrestore(&dev->lock, flags); 822 } 823 824 /* Change our operational Interface. */ 825 static int set_interface(struct printer_dev *dev, unsigned number) 826 { 827 int result = 0; 828 829 /* Free the current interface */ 830 printer_reset_interface(dev); 831 832 result = set_printer_interface(dev); 833 if (result) 834 printer_reset_interface(dev); 835 else 836 dev->interface = number; 837 838 if (!result) 839 INFO(dev, "Using interface %x\n", number); 840 841 return result; 842 } 843 844 static void printer_soft_reset(struct printer_dev *dev) 845 { 846 struct usb_request *req; 847 848 INFO(dev, "Received Printer Reset Request\n"); 849 850 if (usb_ep_disable(dev->in_ep)) 851 DBG(dev, "Failed to disable USB in_ep\n"); 852 if (usb_ep_disable(dev->out_ep)) 853 DBG(dev, "Failed to disable USB out_ep\n"); 854 855 if (dev->current_rx_req != NULL) { 856 list_add(&dev->current_rx_req->list, &dev->rx_reqs); 857 dev->current_rx_req = NULL; 858 } 859 dev->current_rx_bytes = 0; 860 dev->current_rx_buf = NULL; 861 dev->reset_printer = 1; 862 863 while (likely(!(list_empty(&dev->rx_buffers)))) { 864 req = container_of(dev->rx_buffers.next, struct usb_request, 865 list); 866 list_del_init(&req->list); 867 list_add(&req->list, &dev->rx_reqs); 868 } 869 870 while (likely(!(list_empty(&dev->rx_reqs_active)))) { 871 req = container_of(dev->rx_buffers.next, struct usb_request, 872 list); 873 list_del_init(&req->list); 874 list_add(&req->list, &dev->rx_reqs); 875 } 876 877 while (likely(!(list_empty(&dev->tx_reqs_active)))) { 878 req = container_of(dev->tx_reqs_active.next, 879 struct usb_request, list); 880 list_del_init(&req->list); 881 list_add(&req->list, &dev->tx_reqs); 882 } 883 884 if (usb_ep_enable(dev->in_ep)) 885 DBG(dev, "Failed to enable USB in_ep\n"); 886 if (usb_ep_enable(dev->out_ep)) 887 DBG(dev, "Failed to enable USB out_ep\n"); 888 889 wake_up_interruptible(&dev->rx_wait); 890 wake_up_interruptible(&dev->tx_wait); 891 wake_up_interruptible(&dev->tx_flush_wait); 892 } 893 894 /*-------------------------------------------------------------------------*/ 895 896 static bool gprinter_req_match(struct usb_function *f, 897 const struct usb_ctrlrequest *ctrl, 898 bool config0) 899 { 900 struct printer_dev *dev = func_to_printer(f); 901 u16 w_index = le16_to_cpu(ctrl->wIndex); 902 u16 w_value = le16_to_cpu(ctrl->wValue); 903 u16 w_length = le16_to_cpu(ctrl->wLength); 904 905 if (config0) 906 return false; 907 908 if ((ctrl->bRequestType & USB_RECIP_MASK) != USB_RECIP_INTERFACE || 909 (ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) 910 return false; 911 912 switch (ctrl->bRequest) { 913 case GET_DEVICE_ID: 914 w_index >>= 8; 915 if (USB_DIR_IN & ctrl->bRequestType) 916 break; 917 return false; 918 case GET_PORT_STATUS: 919 if (!w_value && w_length == 1 && 920 (USB_DIR_IN & ctrl->bRequestType)) 921 break; 922 return false; 923 case SOFT_RESET: 924 if (!w_value && !w_length && 925 !(USB_DIR_IN & ctrl->bRequestType)) 926 break; 927 /* fall through */ 928 default: 929 return false; 930 } 931 return w_index == dev->interface; 932 } 933 934 /* 935 * The setup() callback implements all the ep0 functionality that's not 936 * handled lower down. 937 */ 938 static int printer_func_setup(struct usb_function *f, 939 const struct usb_ctrlrequest *ctrl) 940 { 941 struct printer_dev *dev = func_to_printer(f); 942 struct usb_composite_dev *cdev = f->config->cdev; 943 struct usb_request *req = cdev->req; 944 u8 *buf = req->buf; 945 int value = -EOPNOTSUPP; 946 u16 wIndex = le16_to_cpu(ctrl->wIndex); 947 u16 wValue = le16_to_cpu(ctrl->wValue); 948 u16 wLength = le16_to_cpu(ctrl->wLength); 949 950 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n", 951 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength); 952 953 switch (ctrl->bRequestType&USB_TYPE_MASK) { 954 case USB_TYPE_CLASS: 955 switch (ctrl->bRequest) { 956 case GET_DEVICE_ID: /* Get the IEEE-1284 PNP String */ 957 /* Only one printer interface is supported. */ 958 if ((wIndex>>8) != dev->interface) 959 break; 960 961 if (!dev->pnp_string) { 962 value = 0; 963 break; 964 } 965 value = strlen(dev->pnp_string); 966 buf[0] = (value >> 8) & 0xFF; 967 buf[1] = value & 0xFF; 968 memcpy(buf + 2, dev->pnp_string, value); 969 DBG(dev, "1284 PNP String: %x %s\n", value, 970 dev->pnp_string); 971 break; 972 973 case GET_PORT_STATUS: /* Get Port Status */ 974 /* Only one printer interface is supported. */ 975 if (wIndex != dev->interface) 976 break; 977 978 buf[0] = dev->printer_status; 979 value = min_t(u16, wLength, 1); 980 break; 981 982 case SOFT_RESET: /* Soft Reset */ 983 /* Only one printer interface is supported. */ 984 if (wIndex != dev->interface) 985 break; 986 987 printer_soft_reset(dev); 988 989 value = 0; 990 break; 991 992 default: 993 goto unknown; 994 } 995 break; 996 997 default: 998 unknown: 999 VDBG(dev, 1000 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n", 1001 ctrl->bRequestType, ctrl->bRequest, 1002 wValue, wIndex, wLength); 1003 break; 1004 } 1005 /* host either stalls (value < 0) or reports success */ 1006 if (value >= 0) { 1007 req->length = value; 1008 req->zero = value < wLength; 1009 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 1010 if (value < 0) { 1011 ERROR(dev, "%s:%d Error!\n", __func__, __LINE__); 1012 req->status = 0; 1013 } 1014 } 1015 return value; 1016 } 1017 1018 static int printer_func_bind(struct usb_configuration *c, 1019 struct usb_function *f) 1020 { 1021 struct usb_gadget *gadget = c->cdev->gadget; 1022 struct printer_dev *dev = func_to_printer(f); 1023 struct device *pdev; 1024 struct usb_composite_dev *cdev = c->cdev; 1025 struct usb_ep *in_ep; 1026 struct usb_ep *out_ep = NULL; 1027 struct usb_request *req; 1028 dev_t devt; 1029 int id; 1030 int ret; 1031 u32 i; 1032 1033 id = usb_interface_id(c, f); 1034 if (id < 0) 1035 return id; 1036 intf_desc.bInterfaceNumber = id; 1037 1038 /* finish hookup to lower layer ... */ 1039 dev->gadget = gadget; 1040 1041 /* all we really need is bulk IN/OUT */ 1042 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc); 1043 if (!in_ep) { 1044 autoconf_fail: 1045 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n", 1046 cdev->gadget->name); 1047 return -ENODEV; 1048 } 1049 1050 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc); 1051 if (!out_ep) 1052 goto autoconf_fail; 1053 1054 /* assumes that all endpoints are dual-speed */ 1055 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress; 1056 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress; 1057 ss_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress; 1058 ss_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress; 1059 1060 ret = usb_assign_descriptors(f, fs_printer_function, 1061 hs_printer_function, ss_printer_function, NULL); 1062 if (ret) 1063 return ret; 1064 1065 dev->in_ep = in_ep; 1066 dev->out_ep = out_ep; 1067 1068 ret = -ENOMEM; 1069 for (i = 0; i < dev->q_len; i++) { 1070 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL); 1071 if (!req) 1072 goto fail_tx_reqs; 1073 list_add(&req->list, &dev->tx_reqs); 1074 } 1075 1076 for (i = 0; i < dev->q_len; i++) { 1077 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL); 1078 if (!req) 1079 goto fail_rx_reqs; 1080 list_add(&req->list, &dev->rx_reqs); 1081 } 1082 1083 /* Setup the sysfs files for the printer gadget. */ 1084 devt = MKDEV(major, dev->minor); 1085 pdev = device_create(usb_gadget_class, NULL, devt, 1086 NULL, "g_printer%d", dev->minor); 1087 if (IS_ERR(pdev)) { 1088 ERROR(dev, "Failed to create device: g_printer\n"); 1089 ret = PTR_ERR(pdev); 1090 goto fail_rx_reqs; 1091 } 1092 1093 /* 1094 * Register a character device as an interface to a user mode 1095 * program that handles the printer specific functionality. 1096 */ 1097 cdev_init(&dev->printer_cdev, &printer_io_operations); 1098 dev->printer_cdev.owner = THIS_MODULE; 1099 ret = cdev_add(&dev->printer_cdev, devt, 1); 1100 if (ret) { 1101 ERROR(dev, "Failed to open char device\n"); 1102 goto fail_cdev_add; 1103 } 1104 1105 return 0; 1106 1107 fail_cdev_add: 1108 device_destroy(usb_gadget_class, devt); 1109 1110 fail_rx_reqs: 1111 while (!list_empty(&dev->rx_reqs)) { 1112 req = container_of(dev->rx_reqs.next, struct usb_request, list); 1113 list_del(&req->list); 1114 printer_req_free(dev->out_ep, req); 1115 } 1116 1117 fail_tx_reqs: 1118 while (!list_empty(&dev->tx_reqs)) { 1119 req = container_of(dev->tx_reqs.next, struct usb_request, list); 1120 list_del(&req->list); 1121 printer_req_free(dev->in_ep, req); 1122 } 1123 1124 return ret; 1125 1126 } 1127 1128 static int printer_func_set_alt(struct usb_function *f, 1129 unsigned intf, unsigned alt) 1130 { 1131 struct printer_dev *dev = func_to_printer(f); 1132 int ret = -ENOTSUPP; 1133 1134 if (!alt) 1135 ret = set_interface(dev, intf); 1136 1137 return ret; 1138 } 1139 1140 static void printer_func_disable(struct usb_function *f) 1141 { 1142 struct printer_dev *dev = func_to_printer(f); 1143 1144 DBG(dev, "%s\n", __func__); 1145 1146 printer_reset_interface(dev); 1147 } 1148 1149 static inline struct f_printer_opts 1150 *to_f_printer_opts(struct config_item *item) 1151 { 1152 return container_of(to_config_group(item), struct f_printer_opts, 1153 func_inst.group); 1154 } 1155 1156 static void printer_attr_release(struct config_item *item) 1157 { 1158 struct f_printer_opts *opts = to_f_printer_opts(item); 1159 1160 usb_put_function_instance(&opts->func_inst); 1161 } 1162 1163 static struct configfs_item_operations printer_item_ops = { 1164 .release = printer_attr_release, 1165 }; 1166 1167 static ssize_t f_printer_opts_pnp_string_show(struct config_item *item, 1168 char *page) 1169 { 1170 struct f_printer_opts *opts = to_f_printer_opts(item); 1171 int result = 0; 1172 1173 mutex_lock(&opts->lock); 1174 if (!opts->pnp_string) 1175 goto unlock; 1176 1177 result = strlcpy(page, opts->pnp_string, PAGE_SIZE); 1178 if (result >= PAGE_SIZE) { 1179 result = PAGE_SIZE; 1180 } else if (page[result - 1] != '\n' && result + 1 < PAGE_SIZE) { 1181 page[result++] = '\n'; 1182 page[result] = '\0'; 1183 } 1184 1185 unlock: 1186 mutex_unlock(&opts->lock); 1187 1188 return result; 1189 } 1190 1191 static ssize_t f_printer_opts_pnp_string_store(struct config_item *item, 1192 const char *page, size_t len) 1193 { 1194 struct f_printer_opts *opts = to_f_printer_opts(item); 1195 char *new_pnp; 1196 int result; 1197 1198 mutex_lock(&opts->lock); 1199 1200 new_pnp = kstrndup(page, len, GFP_KERNEL); 1201 if (!new_pnp) { 1202 result = -ENOMEM; 1203 goto unlock; 1204 } 1205 1206 if (opts->pnp_string_allocated) 1207 kfree(opts->pnp_string); 1208 1209 opts->pnp_string_allocated = true; 1210 opts->pnp_string = new_pnp; 1211 result = len; 1212 unlock: 1213 mutex_unlock(&opts->lock); 1214 1215 return result; 1216 } 1217 1218 CONFIGFS_ATTR(f_printer_opts_, pnp_string); 1219 1220 static ssize_t f_printer_opts_q_len_show(struct config_item *item, 1221 char *page) 1222 { 1223 struct f_printer_opts *opts = to_f_printer_opts(item); 1224 int result; 1225 1226 mutex_lock(&opts->lock); 1227 result = sprintf(page, "%d\n", opts->q_len); 1228 mutex_unlock(&opts->lock); 1229 1230 return result; 1231 } 1232 1233 static ssize_t f_printer_opts_q_len_store(struct config_item *item, 1234 const char *page, size_t len) 1235 { 1236 struct f_printer_opts *opts = to_f_printer_opts(item); 1237 int ret; 1238 u16 num; 1239 1240 mutex_lock(&opts->lock); 1241 if (opts->refcnt) { 1242 ret = -EBUSY; 1243 goto end; 1244 } 1245 1246 ret = kstrtou16(page, 0, &num); 1247 if (ret) 1248 goto end; 1249 1250 opts->q_len = (unsigned)num; 1251 ret = len; 1252 end: 1253 mutex_unlock(&opts->lock); 1254 return ret; 1255 } 1256 1257 CONFIGFS_ATTR(f_printer_opts_, q_len); 1258 1259 static struct configfs_attribute *printer_attrs[] = { 1260 &f_printer_opts_attr_pnp_string, 1261 &f_printer_opts_attr_q_len, 1262 NULL, 1263 }; 1264 1265 static struct config_item_type printer_func_type = { 1266 .ct_item_ops = &printer_item_ops, 1267 .ct_attrs = printer_attrs, 1268 .ct_owner = THIS_MODULE, 1269 }; 1270 1271 static inline int gprinter_get_minor(void) 1272 { 1273 int ret; 1274 1275 ret = ida_simple_get(&printer_ida, 0, 0, GFP_KERNEL); 1276 if (ret >= PRINTER_MINORS) { 1277 ida_simple_remove(&printer_ida, ret); 1278 ret = -ENODEV; 1279 } 1280 1281 return ret; 1282 } 1283 1284 static inline void gprinter_put_minor(int minor) 1285 { 1286 ida_simple_remove(&printer_ida, minor); 1287 } 1288 1289 static int gprinter_setup(int); 1290 static void gprinter_cleanup(void); 1291 1292 static void gprinter_free_inst(struct usb_function_instance *f) 1293 { 1294 struct f_printer_opts *opts; 1295 1296 opts = container_of(f, struct f_printer_opts, func_inst); 1297 1298 mutex_lock(&printer_ida_lock); 1299 1300 gprinter_put_minor(opts->minor); 1301 if (ida_is_empty(&printer_ida)) 1302 gprinter_cleanup(); 1303 1304 mutex_unlock(&printer_ida_lock); 1305 1306 if (opts->pnp_string_allocated) 1307 kfree(opts->pnp_string); 1308 kfree(opts); 1309 } 1310 1311 static struct usb_function_instance *gprinter_alloc_inst(void) 1312 { 1313 struct f_printer_opts *opts; 1314 struct usb_function_instance *ret; 1315 int status = 0; 1316 1317 opts = kzalloc(sizeof(*opts), GFP_KERNEL); 1318 if (!opts) 1319 return ERR_PTR(-ENOMEM); 1320 1321 mutex_init(&opts->lock); 1322 opts->func_inst.free_func_inst = gprinter_free_inst; 1323 ret = &opts->func_inst; 1324 1325 mutex_lock(&printer_ida_lock); 1326 1327 if (ida_is_empty(&printer_ida)) { 1328 status = gprinter_setup(PRINTER_MINORS); 1329 if (status) { 1330 ret = ERR_PTR(status); 1331 kfree(opts); 1332 goto unlock; 1333 } 1334 } 1335 1336 opts->minor = gprinter_get_minor(); 1337 if (opts->minor < 0) { 1338 ret = ERR_PTR(opts->minor); 1339 kfree(opts); 1340 if (ida_is_empty(&printer_ida)) 1341 gprinter_cleanup(); 1342 goto unlock; 1343 } 1344 config_group_init_type_name(&opts->func_inst.group, "", 1345 &printer_func_type); 1346 1347 unlock: 1348 mutex_unlock(&printer_ida_lock); 1349 return ret; 1350 } 1351 1352 static void gprinter_free(struct usb_function *f) 1353 { 1354 struct printer_dev *dev = func_to_printer(f); 1355 struct f_printer_opts *opts; 1356 1357 opts = container_of(f->fi, struct f_printer_opts, func_inst); 1358 kfree(dev); 1359 mutex_lock(&opts->lock); 1360 --opts->refcnt; 1361 mutex_unlock(&opts->lock); 1362 } 1363 1364 static void printer_func_unbind(struct usb_configuration *c, 1365 struct usb_function *f) 1366 { 1367 struct printer_dev *dev; 1368 struct usb_request *req; 1369 1370 dev = func_to_printer(f); 1371 1372 device_destroy(usb_gadget_class, MKDEV(major, dev->minor)); 1373 1374 /* Remove Character Device */ 1375 cdev_del(&dev->printer_cdev); 1376 1377 /* we must already have been disconnected ... no i/o may be active */ 1378 WARN_ON(!list_empty(&dev->tx_reqs_active)); 1379 WARN_ON(!list_empty(&dev->rx_reqs_active)); 1380 1381 /* Free all memory for this driver. */ 1382 while (!list_empty(&dev->tx_reqs)) { 1383 req = container_of(dev->tx_reqs.next, struct usb_request, 1384 list); 1385 list_del(&req->list); 1386 printer_req_free(dev->in_ep, req); 1387 } 1388 1389 if (dev->current_rx_req != NULL) 1390 printer_req_free(dev->out_ep, dev->current_rx_req); 1391 1392 while (!list_empty(&dev->rx_reqs)) { 1393 req = container_of(dev->rx_reqs.next, 1394 struct usb_request, list); 1395 list_del(&req->list); 1396 printer_req_free(dev->out_ep, req); 1397 } 1398 1399 while (!list_empty(&dev->rx_buffers)) { 1400 req = container_of(dev->rx_buffers.next, 1401 struct usb_request, list); 1402 list_del(&req->list); 1403 printer_req_free(dev->out_ep, req); 1404 } 1405 usb_free_all_descriptors(f); 1406 } 1407 1408 static struct usb_function *gprinter_alloc(struct usb_function_instance *fi) 1409 { 1410 struct printer_dev *dev; 1411 struct f_printer_opts *opts; 1412 1413 opts = container_of(fi, struct f_printer_opts, func_inst); 1414 1415 mutex_lock(&opts->lock); 1416 if (opts->minor >= minors) { 1417 mutex_unlock(&opts->lock); 1418 return ERR_PTR(-ENOENT); 1419 } 1420 1421 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1422 if (!dev) { 1423 mutex_unlock(&opts->lock); 1424 return ERR_PTR(-ENOMEM); 1425 } 1426 1427 ++opts->refcnt; 1428 dev->minor = opts->minor; 1429 dev->pnp_string = opts->pnp_string; 1430 dev->q_len = opts->q_len; 1431 mutex_unlock(&opts->lock); 1432 1433 dev->function.name = "printer"; 1434 dev->function.bind = printer_func_bind; 1435 dev->function.setup = printer_func_setup; 1436 dev->function.unbind = printer_func_unbind; 1437 dev->function.set_alt = printer_func_set_alt; 1438 dev->function.disable = printer_func_disable; 1439 dev->function.req_match = gprinter_req_match; 1440 dev->function.free_func = gprinter_free; 1441 1442 INIT_LIST_HEAD(&dev->tx_reqs); 1443 INIT_LIST_HEAD(&dev->rx_reqs); 1444 INIT_LIST_HEAD(&dev->rx_buffers); 1445 INIT_LIST_HEAD(&dev->tx_reqs_active); 1446 INIT_LIST_HEAD(&dev->rx_reqs_active); 1447 1448 spin_lock_init(&dev->lock); 1449 mutex_init(&dev->lock_printer_io); 1450 init_waitqueue_head(&dev->rx_wait); 1451 init_waitqueue_head(&dev->tx_wait); 1452 init_waitqueue_head(&dev->tx_flush_wait); 1453 1454 dev->interface = -1; 1455 dev->printer_cdev_open = 0; 1456 dev->printer_status = PRINTER_NOT_ERROR; 1457 dev->current_rx_req = NULL; 1458 dev->current_rx_bytes = 0; 1459 dev->current_rx_buf = NULL; 1460 1461 return &dev->function; 1462 } 1463 1464 DECLARE_USB_FUNCTION_INIT(printer, gprinter_alloc_inst, gprinter_alloc); 1465 MODULE_LICENSE("GPL"); 1466 MODULE_AUTHOR("Craig Nadler"); 1467 1468 static int gprinter_setup(int count) 1469 { 1470 int status; 1471 dev_t devt; 1472 1473 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget"); 1474 if (IS_ERR(usb_gadget_class)) { 1475 status = PTR_ERR(usb_gadget_class); 1476 usb_gadget_class = NULL; 1477 pr_err("unable to create usb_gadget class %d\n", status); 1478 return status; 1479 } 1480 1481 status = alloc_chrdev_region(&devt, 0, count, "USB printer gadget"); 1482 if (status) { 1483 pr_err("alloc_chrdev_region %d\n", status); 1484 class_destroy(usb_gadget_class); 1485 usb_gadget_class = NULL; 1486 return status; 1487 } 1488 1489 major = MAJOR(devt); 1490 minors = count; 1491 1492 return status; 1493 } 1494 1495 static void gprinter_cleanup(void) 1496 { 1497 if (major) { 1498 unregister_chrdev_region(MKDEV(major, 0), minors); 1499 major = minors = 0; 1500 } 1501 class_destroy(usb_gadget_class); 1502 usb_gadget_class = NULL; 1503 } 1504