1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright 2024 Google LLC
4 */
5 #ifndef __ANDROID_ACCESSORY_H
6 #define __ANDROID_ACCESSORY_H
7
8 #include <linux/usb/composite.h>
9 #include <linux/usb/ch9.h>
10
11 #ifdef CONFIG_ANDROID_USB_CONFIGFS_F_ACC
12
13 /**
14 * android_acc_req_match_composite - used to check if the android accessory
15 * driver can handle a usb_ctrlrequest
16 * @cdev - the usb_composite_dev instance associated with the incoming ctrl
17 * request
18 * @ctrl - a usb_ctrlrequest for the accessory driver to check
19 *
20 * This function should be called in composite_setup() after other req_match
21 * checks have failed and the usb_ctrlrequest is still unhandled.
22 *
23 * The reason this must be implemented instead of the standard req_match
24 * interface is that the accessory function does not get bound to a config
25 * by userspace until a connected device sends the ACCESSORY_START control
26 * request, and therefore the composite driver does not know about f_accessory
27 * yet we need to check for the control requests.
28 *
29 * Returns: true if the accessory driver can handle the request, false if not
30 */
31 bool android_acc_req_match_composite(struct usb_composite_dev *cdev,
32 const struct usb_ctrlrequest *ctrl);
33
34 /**
35 * android_acc_setup_composite - function for the f_accessory driver to handle
36 * usb_ctrlrequests
37 * @cdev - the usb_composite_dev instance associated with the incoming ctrl
38 * request.
39 * @ctrl - a usb_ctrlrequest to be handled by the f_accessory driver.
40 *
41 * This function should be called in composite_setup() after successfully
42 * checking for ctrl request support in android_acc_req_match_composite().
43 *
44 * The reason this additional api must be defined is due to the fact that
45 * userspace does not bind the f_accessory instance to a gadget config until
46 * after receiving an ACCESSORY_START control request from a connected
47 * accessory device, and therefore we have a circular dependency. The addition
48 * of this allows compatibility with the existing Android userspace, but is
49 * not ideal and should be refactored in the future.
50 *
51 * Returns: Negative error value upon failure, >=0 upon successful handdling
52 * of the usb_ctrlrequest aligned with the standard composite function driver
53 * setup() api.
54 */
55 int android_acc_setup_composite(struct usb_composite_dev *cdev,
56 const struct usb_ctrlrequest *ctrl);
57
58 /**
59 * android_acc_disconnect - used to cleanup the accessory function
60 * and update the connection state on device disconnection.
61 *
62 * This should be called in the composite driver's __composite_disconnect()
63 * path to notify the accessory function of device disconnect. This is
64 * required because the accessory function exists outside of a gadget config
65 * and therefore the composite driver's standard cleanup paths may not apply.
66 */
67 void android_acc_disconnect(void);
68
69 #else
70
android_acc_req_match_composite(struct usb_composite_dev * cdev,const struct usb_ctrlrequest * ctrl)71 static inline bool android_acc_req_match_composite(struct usb_composite_dev *cdev,
72 const struct usb_ctrlrequest *ctrl)
73 {
74 return false;
75 }
76
android_acc_setup_composite(struct usb_composite_dev * cdev,const struct usb_ctrlrequest * ctrl)77 static inline int android_acc_setup_composite(struct usb_composite_dev *cdev,
78 const struct usb_ctrlrequest *ctrl)
79 {
80 return 0;
81 }
82
android_acc_disconnect(void)83 static inline void android_acc_disconnect(void)
84 {
85 }
86 #endif /* CONFIG_ANDROID_USB_CONFIGFS_F_ACC */
87 #endif /* __ANDROID_ACCESSORY_H */
88