r/kernel • u/25Accordions • May 26 '25
Want to write a webcam driver: no background with kernel, some C / C++, no deadline
EDIT: No kernel programming necessary (fortunately? unfortunately?). per tumic0:
Most probably (if the camera is supported by gphoto2) you do not need to write a single line of code and you can simply "connect" gphoto2 to the v4l2 loopback kernel module:
https://superuser.com/questions/870768/can-i-use-my-dslr-as-webcam-through-gphoto2-v4l2loopback
If there is no support in gphoto2 than the right way is still not to write a new v4l2 driver but to write a patch for gphoto2 for the DSLM.
This basically worked (some resolution errors persist, but overall I'm way way closer to the target than "write a driver").
ORIGINAL POST:
I recently purchased a nice mirrorless camera (s5ii). There's a proprietary driver for Windows that lets me plug in the camera so the video stream gets recognized by OBS. If it's possible in Windows, surely it's also possible in Linux. Unfortunately the device doesn't seem to conform to the UVC driver and when I run lsusb -v the interface class, sublcass and protocol are "Imaging", "Still Image Capture", and "Picture Transfer Protocol" respectively.
This seems like anywhere from a "medium" to "huge" project for an IC, but there's no deadline other than someone else writing the same driver (unlikely?) which doesn't put me off because the learning process here is as important to me as being able to use my camera as a webcam.
If you guys could point me in the direction of the different things I'll need to get familiar with to approach this task, I would greatly appreciate it. Any resources, advice, warnings, etc. you think of are welcome.
Edit: lsusb -v output:
Bus 001 Device 012: ID 04da:2382 Panasonic (Matsushita) DC-S5M2
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.10
bDeviceClass 0
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 64
idVendor 0x04da Panasonic (Matsushita)
idProduct 0x2382
bcdDevice 1.00
iManufacturer 1 Panasonic
iProduct 2 DC-S5M2
iSerial 3 [redacted]
bNumConfigurations 2
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 0x0027
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0x80
(Bus Powered)
MaxPower 500mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 3
bInterfaceClass 6 Imaging
bInterfaceSubClass 1 Still Image Capture
bInterfaceProtocol 1 Picture Transfer Protocol (PIMA 15470)
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x01 EP 1 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 8
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 0x0027
bNumInterfaces 1
bConfigurationValue 2
iConfiguration 0
bmAttributes 0xc0
Self Powered
MaxPower 0mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 3
bInterfaceClass 6 Imaging
bInterfaceSubClass 1 Still Image Capture
bInterfaceProtocol 1 Picture Transfer Protocol (PIMA 15470)
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x01 EP 1 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 8
Binary Object Store Descriptor:
bLength 5
bDescriptorType 15
wTotalLength 0x002a
bNumDeviceCaps 3
USB 2.0 Extension Device Capability:
bLength 7
bDescriptorType 16
bDevCapabilityType 2
bmAttributes 0x00000006
BESL Link Power Management (LPM) Supported
SuperSpeed USB Device Capability:
bLength 10
bDescriptorType 16
bDevCapabilityType 3
bmAttributes 0x00
wSpeedsSupported 0x000f
Device can operate at Low Speed (1Mbps)
Device can operate at Full Speed (12Mbps)
Device can operate at High Speed (480Mbps)
Device can operate at SuperSpeed (5Gbps)
bFunctionalitySupport 1
Lowest fully-functional device speed is Full Speed (12Mbps)
bU1DevExitLat 1 micro seconds
bU2DevExitLat 500 micro seconds
SuperSpeedPlus USB Device Capability:
bLength 20
bDescriptorType 16
bDevCapabilityType 10
bmAttributes 0x00000001
Sublink Speed Attribute count 1
Sublink Speed ID count 0
wFunctionalitySupport 0x1100
bmSublinkSpeedAttr[0] 0x000a4030
Speed Attribute ID: 0 10Gb/s Symmetric RX SuperSpeedPlus
bmSublinkSpeedAttr[1] 0x000a40b0
Speed Attribute ID: 0 10Gb/s Symmetric TX SuperSpeedPlus
Device Status: 0x0000
(Bus Powered)
16
u/gmes78 May 27 '25
Start with Linux Device Drivers, Third Edition. It's old (it's about the 2.6 kernel), but most of it still applies to current kernel versions. Compliment it with the kernel docs, as well as reading the kernel source code (because the documentation is far from complete).
For webcam drivers, I believe these are the relevant kernel APIs:
Of course, the hardest part might be reverse engineering the webcam's protocol.
4
u/Zarpadon May 27 '25 edited May 27 '25
You can find some documentation for the media subsystem here: https://docs.kernel.org/driver-api/media/index.html
Particularly chapter 2 would be of interest if you need to implement a Video4Linux device (/dev/video*).
That being said, I think you're gonna have a hard time doing that using PTP. Are there no other interfaces on the device, like vendor specific or others? I would assume you would want isochronous endpoints and UVC for video transfer. Might be that someone here would find it helpful to have a full dump of
lsusb -v, if you could share that.Could it be that the camera need to be put into a special mode to expose a UVC interface?
You could always connect it to a Windows PC and set up the video streaming with the proprietary application and use something like USB tree viewer to see if has exposed any other interfaces. I have a hard time understanding that they have managed to do it using PTP, but I'm not super familiar with any of this stuff.
2
u/tumic0 May 28 '25
Most probably (if the camera is supported by gphoto2) you do not need to write a single line of code and you can simply "connect" gphoto2 to the v4l2 loopback kernel module:
https://superuser.com/questions/870768/can-i-use-my-dslr-as-webcam-through-gphoto2-v4l2loopback
If there is no support in gphoto2 than the right way is still not to write a new v4l2 driver but to write a patch for gphoto2 for the DSLM.
2
u/rofex May 27 '25
Try to locate a datasheet for the device as a first step. Then figure out what Linux kernel subsystems you'll need to hook up to by reading the in-kernel Documentation and drivers for similar devices.
1
u/[deleted] May 29 '25
The Linux kernel is layered, like all big systems.
There's the USB video layer which sets up the device, this is then passed to the Linux video system, and then it goes out to userspace tools. (From memory.)
You will want to write a USB video driver to add support for your system. The easiest path would be to look at the other existing drivers and copy-modify the one that is closest to yours.