usb_notify_sysfs.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * Copyright (C) 2015-2023 Samsung, Inc.
  5. * Author: Dongrak Shin <[email protected]>
  6. *
  7. */
  8. /* usb notify layer v4.0 */
  9. #define pr_fmt(fmt) "usb_notify: " fmt
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/init.h>
  13. #include <linux/device.h>
  14. #include <linux/slab.h>
  15. #include <linux/fs.h>
  16. #include <linux/err.h>
  17. #include <linux/usb.h>
  18. #include <linux/usb/ch9.h>
  19. #include <linux/usb_notify.h>
  20. #include <linux/string.h>
  21. #include "usb_notify_sysfs.h"
  22. #define MAX_STRING_LEN 20
  23. #if defined(CONFIG_USB_HW_PARAM)
  24. const char
  25. usb_hw_param_print[USB_CCIC_HW_PARAM_MAX][MAX_HWPARAM_STRING] = {
  26. {"CC_WATER"},
  27. {"CC_DRY"},
  28. {"CC_I2C"},
  29. {"CC_OVC"},
  30. {"CC_OTG"},
  31. {"CC_DP"},
  32. {"CC_VR"},
  33. {"H_SUPER"},
  34. {"H_HIGH"},
  35. {"H_FULL"},
  36. {"H_LOW"},
  37. {"C_SUPER"},
  38. {"C_HIGH"},
  39. {"H_AUDIO"},
  40. {"H_AUDSS"},
  41. {"H_RBYPS"},
  42. {"H_COMM"},
  43. {"H_HID"},
  44. {"H_PHYSIC"},
  45. {"H_IMAGE"},
  46. {"H_PRINTER"},
  47. {"H_STORAGE"},
  48. {"H_STO_S"},
  49. {"H_STO_H"},
  50. {"H_STO_F"},
  51. {"H_HUB"},
  52. {"H_CDC"},
  53. {"H_CSCID"},
  54. {"H_CONTENT"},
  55. {"H_VIDEO"},
  56. {"H_WIRE"},
  57. {"H_MISC"},
  58. {"H_APP"},
  59. {"H_VENDOR"},
  60. {"CC_DEX"},
  61. {"CC_WTIME"},
  62. {"CC_WVBUS"},
  63. {"CC_WVTIME"},
  64. {"CC_WLVBS"},
  65. {"CC_WLVTM"},
  66. {"CC_CSHORT"},
  67. {"CC_SVSHT"},
  68. {"CC_SGSHT"},
  69. {"M_AFCNAK"},
  70. {"M_AFCERR"},
  71. {"M_DCDTMO"},
  72. {"F_CNT"},
  73. {"CC_KILLER"},
  74. {"CC_FWERR"},
  75. {"M_B12RS"},
  76. {"CC_PRS"},
  77. {"CC_DRS"},
  78. {"C_ARP"},
  79. {"CC_UMVS"},
  80. {"CC_STUCK"},
  81. {"H_SB"},
  82. {"H_OAD"},
  83. {"CC_VER"},
  84. };
  85. #endif /* CONFIG_USB_HW_PARAM */
  86. struct notify_data {
  87. struct class *usb_notify_class;
  88. atomic_t device_count;
  89. };
  90. static struct notify_data usb_notify_data;
  91. static int is_valid_cmd(char *cur_cmd, char *prev_cmd)
  92. {
  93. unl_info("%s : current state=%s, previous state=%s\n",
  94. __func__, cur_cmd, prev_cmd);
  95. if (!strcmp(cur_cmd, "ON") ||
  96. !strncmp(cur_cmd, "ON_ALL_", 7)) {
  97. if (!strcmp(prev_cmd, "ON") ||
  98. !strncmp(prev_cmd, "ON_ALL_", 7)) {
  99. goto ignore;
  100. } else if (!strncmp(prev_cmd, "ON_HOST_", 8)) {
  101. goto all;
  102. } else if (!strncmp(prev_cmd, "ON_CLIENT_", 10)) {
  103. goto all;
  104. } else if (!strcmp(prev_cmd, "OFF")) {
  105. goto all;
  106. } else {
  107. goto invalid;
  108. }
  109. } else if (!strcmp(cur_cmd, "OFF")) {
  110. if (!strcmp(prev_cmd, "ON") ||
  111. !strncmp(prev_cmd, "ON_ALL_", 7)) {
  112. goto off;
  113. } else if (!strncmp(prev_cmd, "ON_HOST_", 8)) {
  114. goto off;
  115. } else if (!strncmp(prev_cmd, "ON_CLIENT_", 10)) {
  116. goto off;
  117. } else if (!strcmp(prev_cmd, "OFF")) {
  118. goto ignore;
  119. } else {
  120. goto invalid;
  121. }
  122. } else if (!strncmp(cur_cmd, "ON_HOST_", 8)) {
  123. if (!strcmp(prev_cmd, "ON") ||
  124. !strncmp(prev_cmd, "ON_ALL_", 7)) {
  125. goto host;
  126. } else if (!strncmp(prev_cmd, "ON_HOST_", 8)) {
  127. goto ignore;
  128. } else if (!strncmp(prev_cmd, "ON_CLIENT_", 10)) {
  129. goto host;
  130. } else if (!strcmp(prev_cmd, "OFF")) {
  131. goto host;
  132. } else {
  133. goto invalid;
  134. }
  135. } else if (!strncmp(cur_cmd, "ON_CLIENT_", 10)) {
  136. if (!strcmp(prev_cmd, "ON") ||
  137. !strncmp(prev_cmd, "ON_ALL_", 7)) {
  138. goto client;
  139. } else if (!strncmp(prev_cmd, "ON_HOST_", 8)) {
  140. goto client;
  141. } else if (!strncmp(prev_cmd, "ON_CLIENT_", 10)) {
  142. goto ignore;
  143. } else if (!strcmp(prev_cmd, "OFF")) {
  144. goto client;
  145. } else {
  146. goto invalid;
  147. }
  148. } else {
  149. goto invalid;
  150. }
  151. host:
  152. unl_info("%s cmd=%s is accepted.\n", __func__, cur_cmd);
  153. return NOTIFY_BLOCK_TYPE_HOST;
  154. client:
  155. unl_info("%s cmd=%s is accepted.\n", __func__, cur_cmd);
  156. return NOTIFY_BLOCK_TYPE_CLIENT;
  157. all:
  158. unl_info("%s cmd=%s is accepted.\n", __func__, cur_cmd);
  159. return NOTIFY_BLOCK_TYPE_ALL;
  160. off:
  161. unl_info("%s cmd=%s is accepted.\n", __func__, cur_cmd);
  162. return NOTIFY_BLOCK_TYPE_NONE;
  163. ignore:
  164. unl_err("%s cmd=%s is ignored but saved.\n", __func__, cur_cmd);
  165. return -EEXIST;
  166. invalid:
  167. unl_err("%s cmd=%s is invalid.\n", __func__, cur_cmd);
  168. return -EINVAL;
  169. }
  170. static ssize_t disable_show(
  171. struct device *dev, struct device_attribute *attr,
  172. char *buf)
  173. {
  174. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  175. dev_get_drvdata(dev);
  176. unl_info("read disable_state %s\n", udev->disable_state_cmd);
  177. return sprintf(buf, "%s\n", udev->disable_state_cmd);
  178. }
  179. static ssize_t disable_store(
  180. struct device *dev, struct device_attribute *attr,
  181. const char *buf, size_t size)
  182. {
  183. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  184. dev_get_drvdata(dev);
  185. char *disable;
  186. int sret, param = -EINVAL;
  187. size_t ret = -ENOMEM;
  188. if (size > MAX_DISABLE_STR_LEN) {
  189. unl_err("%s size(%zu) is too long.\n", __func__, size);
  190. goto error;
  191. }
  192. if (size < strlen(buf))
  193. goto error;
  194. disable = kzalloc(size+1, GFP_KERNEL);
  195. if (!disable)
  196. goto error;
  197. sret = sscanf(buf, "%s", disable);
  198. if (sret != 1)
  199. goto error1;
  200. if (udev->set_disable) {
  201. param = is_valid_cmd(disable, udev->disable_state_cmd);
  202. if (param == -EINVAL) {
  203. ret = param;
  204. } else {
  205. if (param != -EEXIST) {
  206. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  207. udev->first_restrict = false;
  208. #endif
  209. udev->set_disable(udev, param);
  210. }
  211. strncpy(udev->disable_state_cmd,
  212. disable, sizeof(udev->disable_state_cmd)-1);
  213. ret = size;
  214. }
  215. } else
  216. unl_err("set_disable func is NULL\n");
  217. error1:
  218. kfree(disable);
  219. error:
  220. return ret;
  221. }
  222. static ssize_t usb_data_enabled_show(
  223. struct device *dev, struct device_attribute *attr,
  224. char *buf)
  225. {
  226. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  227. dev_get_drvdata(dev);
  228. unl_info("read usb_data_enabled %lu\n", udev->usb_data_enabled);
  229. return sprintf(buf, "%lu\n", udev->usb_data_enabled);
  230. }
  231. static ssize_t usb_data_enabled_store(
  232. struct device *dev, struct device_attribute *attr,
  233. const char *buf, size_t size)
  234. {
  235. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  236. dev_get_drvdata(dev);
  237. size_t ret = -ENOMEM;
  238. int sret = -EINVAL;
  239. int param = 0;
  240. char *usb_data_enabled;
  241. if (size > PAGE_SIZE) {
  242. unl_err("%s size(%zu) is too long.\n", __func__, size);
  243. goto error;
  244. }
  245. usb_data_enabled = kzalloc(size+1, GFP_KERNEL);
  246. if (!usb_data_enabled)
  247. goto error;
  248. sret = sscanf(buf, "%s", usb_data_enabled);
  249. if (sret != 1)
  250. goto error1;
  251. if (udev->set_disable) {
  252. if (strcmp(usb_data_enabled, "0") == 0) {
  253. param = NOTIFY_BLOCK_TYPE_ALL;
  254. udev->usb_data_enabled = 0;
  255. } else if (strcmp(usb_data_enabled, "1") == 0) {
  256. param = NOTIFY_BLOCK_TYPE_NONE;
  257. udev->usb_data_enabled = 1;
  258. } else {
  259. unl_err("%s usb_data_enabled(%s) error.\n",
  260. __func__, usb_data_enabled);
  261. goto error1;
  262. }
  263. unl_info("%s usb_data_enabled=%s\n",
  264. __func__, usb_data_enabled);
  265. udev->set_disable(udev, param);
  266. ret = size;
  267. } else {
  268. unl_err("%s set_disable func is NULL\n", __func__);
  269. }
  270. error1:
  271. kfree(usb_data_enabled);
  272. error:
  273. return ret;
  274. }
  275. static ssize_t support_show(struct device *dev,
  276. struct device_attribute *attr, char *buf)
  277. {
  278. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  279. dev_get_drvdata(dev);
  280. struct otg_notify *n = udev->o_notify;
  281. char *support;
  282. if (n->unsupport_host || !IS_ENABLED(CONFIG_USB_HOST_NOTIFY))
  283. support = "CLIENT";
  284. else
  285. support = "ALL";
  286. unl_info("read support %s\n", support);
  287. return sprintf(buf, "%s\n", support);
  288. }
  289. static ssize_t otg_speed_show(struct device *dev,
  290. struct device_attribute *attr, char *buf)
  291. {
  292. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  293. dev_get_drvdata(dev);
  294. struct otg_notify *n = udev->o_notify;
  295. int dev_max_speed = 0;
  296. char *speed;
  297. dev_max_speed = get_con_dev_max_speed(n);
  298. switch (dev_max_speed) {
  299. case USB_SPEED_SUPER_PLUS:
  300. speed = "SUPER PLUS";
  301. break;
  302. case USB_SPEED_SUPER:
  303. speed = "SUPER";
  304. break;
  305. case USB_SPEED_HIGH:
  306. speed = "HIGH";
  307. break;
  308. case USB_SPEED_FULL:
  309. speed = "FULL";
  310. break;
  311. case USB_SPEED_LOW:
  312. speed = "LOW";
  313. break;
  314. default:
  315. speed = "UNKNOWN";
  316. break;
  317. }
  318. unl_info("%s : read otg speed %s\n", __func__, speed);
  319. return sprintf(buf, "%s\n", speed);
  320. }
  321. static ssize_t gadget_speed_show(struct device *dev,
  322. struct device_attribute *attr, char *buf)
  323. {
  324. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  325. dev_get_drvdata(dev);
  326. struct otg_notify *n = udev->o_notify;
  327. const char *speed;
  328. if (n->get_gadget_speed)
  329. speed = usb_speed_string(n->get_gadget_speed());
  330. else
  331. speed = "UNKNOWN";
  332. unl_info("%s : read gadget speed %s\n", __func__, speed);
  333. return snprintf(buf, MAX_STRING_LEN, "%s\n", speed);
  334. }
  335. static const char *const max_speed_str[] = {
  336. [USB_SPEED_UNKNOWN] = "UNKNOWN",
  337. [USB_SPEED_LOW] = "low-speed",
  338. [USB_SPEED_FULL] = "full-speed",
  339. [USB_SPEED_HIGH] = "high-speed",
  340. [USB_SPEED_WIRELESS] = "wireless-usb",
  341. [USB_SPEED_SUPER] = "super-speed",
  342. [USB_SPEED_SUPER_PLUS] = "super-speed+",
  343. };
  344. static ssize_t usb_maximum_speed_show(
  345. struct device *dev, struct device_attribute *attr,
  346. char *buf)
  347. {
  348. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  349. dev_get_drvdata(dev);
  350. int ret = 0;
  351. ret = udev->control_usb_max_speed(udev, -1);
  352. return sprintf(buf, "%s\n", max_speed_str[ret]);
  353. }
  354. static ssize_t usb_maximum_speed_store(
  355. struct device *dev, struct device_attribute *attr,
  356. const char *buf, size_t size)
  357. {
  358. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  359. dev_get_drvdata(dev);
  360. int max_speed_idx = USB_SPEED_UNKNOWN;
  361. char *max_speed;
  362. size_t ret = -ENOMEM, i, sret;
  363. unl_info("%s\n", __func__);
  364. if (size > MAX_USB_SPEED_STR_LEN) {
  365. unl_err("%s size(%zu) is too long.\n", __func__, size);
  366. goto error;
  367. }
  368. max_speed = kzalloc(size+1, GFP_KERNEL);
  369. if (!max_speed)
  370. goto error;
  371. sret = sscanf(buf, "%s", max_speed);
  372. if (sret != 1)
  373. goto error1;
  374. for (i = 0; i < ARRAY_SIZE(max_speed_str); i++) {
  375. if (strncmp(max_speed, max_speed_str[i],
  376. strlen(max_speed_str[i])) == 0) {
  377. max_speed_idx = i;
  378. break;
  379. }
  380. }
  381. if (max_speed_idx == USB_SPEED_UNKNOWN) {
  382. ret = -EINVAL;
  383. goto error1;
  384. } else {
  385. sret = udev->control_usb_max_speed(udev, max_speed_idx);
  386. }
  387. unl_info("%s req=%s now=%s\n", __func__, max_speed,
  388. max_speed_str[max_speed_idx]);
  389. ret = size;
  390. error1:
  391. kfree(max_speed);
  392. error:
  393. return ret;
  394. }
  395. #if defined(CONFIG_USB_HW_PARAM)
  396. static unsigned long long strtoull(char *ptr, char **end, int base)
  397. {
  398. unsigned long long ret = 0;
  399. if (base > 36)
  400. goto out;
  401. while (*ptr) {
  402. int digit;
  403. if (*ptr >= '0' && *ptr <= '9' && *ptr < '0' + base)
  404. digit = *ptr - '0';
  405. else if (*ptr >= 'A' && *ptr < 'A' + base - 10)
  406. digit = *ptr - 'A' + 10;
  407. else if (*ptr >= 'a' && *ptr < 'a' + base - 10)
  408. digit = *ptr - 'a' + 10;
  409. else
  410. break;
  411. ret *= base;
  412. ret += digit;
  413. ptr++;
  414. }
  415. out:
  416. if (end)
  417. *end = (char *)ptr;
  418. return ret;
  419. }
  420. static ssize_t usb_hw_param_show(struct device *dev,
  421. struct device_attribute *attr, char *buf)
  422. {
  423. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  424. dev_get_drvdata(dev);
  425. struct otg_notify *n = udev->o_notify;
  426. int index, ret = 0;
  427. unsigned long long *p_param = NULL;
  428. if (udev->fp_hw_param_manager) {
  429. p_param = get_hw_param(n, USB_CCIC_WATER_INT_COUNT);
  430. if (p_param)
  431. *p_param += udev->fp_hw_param_manager
  432. (USB_CCIC_WATER_INT_COUNT);
  433. p_param = get_hw_param(n, USB_CCIC_DRY_INT_COUNT);
  434. if (p_param)
  435. *p_param += udev->fp_hw_param_manager
  436. (USB_CCIC_DRY_INT_COUNT);
  437. p_param = get_hw_param(n, USB_CLIENT_SUPER_SPEED_COUNT);
  438. if (p_param)
  439. *p_param += udev->fp_hw_param_manager
  440. (USB_CLIENT_SUPER_SPEED_COUNT);
  441. p_param = get_hw_param(n, USB_CLIENT_HIGH_SPEED_COUNT);
  442. if (p_param)
  443. *p_param += udev->fp_hw_param_manager
  444. (USB_CLIENT_HIGH_SPEED_COUNT);
  445. p_param = get_hw_param(n, USB_CCIC_WATER_TIME_DURATION);
  446. if (p_param)
  447. *p_param += udev->fp_hw_param_manager
  448. (USB_CCIC_WATER_TIME_DURATION);
  449. p_param = get_hw_param(n, USB_CCIC_WATER_VBUS_COUNT);
  450. if (p_param)
  451. *p_param += udev->fp_hw_param_manager
  452. (USB_CCIC_WATER_VBUS_COUNT);
  453. p_param = get_hw_param(n, USB_CCIC_WATER_LPM_VBUS_COUNT);
  454. if (p_param)
  455. *p_param += udev->fp_hw_param_manager
  456. (USB_CCIC_WATER_LPM_VBUS_COUNT);
  457. p_param = get_hw_param(n, USB_CCIC_WATER_VBUS_TIME_DURATION);
  458. if (p_param)
  459. *p_param += udev->fp_hw_param_manager
  460. (USB_CCIC_WATER_VBUS_TIME_DURATION);
  461. p_param = get_hw_param(n,
  462. USB_CCIC_WATER_LPM_VBUS_TIME_DURATION);
  463. if (p_param)
  464. *p_param += udev->fp_hw_param_manager
  465. (USB_CCIC_WATER_LPM_VBUS_TIME_DURATION);
  466. }
  467. p_param = get_hw_param(n, USB_CCIC_VERSION);
  468. if (p_param)
  469. *p_param = show_ccic_version();
  470. for (index = 0; index < USB_CCIC_HW_PARAM_MAX - 1; index++) {
  471. p_param = get_hw_param(n, index);
  472. if (p_param)
  473. ret += sprintf(buf + ret, "%llu ", *p_param);
  474. else
  475. ret += sprintf(buf + ret, "0 ");
  476. }
  477. p_param = get_hw_param(n, index);
  478. if (p_param)
  479. ret += sprintf(buf + ret, "%llu\n", *p_param);
  480. else
  481. ret += sprintf(buf + ret, "0\n");
  482. unl_info("%s - ret : %d\n", __func__, ret);
  483. return ret;
  484. }
  485. static ssize_t usb_hw_param_store(
  486. struct device *dev, struct device_attribute *attr,
  487. const char *buf, size_t size)
  488. {
  489. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  490. dev_get_drvdata(dev);
  491. struct otg_notify *n = udev->o_notify;
  492. unsigned long long prev_hw_param[USB_CCIC_HW_PARAM_MAX] = {0, };
  493. unsigned long long *p_param = NULL;
  494. int index = 0;
  495. size_t ret = -ENOMEM;
  496. char *token, *str = (char *)buf;
  497. if (size > MAX_HWPARAM_STR_LEN) {
  498. unl_err("%s size(%zu) is too long.\n", __func__, size);
  499. goto error;
  500. }
  501. ret = size;
  502. if (size < USB_CCIC_HW_PARAM_MAX) {
  503. unl_err("%s efs file is not created correctly.\n", __func__);
  504. goto error;
  505. }
  506. for (index = 0; index < (USB_CCIC_HW_PARAM_MAX - 1); index++) {
  507. token = strsep(&str, " ");
  508. if (token)
  509. prev_hw_param[index] = strtoull(token, NULL, 10);
  510. if (!token || (prev_hw_param[index] > HWPARAM_DATA_LIMIT))
  511. goto error;
  512. }
  513. for (index = 0; index < (USB_CCIC_HW_PARAM_MAX - 1); index++) {
  514. p_param = get_hw_param(n, index);
  515. if (p_param)
  516. *p_param += prev_hw_param[index];
  517. }
  518. unl_info("%s - ret : %zu\n", __func__, ret);
  519. error:
  520. return ret;
  521. }
  522. static int is_skip_list(struct otg_notify *n, int index)
  523. {
  524. if (!n)
  525. return 0;
  526. if (n->is_skip_list)
  527. return n->is_skip_list(index);
  528. return 0;
  529. }
  530. static ssize_t hw_param_show(struct device *dev,
  531. struct device_attribute *attr, char *buf)
  532. {
  533. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  534. dev_get_drvdata(dev);
  535. struct otg_notify *n = udev->o_notify;
  536. int index = 0, ret = 0;
  537. unsigned long long *p_param = NULL;
  538. if (udev->fp_hw_param_manager) {
  539. p_param = get_hw_param(n, USB_CCIC_WATER_INT_COUNT);
  540. if (p_param)
  541. *p_param += udev->fp_hw_param_manager
  542. (USB_CCIC_WATER_INT_COUNT);
  543. p_param = get_hw_param(n, USB_CCIC_DRY_INT_COUNT);
  544. if (p_param)
  545. *p_param += udev->fp_hw_param_manager
  546. (USB_CCIC_DRY_INT_COUNT);
  547. p_param = get_hw_param(n, USB_CLIENT_SUPER_SPEED_COUNT);
  548. if (p_param)
  549. *p_param += udev->fp_hw_param_manager
  550. (USB_CLIENT_SUPER_SPEED_COUNT);
  551. p_param = get_hw_param(n, USB_CLIENT_HIGH_SPEED_COUNT);
  552. if (p_param)
  553. *p_param += udev->fp_hw_param_manager
  554. (USB_CLIENT_HIGH_SPEED_COUNT);
  555. p_param = get_hw_param(n, USB_CCIC_WATER_TIME_DURATION);
  556. if (p_param)
  557. *p_param += udev->fp_hw_param_manager
  558. (USB_CCIC_WATER_TIME_DURATION);
  559. p_param = get_hw_param(n, USB_CCIC_WATER_VBUS_COUNT);
  560. if (p_param)
  561. *p_param += udev->fp_hw_param_manager
  562. (USB_CCIC_WATER_VBUS_COUNT);
  563. p_param = get_hw_param(n, USB_CCIC_WATER_LPM_VBUS_COUNT);
  564. if (p_param)
  565. *p_param += udev->fp_hw_param_manager
  566. (USB_CCIC_WATER_LPM_VBUS_COUNT);
  567. p_param = get_hw_param(n, USB_CCIC_WATER_VBUS_TIME_DURATION);
  568. if (p_param)
  569. *p_param += udev->fp_hw_param_manager
  570. (USB_CCIC_WATER_VBUS_TIME_DURATION);
  571. p_param = get_hw_param(n,
  572. USB_CCIC_WATER_LPM_VBUS_TIME_DURATION);
  573. if (p_param)
  574. *p_param += udev->fp_hw_param_manager
  575. (USB_CCIC_WATER_LPM_VBUS_TIME_DURATION);
  576. }
  577. if (!is_skip_list(n, USB_CCIC_VERSION)) {
  578. p_param = get_hw_param(n, USB_CCIC_VERSION);
  579. if (p_param)
  580. *p_param = show_ccic_version();
  581. }
  582. for (index = 0; index < USB_CCIC_HW_PARAM_MAX - 1; index++) {
  583. if (!is_skip_list(n, index)) {
  584. p_param = get_hw_param(n, index);
  585. if (p_param)
  586. ret += sprintf(buf + ret, "\"%s\":\"%llu\",",
  587. usb_hw_param_print[index], *p_param);
  588. else
  589. ret += sprintf(buf + ret, "\"%s\":\"0\",",
  590. usb_hw_param_print[index]);
  591. }
  592. }
  593. if (!is_skip_list(n, USB_CCIC_VERSION)) {
  594. /* CCIC FW version */
  595. ret += sprintf(buf + ret, "\"%s\":\"",
  596. usb_hw_param_print[USB_CCIC_VERSION]);
  597. p_param = get_hw_param(n, USB_CCIC_VERSION);
  598. if (p_param) {
  599. /* HW Version */
  600. ret += sprintf(buf + ret, "%02X%02X%02X%02X",
  601. *((unsigned char *)p_param + 3),
  602. *((unsigned char *)p_param + 2),
  603. *((unsigned char *)p_param + 1),
  604. *((unsigned char *)p_param));
  605. /* SW Main Version */
  606. ret += sprintf(buf + ret, "%02X%02X%02X",
  607. *((unsigned char *)p_param + 6),
  608. *((unsigned char *)p_param + 5),
  609. *((unsigned char *)p_param + 4));
  610. /* SW Boot Version */
  611. ret += sprintf(buf + ret, "%02X",
  612. *((unsigned char *)p_param + 7));
  613. ret += sprintf(buf + ret, "\"\n");
  614. } else {
  615. ret += sprintf(buf + ret, "0000000000000000\"\n");
  616. }
  617. } else {
  618. ret += sprintf(buf + ret - 1, "\n");
  619. }
  620. unl_info("%s - ret : %d\n", __func__, ret);
  621. return ret;
  622. }
  623. static ssize_t hw_param_store(
  624. struct device *dev, struct device_attribute *attr,
  625. const char *buf, size_t size)
  626. {
  627. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  628. dev_get_drvdata(dev);
  629. struct otg_notify *n = udev->o_notify;
  630. int index = 0;
  631. size_t ret = -ENOMEM;
  632. char *str = (char *)buf;
  633. unsigned long long *p_param = NULL;
  634. if (size > 2) {
  635. unl_err("%s size(%zu) is too long.\n", __func__, size);
  636. goto error;
  637. }
  638. ret = size;
  639. unl_info("%s : %s\n", __func__, str);
  640. if (!strncmp(str, "c", 1))
  641. for (index = 0; index < USB_CCIC_HW_PARAM_MAX; index++) {
  642. p_param = get_hw_param(n, index);
  643. if (p_param)
  644. *p_param = 0;
  645. }
  646. error:
  647. return ret;
  648. }
  649. #endif
  650. char interface_class_name[USB_CLASS_VENDOR_SPEC][4] = {
  651. [U_CLASS_PER_INTERFACE] = {"PER"},
  652. [U_CLASS_AUDIO] = {"AUD"},
  653. [U_CLASS_COMM] = {"COM"},
  654. [U_CLASS_HID] = {"HID"},
  655. [U_CLASS_PHYSICAL] = {"PHY"},
  656. [U_CLASS_STILL_IMAGE] = {"STI"},
  657. [U_CLASS_PRINTER] = {"PRI"},
  658. [U_CLASS_MASS_STORAGE] = {"MAS"},
  659. [U_CLASS_HUB] = {"HUB"},
  660. [U_CLASS_CDC_DATA] = {"CDC"},
  661. [U_CLASS_CSCID] = {"CSC"},
  662. [U_CLASS_CONTENT_SEC] = {"CON"},
  663. [U_CLASS_VIDEO] = {"VID"},
  664. [U_CLASS_WIRELESS_CONTROLLER] = {"WIR"},
  665. [U_CLASS_MISC] = {"MIS"},
  666. [U_CLASS_APP_SPEC] = {"APP"},
  667. [U_CLASS_VENDOR_SPEC] = {"VEN"}
  668. };
  669. void init_usb_whitelist_array(int *whitelist_array)
  670. {
  671. int i;
  672. for (i = 1; i <= MAX_CLASS_TYPE_NUM; i++)
  673. whitelist_array[i] = 0;
  674. }
  675. void init_usb_whitelist_array_for_id(int *whitelist_array, int size)
  676. {
  677. int i;
  678. for (i = 0; i < size; i++)
  679. whitelist_array[i] = 0;
  680. }
  681. int set_usb_allowlist_array(const char *buf, int *whitelist_array)
  682. {
  683. int valid_class_count = 0;
  684. char *ptr = NULL;
  685. int i;
  686. char *source;
  687. source = (char *)buf;
  688. while ((ptr = strsep(&source, ":")) != NULL) {
  689. if (strlen(ptr) < 3)
  690. continue;
  691. unl_info("%s token = %c%c%c!\n", __func__,
  692. ptr[0], ptr[1], ptr[2]);
  693. for (i = U_CLASS_PER_INTERFACE; i <= U_CLASS_VENDOR_SPEC; i++) {
  694. if (!strncmp(ptr, interface_class_name[i], 3))
  695. whitelist_array[i] = 1;
  696. }
  697. }
  698. for (i = U_CLASS_PER_INTERFACE; i <= U_CLASS_VENDOR_SPEC; i++) {
  699. if (whitelist_array[i])
  700. valid_class_count++;
  701. }
  702. unl_info("%s valid_class_count = %d!\n", __func__, valid_class_count);
  703. return valid_class_count;
  704. }
  705. int set_usb_allowlist_array_for_id(const char *buf, int *whitelist_array)
  706. {
  707. int valid_product_count = 0;
  708. int vid = 0, pid = 0, ret = 0;
  709. char *ptr_vid = NULL;
  710. char *ptr_pid = NULL;
  711. char *source;
  712. source = (char *)buf;
  713. while ((ptr_vid = strsep(&source, ":")) != NULL) {
  714. if (strlen(ptr_vid) < 4) {
  715. unl_err("%s short strlen(vid)\n", __func__);
  716. break;
  717. }
  718. ptr_pid = strsep(&source, ":");
  719. if (ptr_pid == NULL || strlen(ptr_pid) < 4) {
  720. unl_err("%s short strlen(pid)\n", __func__);
  721. break;
  722. }
  723. if (!ptr_vid[0] || !ptr_vid[1] || !ptr_vid[2] || !ptr_vid[3] ||
  724. !ptr_pid[0] || !ptr_pid[1] || !ptr_pid[2] || !ptr_pid[3])
  725. break;
  726. ret = kstrtoint(ptr_vid, 16, &vid);
  727. if (ret) {
  728. unl_err("%s ptr_vid error. ret %d\n", __func__, ret);
  729. break;
  730. }
  731. whitelist_array[valid_product_count] = vid;
  732. ret = kstrtoint(ptr_pid, 16, &pid);
  733. if (ret) {
  734. unl_err("%s ptr_pid error. ret %d\n", __func__, ret);
  735. break;
  736. }
  737. whitelist_array[valid_product_count+1] = pid;
  738. unl_info("%s : allowlist_array[%d]=%04x, allowlist_array[%d]=%04x\n",
  739. __func__, valid_product_count, whitelist_array[valid_product_count],
  740. valid_product_count+1, whitelist_array[valid_product_count+1]);
  741. valid_product_count += 2;
  742. }
  743. valid_product_count /= 2;
  744. unl_info("%s valid_product_count = %d!\n", __func__, valid_product_count);
  745. return valid_product_count;
  746. }
  747. static ssize_t whitelist_for_mdm_show(struct device *dev,
  748. struct device_attribute *attr, char *buf)
  749. {
  750. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  751. dev_get_drvdata(dev);
  752. if (udev == NULL) {
  753. unl_err("udev is NULL\n");
  754. return -EINVAL;
  755. }
  756. unl_info("allowlist_for_mdm read allowlist_classes %s\n",
  757. udev->whitelist_str);
  758. return sprintf(buf, "%s\n", udev->whitelist_str);
  759. }
  760. static ssize_t whitelist_for_mdm_store(
  761. struct device *dev, struct device_attribute *attr,
  762. const char *buf, size_t size)
  763. {
  764. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  765. dev_get_drvdata(dev);
  766. char *disable;
  767. int sret;
  768. size_t ret = -ENOMEM;
  769. int mdm_disable;
  770. int valid_whilelist_count;
  771. if (udev == NULL) {
  772. unl_err("udev is NULL\n");
  773. ret = -EINVAL;
  774. goto error;
  775. }
  776. if (size < 3) {
  777. unl_err("allowlist usage was wrong. The size(%zu) is too short.\n", size);
  778. goto error;
  779. }
  780. if (size < strlen(buf))
  781. goto error;
  782. disable = kzalloc(size+1, GFP_KERNEL);
  783. if (!disable)
  784. goto error;
  785. sret = sscanf(buf, "%s", disable);
  786. if (sret != 1)
  787. goto error1;
  788. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  789. if (!strncmp(buf, "VPID:", ALLOWLIST_PREFIX_SIZE)) {
  790. unl_info("allowlist_for_mdm_store VID, PID buf=%s\n", disable);
  791. if (size >= MAX_ALLOWLIST_BUFFER) {
  792. unl_err("allowlist_for_lockscreen size(%zu) is invalid.\n", size);
  793. goto error1;
  794. }
  795. mutex_lock(&udev->lockscreen_enabled_lock);
  796. init_usb_whitelist_array_for_id(udev->allowlist_array_lockscreen_enabled_id,
  797. MAX_ALLOWLIST_DEVICE_BUFFER_INDEX);
  798. valid_whilelist_count = set_usb_allowlist_array_for_id
  799. (buf+ALLOWLIST_PREFIX_SIZE, udev->allowlist_array_lockscreen_enabled_id);
  800. // for furture use ex:) show function
  801. strncpy(udev->allowlist_str_lockscreen_enabled_id,
  802. disable, sizeof(udev->allowlist_str_lockscreen_enabled_id)-1);
  803. mutex_unlock(&udev->lockscreen_enabled_lock);
  804. ret = size;
  805. unl_info("%s vpid allowlist update done!\n", __func__);
  806. } else {
  807. #endif
  808. unl_info("allowlist_for_mdm_store interface buf=%s\n", disable);
  809. /* To active displayport, hub class must be enabled */
  810. if (size > MAX_WHITELIST_STR_LEN) {
  811. unl_err("allowlist_for_mdm_store size(%zu) is invalid.\n", size);
  812. goto error1;
  813. }
  814. init_usb_whitelist_array(udev->whitelist_array_for_mdm);
  815. if (!strncmp(buf, "ABL", 3)) {
  816. udev->whitelist_array_for_mdm[U_CLASS_HUB] = 1;
  817. mdm_disable = NOTIFY_MDM_TYPE_ON;
  818. } else if (!strncmp(buf, "OFF", 3))
  819. mdm_disable = NOTIFY_MDM_TYPE_OFF;
  820. else {
  821. valid_whilelist_count = set_usb_allowlist_array
  822. (buf, udev->whitelist_array_for_mdm);
  823. if (valid_whilelist_count > 0) {
  824. udev->whitelist_array_for_mdm[U_CLASS_HUB] = 1;
  825. mdm_disable = NOTIFY_MDM_TYPE_ON;
  826. } else
  827. mdm_disable = NOTIFY_MDM_TYPE_OFF;
  828. }
  829. strncpy(udev->whitelist_str,
  830. disable, sizeof(udev->whitelist_str)-1);
  831. if (udev->set_mdm) {
  832. udev->set_mdm(udev, mdm_disable);
  833. ret = size;
  834. } else {
  835. unl_err("set_mdm func is NULL\n");
  836. ret = -EINVAL;
  837. }
  838. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  839. }
  840. #endif
  841. error1:
  842. kfree(disable);
  843. error:
  844. return ret;
  845. }
  846. static ssize_t whitelist_for_disa_show(struct device *dev,
  847. struct device_attribute *attr, char *buf)
  848. {
  849. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  850. dev_get_drvdata(dev);
  851. if (udev == NULL) {
  852. unl_err("udev is NULL\n");
  853. return -EINVAL;
  854. }
  855. unl_info("%s read allowlist_classes %s\n",
  856. __func__, udev->whitelist_str_for_id);
  857. return sprintf(buf, "%s\n", udev->whitelist_str_for_id);
  858. }
  859. static ssize_t whitelist_for_disa_store(
  860. struct device *dev, struct device_attribute *attr,
  861. const char *buf, size_t size)
  862. {
  863. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  864. dev_get_drvdata(dev);
  865. char *disable;
  866. int sret;
  867. size_t ret = -ENOMEM;
  868. int mdm_disable;
  869. int valid_whilelist_count;
  870. if (udev == NULL) {
  871. unl_err("udev is NULL\n");
  872. ret = -EINVAL;
  873. goto error;
  874. }
  875. if (size > MAX_WHITELIST_STR_LEN) {
  876. unl_err("%s size(%zu) is too long.\n", __func__, size);
  877. goto error;
  878. }
  879. if (size < strlen(buf))
  880. goto error;
  881. disable = kzalloc(size+1, GFP_KERNEL);
  882. if (!disable)
  883. goto error;
  884. sret = sscanf(buf, "%s", disable);
  885. if (sret != 1)
  886. goto error1;
  887. unl_info("allowlist_for_disa_store buf=%s\n", disable);
  888. init_usb_whitelist_array_for_id(udev->whitelist_array_for_mdm_for_id, MAX_WHITELIST_STR_LEN);
  889. /* To active displayport, hub class must be enabled */
  890. if (!strncmp(buf, "OFF", 3)) {
  891. unl_info("%s OFF\n", __func__);
  892. mdm_disable = NOTIFY_MDM_TYPE_OFF;
  893. } else {
  894. unl_info("%s ALLOWLIST\n", __func__);
  895. valid_whilelist_count = set_usb_allowlist_array_for_id
  896. (buf, udev->whitelist_array_for_mdm_for_id);
  897. if (valid_whilelist_count > 0)
  898. mdm_disable = NOTIFY_MDM_TYPE_ON;
  899. else
  900. mdm_disable = NOTIFY_MDM_TYPE_OFF;
  901. }
  902. strncpy(udev->whitelist_str_for_id,
  903. disable, sizeof(udev->whitelist_str_for_id)-1);
  904. if (udev->set_mdm_for_id) {
  905. udev->set_mdm_for_id(udev, mdm_disable);
  906. ret = size;
  907. } else {
  908. unl_err("set_mdm_for_id func is NULL\n");
  909. ret = -EINVAL;
  910. }
  911. error1:
  912. kfree(disable);
  913. error:
  914. return ret;
  915. }
  916. static ssize_t whitelist_for_serial_show(struct device *dev,
  917. struct device_attribute *attr, char *buf)
  918. {
  919. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  920. dev_get_drvdata(dev);
  921. if (udev == NULL) {
  922. unl_err("udev is NULL\n");
  923. return -EINVAL;
  924. }
  925. unl_info("%s read allowlist_classes %s\n",
  926. __func__, udev->whitelist_array_for_mdm_for_serial);
  927. return sprintf(buf, "%s\n", udev->whitelist_array_for_mdm_for_serial);
  928. }
  929. static ssize_t whitelist_for_serial_store(
  930. struct device *dev, struct device_attribute *attr,
  931. const char *buf, size_t size)
  932. {
  933. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  934. dev_get_drvdata(dev);
  935. char *disable;
  936. int sret;
  937. size_t ret = -ENOMEM;
  938. int mdm_disable;
  939. if (udev == NULL) {
  940. unl_err("udev is NULL\n");
  941. ret = -EINVAL;
  942. goto error;
  943. }
  944. if (size > MAX_WHITELIST_STR_LEN) {
  945. unl_err("%s size(%zu) is too long.\n", __func__, size);
  946. goto error;
  947. }
  948. if (size < strlen(buf))
  949. goto error;
  950. disable = kzalloc(size+1, GFP_KERNEL);
  951. if (!disable)
  952. goto error;
  953. sret = sscanf(buf, "%s", disable);
  954. if (sret != 1)
  955. goto error1;
  956. unl_info("allowlist_for_serial_store buf=%s\n", disable);
  957. strncpy(udev->whitelist_array_for_mdm_for_serial,
  958. disable, sizeof(udev->whitelist_array_for_mdm_for_serial)-1);
  959. /* To active displayport, hub class must be enabled */
  960. if (!strncmp(buf, "OFF", 3)) {
  961. unl_info("%s OFF\n", __func__);
  962. mdm_disable = NOTIFY_MDM_TYPE_OFF;
  963. } else {
  964. unl_info("%s ALLOWLIST\n", __func__);
  965. mdm_disable = NOTIFY_MDM_TYPE_ON;
  966. }
  967. if (udev->set_mdm_for_serial) {
  968. udev->set_mdm_for_serial(udev, mdm_disable);
  969. ret = size;
  970. } else {
  971. unl_err("set_mdm_for_serial func is NULL\n");
  972. ret = -EINVAL;
  973. }
  974. error1:
  975. kfree(disable);
  976. error:
  977. return ret;
  978. }
  979. static ssize_t usb_request_action_show(struct device *dev,
  980. struct device_attribute *attr, char *buf)
  981. {
  982. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  983. dev_get_drvdata(dev);
  984. if (udev == NULL) {
  985. unl_err("udev is NULL\n");
  986. return -EINVAL;
  987. }
  988. unl_info("%s request_action = %u\n",
  989. __func__, udev->request_action);
  990. return sprintf(buf, "%u\n", udev->request_action);
  991. }
  992. static ssize_t usb_request_action_store(
  993. struct device *dev, struct device_attribute *attr,
  994. const char *buf, size_t size)
  995. {
  996. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  997. dev_get_drvdata(dev);
  998. unsigned int request_action = 0;
  999. int sret = -EINVAL;
  1000. size_t ret = -ENOMEM;
  1001. if (udev == NULL) {
  1002. unl_err("udev is NULL\n");
  1003. return -EINVAL;
  1004. }
  1005. if (size > PAGE_SIZE) {
  1006. unl_err("%s size(%zu) is too long.\n", __func__, size);
  1007. goto error;
  1008. }
  1009. sret = sscanf(buf, "%u", &request_action);
  1010. if (sret != 1)
  1011. goto error;
  1012. udev->request_action = request_action;
  1013. unl_info("%s request_action = %d\n",
  1014. __func__, udev->request_action);
  1015. ret = size;
  1016. error:
  1017. return ret;
  1018. }
  1019. static ssize_t cards_show(
  1020. struct device *dev, struct device_attribute *attr,
  1021. char *buf)
  1022. {
  1023. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  1024. dev_get_drvdata(dev);
  1025. char card_strings[MAX_CARD_STR_LEN] = {0,};
  1026. char buf_card[15] = {0,};
  1027. int i;
  1028. int cnt = 0;
  1029. for (i = 0; i < MAX_USB_AUDIO_CARDS; i++) {
  1030. if (udev->usb_audio_cards[i].cards) {
  1031. cnt += snprintf(buf_card, sizeof(buf_card),
  1032. "<%scard%d>",
  1033. udev->usb_audio_cards[i].bundle ? "*" : "", i);
  1034. if (cnt < 0) {
  1035. unl_err("%s snprintf return %d\n",
  1036. __func__, cnt);
  1037. continue;
  1038. }
  1039. if (cnt >= MAX_CARD_STR_LEN) {
  1040. unl_err("%s overflow\n", __func__);
  1041. goto err;
  1042. }
  1043. strlcat(card_strings, buf_card, sizeof(card_strings));
  1044. }
  1045. }
  1046. err:
  1047. unl_info("card_strings %s\n", card_strings);
  1048. return sprintf(buf, "%s\n", card_strings);
  1049. }
  1050. int usb_notify_dev_uevent(struct usb_notify_dev *udev, char *envp_ext[])
  1051. {
  1052. int ret = 0;
  1053. if (!udev || !udev->dev) {
  1054. unl_err("%s udev or udev->dev NULL\n", __func__);
  1055. ret = -EINVAL;
  1056. goto err;
  1057. }
  1058. if (strncmp("TYPE", envp_ext[0], 4)) {
  1059. unl_err("%s error.first array must be filled TYPE\n",
  1060. __func__);
  1061. ret = -EINVAL;
  1062. goto err;
  1063. }
  1064. if (strncmp("STATE", envp_ext[1], 5)) {
  1065. unl_err("%s error.second array must be filled STATE\n",
  1066. __func__);
  1067. ret = -EINVAL;
  1068. goto err;
  1069. }
  1070. kobject_uevent_env(&udev->dev->kobj, KOBJ_CHANGE, envp_ext);
  1071. unl_info("%s\n", __func__);
  1072. err:
  1073. return ret;
  1074. }
  1075. EXPORT_SYMBOL_GPL(usb_notify_dev_uevent);
  1076. #if defined(CONFIG_USB_LPM_CHARGING_SYNC)
  1077. static ssize_t lpm_charging_type_done_show(struct device *dev,
  1078. struct device_attribute *attr, char *buf)
  1079. {
  1080. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  1081. dev_get_drvdata(dev);
  1082. if (udev == NULL) {
  1083. pr_err("udev is NULL\n");
  1084. return -EINVAL;
  1085. }
  1086. return sprintf(buf, "%u\n", udev->lpm_charging_type_done);
  1087. }
  1088. #endif
  1089. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  1090. static const char *lock_string(enum usb_lock_state lock_state)
  1091. {
  1092. switch (lock_state) {
  1093. case USB_NOTIFY_INIT_STATE:
  1094. return "init";
  1095. case USB_NOTIFY_UNLOCK:
  1096. return "unlock";
  1097. case USB_NOTIFY_LOCK_USB_WORK:
  1098. return "usb work lock";
  1099. case USB_NOTIFY_LOCK_USB_RESTRICT:
  1100. return "usb restrict lock";
  1101. default:
  1102. return "undefined";
  1103. }
  1104. }
  1105. #endif
  1106. static ssize_t usb_sl_show(struct device *dev,
  1107. struct device_attribute *attr, char *buf)
  1108. {
  1109. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  1110. dev_get_drvdata(dev);
  1111. if (udev == NULL) {
  1112. unl_err("udev is NULL\n");
  1113. return -EINVAL;
  1114. }
  1115. unl_info("%s secure_lock = %lu\n",
  1116. __func__, udev->secure_lock);
  1117. return sprintf(buf, "%lu\n", udev->secure_lock);
  1118. }
  1119. static ssize_t usb_sl_store(
  1120. struct device *dev, struct device_attribute *attr,
  1121. const char *buf, size_t size)
  1122. {
  1123. struct usb_notify_dev *udev = (struct usb_notify_dev *)
  1124. dev_get_drvdata(dev);
  1125. unsigned long secure_lock = 0;
  1126. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  1127. unsigned long prev_secure_lock = 0;
  1128. #endif
  1129. int sret = -EINVAL;
  1130. size_t ret = -ENOMEM;
  1131. if (udev == NULL) {
  1132. unl_err("udev is NULL\n");
  1133. return -EINVAL;
  1134. }
  1135. if (size > PAGE_SIZE) {
  1136. unl_err("%s size(%zu) is too long.\n", __func__, size);
  1137. goto error;
  1138. }
  1139. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  1140. unl_info("%s before secure_lock = %s first_restrict = %d +\n",
  1141. __func__, lock_string(udev->secure_lock), udev->first_restrict);
  1142. #else
  1143. unl_info("%s before secure_lock = %lu +\n",
  1144. __func__, udev->secure_lock);
  1145. #endif
  1146. sret = sscanf(buf, "%lu", &secure_lock);
  1147. if (sret != 1)
  1148. goto error;
  1149. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  1150. prev_secure_lock = udev->secure_lock;
  1151. #endif
  1152. udev->secure_lock = secure_lock;
  1153. udev->set_lock_state(udev);
  1154. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  1155. if (prev_secure_lock == USB_NOTIFY_INIT_STATE
  1156. && secure_lock == USB_NOTIFY_LOCK_USB_RESTRICT) {
  1157. if (udev->set_disable) {
  1158. udev->set_disable(udev, NOTIFY_BLOCK_TYPE_ALL);
  1159. udev->first_restrict = true;
  1160. }
  1161. } else if (udev->first_restrict && prev_secure_lock == USB_NOTIFY_LOCK_USB_RESTRICT
  1162. && (secure_lock == USB_NOTIFY_UNLOCK
  1163. || secure_lock == USB_NOTIFY_LOCK_USB_WORK)) {
  1164. if (udev->set_disable) {
  1165. udev->set_disable(udev, NOTIFY_BLOCK_TYPE_NONE);
  1166. udev->first_restrict = false;
  1167. }
  1168. }
  1169. unl_info("%s after secure_lock = %s -\n",
  1170. __func__, lock_string(udev->secure_lock));
  1171. #else
  1172. unl_info("%s after secure_lock = %lu -\n",
  1173. __func__, udev->secure_lock);
  1174. #endif
  1175. ret = size;
  1176. error:
  1177. return ret;
  1178. }
  1179. static DEVICE_ATTR_RW(disable);
  1180. static DEVICE_ATTR_RW(usb_data_enabled);
  1181. static DEVICE_ATTR_RO(support);
  1182. static DEVICE_ATTR_RO(otg_speed);
  1183. static DEVICE_ATTR_RO(gadget_speed);
  1184. static DEVICE_ATTR_RW(usb_maximum_speed);
  1185. static DEVICE_ATTR_RW(whitelist_for_mdm);
  1186. static DEVICE_ATTR_RW(whitelist_for_disa);
  1187. static DEVICE_ATTR_RW(whitelist_for_serial);
  1188. static DEVICE_ATTR_RO(cards);
  1189. #if defined(CONFIG_USB_HW_PARAM)
  1190. static DEVICE_ATTR_RW(usb_hw_param);
  1191. static DEVICE_ATTR_RW(hw_param);
  1192. #endif
  1193. static DEVICE_ATTR_RW(usb_request_action);
  1194. #if defined(CONFIG_USB_LPM_CHARGING_SYNC)
  1195. static DEVICE_ATTR_RO(lpm_charging_type_done);
  1196. #endif
  1197. static DEVICE_ATTR_RW(usb_sl);
  1198. static struct attribute *usb_notify_attrs[] = {
  1199. &dev_attr_disable.attr,
  1200. &dev_attr_usb_data_enabled.attr,
  1201. &dev_attr_support.attr,
  1202. &dev_attr_otg_speed.attr,
  1203. &dev_attr_gadget_speed.attr,
  1204. &dev_attr_usb_maximum_speed.attr,
  1205. &dev_attr_whitelist_for_mdm.attr,
  1206. &dev_attr_whitelist_for_disa.attr,
  1207. &dev_attr_whitelist_for_serial.attr,
  1208. &dev_attr_cards.attr,
  1209. #if defined(CONFIG_USB_HW_PARAM)
  1210. &dev_attr_usb_hw_param.attr,
  1211. &dev_attr_hw_param.attr,
  1212. #endif
  1213. &dev_attr_usb_request_action.attr,
  1214. #if defined(CONFIG_USB_LPM_CHARGING_SYNC)
  1215. &dev_attr_lpm_charging_type_done.attr,
  1216. #endif
  1217. &dev_attr_usb_sl.attr,
  1218. NULL,
  1219. };
  1220. static struct attribute_group usb_notify_attr_grp = {
  1221. .attrs = usb_notify_attrs,
  1222. };
  1223. static int create_usb_notify_class(void)
  1224. {
  1225. if (!usb_notify_data.usb_notify_class) {
  1226. usb_notify_data.usb_notify_class
  1227. = class_create(THIS_MODULE, "usb_notify");
  1228. if (IS_ERR(usb_notify_data.usb_notify_class))
  1229. return PTR_ERR(usb_notify_data.usb_notify_class);
  1230. atomic_set(&usb_notify_data.device_count, 0);
  1231. }
  1232. return 0;
  1233. }
  1234. int usb_notify_dev_register(struct usb_notify_dev *udev)
  1235. {
  1236. int ret;
  1237. if (!usb_notify_data.usb_notify_class) {
  1238. ret = create_usb_notify_class();
  1239. if (ret < 0)
  1240. return ret;
  1241. }
  1242. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  1243. mutex_init(&udev->lockscreen_enabled_lock);
  1244. #endif
  1245. udev->index = atomic_inc_return(&usb_notify_data.device_count);
  1246. udev->dev = device_create(usb_notify_data.usb_notify_class, NULL,
  1247. MKDEV(0, udev->index), NULL, "%s", udev->name);
  1248. if (IS_ERR(udev->dev))
  1249. return PTR_ERR(udev->dev);
  1250. udev->disable_state = 0;
  1251. udev->usb_data_enabled = 1;
  1252. strncpy(udev->disable_state_cmd, "OFF",
  1253. sizeof(udev->disable_state_cmd)-1);
  1254. dev_set_drvdata(udev->dev, udev);
  1255. ret = sysfs_create_group(&udev->dev->kobj, &usb_notify_attr_grp);
  1256. if (ret < 0) {
  1257. device_destroy(usb_notify_data.usb_notify_class,
  1258. MKDEV(0, udev->index));
  1259. return ret;
  1260. }
  1261. return 0;
  1262. }
  1263. EXPORT_SYMBOL_GPL(usb_notify_dev_register);
  1264. void usb_notify_dev_unregister(struct usb_notify_dev *udev)
  1265. {
  1266. sysfs_remove_group(&udev->dev->kobj, &usb_notify_attr_grp);
  1267. device_destroy(usb_notify_data.usb_notify_class, MKDEV(0, udev->index));
  1268. dev_set_drvdata(udev->dev, NULL);
  1269. }
  1270. EXPORT_SYMBOL_GPL(usb_notify_dev_unregister);
  1271. int usb_notify_class_init(void)
  1272. {
  1273. return create_usb_notify_class();
  1274. }
  1275. EXPORT_SYMBOL_GPL(usb_notify_class_init);
  1276. void usb_notify_class_exit(void)
  1277. {
  1278. class_destroy(usb_notify_data.usb_notify_class);
  1279. }
  1280. EXPORT_SYMBOL_GPL(usb_notify_class_exit);