gschorcht / spi-ch341-usb

Linux kernel driver for CH341A USB to SPI and GPIO adapters
160 stars 63 forks source link

Fails to handle multiple physical adapters #11

Open Hugobros3 opened 4 years ago

Hugobros3 commented 4 years ago

I found out that plugging more than one ch341 at a time makes the driver very unhappy. The dmesg log complains about creating a duplicate gpio4 file, which to me indicates this driver was not written to support multiple ch341-based devices at once.

However I do not need the GPIO functionality, I just need multiple SPI interfaces (not multiple slaves sharing a bus, I mean multiple buses using multiple physical ch341-based adapters), and that seems to probe fine before crashing and burning when it tries to probe for GPIO later on.

I'm no kernel hacker, but my understanding here is all I need to do is get rid/comment out the GPIO functionality and I would have a working driver that can handle however many devices I can plug in. Am I correct in that assumption ? I'm willing to give it a shot in a PR, but I would certainly appreciate some guidance on how to get that working. Would commenting out the calls to ch341_gpio_probe/ch341_gpio_remove do it ?

dmesg log:

[  390.504201] spi_ch341_usb: loading out-of-tree module taints kernel.
[  390.505692] spi-ch341-usb 1-1.5.2:1.0: ch341_cfg_probe: output cs0 SPI slave with cs=0
[  390.505705] spi-ch341-usb 1-1.5.2:1.0: ch341_cfg_probe: output cs1 SPI slave with cs=1
[  390.505715] spi-ch341-usb 1-1.5.2:1.0: ch341_cfg_probe: output cs2 SPI slave with cs=2
[  390.505726] spi-ch341-usb 1-1.5.2:1.0: ch341_cfg_probe: input  gpio4 gpio=0 irq=0 (hwirq)
[  390.505738] spi-ch341-usb 1-1.5.2:1.0: ch341_cfg_probe: input  gpio5 gpio=1 irq=1
[  390.505753] spi-ch341-usb 1-1.5.2:1.0: ch341_spi_probe: SPI master connected to SPI bus 0
[  390.508672] spi-ch341-usb 1-1.5.2:1.0: ch341_spi_probe: SPI device /dev/spidev0.0 created
[  390.509075] spi-ch341-usb 1-1.5.2:1.0: ch341_spi_probe: SPI device /dev/spidev0.1 created
[  390.509398] spi-ch341-usb 1-1.5.2:1.0: ch341_spi_probe: SPI device /dev/spidev0.2 created
[  390.510421] spi-ch341-usb 1-1.5.2:1.0: ch341_usb_probe: connected
[  390.510590] usbcore: registered new interface driver spi-ch341-usb
[  395.512838] Voltage normalised (0x00000000)
[  409.442825] usb 1-1.5.3: new full-speed USB device number 8 using dwc_otg
[  409.574854] usb 1-1.5.3: New USB device found, idVendor=1a86, idProduct=5512, bcdDevice= 3.04
[  409.574868] usb 1-1.5.3: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[  409.576333] spi-ch341-usb 1-1.5.3:1.0: ch341_cfg_probe: output cs0 SPI slave with cs=0
[  409.576347] spi-ch341-usb 1-1.5.3:1.0: ch341_cfg_probe: output cs1 SPI slave with cs=1
[  409.576357] spi-ch341-usb 1-1.5.3:1.0: ch341_cfg_probe: output cs2 SPI slave with cs=2
[  409.576369] spi-ch341-usb 1-1.5.3:1.0: ch341_cfg_probe: input  gpio4 gpio=0 irq=0 (hwirq)
[  409.576380] spi-ch341-usb 1-1.5.3:1.0: ch341_cfg_probe: input  gpio5 gpio=1 irq=1
[  409.576404] spi-ch341-usb 1-1.5.3:1.0: ch341_spi_probe: SPI master connected to SPI bus 1
[  409.577385] spi-ch341-usb 1-1.5.3:1.0: ch341_spi_probe: SPI device /dev/spidev1.0 created
[  409.578487] spi-ch341-usb 1-1.5.3:1.0: ch341_spi_probe: SPI device /dev/spidev1.1 created
[  409.579272] spi-ch341-usb 1-1.5.3:1.0: ch341_spi_probe: SPI device /dev/spidev1.2 created
[  409.579670] gpio gpiochip4: Detected name collision for GPIO name 'gpio4'
[  409.579683] gpio gpiochip4: Detected name collision for GPIO name 'gpio5'
[  409.580055] sysfs: cannot create duplicate filename '/class/gpio/gpio4'
[  409.580071] CPU: 0 PID: 531 Comm: kworker/0:0 Tainted: G         C O      4.19.75-v7+ #1270
[  409.580076] Hardware name: BCM2835
[  409.580100] Workqueue: usb_hub_wq hub_event
[  409.580156] [<80111fcc>] (unwind_backtrace) from [<8010d544>] (show_stack+0x20/0x24)
[  409.580181] [<8010d544>] (show_stack) from [<80819bc0>] (dump_stack+0xd4/0x118)
[  409.580203] [<80819bc0>] (dump_stack) from [<80333de0>] (sysfs_warn_dup+0x6c/0x78)
[  409.580219] [<80333de0>] (sysfs_warn_dup) from [<80334128>] (sysfs_do_create_link_sd+0xc4/0xd4)
[  409.580232] [<80334128>] (sysfs_do_create_link_sd) from [<80334170>] (sysfs_create_link+0x38/0x44)
[  409.580250] [<80334170>] (sysfs_create_link) from [<805a774c>] (device_add+0x2a8/0x600)
[  409.580268] [<805a774c>] (device_add) from [<805a7c6c>] (device_create_groups_vargs+0xc0/0xd0)
[  409.580284] [<805a7c6c>] (device_create_groups_vargs) from [<805a7d54>] (device_create_with_groups+0x44/0x60)
[  409.580302] [<805a7d54>] (device_create_with_groups) from [<8052d680>] (gpiod_export+0x170/0x1e0)
[  409.580334] [<8052d680>] (gpiod_export) from [<7f64116c>] (ch341_usb_probe+0x5d8/0xa58 [spi_ch341_usb])
[  409.580383] [<7f64116c>] (ch341_usb_probe [spi_ch341_usb]) from [<8062463c>] (usb_probe_interface+0x110/0x294)
[  409.580402] [<8062463c>] (usb_probe_interface) from [<805aae78>] (really_probe+0x23c/0x2d4)
[  409.580421] [<805aae78>] (really_probe) from [<805ab0e4>] (driver_probe_device+0x70/0x1ac)
[  409.580441] [<805ab0e4>] (driver_probe_device) from [<805ab3f4>] (__device_attach_driver+0x98/0xa8)
[  409.580459] [<805ab3f4>] (__device_attach_driver) from [<805a8ca8>] (bus_for_each_drv+0x88/0xcc)
[  409.580476] [<805a8ca8>] (bus_for_each_drv) from [<805aabb8>] (__device_attach+0xe0/0x144)
[  409.580493] [<805aabb8>] (__device_attach) from [<805ab420>] (device_initial_probe+0x1c/0x20)
[  409.580510] [<805ab420>] (device_initial_probe) from [<805a9e7c>] (bus_probe_device+0x94/0x9c)
[  409.580527] [<805a9e7c>] (bus_probe_device) from [<805a790c>] (device_add+0x468/0x600)
[  409.580543] [<805a790c>] (device_add) from [<8062220c>] (usb_set_configuration+0x51c/0xa5c)
[  409.580559] [<8062220c>] (usb_set_configuration) from [<8062db58>] (generic_probe+0x3c/0x88)
[  409.580574] [<8062db58>] (generic_probe) from [<80624514>] (usb_probe_device+0x34/0x4c)
[  409.580590] [<80624514>] (usb_probe_device) from [<805aae78>] (really_probe+0x23c/0x2d4)
[  409.580608] [<805aae78>] (really_probe) from [<805ab0e4>] (driver_probe_device+0x70/0x1ac)
[  409.580626] [<805ab0e4>] (driver_probe_device) from [<805ab3f4>] (__device_attach_driver+0x98/0xa8)
[  409.580643] [<805ab3f4>] (__device_attach_driver) from [<805a8ca8>] (bus_for_each_drv+0x88/0xcc)
[  409.580660] [<805a8ca8>] (bus_for_each_drv) from [<805aabb8>] (__device_attach+0xe0/0x144)
[  409.580677] [<805aabb8>] (__device_attach) from [<805ab420>] (device_initial_probe+0x1c/0x20)
[  409.580695] [<805ab420>] (device_initial_probe) from [<805a9e7c>] (bus_probe_device+0x94/0x9c)
[  409.580711] [<805a9e7c>] (bus_probe_device) from [<805a790c>] (device_add+0x468/0x600)
[  409.580728] [<805a790c>] (device_add) from [<80618534>] (usb_new_device+0x27c/0x450)
[  409.580747] [<80618534>] (usb_new_device) from [<8061a048>] (hub_event+0xdc0/0x11d8)
[  409.580765] [<8061a048>] (hub_event) from [<8013bf64>] (process_one_work+0x170/0x458)
[  409.580782] [<8013bf64>] (process_one_work) from [<8013c2a8>] (worker_thread+0x5c/0x5a4)
[  409.580799] [<8013c2a8>] (worker_thread) from [<80142594>] (kthread+0x138/0x168)
[  409.580816] [<80142594>] (kthread) from [<801010ac>] (ret_from_fork+0x14/0x28)
[  409.580824] Exception stack(0xb7257fb0 to 0xb7257ff8)
[  409.580834] 7fa0:                                     00000000 00000000 00000000 00000000
[  409.580846] 7fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[  409.580856] 7fe0: 00000000 00000000 00000000 00000000 00000013 00000000
[  409.580976] spi-ch341-usb 1-1.5.3:1.0: ch341_gpio_probe: failed to export GPIO gpio4: -17
[  409.581122] gpio gpiochip4: REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED
[  409.590168] ------------[ cut here ]------------
[  409.590201] WARNING: CPU: 0 PID: 531 at lib/refcount.c:187 refcount_sub_and_test_checked+0xa8/0xb8
[  409.590207] refcount_t: underflow; use-after-free.
[  409.590213] Modules linked in: spidev spi_ch341_usb(O) bnep hci_uart btbcm serdev bluetooth ecdh_generic brcmfmac brcmutil sha256_generic cfg80211 snd_bcm2835(C) rfkill bcm2835_codec(C) bcm2835_v4l2(C) raspberrypi_hwmon v4l2_mem2mem hwmon snd_pcm bcm2835_mmal_vchiq(C) snd_timer v4l2_common videobuf2_dma_contig videobuf2_vmalloc snd videobuf2_memops videobuf2_v4l2 videobuf2_common videodev vc_sm_cma(C) media uio_pdrv_genirq uio fixed ip_tables x_tables ipv6
[  409.590500] CPU: 0 PID: 531 Comm: kworker/0:0 Tainted: G         C O      4.19.75-v7+ #1270
[  409.590505] Hardware name: BCM2835
[  409.590525] Workqueue: usb_hub_wq hub_event
[  409.590557] [<80111fcc>] (unwind_backtrace) from [<8010d544>] (show_stack+0x20/0x24)
[  409.590575] [<8010d544>] (show_stack) from [<80819bc0>] (dump_stack+0xd4/0x118)
[  409.590594] [<80819bc0>] (dump_stack) from [<80120a88>] (__warn+0x104/0x11c)
[  409.590610] [<80120a88>] (__warn) from [<80120af8>] (warn_slowpath_fmt+0x58/0x74)
[  409.590627] [<80120af8>] (warn_slowpath_fmt) from [<80505078>] (refcount_sub_and_test_checked+0xa8/0xb8)
[  409.590645] [<80505078>] (refcount_sub_and_test_checked) from [<805050a0>] (refcount_dec_and_test_checked+0x18/0x1c)
[  409.590663] [<805050a0>] (refcount_dec_and_test_checked) from [<8081e694>] (kobject_put+0x2c/0xd4)
[  409.590683] [<8081e694>] (kobject_put) from [<805a5440>] (put_device+0x24/0x28)
[  409.590711] [<805a5440>] (put_device) from [<7f640af8>] (ch341_usb_free_device.part.0+0xd8/0x12c [spi_ch341_usb])
[  409.590743] [<7f640af8>] (ch341_usb_free_device.part.0 [spi_ch341_usb]) from [<7f641450>] (ch341_usb_probe+0x8bc/0xa58 [spi_ch341_usb])
[  409.590768] [<7f641450>] (ch341_usb_probe [spi_ch341_usb]) from [<8062463c>] (usb_probe_interface+0x110/0x294)
[  409.590788] [<8062463c>] (usb_probe_interface) from [<805aae78>] (really_probe+0x23c/0x2d4)
[  409.590807] [<805aae78>] (really_probe) from [<805ab0e4>] (driver_probe_device+0x70/0x1ac)
[  409.590826] [<805ab0e4>] (driver_probe_device) from [<805ab3f4>] (__device_attach_driver+0x98/0xa8)
[  409.590843] [<805ab3f4>] (__device_attach_driver) from [<805a8ca8>] (bus_for_each_drv+0x88/0xcc)
[  409.590860] [<805a8ca8>] (bus_for_each_drv) from [<805aabb8>] (__device_attach+0xe0/0x144)
[  409.590878] [<805aabb8>] (__device_attach) from [<805ab420>] (device_initial_probe+0x1c/0x20)
[  409.590895] [<805ab420>] (device_initial_probe) from [<805a9e7c>] (bus_probe_device+0x94/0x9c)
[  409.590912] [<805a9e7c>] (bus_probe_device) from [<805a790c>] (device_add+0x468/0x600)
[  409.590928] [<805a790c>] (device_add) from [<8062220c>] (usb_set_configuration+0x51c/0xa5c)
[  409.590945] [<8062220c>] (usb_set_configuration) from [<8062db58>] (generic_probe+0x3c/0x88)
[  409.590960] [<8062db58>] (generic_probe) from [<80624514>] (usb_probe_device+0x34/0x4c)
[  409.590977] [<80624514>] (usb_probe_device) from [<805aae78>] (really_probe+0x23c/0x2d4)
[  409.590995] [<805aae78>] (really_probe) from [<805ab0e4>] (driver_probe_device+0x70/0x1ac)
[  409.591013] [<805ab0e4>] (driver_probe_device) from [<805ab3f4>] (__device_attach_driver+0x98/0xa8)
[  409.591030] [<805ab3f4>] (__device_attach_driver) from [<805a8ca8>] (bus_for_each_drv+0x88/0xcc)
[  409.591046] [<805a8ca8>] (bus_for_each_drv) from [<805aabb8>] (__device_attach+0xe0/0x144)
[  409.591064] [<805aabb8>] (__device_attach) from [<805ab420>] (device_initial_probe+0x1c/0x20)
[  409.591081] [<805ab420>] (device_initial_probe) from [<805a9e7c>] (bus_probe_device+0x94/0x9c)
[  409.591097] [<805a9e7c>] (bus_probe_device) from [<805a790c>] (device_add+0x468/0x600)
[  409.591117] [<805a790c>] (device_add) from [<80618534>] (usb_new_device+0x27c/0x450)
[  409.591136] [<80618534>] (usb_new_device) from [<8061a048>] (hub_event+0xdc0/0x11d8)
[  409.591156] [<8061a048>] (hub_event) from [<8013bf64>] (process_one_work+0x170/0x458)
[  409.591174] [<8013bf64>] (process_one_work) from [<8013c2a8>] (worker_thread+0x5c/0x5a4)
[  409.591191] [<8013c2a8>] (worker_thread) from [<80142594>] (kthread+0x138/0x168)
[  409.591210] [<80142594>] (kthread) from [<801010ac>] (ret_from_fork+0x14/0x28)
[  409.591217] Exception stack(0xb7257fb0 to 0xb7257ff8)
[  409.591227] 7fa0:                                     00000000 00000000 00000000 00000000
[  409.591239] 7fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[  409.591249] 7fe0: 00000000 00000000 00000000 00000000 00000013 00000000
[  409.591259] ---[ end trace b7780a7e024bdc6f ]---
[  409.591340] spi-ch341-usb: probe of 1-1.5.3:1.0 failed with error -17
Hugobros3 commented 4 years ago

Re: Commenting those out allowed the devices to show up and they seemingly don't have issues (tried them out using rc522s). Will keep the issue open for further discussion (I feel like it shouldn't be that easy!)