USB Gadget on PineTab2
#1
Sad 
Dear Piners,

has anybody of you tried to get a USB gadget working for the second USB interface on the PineTab2? This is a fully functional OTG interface that you can use for much more than only charging. Anyway, as long as you don't connect an OTG cable, it will reside in device mode, so it should be possible to program USB gadgets following such instructions:
https://openwrt.org/docs/guide-user/hardware/usb_gadget
https://trac.gateworks.com/wiki/linux/OTG
https://developer.toradex.com/linux-bsp/...ode-linux/
https://www.kernel.org/doc/Documentation...nfigfs.txt
https://github.com/torvalds/linux/blob/m...usb-gadget
Based on these (and some other web sites) I contrived a script for a mass storage gadget. First I prepared an 8 GB big container file using these commands:
Code:
dd if=/dev/zero of=/stick.img bs=1M count=8192
fdisk /stick.img
# Inside fdisk I prepared the image using the following keys:
# o x s 32 h 128 c 4096 r n p 1 \n \n t c p w
losetup -o 1M /dev/loop0 /stick.img
mkfs.vfat /dev/loop0
mount -t vfat /dev/loop0 /mnt
echo hallo >/mnt/test.txt
umount /mnt
losetup -d /dev/loop0
The following script intends to simulate a USB stick on the device mode USB port:
Code:
#!/bin/sh
cd /sys/kernel/config/usb_gadget
mkdir g1
sleep 1
cd g1

echo 0x1d6b >idVendor  # reserved for the Linux Foundation
echo 0x0104 >idProduct  # multifunction is sufficient
#echo 0x0615 > bcdDevice  This is preset for some reason.
echo 0x0200 > bcdUSB    # USB 2.0
# Is the following correct for a flash stick?
echo 0x08 > bDeviceClass
echo 0x06 > bDeviceSubClass
echo 0x80 > bDeviceProtocol
#echo 0x62 > bDeviceProtocol
echo 0x40 >bMaxPacketSize0
echo high-speed >max_speed

# The following strings can be chosen freely; for the serial number I take the
# actual SN of my PineTab2.
cd strings
mkdir 0x409 && cd 0x409
sleep 1
cat /proc/device-tree/serial-number >serialnumber
echo Pine64 >manufacturer
echo PineTab2\ v2.0 >product
# Alternatively one could also use some of the information in
# /sys/bus/i2c/drivers/husb311/0-004e to fill in the above.

cd ../../configs
mkdir c.1 && cd c.1
sleep 1
mkdir strings/0x409
sleep 1
echo "Memory stick emulation based on image file" >strings/0x409/configuration
# German:
mkdir strings/0x407
sleep 1
echo "Speicherstick-Emulation mit einer Image-Datei" >strings/0x407/configuration
echo 0xc0 >bmAttributes  # self-powered
echo 2 >MaxPower        # probably irrelevant when self-powered

cd ../../functions
mkdir mass_storage.usb0
sleep 1
cd mass_storage.usb0
echo 1 >stall
cd lun.0
echo /stick.img >file
echo 0 >removable
echo 1 >nofua
cd ../../..
ln -s functions/mass_storage.usb0 configs/c.1
sleep 1

# and now make it active:
ls /sys/class/udc >UDC

# to unbind it: echo "" >UDC; sleep 1; cd ..; rm -rf g1
# in emergency: echo 1 >functions/mass_storage.usb0/lun.0/forced_eject
Several of these settings are not absolutely necessary, also the sleep statements; they reflect only my desperate attempts to get it working. When I execute it, not having anything connected to the USB port yet, I see these lines in my dmesg:
Code:
Mass Storage Function, version: 2009/09/11
LUN: removable file: (no medium)
dwc3 fcc00000.usb: failed to enable ep0out
Why "removable"? Why "no medium"? What exactly "failed" w.r.t. endpoint 0?

Then I connect my PineTab2 to a PC's USB-2 port, which results in four more lines in the dmesg:
Code:
typec-extcon typec-extcon: extcon changed sdp=1 cdp=0 dcp=0 usb=0 usb_host=0 dp=0
typec-extcon typec-extcon: extcon changed sdp=1 cdp=0 dcp=0 usb=1 usb_host=0 dp=0
typec-extcon typec-extcon: extcon changed sdp=1 cdp=0 dcp=0 usb=0 usb_host=0 dp=0
typec-extcon typec-extcon: extcon changed sdp=1 cdp=0 dcp=0 usb=1 usb_host=0 dp=0
The dmesg of the PC is full of errors then:
Code:
usb 4-10: new high-speed USB device number 5 using xhci_hcd
usb 4-10: device descriptor read/64, error -71
usb 4-10: device descriptor read/64, error -71
usb 4-10: new high-speed USB device number 6 using xhci_hcd
usb 4-10: device descriptor read/64, error -71
usb 4-10: device descriptor read/64, error -71
usb usb4-port10: attempt power cycle
usb 4-10: new high-speed USB device number 7 using xhci_hcd
usb 4-10: Device not responding to setup address.
usb 4-10: Device not responding to setup address.
usb 4-10: device not accepting address 7, error -71
usb 4-10: new high-speed USB device number 8 using xhci_hcd
usb 4-10: Device not responding to setup address.
usb 4-10: Device not responding to setup address.
usb 4-10: device not accepting address 8, error -71
usb usb4-port10: unable to enumerate USB device
Obviously the gadget isn't functional. When I unplug the cable again, the PC's dmesg shows nothing, the PineTab2's just the ususal extcon stuff:
Code:
typec-extcon typec-extcon: extcon changed sdp=1 cdp=0 dcp=0 usb=0 usb_host=0 dp=0
typec-extcon typec-extcon: extcon changed sdp=0 cdp=0 dcp=0 usb=0 usb_host=0 dp=0
The worst thing I observe when I try to switch off the gadget again using echo "" >UDC; it seems to be some kind of crash:
Code:
dwc3 fcc00000.usb: wait for SETUP phase timed out
dwc3 fcc00000.usb: failed to set STALL on ep0out
------------[ cut here ]------------
WARNING: CPU: 0 PID: 5177 at dwc3_ep0_out_start+0xc8/0xe0
Modules linked in: aes_ce_ccm snd_seq_dummy snd_hrtimer snd_seq snd_seq_device zram aes_ce_blk aes_ce_cipher polyval_ce polyval_generic ghash_ce bes2600(C) sm4 sha2_ce sha256_arm64 sha1_ce snd_soc_simple_amplifier mac80211 st_accel_i2c st_sensors_i2c libarc4 ov5648 st_accel st_sensors v4l2_fwnode dw9714 snd_soc_simple_card v4l2_async cfg80211 snd_soc_simple_card_utils phy_rockchip_inno_csidphy snd_soc_rockchip_i2s_tdm snd_soc_rk817 rfkill snd_soc_hdmi_codec snd_soc_core snd_pcm_dmaengine snd_pcm snd_timer hantro_vpu snd v4l2_vp9 v4l2_jpeg v4l2_h264 rockchip_rga videobuf2_dma_contig soundcore v4l2_mem2mem videobuf2_dma_sg videobuf2_memops videobuf2_v4l2 videodev videobuf2_common mc joydev mousedev sch_fq_codel crypto_user loop fuse nfnetlink ip_tables x_tables ipv6 crc_ccitt dw_hdmi_cec panfrost dw_hdmi_i2s_audio drm_shmem_helper gpu_sched hid_multitouch
CPU: 0 UID: 0 PID: 5177 Comm: bash Tainted: G        C          6.15.2-danctnix2-1-pinetab2 #1 PREEMPT(full)  2075623fc0cc7105688baa1cc4ca060147351f05
[ 3357.684396] Tainted: [C]=CRAP
[ 3357.684413] Hardware name: Pine64 PineTab2 v2.0 (DT)
[ 3357.684434] pstate: 604000c9 (nZCv daIF +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[ 3357.684464] pc : dwc3_ep0_out_start+0xc8/0xe0
[ 3357.684504] lr : dwc3_ep0_out_start+0x4c/0xe0
[ 3357.684535] sp : ffff80008778ba70
[ 3357.684552] x29: ffff80008778ba70 x28: ffff00010088b900 x27: 0000000000000000
[ 3357.684605] x26: 0000000000000000 x25: 0000000000000000 x24: ffff00013c924840
[ 3357.684655] x23: ffff0001039f0810 x22: 0000000000000000 x21: ffff000100b76990
[ 3357.684705] x20: ffff000100b76880 x19: ffff00010434f200 x18: ffff8000842730b8
[ 3357.684755] x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000006
[ 3357.684804] x14: 0000000000000000 x13: ffffffffffff3f08 x12: ffff800081cd3d70
[ 3357.684854] x11: ffffffffffff3f04 x10: ffff800081cd3dc8 x9 : ffffffffffff3f00
[ 3357.684905] x8 : ffffffffffff3f0c x7 : ffff80008435d000 x6 : 0000000000000000
[ 3357.684954] x5 : ffff800081c4f000 x4 : 0000000000000000 x3 : 0000000000000000
[ 3357.685003] x2 : 0000000000000000 x1 : ffff00010088b900 x0 : 00000000ffffff92
[ 3357.685054] Call trace:
[ 3357.685073]  dwc3_ep0_out_start+0xc8/0xe0 (P)
[ 3357.685118]  dwc3_ep0_stall_and_restart+0xb4/0xe0
[ 3357.685158]  dwc3_ep0_reset_state+0x68/0xa0
[ 3357.685194]  dwc3_gadget_soft_disconnect+0x13c/0x1d0
[ 3357.685231]  dwc3_gadget_pullup+0x8c/0x138
[ 3357.685267]  usb_gadget_disconnect_locked+0x4c/0x170
[ 3357.685303]  gadget_unbind_driver+0x54/0x158
[ 3357.685334]  device_remove+0x54/0x90
[ 3357.685371]  device_release_driver_internal+0x1d4/0x240
[ 3357.685407]  driver_detach+0x54/0xc0
[ 3357.685440]  bus_remove_driver+0x78/0x108
[ 3357.685472]  driver_unregister+0x38/0x78
[ 3357.685506]  usb_gadget_unregister_driver+0x28/0x50
[ 3357.685538]  gadget_dev_desc_UDC_store+0xc0/0x150
[ 3357.685580]  configfs_write_iter+0xcc/0x130
[ 3357.685617]  vfs_write+0x24c/0x388
[ 3357.685658]  ksys_write+0x78/0x120
[ 3357.685695]  __arm64_sys_write+0x24/0x40
[ 3357.685734]  invoke_syscall+0x6c/0x100
[ 3357.685777]  el0_svc_common.constprop.0+0x48/0xf0
[ 3357.685817]  do_el0_svc+0x24/0x38
[ 3357.685846]  el0_svc+0x2c/0x138
[ 3357.685883]  el0t_64_sync_handler+0x10c/0x140
[ 3357.685916]  el0t_64_sync+0x160/0x168
[ 3357.685951] ---[ end trace 0000000000000000 ]---
Also, I can't delete the directory tree of my gadget in the configfs: not permitted.

Further observations:
* After this disaster I can plug a USB stick into this very socket using an OTG cable. It works, switches to host mode and mounts the stick.
* Also, charging the PineTab2 will work as always.
* The message "dwc3 fcc00000.usb: failed to enable ep0out" always shows up, also when I try other configurations, e.g. the script here near the bottom of the page (8814.usbconfigfs.sh.txt for an acm gadget):
https://e2e.ti.com/support/processors-gr...-am62x_dts

My questions:
a) Has anybody got a working USB gadget of any flavour (mass_storage, acm, ecm, ...) on the PineTab2? Please show me your script or tell me my mistake.
b) Could this be a broken kernel driver? Should I report an issue to danctnix?
c) Can you reproduce the problem on your own PineTab2 using my script?

Sorry for this terribly long write-up.

Any ideas?
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pinetab2 danctnix broken during upgrade - shell error acruhl 6 9,605 07-23-2025, 06:27 PM
Last Post: acruhl
  PineTab2 corrupted EMMC explit 6 5,171 09-13-2024, 09:09 AM
Last Post: Evergreen
  OS Options for Pinetab2 Evergreen 2 2,209 09-07-2024, 07:35 AM
Last Post: moobythegoldensock
  Cannot get into Pinetab2 joespodie 1 1,412 09-02-2024, 11:52 AM
Last Post: KC9UDX
Video Pinetab2 camera drivers cringeops 9 6,091 03-18-2024, 11:40 AM
Last Post: cringeops
  eMMC backup - pinetab2 pinetar.gz 0 1,723 01-07-2024, 09:08 PM
Last Post: pinetar.gz
  [PineTab2] Issues with Mobian Networking Cornelia Xaos 7 7,673 12-30-2023, 10:02 PM
Last Post: Cornelia Xaos
  Touch doesnt rotate on Sway in PineTab2 cgbarros 4 5,639 08-03-2023, 05:49 PM
Last Post: cgbarros
  Disabling PineTab2 physical keyboard and touchpad? johns 7 4,941 08-03-2023, 11:55 AM
Last Post: cgbarros
  Will sudo pacman -Syu brick the PineTab2? cgbarros 10 7,440 07-24-2023, 01:15 PM
Last Post: rqkeating

Forum Jump:


Users browsing this thread: 1 Guest(s)