^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * USB Skeleton driver - 2.2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * but has been rewritten to be easier to read and use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* Define these values to match your devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define USB_SKEL_VENDOR_ID 0xfff0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define USB_SKEL_PRODUCT_ID 0xfff0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* table of devices that work with this driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static const struct usb_device_id skel_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) { } /* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) MODULE_DEVICE_TABLE(usb, skel_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* Get a minor range for your devices from the usb maintainer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define USB_SKEL_MINOR_BASE 192
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* our private defines. if this grows any larger, use your own .h file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define MAX_TRANSFER (PAGE_SIZE - 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * MAX_TRANSFER is chosen so that the VM is not stressed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * allocations > PAGE_SIZE and the number of packets in a page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * is an integer 512 is the largest possible packet on EHCI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define WRITES_IN_FLIGHT 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* arbitrarily chosen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* Structure to hold all of our device specific stuff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct usb_skel {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct usb_device *udev; /* the usb device for this device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct usb_interface *interface; /* the interface for this device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct semaphore limit_sem; /* limiting the number of writes in progress */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct usb_anchor submitted; /* in case we need to retract our submissions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct urb *bulk_in_urb; /* the urb to read data with */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned char *bulk_in_buffer; /* the buffer to receive data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) size_t bulk_in_size; /* the size of the receive buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) size_t bulk_in_filled; /* number of bytes in the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) size_t bulk_in_copied; /* already copied to user space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int errors; /* the last request tanked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) bool ongoing_read; /* a read is going on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) spinlock_t err_lock; /* lock for errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct kref kref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct mutex io_mutex; /* synchronize I/O with disconnect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) unsigned long disconnected:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) wait_queue_head_t bulk_in_wait; /* to wait for an ongoing read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define to_skel_dev(d) container_of(d, struct usb_skel, kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static struct usb_driver skel_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static void skel_draw_down(struct usb_skel *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static void skel_delete(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct usb_skel *dev = to_skel_dev(kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) usb_free_urb(dev->bulk_in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) usb_put_intf(dev->interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) usb_put_dev(dev->udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) kfree(dev->bulk_in_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static int skel_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct usb_skel *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct usb_interface *interface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int subminor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) subminor = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) interface = usb_find_interface(&skel_driver, subminor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!interface) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pr_err("%s - error, can't find device for minor %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) __func__, subminor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) dev = usb_get_intfdata(interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) retval = usb_autopm_get_interface(interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* increment our usage count for the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) kref_get(&dev->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* save our object in the file's private structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) file->private_data = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int skel_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct usb_skel *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) dev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* allow the device to be autosuspended */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) usb_autopm_put_interface(dev->interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* decrement the count on our device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) kref_put(&dev->kref, skel_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int skel_flush(struct file *file, fl_owner_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct usb_skel *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) dev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* wait for io to stop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) mutex_lock(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) skel_draw_down(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* read out errors, leave subsequent opens a clean slate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) spin_lock_irq(&dev->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) dev->errors = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) spin_unlock_irq(&dev->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) mutex_unlock(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static void skel_read_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct usb_skel *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) dev = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) spin_lock_irqsave(&dev->err_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* sync/async unlink faults aren't errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (!(urb->status == -ENOENT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) urb->status == -ECONNRESET ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) urb->status == -ESHUTDOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) dev_err(&dev->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) "%s - nonzero write bulk status received: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) __func__, urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) dev->errors = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) dev->bulk_in_filled = urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) dev->ongoing_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) spin_unlock_irqrestore(&dev->err_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) wake_up_interruptible(&dev->bulk_in_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int skel_do_read_io(struct usb_skel *dev, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* prepare a read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) usb_fill_bulk_urb(dev->bulk_in_urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) dev->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) usb_rcvbulkpipe(dev->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) dev->bulk_in_endpointAddr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) dev->bulk_in_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) min(dev->bulk_in_size, count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) skel_read_bulk_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* tell everybody to leave the URB alone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) spin_lock_irq(&dev->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) dev->ongoing_read = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) spin_unlock_irq(&dev->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* submit bulk in urb, which means no data to deliver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) dev->bulk_in_filled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) dev->bulk_in_copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /* do it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) rv = usb_submit_urb(dev->bulk_in_urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) dev_err(&dev->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) "%s - failed submitting read urb, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) __func__, rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) rv = (rv == -ENOMEM) ? rv : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) spin_lock_irq(&dev->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) dev->ongoing_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) spin_unlock_irq(&dev->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static ssize_t skel_read(struct file *file, char *buffer, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct usb_skel *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) bool ongoing_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) dev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* no concurrent readers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) rv = mutex_lock_interruptible(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (dev->disconnected) { /* disconnect() was called */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) rv = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /* if IO is under way, we must not touch things */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) spin_lock_irq(&dev->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ongoing_io = dev->ongoing_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) spin_unlock_irq(&dev->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (ongoing_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* nonblocking IO shall not wait */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) rv = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * IO may take forever
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * hence wait in an interruptible state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) rv = wait_event_interruptible(dev->bulk_in_wait, (!dev->ongoing_read));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* errors must be reported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) rv = dev->errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /* any error is reported once */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) dev->errors = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /* to preserve notifications about reset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) rv = (rv == -EPIPE) ? rv : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /* report it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * if the buffer is filled we may satisfy the read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * else we need to start IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (dev->bulk_in_filled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /* we had read data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) size_t available = dev->bulk_in_filled - dev->bulk_in_copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) size_t chunk = min(available, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (!available) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * all data has been used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * actual IO needs to be done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) rv = skel_do_read_io(dev, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * data is available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * chunk tells us how much shall be copied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (copy_to_user(buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) dev->bulk_in_buffer + dev->bulk_in_copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) chunk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) rv = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) rv = chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) dev->bulk_in_copied += chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * if we are asked for more than we have,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * we start IO but don't wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (available < count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) skel_do_read_io(dev, count - chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /* no data in the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) rv = skel_do_read_io(dev, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) mutex_unlock(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static void skel_write_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct usb_skel *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) dev = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /* sync/async unlink faults aren't errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (!(urb->status == -ENOENT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) urb->status == -ECONNRESET ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) urb->status == -ESHUTDOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) dev_err(&dev->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) "%s - nonzero write bulk status received: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) __func__, urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) spin_lock_irqsave(&dev->err_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) dev->errors = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) spin_unlock_irqrestore(&dev->err_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /* free up our allocated buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) usb_free_coherent(urb->dev, urb->transfer_buffer_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) urb->transfer_buffer, urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) up(&dev->limit_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static ssize_t skel_write(struct file *file, const char *user_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct usb_skel *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct urb *urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) char *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) size_t writesize = min(count, (size_t)MAX_TRANSFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) dev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /* verify that we actually have some data to write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * limit the number of URBs in flight to stop a user from using up all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * RAM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (!(file->f_flags & O_NONBLOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (down_interruptible(&dev->limit_sem)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) retval = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (down_trylock(&dev->limit_sem)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) retval = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) spin_lock_irq(&dev->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) retval = dev->errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /* any error is reported once */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) dev->errors = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) /* to preserve notifications about reset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) retval = (retval == -EPIPE) ? retval : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) spin_unlock_irq(&dev->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /* create a urb, and a buffer for it, and copy the data to the urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) buf = usb_alloc_coherent(dev->udev, writesize, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) &urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (copy_from_user(buf, user_buffer, writesize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /* this lock makes sure we don't submit URBs to gone devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) mutex_lock(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (dev->disconnected) { /* disconnect() was called */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) mutex_unlock(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /* initialize the urb properly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) usb_fill_bulk_urb(urb, dev->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) buf, writesize, skel_write_bulk_callback, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) usb_anchor_urb(urb, &dev->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) /* send the data out the bulk port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) retval = usb_submit_urb(urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) mutex_unlock(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) dev_err(&dev->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) "%s - failed submitting write urb, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) __func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) goto error_unanchor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * release our reference to this urb, the USB core will eventually free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * it entirely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return writesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) error_unanchor:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) usb_free_coherent(dev->udev, writesize, buf, urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) up(&dev->limit_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static const struct file_operations skel_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) .read = skel_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) .write = skel_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) .open = skel_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) .release = skel_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) .flush = skel_flush,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * usb class driver info in order to get a minor number from the usb core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * and to have the device registered with the driver core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static struct usb_class_driver skel_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) .name = "skel%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) .fops = &skel_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) .minor_base = USB_SKEL_MINOR_BASE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) static int skel_probe(struct usb_interface *interface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct usb_skel *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct usb_endpoint_descriptor *bulk_in, *bulk_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) /* allocate memory for our device state and initialize it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) dev = kzalloc(sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) kref_init(&dev->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) mutex_init(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) spin_lock_init(&dev->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) init_usb_anchor(&dev->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) init_waitqueue_head(&dev->bulk_in_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) dev->udev = usb_get_dev(interface_to_usbdev(interface));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) dev->interface = usb_get_intf(interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) /* set up the endpoint information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) /* use only the first bulk-in and bulk-out endpoints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) retval = usb_find_common_endpoints(interface->cur_altsetting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) &bulk_in, &bulk_out, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) dev_err(&interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) "Could not find both bulk-in and bulk-out endpoints\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) dev->bulk_in_size = usb_endpoint_maxp(bulk_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) dev->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) dev->bulk_in_buffer = kmalloc(dev->bulk_in_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (!dev->bulk_in_buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (!dev->bulk_in_urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) dev->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) /* save our data pointer in this interface device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) usb_set_intfdata(interface, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) /* we can register the device now, as it is ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) retval = usb_register_dev(interface, &skel_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /* something prevented us from registering this driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) dev_err(&interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) "Not able to get a minor for this device.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) usb_set_intfdata(interface, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) /* let the user know what node this device is now attached to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) dev_info(&interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) "USB Skeleton device now attached to USBSkel-%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) interface->minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) /* this frees allocated memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) kref_put(&dev->kref, skel_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) static void skel_disconnect(struct usb_interface *interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) struct usb_skel *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) int minor = interface->minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) dev = usb_get_intfdata(interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) usb_set_intfdata(interface, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /* give back our minor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) usb_deregister_dev(interface, &skel_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /* prevent more I/O from starting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) mutex_lock(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) dev->disconnected = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) mutex_unlock(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) usb_kill_urb(dev->bulk_in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) usb_kill_anchored_urbs(&dev->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) /* decrement our usage count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) kref_put(&dev->kref, skel_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) static void skel_draw_down(struct usb_skel *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) int time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (!time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) usb_kill_anchored_urbs(&dev->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) usb_kill_urb(dev->bulk_in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) static int skel_suspend(struct usb_interface *intf, pm_message_t message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) struct usb_skel *dev = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) skel_draw_down(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) static int skel_resume(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) static int skel_pre_reset(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) struct usb_skel *dev = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) mutex_lock(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) skel_draw_down(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) static int skel_post_reset(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct usb_skel *dev = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) /* we are sure no URBs are active - no locking needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) dev->errors = -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) mutex_unlock(&dev->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) static struct usb_driver skel_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) .name = "skeleton",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) .probe = skel_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) .disconnect = skel_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) .suspend = skel_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) .resume = skel_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) .pre_reset = skel_pre_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) .post_reset = skel_post_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) .id_table = skel_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) .supports_autosuspend = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) module_usb_driver(skel_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) MODULE_LICENSE("GPL v2");