1ecefae6dSMauro Carvalho Chehab====================
2ecefae6dSMauro Carvalho ChehabHow FunctionFS works
3ecefae6dSMauro Carvalho Chehab====================
4ecefae6dSMauro Carvalho Chehab
5d2f4831eSPaul CercueilOverview
6d2f4831eSPaul Cercueil========
7d2f4831eSPaul Cercueil
8ecefae6dSMauro Carvalho ChehabFrom kernel point of view it is just a composite function with some
9ecefae6dSMauro Carvalho Chehabunique behaviour.  It may be added to an USB configuration only after
10ecefae6dSMauro Carvalho Chehabthe user space driver has registered by writing descriptors and
11ecefae6dSMauro Carvalho Chehabstrings (the user space program has to provide the same information
12ecefae6dSMauro Carvalho Chehabthat kernel level composite functions provide when they are added to
13ecefae6dSMauro Carvalho Chehabthe configuration).
14ecefae6dSMauro Carvalho Chehab
15ecefae6dSMauro Carvalho ChehabThis in particular means that the composite initialisation functions
16ecefae6dSMauro Carvalho Chehabmay not be in init section (ie. may not use the __init tag).
17ecefae6dSMauro Carvalho Chehab
18ecefae6dSMauro Carvalho ChehabFrom user space point of view it is a file system which when
19ecefae6dSMauro Carvalho Chehabmounted provides an "ep0" file.  User space driver need to
20ecefae6dSMauro Carvalho Chehabwrite descriptors and strings to that file.  It does not need
21ecefae6dSMauro Carvalho Chehabto worry about endpoints, interfaces or strings numbers but
22ecefae6dSMauro Carvalho Chehabsimply provide descriptors such as if the function was the
23ecefae6dSMauro Carvalho Chehabonly one (endpoints and strings numbers starting from one and
24ecefae6dSMauro Carvalho Chehabinterface numbers starting from zero).  The FunctionFS changes
25ecefae6dSMauro Carvalho Chehabthem as needed also handling situation when numbers differ in
26ecefae6dSMauro Carvalho Chehabdifferent configurations.
27ecefae6dSMauro Carvalho Chehab
28*c26cee81SDavid SandsFor more information about FunctionFS descriptors see :doc:`functionfs-desc`
29*c26cee81SDavid Sands
30ecefae6dSMauro Carvalho ChehabWhen descriptors and strings are written "ep#" files appear
31ecefae6dSMauro Carvalho Chehab(one for each declared endpoint) which handle communication on
32ecefae6dSMauro Carvalho Chehaba single endpoint.  Again, FunctionFS takes care of the real
33ecefae6dSMauro Carvalho Chehabnumbers and changing of the configuration (which means that
34ecefae6dSMauro Carvalho Chehab"ep1" file may be really mapped to (say) endpoint 3 (and when
35ecefae6dSMauro Carvalho Chehabconfiguration changes to (say) endpoint 2)).  "ep0" is used
36ecefae6dSMauro Carvalho Chehabfor receiving events and handling setup requests.
37ecefae6dSMauro Carvalho Chehab
38ecefae6dSMauro Carvalho ChehabWhen all files are closed the function disables itself.
39ecefae6dSMauro Carvalho Chehab
40ecefae6dSMauro Carvalho ChehabWhat I also want to mention is that the FunctionFS is designed in such
41ecefae6dSMauro Carvalho Chehaba way that it is possible to mount it several times so in the end
42ecefae6dSMauro Carvalho Chehaba gadget could use several FunctionFS functions. The idea is that
43ecefae6dSMauro Carvalho Chehabeach FunctionFS instance is identified by the device name used
44ecefae6dSMauro Carvalho Chehabwhen mounting.
45ecefae6dSMauro Carvalho Chehab
46ecefae6dSMauro Carvalho ChehabOne can imagine a gadget that has an Ethernet, MTP and HID interfaces
47ecefae6dSMauro Carvalho Chehabwhere the last two are implemented via FunctionFS.  On user space
48ecefae6dSMauro Carvalho Chehablevel it would look like this::
49ecefae6dSMauro Carvalho Chehab
50ecefae6dSMauro Carvalho Chehab  $ insmod g_ffs.ko idVendor=<ID> iSerialNumber=<string> functions=mtp,hid
51ecefae6dSMauro Carvalho Chehab  $ mkdir /dev/ffs-mtp && mount -t functionfs mtp /dev/ffs-mtp
52ecefae6dSMauro Carvalho Chehab  $ ( cd /dev/ffs-mtp && mtp-daemon ) &
53ecefae6dSMauro Carvalho Chehab  $ mkdir /dev/ffs-hid && mount -t functionfs hid /dev/ffs-hid
54ecefae6dSMauro Carvalho Chehab  $ ( cd /dev/ffs-hid && hid-daemon ) &
55ecefae6dSMauro Carvalho Chehab
56ecefae6dSMauro Carvalho ChehabOn kernel level the gadget checks ffs_data->dev_name to identify
57f13039ceSRandy Dunlapwhether its FunctionFS is designed for MTP ("mtp") or HID ("hid").
58ecefae6dSMauro Carvalho Chehab
59ecefae6dSMauro Carvalho ChehabIf no "functions" module parameters is supplied, the driver accepts
60ecefae6dSMauro Carvalho Chehabjust one function with any name.
61ecefae6dSMauro Carvalho Chehab
62ecefae6dSMauro Carvalho ChehabWhen "functions" module parameter is supplied, only functions
63ecefae6dSMauro Carvalho Chehabwith listed names are accepted. In particular, if the "functions"
64ecefae6dSMauro Carvalho Chehabparameter's value is just a one-element list, then the behaviour
65ecefae6dSMauro Carvalho Chehabis similar to when there is no "functions" at all; however,
66ecefae6dSMauro Carvalho Chehabonly a function with the specified name is accepted.
67ecefae6dSMauro Carvalho Chehab
68ecefae6dSMauro Carvalho ChehabThe gadget is registered only after all the declared function
69ecefae6dSMauro Carvalho Chehabfilesystems have been mounted and USB descriptors of all functions
70ecefae6dSMauro Carvalho Chehabhave been written to their ep0's.
71ecefae6dSMauro Carvalho Chehab
72ecefae6dSMauro Carvalho ChehabConversely, the gadget is unregistered after the first USB function
73ecefae6dSMauro Carvalho Chehabcloses its endpoints.
74d2f4831eSPaul Cercueil
75d2f4831eSPaul CercueilDMABUF interface
76d2f4831eSPaul Cercueil================
77d2f4831eSPaul Cercueil
78d2f4831eSPaul CercueilFunctionFS additionally supports a DMABUF based interface, where the
79d2f4831eSPaul Cercueiluserspace can attach DMABUF objects (externally created) to an endpoint,
80d2f4831eSPaul Cercueiland subsequently use them for data transfers.
81d2f4831eSPaul Cercueil
82d2f4831eSPaul CercueilA userspace application can then use this interface to share DMABUF
83d2f4831eSPaul Cercueilobjects between several interfaces, allowing it to transfer data in a
84d2f4831eSPaul Cercueilzero-copy fashion, for instance between IIO and the USB stack.
85d2f4831eSPaul Cercueil
86d2f4831eSPaul CercueilAs part of this interface, three new IOCTLs have been added. These three
87d2f4831eSPaul CercueilIOCTLs have to be performed on a data endpoint (ie. not ep0). They are:
88d2f4831eSPaul Cercueil
89d2f4831eSPaul Cercueil  ``FUNCTIONFS_DMABUF_ATTACH(int)``
90d2f4831eSPaul Cercueil    Attach the DMABUF object, identified by its file descriptor, to the
91d2f4831eSPaul Cercueil    data endpoint. Returns zero on success, and a negative errno value
92d2f4831eSPaul Cercueil    on error.
93d2f4831eSPaul Cercueil
94d2f4831eSPaul Cercueil  ``FUNCTIONFS_DMABUF_DETACH(int)``
95d2f4831eSPaul Cercueil    Detach the given DMABUF object, identified by its file descriptor,
96d2f4831eSPaul Cercueil    from the data endpoint. Returns zero on success, and a negative
97d2f4831eSPaul Cercueil    errno value on error. Note that closing the endpoint's file
98d2f4831eSPaul Cercueil    descriptor will automatically detach all attached DMABUFs.
99d2f4831eSPaul Cercueil
100d2f4831eSPaul Cercueil  ``FUNCTIONFS_DMABUF_TRANSFER(struct usb_ffs_dmabuf_transfer_req *)``
101d2f4831eSPaul Cercueil    Enqueue the previously attached DMABUF to the transfer queue.
102d2f4831eSPaul Cercueil    The argument is a structure that packs the DMABUF's file descriptor,
103d2f4831eSPaul Cercueil    the size in bytes to transfer (which should generally correspond to
104d2f4831eSPaul Cercueil    the size of the DMABUF), and a 'flags' field which is unused
105d2f4831eSPaul Cercueil    for now. Returns zero on success, and a negative errno value on
106d2f4831eSPaul Cercueil    error.
107