15 #include <linux/kernel.h>
16 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
27 #define USB_SKEL_VENDOR_ID 0xfff0
28 #define USB_SKEL_PRODUCT_ID 0xfff0
39 #define USB_SKEL_MINOR_BASE 192
42 #define MAX_TRANSFER (PAGE_SIZE - 512)
46 #define WRITES_IN_FLIGHT 8
70 #define to_skel_dev(d) container_of(d, struct usb_skel, kref)
72 static struct usb_driver skel_driver;
75 static void skel_delete(
struct kref *
kref)
92 subminor = iminor(inode);
96 pr_err(
"%s - error, can't find device for minor %d\n",
102 dev = usb_get_intfdata(interface);
109 kref_get(&dev->
kref);
115 retval = usb_autopm_get_interface(interface);
127 static int skel_release(
struct inode *inode,
struct file *file)
138 usb_autopm_put_interface(dev->
interface);
142 kref_put(&dev->
kref, skel_delete);
146 static int skel_flush(
struct file *file,
fl_owner_t id)
170 static void skel_read_bulk_callback(
struct urb *
urb)
179 if (!(urb->status == -
ENOENT ||
183 "%s - nonzero write bulk status received: %d\n",
184 __func__, urb->status);
186 dev->
errors = urb->status;
196 static int skel_do_read_io(
struct usb_skel *dev,
size_t count)
203 usb_rcvbulkpipe(dev->
udev,
207 skel_read_bulk_callback,
218 "%s - failed submitting read urb, error %d\n",
230 static ssize_t skel_read(
struct file *file,
char *
buffer,
size_t count,
311 size_t chunk =
min(available, count);
318 rv = skel_do_read_io(dev, count);
342 if (available < count)
343 skel_do_read_io(dev, count - chunk);
346 rv = skel_do_read_io(dev, count);
358 static void skel_write_bulk_callback(
struct urb *urb)
366 if (!(urb->status == -
ENOENT ||
370 "%s - nonzero write bulk status received: %d\n",
371 __func__, urb->status);
374 dev->
errors = urb->status;
380 urb->transfer_buffer, urb->transfer_dma);
384 static ssize_t skel_write(
struct file *file,
const char *user_buffer,
385 size_t count, loff_t *ppos)
389 struct urb *urb =
NULL;
421 retval = (retval == -
EPIPE) ? retval : -
EIO;
455 usb_fill_bulk_urb(urb, dev->
udev,
457 buf, writesize, skel_write_bulk_callback, dev);
458 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
466 "%s - failed submitting write urb, error %d\n",
498 .release = skel_release,
507 static struct usb_class_driver skel_class = {
517 struct usb_host_interface *iface_desc;
526 dev_err(&interface->dev,
"Out of memory\n");
529 kref_init(&dev->
kref);
541 iface_desc = interface->cur_altsetting;
542 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++
i) {
543 endpoint = &iface_desc->endpoint[
i].desc;
546 usb_endpoint_is_bulk_in(endpoint)) {
548 buffer_size = usb_endpoint_maxp(endpoint);
554 "Could not allocate bulk_in_buffer\n");
560 "Could not allocate bulk_in_urb\n");
566 usb_endpoint_is_bulk_out(endpoint)) {
573 "Could not find both bulk-in and bulk-out endpoints\n");
578 usb_set_intfdata(interface, dev);
585 "Not able to get a minor for this device.\n");
586 usb_set_intfdata(interface,
NULL);
592 "USB Skeleton device now attached to USBSkel-%d",
599 kref_put(&dev->
kref, skel_delete);
606 int minor = interface->minor;
608 dev = usb_get_intfdata(interface);
609 usb_set_intfdata(interface,
NULL);
622 kref_put(&dev->
kref, skel_delete);
624 dev_info(&interface->dev,
"USB Skeleton #%d now disconnected", minor);
627 static void skel_draw_down(
struct usb_skel *dev)
639 struct usb_skel *dev = usb_get_intfdata(intf);
654 struct usb_skel *dev = usb_get_intfdata(intf);
664 struct usb_skel *dev = usb_get_intfdata(intf);
673 static struct usb_driver skel_driver = {
676 .disconnect = skel_disconnect,
677 .suspend = skel_suspend,
678 .resume = skel_resume,
679 .pre_reset = skel_pre_reset,
680 .post_reset = skel_post_reset,
681 .id_table = skel_table,
682 .supports_autosuspend = 1,