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