drivers/usb/misc/ldusb.c
Source file repositories/reference/linux-study-clean/drivers/usb/misc/ldusb.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/usb/misc/ldusb.c- Extension
.c- Size
- 24837 bytes
- Lines
- 797
- Domain
- Driver Families
- Bucket
- drivers/usb
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel.hlinux/errno.hlinux/slab.hlinux/module.hlinux/mutex.hlinux/uaccess.hlinux/input.hlinux/usb.hlinux/poll.h
Detected Declarations
struct ld_usbfunction ld_usb_abort_transfersfunction ld_usb_deletefunction ld_usb_interrupt_in_callbackfunction ld_usb_interrupt_out_callbackfunction ld_usb_openfunction ld_usb_releasefunction ld_usb_pollfunction ld_usb_readfunction ld_usb_writefunction ld_usb_probefunction ld_usb_disconnect
Annotated Snippet
static const struct file_operations ld_usb_fops = {
.owner = THIS_MODULE,
.read = ld_usb_read,
.write = ld_usb_write,
.open = ld_usb_open,
.release = ld_usb_release,
.poll = ld_usb_poll,
};
/*
* usb class driver info in order to get a minor number from the usb core,
* and to have the device registered with the driver core
*/
static struct usb_class_driver ld_usb_class = {
.name = "ldusb%d",
.fops = &ld_usb_fops,
.minor_base = USB_LD_MINOR_BASE,
};
/*
* ld_usb_probe
*
* Called by the usb core when a new device is connected that it thinks
* this driver might be interested in.
*/
static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
struct usb_device *udev = interface_to_usbdev(intf);
struct ld_usb *dev = NULL;
struct usb_host_interface *iface_desc;
char *buffer;
int retval = -ENOMEM;
int res;
/* allocate memory for our device state and initialize it */
dev = kzalloc_obj(*dev);
if (!dev)
goto exit;
mutex_init(&dev->mutex);
spin_lock_init(&dev->rbsl);
dev->intf = intf;
init_waitqueue_head(&dev->read_wait);
init_waitqueue_head(&dev->write_wait);
/* workaround for early firmware versions on fast computers */
if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
(le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
(le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
buffer = kmalloc(256, GFP_KERNEL);
if (!buffer)
goto error;
/* usb_string makes SETUP+STALL to leave always ControlReadLoop */
usb_string(udev, 255, buffer, 256);
kfree(buffer);
}
iface_desc = intf->cur_altsetting;
res = usb_find_last_int_in_endpoint(iface_desc,
&dev->interrupt_in_endpoint);
if (res) {
dev_err(&intf->dev, "Interrupt in endpoint not found\n");
retval = res;
goto error;
}
res = usb_find_last_int_out_endpoint(iface_desc,
&dev->interrupt_out_endpoint);
if (res)
dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
dev->interrupt_in_endpoint_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
dev->ring_buffer = kcalloc(ring_buffer_size,
sizeof(size_t) + dev->interrupt_in_endpoint_size,
GFP_KERNEL);
if (!dev->ring_buffer)
goto error;
dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
if (!dev->interrupt_in_buffer)
goto error;
dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!dev->interrupt_in_urb)
goto error;
dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? usb_endpoint_maxp(dev->interrupt_out_endpoint) :
udev->descriptor.bMaxPacketSize0;
dev->interrupt_out_buffer =
kmalloc_array(write_buffer_size,
dev->interrupt_out_endpoint_size, GFP_KERNEL);
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/errno.h`, `linux/slab.h`, `linux/module.h`, `linux/mutex.h`, `linux/uaccess.h`, `linux/input.h`, `linux/usb.h`.
- Detected declarations: `struct ld_usb`, `function ld_usb_abort_transfers`, `function ld_usb_delete`, `function ld_usb_interrupt_in_callback`, `function ld_usb_interrupt_out_callback`, `function ld_usb_open`, `function ld_usb_release`, `function ld_usb_poll`, `function ld_usb_read`, `function ld_usb_write`.
- Atlas domain: Driver Families / drivers/usb.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.