usb_notify.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * usb notify header
  4. *
  5. * Copyright (C) 2011-2023 Samsung, Inc.
  6. * Author: Dongrak Shin <[email protected]>
  7. *
  8. */
  9. /* usb notify layer v4.0 */
  10. #ifndef __LINUX_USB_NOTIFY_H__
  11. #define __LINUX_USB_NOTIFY_H__
  12. #include <linux/notifier.h>
  13. #include <linux/host_notify.h>
  14. #include <linux/external_notify.h>
  15. #include <linux/usblog_proc_notify.h>
  16. #if defined(CONFIG_USB_HW_PARAM)
  17. #include <linux/usb_hw_param.h>
  18. #endif
  19. #include <linux/usb.h>
  20. enum otg_notify_events {
  21. NOTIFY_EVENT_NONE,
  22. NOTIFY_EVENT_VBUS,
  23. NOTIFY_EVENT_HOST,
  24. NOTIFY_EVENT_CHARGER,
  25. NOTIFY_EVENT_SMARTDOCK_TA,
  26. NOTIFY_EVENT_SMARTDOCK_USB,
  27. NOTIFY_EVENT_AUDIODOCK,
  28. NOTIFY_EVENT_LANHUB,
  29. NOTIFY_EVENT_LANHUB_TA,
  30. NOTIFY_EVENT_MMDOCK,
  31. NOTIFY_EVENT_HMT,
  32. NOTIFY_EVENT_GAMEPAD,
  33. NOTIFY_EVENT_POGO,
  34. NOTIFY_EVENT_HOST_RELOAD,
  35. NOTIFY_EVENT_DRIVE_VBUS,
  36. NOTIFY_EVENT_ALL_DISABLE,
  37. NOTIFY_EVENT_HOST_DISABLE,
  38. NOTIFY_EVENT_CLIENT_DISABLE,
  39. NOTIFY_EVENT_MDM_ON_OFF,
  40. NOTIFY_EVENT_MDM_ON_OFF_FOR_ID,
  41. NOTIFY_EVENT_MDM_ON_OFF_FOR_SERIAL,
  42. NOTIFY_EVENT_OVERCURRENT,
  43. NOTIFY_EVENT_SMSC_OVC,
  44. NOTIFY_EVENT_SMTD_EXT_CURRENT,
  45. NOTIFY_EVENT_MMD_EXT_CURRENT,
  46. NOTIFY_EVENT_HMD_EXT_CURRENT,
  47. NOTIFY_EVENT_DEVICE_CONNECT,
  48. NOTIFY_EVENT_GAMEPAD_CONNECT,
  49. NOTIFY_EVENT_LANHUB_CONNECT,
  50. NOTIFY_EVENT_POWER_SOURCE,
  51. NOTIFY_EVENT_PD_CONTRACT,
  52. NOTIFY_EVENT_VBUS_RESET,
  53. NOTIFY_EVENT_RESERVE_BOOSTER,
  54. NOTIFY_EVENT_USB_CABLE,
  55. NOTIFY_EVENT_USBD_SUSPENDED,
  56. NOTIFY_EVENT_USBD_UNCONFIGURED,
  57. NOTIFY_EVENT_USBD_CONFIGURED,
  58. NOTIFY_EVENT_VBUSPOWER,
  59. NOTIFY_EVENT_DR_SWAP,
  60. NOTIFY_EVENT_REVERSE_BYPASS_DEVICE_CONNECT,
  61. NOTIFY_EVENT_REVERSE_BYPASS_DEVICE_ATTACH,
  62. NOTIFY_EVENT_VIRTUAL,
  63. };
  64. #define VIRT_EVENT(a) (a+NOTIFY_EVENT_VIRTUAL)
  65. #define PHY_EVENT(a) (a%NOTIFY_EVENT_VIRTUAL)
  66. #define IS_VIRTUAL(a) (a >= NOTIFY_EVENT_VIRTUAL ? 1 : 0)
  67. enum otg_notify_event_status {
  68. NOTIFY_EVENT_DISABLED,
  69. NOTIFY_EVENT_DISABLING,
  70. NOTIFY_EVENT_ENABLED,
  71. NOTIFY_EVENT_ENABLING,
  72. NOTIFY_EVENT_BLOCKED,
  73. NOTIFY_EVENT_BLOCKING,
  74. };
  75. enum otg_notify_evt_type {
  76. NOTIFY_EVENT_EXTRA = (1 << 0),
  77. NOTIFY_EVENT_STATE = (1 << 1),
  78. NOTIFY_EVENT_DELAY = (1 << 2),
  79. NOTIFY_EVENT_NEED_VBUSDRIVE = (1 << 3),
  80. NOTIFY_EVENT_NOBLOCKING = (1 << 4),
  81. NOTIFY_EVENT_NOSAVE = (1 << 5),
  82. NOTIFY_EVENT_NEED_HOST = (1 << 6),
  83. NOTIFY_EVENT_NEED_CLIENT = (1 << 7),
  84. };
  85. enum otg_notify_block_type {
  86. NOTIFY_BLOCK_TYPE_NONE = 0,
  87. NOTIFY_BLOCK_TYPE_HOST = (1 << 0),
  88. NOTIFY_BLOCK_TYPE_CLIENT = (1 << 1),
  89. NOTIFY_BLOCK_TYPE_ALL = (1 << 0 | 1 << 1),
  90. };
  91. enum otg_notify_mdm_type {
  92. NOTIFY_MDM_TYPE_OFF,
  93. NOTIFY_MDM_TYPE_ON,
  94. };
  95. enum otg_notify_gpio {
  96. NOTIFY_VBUS,
  97. NOTIFY_REDRIVER,
  98. };
  99. enum otg_op_pos {
  100. NOTIFY_OP_OFF,
  101. NOTIFY_OP_POST,
  102. NOTIFY_OP_PRE,
  103. };
  104. enum ovc_check_value {
  105. HNOTIFY_LOW,
  106. HNOTIFY_HIGH,
  107. HNOTIFY_INITIAL,
  108. };
  109. enum otg_notify_power_role {
  110. HNOTIFY_SINK,
  111. HNOTIFY_SOURCE,
  112. };
  113. enum otg_notify_data_role {
  114. HNOTIFY_UFP,
  115. HNOTIFY_DFP,
  116. };
  117. enum usb_restrict_type {
  118. USB_SECURE_RESTRICTED,
  119. USB_TIME_SECURE_RESTRICTED,
  120. USB_SECURE_RELEASE,
  121. };
  122. enum usb_restrict_group {
  123. USB_GROUP_AUDIO,
  124. USB_GROUP_OTEHR,
  125. USB_GROUP_MAX,
  126. };
  127. enum usb_certi_type {
  128. USB_CERTI_UNSUPPORT_ACCESSORY,
  129. USB_CERTI_NO_RESPONSE,
  130. USB_CERTI_HUB_DEPTH_EXCEED,
  131. USB_CERTI_HUB_POWER_EXCEED,
  132. USB_CERTI_HOST_RESOURCE_EXCEED,
  133. USB_CERTI_WARM_RESET,
  134. };
  135. enum usb_err_type {
  136. USB_ERR_ABNORMAL_RESET,
  137. };
  138. enum usb_itracker_type {
  139. NOTIFY_USB_CC_REPEAT,
  140. };
  141. enum usb_current_state {
  142. NOTIFY_USB_UNCONFIGURED,
  143. NOTIFY_USB_SUSPENDED,
  144. NOTIFY_USB_CONFIGURED,
  145. };
  146. enum usb_allowlist_state {
  147. NOTIFY_MDM_NONE = 0,
  148. NOTIFY_MDM_SERIAL,
  149. NOTIFY_MDM_ID,
  150. NOTIFY_MDM_ID_AND_SERIAL,
  151. };
  152. enum usb_request_action_type {
  153. USB_REQUEST_NOTHING,
  154. USB_REQUEST_DUMPSTATE,
  155. };
  156. enum otg_notify_reverse_bypass_status {
  157. NOTIFY_EVENT_REVERSE_BYPASS_OFF,
  158. NOTIFY_EVENT_REVERSE_BYPASS_PREPARE,
  159. NOTIFY_EVENT_REVERSE_BYPASS_ON,
  160. };
  161. enum otg_notify_illegal_type {
  162. NOTIFY_EVENT_AUDIO_DESCRIPTOR,
  163. NOTIFY_EVENT_SECURE_DISCONNECTION,
  164. };
  165. enum usb_lock_state {
  166. USB_NOTIFY_UNLOCK = 0,
  167. USB_NOTIFY_LOCK_USB_WORK,
  168. USB_NOTIFY_LOCK_USB_RESTRICT,
  169. USB_NOTIFY_INIT_STATE = 3,
  170. };
  171. enum usb_check_allowlist_result {
  172. USB_NOTIFY_NOLIST = 0,
  173. USB_NOTIFY_ALLOWLOST,
  174. USB_NOTIFY_NORESTRICT,
  175. };
  176. struct otg_notify {
  177. int vbus_detect_gpio;
  178. int redriver_en_gpio;
  179. int is_wakelock;
  180. int is_host_wakelock; /*unused field*/
  181. int unsupport_host;
  182. int smsc_ovc_poll_sec;
  183. int auto_drive_vbus;
  184. int booting_delay_sec;
  185. int disable_control;
  186. int device_check_sec;
  187. int pre_peri_delay_us;
  188. int booting_delay_sync_usb;
  189. int (*pre_gpio)(int gpio, int use);
  190. int (*post_gpio)(int gpio, int use);
  191. int (*vbus_drive)(bool enable);
  192. int (*reverse_bypass_drive)(int mode);
  193. int (*get_support_reverse_bypass_en)(void *data);
  194. int (*set_host)(bool enable);
  195. int (*set_peripheral)(bool enable);
  196. int (*set_charger)(bool enable);
  197. int (*post_vbus_detect)(bool on);
  198. int (*set_lanhubta)(int enable);
  199. int (*set_battcall)(int event, int enable);
  200. int (*set_chg_current)(int state);
  201. void (*set_ldo_onoff)(void *data, unsigned int onoff);
  202. int (*get_gadget_speed)(void);
  203. int (*is_skip_list)(int index);
  204. int (*usb_maximum_speed)(int speed);
  205. void *o_data;
  206. void *u_notify;
  207. };
  208. struct otg_booster {
  209. char *name;
  210. int (*booster)(bool enable);
  211. };
  212. #if IS_ENABLED(CONFIG_USB_NOTIFY_LAYER)
  213. extern const char *event_string(enum otg_notify_events event);
  214. extern const char *status_string(enum otg_notify_event_status status);
  215. extern void send_usb_mdm_uevent(void);
  216. extern void send_usb_certi_uevent(int usb_certi);
  217. extern void send_usb_err_uevent(int usb_certi, int mode);
  218. extern void send_usb_itracker_uevent(int err_type);
  219. extern int usb_check_whitelist_for_id(struct usb_device *dev);
  220. extern int usb_check_whitelist_for_serial(struct usb_device *dev);
  221. extern int usb_check_whitelist_for_mdm(struct usb_device *dev);
  222. extern int usb_check_whitelist_enable_state(void);
  223. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  224. extern int usb_check_allowlist_for_lockscreen_enabled_id(struct usb_device *dev);
  225. #endif
  226. extern int usb_otg_restart_accessory(struct usb_device *dev);
  227. extern void send_otg_notify(struct otg_notify *n,
  228. unsigned long event, int enable);
  229. extern int get_typec_status(struct otg_notify *n, int event);
  230. extern struct otg_booster *find_get_booster(struct otg_notify *n);
  231. extern int register_booster(struct otg_notify *n, struct otg_booster *b);
  232. extern int register_ovc_func(struct otg_notify *n,
  233. int (*check_state)(void *), void *data);
  234. extern int get_booster(struct otg_notify *n);
  235. extern int get_usb_mode(struct otg_notify *n);
  236. extern unsigned long get_cable_type(struct otg_notify *n);
  237. extern int is_usb_host(struct otg_notify *n);
  238. extern bool is_blocked(struct otg_notify *n, int type);
  239. extern bool is_snkdfp_usb_device_connected(struct otg_notify *n);
  240. extern int get_con_dev_max_speed(struct otg_notify *n);
  241. extern void set_con_dev_max_speed
  242. (struct otg_notify *n, int speed);
  243. extern void set_con_dev_hub(struct otg_notify *n, int speed, int conn);
  244. extern void set_request_action(struct otg_notify *n, unsigned int request_action);
  245. extern int is_known_usbaudio(struct usb_device *dev);
  246. extern void set_usb_audio_cardnum(int card_num, int bundle, int attach);
  247. extern void send_usb_audio_uevent(struct usb_device *dev,
  248. int cardnum, int attach);
  249. extern int send_usb_notify_uevent
  250. (struct otg_notify *n, char *envp_ext[]);
  251. extern int check_new_device_added(struct usb_device *udev);
  252. extern int set_lpm_charging_type_done(struct otg_notify *n,
  253. unsigned int state);
  254. extern int detect_illegal_condition(int type);
  255. extern int check_usbaudio(struct usb_device *dev);
  256. extern int check_usbgroup(struct usb_device *dev);
  257. extern int is_usbhub(struct usb_device *dev);
  258. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  259. extern int disconnect_unauthorized_device(struct usb_device *dev);
  260. extern bool check_usb_restrict_lock_state(struct otg_notify *n);
  261. #endif
  262. extern void send_usb_restrict_uevent(int usb_restrict);
  263. #if defined(CONFIG_USB_HW_PARAM)
  264. extern unsigned long long *get_hw_param(struct otg_notify *n,
  265. enum usb_hw_param index);
  266. extern int inc_hw_param(struct otg_notify *n,
  267. enum usb_hw_param index);
  268. extern int inc_hw_param_host(struct host_notify_dev *dev,
  269. enum usb_hw_param index);
  270. extern int register_hw_param_manager(struct otg_notify *n,
  271. unsigned long (*fptr)(int));
  272. #endif
  273. extern void *get_notify_data(struct otg_notify *n);
  274. extern void set_notify_data(struct otg_notify *n, void *data);
  275. extern struct otg_notify *get_otg_notify(void);
  276. extern void enable_usb_notify(void);
  277. extern int set_otg_notify(struct otg_notify *n);
  278. extern void put_otg_notify(struct otg_notify *n);
  279. #else
  280. static inline const char *event_string(enum otg_notify_events event)
  281. {return NULL; }
  282. static inline const char *status_string(enum otg_notify_event_status status)
  283. {return NULL; }
  284. static inline void send_usb_mdm_uevent(void) {}
  285. static inline void send_usb_certi_uevent(int usb_certi) {}
  286. static inline void send_usb_err_uevent(int usb_certi, int mode) {}
  287. static inline void send_usb_itracker_uevent(int err_type) {}
  288. static inline int usb_check_whitelist_for_mdm(struct usb_device *dev)
  289. {return 0; }
  290. static inline int usb_check_whitelist_for_id(struct usb_device *dev)
  291. {return 0; }
  292. static inline int usb_check_whitelist_for_serial(struct usb_device *dev)
  293. {return 0; }
  294. extern inline int usb_check_whitelist_enable_state(void)
  295. {return 0; }
  296. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  297. extern inline int usb_check_allowlist_for_lockscreen_enabled_id(struct usb_device *dev)
  298. {return 0; }
  299. #endif
  300. static inline int usb_otg_restart_accessory(struct usb_device *dev)
  301. {return 0; }
  302. static inline void send_otg_notify(struct otg_notify *n,
  303. unsigned long event, int enable) { }
  304. static inline int get_typec_status(struct otg_notify *n, int event) {return 0; }
  305. static inline struct otg_booster *find_get_booster(struct otg_notify *n)
  306. {return NULL; }
  307. static inline int register_booster(struct otg_notify *n,
  308. struct otg_booster *b) {return 0; }
  309. static inline int register_ovc_func(struct otg_notify *n,
  310. int (*check_state)(void *), void *data) {return 0; }
  311. static inline int get_booster(struct otg_notify *n) {return 0; }
  312. static inline int get_usb_mode(struct otg_notify *n) {return 0; }
  313. static inline unsigned long get_cable_type(struct otg_notify *n) {return 0; }
  314. static inline int is_usb_host(struct otg_notify *n) {return 0; }
  315. static inline bool is_blocked(struct otg_notify *n, int type) {return false; }
  316. static inline bool is_snkdfp_usb_device_connected(struct otg_notify *n)
  317. {return false; }
  318. static inline int get_con_dev_max_speed(struct otg_notify *n)
  319. {return 0; }
  320. static inline void set_con_dev_max_speed
  321. (struct otg_notify *n, int speed) {}
  322. static inline void set_con_dev_hub(struct otg_notify *n, int speed, int conn) {}
  323. static inline void set_request_action
  324. (struct otg_notify *n, unsigned int request_action) {}
  325. static inline int is_known_usbaudio(struct usb_device *dev) {return 0; }
  326. static inline void set_usb_audio_cardnum(int card_num,
  327. int bundle, int attach) {}
  328. static inline void send_usb_audio_uevent(struct usb_device *dev,
  329. int cardnum, int attach) {}
  330. static inline int send_usb_notify_uevent
  331. (struct otg_notify *n, char *envp_ext[]) {return 0; }
  332. static inline int check_new_device_added(struct usb_device *udev) {return 0; }
  333. static inline int set_lpm_charging_type_done(struct otg_notify *n,
  334. unsigned int state) {return 0; }
  335. static inline int detect_illegal_condition(int type) {return 0; }
  336. static inline int check_usbaudio(struct usb_device *dev) {return 0; }
  337. static inline int check_usbgroup(struct usb_device *dev) {return 0; }
  338. static inline int is_usbhub(struct usb_device *dev) {return 0; }
  339. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  340. static inline int disconnect_unauthorized_device(struct usb_device *dev) {return 0; }
  341. static inline bool check_usb_restrict_lock_state(struct otg_notify *n) {return false; }
  342. #endif
  343. static inline void send_usb_restrict_uevent(int usb_restrict) {}
  344. #if defined(CONFIG_USB_HW_PARAM)
  345. static inline unsigned long long *get_hw_param(struct otg_notify *n,
  346. enum usb_hw_param index) {return NULL; }
  347. static inline int inc_hw_param(struct otg_notify *n,
  348. enum usb_hw_param index) {return 0; }
  349. static inline int inc_hw_param_host(struct host_notify_dev *dev,
  350. enum usb_hw_param index) {return 0; }
  351. static inline int register_hw_param_manager(struct otg_notify *n,
  352. unsigned long (*fptr)(int)) {return 0; }
  353. #endif
  354. static inline void *get_notify_data(struct otg_notify *n) {return NULL; }
  355. static inline void set_notify_data(struct otg_notify *n, void *data) {}
  356. static inline struct otg_notify *get_otg_notify(void) {return NULL; }
  357. static inline void enable_usb_notify(void) {}
  358. static inline int set_otg_notify(struct otg_notify *n) {return 0; }
  359. static inline void put_otg_notify(struct otg_notify *n) {}
  360. #endif
  361. #define unl_info(fmt, ...) \
  362. ({ \
  363. pr_info(fmt, ##__VA_ARGS__); \
  364. printk_usb(NOTIFY_PRINTK_USB_NORMAL, fmt, ##__VA_ARGS__); \
  365. })
  366. #define unl_err(fmt, ...) \
  367. ({ \
  368. pr_err(fmt, ##__VA_ARGS__); \
  369. printk_usb(NOTIFY_PRINTK_USB_NORMAL, fmt, ##__VA_ARGS__); \
  370. })
  371. #endif /* __LINUX_USB_NOTIFY_H__ */