sysfs.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/usb/core/sysfs.c
  4. *
  5. * (C) Copyright 2002 David Brownell
  6. * (C) Copyright 2002,2004 Greg Kroah-Hartman
  7. * (C) Copyright 2002,2004 IBM Corp.
  8. *
  9. * All of the sysfs file attributes for usb devices and interfaces.
  10. *
  11. * Released under the GPLv2 only.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/kstrtox.h>
  15. #include <linux/string.h>
  16. #include <linux/usb.h>
  17. #include <linux/usb/hcd.h>
  18. #include <linux/usb/quirks.h>
  19. #include <linux/of.h>
  20. #include "usb.h"
  21. /* Active configuration fields */
  22. #define usb_actconfig_show(field, format_string) \
  23. static ssize_t field##_show(struct device *dev, \
  24. struct device_attribute *attr, char *buf) \
  25. { \
  26. struct usb_device *udev; \
  27. struct usb_host_config *actconfig; \
  28. ssize_t rc; \
  29. \
  30. udev = to_usb_device(dev); \
  31. rc = usb_lock_device_interruptible(udev); \
  32. if (rc < 0) \
  33. return -EINTR; \
  34. actconfig = udev->actconfig; \
  35. if (actconfig) \
  36. rc = sysfs_emit(buf, format_string, \
  37. actconfig->desc.field); \
  38. usb_unlock_device(udev); \
  39. return rc; \
  40. } \
  41. #define usb_actconfig_attr(field, format_string) \
  42. usb_actconfig_show(field, format_string) \
  43. static DEVICE_ATTR_RO(field)
  44. usb_actconfig_attr(bNumInterfaces, "%2d\n");
  45. usb_actconfig_attr(bmAttributes, "%2x\n");
  46. static ssize_t bMaxPower_show(struct device *dev,
  47. struct device_attribute *attr, char *buf)
  48. {
  49. struct usb_device *udev;
  50. struct usb_host_config *actconfig;
  51. ssize_t rc;
  52. udev = to_usb_device(dev);
  53. rc = usb_lock_device_interruptible(udev);
  54. if (rc < 0)
  55. return -EINTR;
  56. actconfig = udev->actconfig;
  57. if (actconfig)
  58. rc = sysfs_emit(buf, "%dmA\n", usb_get_max_power(udev, actconfig));
  59. usb_unlock_device(udev);
  60. return rc;
  61. }
  62. static DEVICE_ATTR_RO(bMaxPower);
  63. static ssize_t configuration_show(struct device *dev,
  64. struct device_attribute *attr, char *buf)
  65. {
  66. struct usb_device *udev;
  67. struct usb_host_config *actconfig;
  68. ssize_t rc;
  69. udev = to_usb_device(dev);
  70. rc = usb_lock_device_interruptible(udev);
  71. if (rc < 0)
  72. return -EINTR;
  73. actconfig = udev->actconfig;
  74. if (actconfig && actconfig->string)
  75. rc = sysfs_emit(buf, "%s\n", actconfig->string);
  76. usb_unlock_device(udev);
  77. return rc;
  78. }
  79. static DEVICE_ATTR_RO(configuration);
  80. /* configuration value is always present, and r/w */
  81. usb_actconfig_show(bConfigurationValue, "%u\n");
  82. static ssize_t bConfigurationValue_store(struct device *dev,
  83. struct device_attribute *attr,
  84. const char *buf, size_t count)
  85. {
  86. struct usb_device *udev = to_usb_device(dev);
  87. int config, value, rc;
  88. if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
  89. return -EINVAL;
  90. rc = usb_lock_device_interruptible(udev);
  91. if (rc < 0)
  92. return -EINTR;
  93. value = usb_set_configuration(udev, config);
  94. usb_unlock_device(udev);
  95. return (value < 0) ? value : count;
  96. }
  97. static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,
  98. bConfigurationValue_show, bConfigurationValue_store);
  99. #ifdef CONFIG_OF
  100. static ssize_t devspec_show(struct device *dev, struct device_attribute *attr,
  101. char *buf)
  102. {
  103. struct device_node *of_node = dev->of_node;
  104. return sysfs_emit(buf, "%pOF\n", of_node);
  105. }
  106. static DEVICE_ATTR_RO(devspec);
  107. #endif
  108. /* String fields */
  109. #define usb_string_attr(name) \
  110. static ssize_t name##_show(struct device *dev, \
  111. struct device_attribute *attr, char *buf) \
  112. { \
  113. struct usb_device *udev; \
  114. int retval; \
  115. \
  116. udev = to_usb_device(dev); \
  117. retval = usb_lock_device_interruptible(udev); \
  118. if (retval < 0) \
  119. return -EINTR; \
  120. retval = sysfs_emit(buf, "%s\n", udev->name); \
  121. usb_unlock_device(udev); \
  122. return retval; \
  123. } \
  124. static DEVICE_ATTR_RO(name)
  125. usb_string_attr(product);
  126. usb_string_attr(manufacturer);
  127. usb_string_attr(serial);
  128. static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
  129. char *buf)
  130. {
  131. struct usb_device *udev;
  132. char *speed;
  133. udev = to_usb_device(dev);
  134. switch (udev->speed) {
  135. case USB_SPEED_LOW:
  136. speed = "1.5";
  137. break;
  138. case USB_SPEED_UNKNOWN:
  139. case USB_SPEED_FULL:
  140. speed = "12";
  141. break;
  142. case USB_SPEED_HIGH:
  143. speed = "480";
  144. break;
  145. case USB_SPEED_SUPER:
  146. speed = "5000";
  147. break;
  148. case USB_SPEED_SUPER_PLUS:
  149. if (udev->ssp_rate == USB_SSP_GEN_2x2)
  150. speed = "20000";
  151. else
  152. speed = "10000";
  153. break;
  154. default:
  155. speed = "unknown";
  156. }
  157. return sysfs_emit(buf, "%s\n", speed);
  158. }
  159. static DEVICE_ATTR_RO(speed);
  160. static ssize_t rx_lanes_show(struct device *dev, struct device_attribute *attr,
  161. char *buf)
  162. {
  163. struct usb_device *udev;
  164. udev = to_usb_device(dev);
  165. return sysfs_emit(buf, "%d\n", udev->rx_lanes);
  166. }
  167. static DEVICE_ATTR_RO(rx_lanes);
  168. static ssize_t tx_lanes_show(struct device *dev, struct device_attribute *attr,
  169. char *buf)
  170. {
  171. struct usb_device *udev;
  172. udev = to_usb_device(dev);
  173. return sysfs_emit(buf, "%d\n", udev->tx_lanes);
  174. }
  175. static DEVICE_ATTR_RO(tx_lanes);
  176. static ssize_t busnum_show(struct device *dev, struct device_attribute *attr,
  177. char *buf)
  178. {
  179. struct usb_device *udev;
  180. udev = to_usb_device(dev);
  181. return sysfs_emit(buf, "%d\n", udev->bus->busnum);
  182. }
  183. static DEVICE_ATTR_RO(busnum);
  184. static ssize_t devnum_show(struct device *dev, struct device_attribute *attr,
  185. char *buf)
  186. {
  187. struct usb_device *udev;
  188. udev = to_usb_device(dev);
  189. return sysfs_emit(buf, "%d\n", udev->devnum);
  190. }
  191. static DEVICE_ATTR_RO(devnum);
  192. static ssize_t devpath_show(struct device *dev, struct device_attribute *attr,
  193. char *buf)
  194. {
  195. struct usb_device *udev;
  196. udev = to_usb_device(dev);
  197. return sysfs_emit(buf, "%s\n", udev->devpath);
  198. }
  199. static DEVICE_ATTR_RO(devpath);
  200. static ssize_t version_show(struct device *dev, struct device_attribute *attr,
  201. char *buf)
  202. {
  203. struct usb_device *udev;
  204. u16 bcdUSB;
  205. udev = to_usb_device(dev);
  206. bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
  207. return sysfs_emit(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
  208. }
  209. static DEVICE_ATTR_RO(version);
  210. static ssize_t maxchild_show(struct device *dev, struct device_attribute *attr,
  211. char *buf)
  212. {
  213. struct usb_device *udev;
  214. udev = to_usb_device(dev);
  215. return sysfs_emit(buf, "%d\n", udev->maxchild);
  216. }
  217. static DEVICE_ATTR_RO(maxchild);
  218. static ssize_t quirks_show(struct device *dev, struct device_attribute *attr,
  219. char *buf)
  220. {
  221. struct usb_device *udev;
  222. udev = to_usb_device(dev);
  223. return sysfs_emit(buf, "0x%x\n", udev->quirks);
  224. }
  225. static DEVICE_ATTR_RO(quirks);
  226. static ssize_t avoid_reset_quirk_show(struct device *dev,
  227. struct device_attribute *attr, char *buf)
  228. {
  229. struct usb_device *udev;
  230. udev = to_usb_device(dev);
  231. return sysfs_emit(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
  232. }
  233. static ssize_t avoid_reset_quirk_store(struct device *dev,
  234. struct device_attribute *attr,
  235. const char *buf, size_t count)
  236. {
  237. struct usb_device *udev = to_usb_device(dev);
  238. bool val;
  239. int rc;
  240. if (kstrtobool(buf, &val) != 0)
  241. return -EINVAL;
  242. rc = usb_lock_device_interruptible(udev);
  243. if (rc < 0)
  244. return -EINTR;
  245. if (val)
  246. udev->quirks |= USB_QUIRK_RESET;
  247. else
  248. udev->quirks &= ~USB_QUIRK_RESET;
  249. usb_unlock_device(udev);
  250. return count;
  251. }
  252. static DEVICE_ATTR_RW(avoid_reset_quirk);
  253. static ssize_t urbnum_show(struct device *dev, struct device_attribute *attr,
  254. char *buf)
  255. {
  256. struct usb_device *udev;
  257. udev = to_usb_device(dev);
  258. return sysfs_emit(buf, "%d\n", atomic_read(&udev->urbnum));
  259. }
  260. static DEVICE_ATTR_RO(urbnum);
  261. static ssize_t ltm_capable_show(struct device *dev,
  262. struct device_attribute *attr, char *buf)
  263. {
  264. if (usb_device_supports_ltm(to_usb_device(dev)))
  265. return sysfs_emit(buf, "%s\n", "yes");
  266. return sysfs_emit(buf, "%s\n", "no");
  267. }
  268. static DEVICE_ATTR_RO(ltm_capable);
  269. #ifdef CONFIG_PM
  270. static ssize_t persist_show(struct device *dev, struct device_attribute *attr,
  271. char *buf)
  272. {
  273. struct usb_device *udev = to_usb_device(dev);
  274. return sysfs_emit(buf, "%d\n", udev->persist_enabled);
  275. }
  276. static ssize_t persist_store(struct device *dev, struct device_attribute *attr,
  277. const char *buf, size_t count)
  278. {
  279. struct usb_device *udev = to_usb_device(dev);
  280. bool value;
  281. int rc;
  282. /* Hubs are always enabled for USB_PERSIST */
  283. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  284. return -EPERM;
  285. if (kstrtobool(buf, &value) != 0)
  286. return -EINVAL;
  287. rc = usb_lock_device_interruptible(udev);
  288. if (rc < 0)
  289. return -EINTR;
  290. udev->persist_enabled = !!value;
  291. usb_unlock_device(udev);
  292. return count;
  293. }
  294. static DEVICE_ATTR_RW(persist);
  295. static int add_persist_attributes(struct device *dev)
  296. {
  297. int rc = 0;
  298. if (is_usb_device(dev)) {
  299. struct usb_device *udev = to_usb_device(dev);
  300. /* Hubs are automatically enabled for USB_PERSIST,
  301. * no point in creating the attribute file.
  302. */
  303. if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
  304. rc = sysfs_add_file_to_group(&dev->kobj,
  305. &dev_attr_persist.attr,
  306. power_group_name);
  307. }
  308. return rc;
  309. }
  310. static void remove_persist_attributes(struct device *dev)
  311. {
  312. sysfs_remove_file_from_group(&dev->kobj,
  313. &dev_attr_persist.attr,
  314. power_group_name);
  315. }
  316. static ssize_t connected_duration_show(struct device *dev,
  317. struct device_attribute *attr, char *buf)
  318. {
  319. struct usb_device *udev = to_usb_device(dev);
  320. return sysfs_emit(buf, "%u\n",
  321. jiffies_to_msecs(jiffies - udev->connect_time));
  322. }
  323. static DEVICE_ATTR_RO(connected_duration);
  324. /*
  325. * If the device is resumed, the last time the device was suspended has
  326. * been pre-subtracted from active_duration. We add the current time to
  327. * get the duration that the device was actually active.
  328. *
  329. * If the device is suspended, the active_duration is up-to-date.
  330. */
  331. static ssize_t active_duration_show(struct device *dev,
  332. struct device_attribute *attr, char *buf)
  333. {
  334. struct usb_device *udev = to_usb_device(dev);
  335. int duration;
  336. if (udev->state != USB_STATE_SUSPENDED)
  337. duration = jiffies_to_msecs(jiffies + udev->active_duration);
  338. else
  339. duration = jiffies_to_msecs(udev->active_duration);
  340. return sysfs_emit(buf, "%u\n", duration);
  341. }
  342. static DEVICE_ATTR_RO(active_duration);
  343. static ssize_t autosuspend_show(struct device *dev,
  344. struct device_attribute *attr, char *buf)
  345. {
  346. return sysfs_emit(buf, "%d\n", dev->power.autosuspend_delay / 1000);
  347. }
  348. static ssize_t autosuspend_store(struct device *dev,
  349. struct device_attribute *attr, const char *buf,
  350. size_t count)
  351. {
  352. int value;
  353. if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
  354. value <= -INT_MAX/1000)
  355. return -EINVAL;
  356. pm_runtime_set_autosuspend_delay(dev, value * 1000);
  357. return count;
  358. }
  359. static DEVICE_ATTR_RW(autosuspend);
  360. static const char on_string[] = "on";
  361. static const char auto_string[] = "auto";
  362. static void warn_level(void)
  363. {
  364. static int level_warned;
  365. if (!level_warned) {
  366. level_warned = 1;
  367. printk(KERN_WARNING "WARNING! power/level is deprecated; "
  368. "use power/control instead\n");
  369. }
  370. }
  371. static ssize_t level_show(struct device *dev, struct device_attribute *attr,
  372. char *buf)
  373. {
  374. struct usb_device *udev = to_usb_device(dev);
  375. const char *p = auto_string;
  376. warn_level();
  377. if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
  378. p = on_string;
  379. return sysfs_emit(buf, "%s\n", p);
  380. }
  381. static ssize_t level_store(struct device *dev, struct device_attribute *attr,
  382. const char *buf, size_t count)
  383. {
  384. struct usb_device *udev = to_usb_device(dev);
  385. int len = count;
  386. char *cp;
  387. int rc = count;
  388. int rv;
  389. warn_level();
  390. cp = memchr(buf, '\n', count);
  391. if (cp)
  392. len = cp - buf;
  393. rv = usb_lock_device_interruptible(udev);
  394. if (rv < 0)
  395. return -EINTR;
  396. if (len == sizeof on_string - 1 &&
  397. strncmp(buf, on_string, len) == 0)
  398. usb_disable_autosuspend(udev);
  399. else if (len == sizeof auto_string - 1 &&
  400. strncmp(buf, auto_string, len) == 0)
  401. usb_enable_autosuspend(udev);
  402. else
  403. rc = -EINVAL;
  404. usb_unlock_device(udev);
  405. return rc;
  406. }
  407. static DEVICE_ATTR_RW(level);
  408. static ssize_t usb2_hardware_lpm_show(struct device *dev,
  409. struct device_attribute *attr, char *buf)
  410. {
  411. struct usb_device *udev = to_usb_device(dev);
  412. const char *p;
  413. if (udev->usb2_hw_lpm_allowed == 1)
  414. p = "enabled";
  415. else
  416. p = "disabled";
  417. return sysfs_emit(buf, "%s\n", p);
  418. }
  419. static ssize_t usb2_hardware_lpm_store(struct device *dev,
  420. struct device_attribute *attr,
  421. const char *buf, size_t count)
  422. {
  423. struct usb_device *udev = to_usb_device(dev);
  424. bool value;
  425. int ret;
  426. ret = usb_lock_device_interruptible(udev);
  427. if (ret < 0)
  428. return -EINTR;
  429. ret = kstrtobool(buf, &value);
  430. if (!ret) {
  431. udev->usb2_hw_lpm_allowed = value;
  432. if (value)
  433. ret = usb_enable_usb2_hardware_lpm(udev);
  434. else
  435. ret = usb_disable_usb2_hardware_lpm(udev);
  436. }
  437. usb_unlock_device(udev);
  438. if (!ret)
  439. return count;
  440. return ret;
  441. }
  442. static DEVICE_ATTR_RW(usb2_hardware_lpm);
  443. static ssize_t usb2_lpm_l1_timeout_show(struct device *dev,
  444. struct device_attribute *attr,
  445. char *buf)
  446. {
  447. struct usb_device *udev = to_usb_device(dev);
  448. return sysfs_emit(buf, "%d\n", udev->l1_params.timeout);
  449. }
  450. static ssize_t usb2_lpm_l1_timeout_store(struct device *dev,
  451. struct device_attribute *attr,
  452. const char *buf, size_t count)
  453. {
  454. struct usb_device *udev = to_usb_device(dev);
  455. u16 timeout;
  456. if (kstrtou16(buf, 0, &timeout))
  457. return -EINVAL;
  458. udev->l1_params.timeout = timeout;
  459. return count;
  460. }
  461. static DEVICE_ATTR_RW(usb2_lpm_l1_timeout);
  462. static ssize_t usb2_lpm_besl_show(struct device *dev,
  463. struct device_attribute *attr, char *buf)
  464. {
  465. struct usb_device *udev = to_usb_device(dev);
  466. return sysfs_emit(buf, "%d\n", udev->l1_params.besl);
  467. }
  468. static ssize_t usb2_lpm_besl_store(struct device *dev,
  469. struct device_attribute *attr,
  470. const char *buf, size_t count)
  471. {
  472. struct usb_device *udev = to_usb_device(dev);
  473. u8 besl;
  474. if (kstrtou8(buf, 0, &besl) || besl > 15)
  475. return -EINVAL;
  476. udev->l1_params.besl = besl;
  477. return count;
  478. }
  479. static DEVICE_ATTR_RW(usb2_lpm_besl);
  480. static ssize_t usb3_hardware_lpm_u1_show(struct device *dev,
  481. struct device_attribute *attr, char *buf)
  482. {
  483. struct usb_device *udev = to_usb_device(dev);
  484. const char *p;
  485. int rc;
  486. rc = usb_lock_device_interruptible(udev);
  487. if (rc < 0)
  488. return -EINTR;
  489. if (udev->usb3_lpm_u1_enabled)
  490. p = "enabled";
  491. else
  492. p = "disabled";
  493. usb_unlock_device(udev);
  494. return sysfs_emit(buf, "%s\n", p);
  495. }
  496. static DEVICE_ATTR_RO(usb3_hardware_lpm_u1);
  497. static ssize_t usb3_hardware_lpm_u2_show(struct device *dev,
  498. struct device_attribute *attr, char *buf)
  499. {
  500. struct usb_device *udev = to_usb_device(dev);
  501. const char *p;
  502. int rc;
  503. rc = usb_lock_device_interruptible(udev);
  504. if (rc < 0)
  505. return -EINTR;
  506. if (udev->usb3_lpm_u2_enabled)
  507. p = "enabled";
  508. else
  509. p = "disabled";
  510. usb_unlock_device(udev);
  511. return sysfs_emit(buf, "%s\n", p);
  512. }
  513. static DEVICE_ATTR_RO(usb3_hardware_lpm_u2);
  514. static struct attribute *usb2_hardware_lpm_attr[] = {
  515. &dev_attr_usb2_hardware_lpm.attr,
  516. &dev_attr_usb2_lpm_l1_timeout.attr,
  517. &dev_attr_usb2_lpm_besl.attr,
  518. NULL,
  519. };
  520. static const struct attribute_group usb2_hardware_lpm_attr_group = {
  521. .name = power_group_name,
  522. .attrs = usb2_hardware_lpm_attr,
  523. };
  524. static struct attribute *usb3_hardware_lpm_attr[] = {
  525. &dev_attr_usb3_hardware_lpm_u1.attr,
  526. &dev_attr_usb3_hardware_lpm_u2.attr,
  527. NULL,
  528. };
  529. static const struct attribute_group usb3_hardware_lpm_attr_group = {
  530. .name = power_group_name,
  531. .attrs = usb3_hardware_lpm_attr,
  532. };
  533. static struct attribute *power_attrs[] = {
  534. &dev_attr_autosuspend.attr,
  535. &dev_attr_level.attr,
  536. &dev_attr_connected_duration.attr,
  537. &dev_attr_active_duration.attr,
  538. NULL,
  539. };
  540. static const struct attribute_group power_attr_group = {
  541. .name = power_group_name,
  542. .attrs = power_attrs,
  543. };
  544. static int add_power_attributes(struct device *dev)
  545. {
  546. int rc = 0;
  547. if (is_usb_device(dev)) {
  548. struct usb_device *udev = to_usb_device(dev);
  549. rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
  550. if (udev->usb2_hw_lpm_capable == 1)
  551. rc = sysfs_merge_group(&dev->kobj,
  552. &usb2_hardware_lpm_attr_group);
  553. if ((udev->speed == USB_SPEED_SUPER ||
  554. udev->speed == USB_SPEED_SUPER_PLUS) &&
  555. udev->lpm_capable == 1)
  556. rc = sysfs_merge_group(&dev->kobj,
  557. &usb3_hardware_lpm_attr_group);
  558. }
  559. return rc;
  560. }
  561. static void remove_power_attributes(struct device *dev)
  562. {
  563. sysfs_unmerge_group(&dev->kobj, &usb3_hardware_lpm_attr_group);
  564. sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
  565. sysfs_unmerge_group(&dev->kobj, &power_attr_group);
  566. }
  567. #else
  568. #define add_persist_attributes(dev) 0
  569. #define remove_persist_attributes(dev) do {} while (0)
  570. #define add_power_attributes(dev) 0
  571. #define remove_power_attributes(dev) do {} while (0)
  572. #endif /* CONFIG_PM */
  573. /* Descriptor fields */
  574. #define usb_descriptor_attr_le16(field, format_string) \
  575. static ssize_t \
  576. field##_show(struct device *dev, struct device_attribute *attr, \
  577. char *buf) \
  578. { \
  579. struct usb_device *udev; \
  580. \
  581. udev = to_usb_device(dev); \
  582. return sysfs_emit(buf, format_string, \
  583. le16_to_cpu(udev->descriptor.field)); \
  584. } \
  585. static DEVICE_ATTR_RO(field)
  586. usb_descriptor_attr_le16(idVendor, "%04x\n");
  587. usb_descriptor_attr_le16(idProduct, "%04x\n");
  588. usb_descriptor_attr_le16(bcdDevice, "%04x\n");
  589. #define usb_descriptor_attr(field, format_string) \
  590. static ssize_t \
  591. field##_show(struct device *dev, struct device_attribute *attr, \
  592. char *buf) \
  593. { \
  594. struct usb_device *udev; \
  595. \
  596. udev = to_usb_device(dev); \
  597. return sysfs_emit(buf, format_string, udev->descriptor.field); \
  598. } \
  599. static DEVICE_ATTR_RO(field)
  600. usb_descriptor_attr(bDeviceClass, "%02x\n");
  601. usb_descriptor_attr(bDeviceSubClass, "%02x\n");
  602. usb_descriptor_attr(bDeviceProtocol, "%02x\n");
  603. usb_descriptor_attr(bNumConfigurations, "%d\n");
  604. usb_descriptor_attr(bMaxPacketSize0, "%d\n");
  605. /* show if the device is authorized (1) or not (0) */
  606. static ssize_t authorized_show(struct device *dev,
  607. struct device_attribute *attr, char *buf)
  608. {
  609. struct usb_device *usb_dev = to_usb_device(dev);
  610. return sysfs_emit(buf, "%u\n", usb_dev->authorized);
  611. }
  612. /*
  613. * Authorize a device to be used in the system
  614. *
  615. * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
  616. */
  617. static ssize_t authorized_store(struct device *dev,
  618. struct device_attribute *attr, const char *buf,
  619. size_t size)
  620. {
  621. ssize_t result;
  622. struct usb_device *usb_dev = to_usb_device(dev);
  623. bool val;
  624. if (kstrtobool(buf, &val) != 0)
  625. result = -EINVAL;
  626. else if (val)
  627. result = usb_authorize_device(usb_dev);
  628. else
  629. result = usb_deauthorize_device(usb_dev);
  630. return result < 0 ? result : size;
  631. }
  632. static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
  633. authorized_show, authorized_store);
  634. /* "Safely remove a device" */
  635. static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
  636. const char *buf, size_t count)
  637. {
  638. struct usb_device *udev = to_usb_device(dev);
  639. int rc = 0;
  640. usb_lock_device(udev);
  641. if (udev->state != USB_STATE_NOTATTACHED) {
  642. /* To avoid races, first unconfigure and then remove */
  643. usb_set_configuration(udev, -1);
  644. rc = usb_remove_device(udev);
  645. }
  646. if (rc == 0)
  647. rc = count;
  648. usb_unlock_device(udev);
  649. return rc;
  650. }
  651. static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store);
  652. static struct attribute *dev_attrs[] = {
  653. /* current configuration's attributes */
  654. &dev_attr_configuration.attr,
  655. &dev_attr_bNumInterfaces.attr,
  656. &dev_attr_bConfigurationValue.attr,
  657. &dev_attr_bmAttributes.attr,
  658. &dev_attr_bMaxPower.attr,
  659. /* device attributes */
  660. &dev_attr_urbnum.attr,
  661. &dev_attr_idVendor.attr,
  662. &dev_attr_idProduct.attr,
  663. &dev_attr_bcdDevice.attr,
  664. &dev_attr_bDeviceClass.attr,
  665. &dev_attr_bDeviceSubClass.attr,
  666. &dev_attr_bDeviceProtocol.attr,
  667. &dev_attr_bNumConfigurations.attr,
  668. &dev_attr_bMaxPacketSize0.attr,
  669. &dev_attr_speed.attr,
  670. &dev_attr_rx_lanes.attr,
  671. &dev_attr_tx_lanes.attr,
  672. &dev_attr_busnum.attr,
  673. &dev_attr_devnum.attr,
  674. &dev_attr_devpath.attr,
  675. &dev_attr_version.attr,
  676. &dev_attr_maxchild.attr,
  677. &dev_attr_quirks.attr,
  678. &dev_attr_avoid_reset_quirk.attr,
  679. &dev_attr_authorized.attr,
  680. &dev_attr_remove.attr,
  681. &dev_attr_ltm_capable.attr,
  682. #ifdef CONFIG_OF
  683. &dev_attr_devspec.attr,
  684. #endif
  685. NULL,
  686. };
  687. static const struct attribute_group dev_attr_grp = {
  688. .attrs = dev_attrs,
  689. };
  690. /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
  691. * accordingly.
  692. */
  693. static struct attribute *dev_string_attrs[] = {
  694. &dev_attr_manufacturer.attr,
  695. &dev_attr_product.attr,
  696. &dev_attr_serial.attr,
  697. NULL
  698. };
  699. static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
  700. struct attribute *a, int n)
  701. {
  702. struct device *dev = kobj_to_dev(kobj);
  703. struct usb_device *udev = to_usb_device(dev);
  704. if (a == &dev_attr_manufacturer.attr) {
  705. if (udev->manufacturer == NULL)
  706. return 0;
  707. } else if (a == &dev_attr_product.attr) {
  708. if (udev->product == NULL)
  709. return 0;
  710. } else if (a == &dev_attr_serial.attr) {
  711. if (udev->serial == NULL)
  712. return 0;
  713. }
  714. return a->mode;
  715. }
  716. static const struct attribute_group dev_string_attr_grp = {
  717. .attrs = dev_string_attrs,
  718. .is_visible = dev_string_attrs_are_visible,
  719. };
  720. /* Binary descriptors */
  721. static ssize_t
  722. descriptors_read(struct file *filp, struct kobject *kobj,
  723. struct bin_attribute *attr,
  724. char *buf, loff_t off, size_t count)
  725. {
  726. struct device *dev = kobj_to_dev(kobj);
  727. struct usb_device *udev = to_usb_device(dev);
  728. size_t nleft = count;
  729. size_t srclen, n;
  730. int cfgno;
  731. void *src;
  732. /* The binary attribute begins with the device descriptor.
  733. * Following that are the raw descriptor entries for all the
  734. * configurations (config plus subsidiary descriptors).
  735. */
  736. for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
  737. nleft > 0; ++cfgno) {
  738. if (cfgno < 0) {
  739. src = &udev->descriptor;
  740. srclen = sizeof(struct usb_device_descriptor);
  741. } else {
  742. src = udev->rawdescriptors[cfgno];
  743. srclen = le16_to_cpu(udev->config[cfgno].desc.
  744. wTotalLength);
  745. }
  746. if (off < srclen) {
  747. n = min(nleft, srclen - (size_t) off);
  748. memcpy(buf, src + off, n);
  749. nleft -= n;
  750. buf += n;
  751. off = 0;
  752. } else {
  753. off -= srclen;
  754. }
  755. }
  756. return count - nleft;
  757. }
  758. static BIN_ATTR_RO(descriptors, 18 + 65535); /* dev descr + max-size raw descriptor */
  759. static ssize_t
  760. bos_descriptors_read(struct file *filp, struct kobject *kobj,
  761. struct bin_attribute *attr,
  762. char *buf, loff_t off, size_t count)
  763. {
  764. struct device *dev = kobj_to_dev(kobj);
  765. struct usb_device *udev = to_usb_device(dev);
  766. struct usb_host_bos *bos = udev->bos;
  767. struct usb_bos_descriptor *desc;
  768. size_t desclen, n = 0;
  769. if (bos) {
  770. desc = bos->desc;
  771. desclen = le16_to_cpu(desc->wTotalLength);
  772. if (off < desclen) {
  773. n = min(count, desclen - (size_t) off);
  774. memcpy(buf, (void *) desc + off, n);
  775. }
  776. }
  777. return n;
  778. }
  779. static BIN_ATTR_RO(bos_descriptors, 65535); /* max-size BOS */
  780. /* When modifying this list, be sure to modify dev_bin_attrs_are_visible()
  781. * accordingly.
  782. */
  783. static struct bin_attribute *dev_bin_attrs[] = {
  784. &bin_attr_descriptors,
  785. &bin_attr_bos_descriptors,
  786. NULL
  787. };
  788. static umode_t dev_bin_attrs_are_visible(struct kobject *kobj,
  789. struct bin_attribute *a, int n)
  790. {
  791. struct device *dev = kobj_to_dev(kobj);
  792. struct usb_device *udev = to_usb_device(dev);
  793. /*
  794. * There's no need to check if the descriptors attribute should
  795. * be visible because all devices have a device descriptor. The
  796. * bos_descriptors attribute should be visible if and only if
  797. * the device has a BOS, so check if it exists here.
  798. */
  799. if (a == &bin_attr_bos_descriptors) {
  800. if (udev->bos == NULL)
  801. return 0;
  802. }
  803. return a->attr.mode;
  804. }
  805. static const struct attribute_group dev_bin_attr_grp = {
  806. .bin_attrs = dev_bin_attrs,
  807. .is_bin_visible = dev_bin_attrs_are_visible,
  808. };
  809. const struct attribute_group *usb_device_groups[] = {
  810. &dev_attr_grp,
  811. &dev_string_attr_grp,
  812. &dev_bin_attr_grp,
  813. NULL
  814. };
  815. /*
  816. * Show & store the current value of authorized_default
  817. */
  818. static ssize_t authorized_default_show(struct device *dev,
  819. struct device_attribute *attr, char *buf)
  820. {
  821. struct usb_device *rh_usb_dev = to_usb_device(dev);
  822. struct usb_bus *usb_bus = rh_usb_dev->bus;
  823. struct usb_hcd *hcd;
  824. hcd = bus_to_hcd(usb_bus);
  825. return sysfs_emit(buf, "%u\n", hcd->dev_policy);
  826. }
  827. static ssize_t authorized_default_store(struct device *dev,
  828. struct device_attribute *attr,
  829. const char *buf, size_t size)
  830. {
  831. ssize_t result;
  832. unsigned int val;
  833. struct usb_device *rh_usb_dev = to_usb_device(dev);
  834. struct usb_bus *usb_bus = rh_usb_dev->bus;
  835. struct usb_hcd *hcd;
  836. hcd = bus_to_hcd(usb_bus);
  837. result = sscanf(buf, "%u\n", &val);
  838. if (result == 1) {
  839. hcd->dev_policy = val <= USB_DEVICE_AUTHORIZE_INTERNAL ?
  840. val : USB_DEVICE_AUTHORIZE_ALL;
  841. result = size;
  842. } else {
  843. result = -EINVAL;
  844. }
  845. return result;
  846. }
  847. static DEVICE_ATTR_RW(authorized_default);
  848. /*
  849. * interface_authorized_default_show - show default authorization status
  850. * for USB interfaces
  851. *
  852. * note: interface_authorized_default is the default value
  853. * for initializing the authorized attribute of interfaces
  854. */
  855. static ssize_t interface_authorized_default_show(struct device *dev,
  856. struct device_attribute *attr, char *buf)
  857. {
  858. struct usb_device *usb_dev = to_usb_device(dev);
  859. struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
  860. return sysfs_emit(buf, "%u\n", !!HCD_INTF_AUTHORIZED(hcd));
  861. }
  862. /*
  863. * interface_authorized_default_store - store default authorization status
  864. * for USB interfaces
  865. *
  866. * note: interface_authorized_default is the default value
  867. * for initializing the authorized attribute of interfaces
  868. */
  869. static ssize_t interface_authorized_default_store(struct device *dev,
  870. struct device_attribute *attr, const char *buf, size_t count)
  871. {
  872. struct usb_device *usb_dev = to_usb_device(dev);
  873. struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
  874. int rc = count;
  875. bool val;
  876. if (kstrtobool(buf, &val) != 0)
  877. return -EINVAL;
  878. if (val)
  879. set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
  880. else
  881. clear_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
  882. return rc;
  883. }
  884. static DEVICE_ATTR_RW(interface_authorized_default);
  885. /* Group all the USB bus attributes */
  886. static struct attribute *usb_bus_attrs[] = {
  887. &dev_attr_authorized_default.attr,
  888. &dev_attr_interface_authorized_default.attr,
  889. NULL,
  890. };
  891. static const struct attribute_group usb_bus_attr_group = {
  892. .name = NULL, /* we want them in the same directory */
  893. .attrs = usb_bus_attrs,
  894. };
  895. static int add_default_authorized_attributes(struct device *dev)
  896. {
  897. int rc = 0;
  898. if (is_usb_device(dev))
  899. rc = sysfs_create_group(&dev->kobj, &usb_bus_attr_group);
  900. return rc;
  901. }
  902. static void remove_default_authorized_attributes(struct device *dev)
  903. {
  904. if (is_usb_device(dev)) {
  905. sysfs_remove_group(&dev->kobj, &usb_bus_attr_group);
  906. }
  907. }
  908. int usb_create_sysfs_dev_files(struct usb_device *udev)
  909. {
  910. struct device *dev = &udev->dev;
  911. int retval;
  912. retval = add_persist_attributes(dev);
  913. if (retval)
  914. goto error;
  915. retval = add_power_attributes(dev);
  916. if (retval)
  917. goto error;
  918. if (is_root_hub(udev)) {
  919. retval = add_default_authorized_attributes(dev);
  920. if (retval)
  921. goto error;
  922. }
  923. return retval;
  924. error:
  925. usb_remove_sysfs_dev_files(udev);
  926. return retval;
  927. }
  928. void usb_remove_sysfs_dev_files(struct usb_device *udev)
  929. {
  930. struct device *dev = &udev->dev;
  931. if (is_root_hub(udev))
  932. remove_default_authorized_attributes(dev);
  933. remove_power_attributes(dev);
  934. remove_persist_attributes(dev);
  935. }
  936. /* Interface Association Descriptor fields */
  937. #define usb_intf_assoc_attr(field, format_string) \
  938. static ssize_t \
  939. iad_##field##_show(struct device *dev, struct device_attribute *attr, \
  940. char *buf) \
  941. { \
  942. struct usb_interface *intf = to_usb_interface(dev); \
  943. \
  944. return sysfs_emit(buf, format_string, \
  945. intf->intf_assoc->field); \
  946. } \
  947. static DEVICE_ATTR_RO(iad_##field)
  948. usb_intf_assoc_attr(bFirstInterface, "%02x\n");
  949. usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
  950. usb_intf_assoc_attr(bFunctionClass, "%02x\n");
  951. usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
  952. usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
  953. /* Interface fields */
  954. #define usb_intf_attr(field, format_string) \
  955. static ssize_t \
  956. field##_show(struct device *dev, struct device_attribute *attr, \
  957. char *buf) \
  958. { \
  959. struct usb_interface *intf = to_usb_interface(dev); \
  960. \
  961. return sysfs_emit(buf, format_string, \
  962. intf->cur_altsetting->desc.field); \
  963. } \
  964. static DEVICE_ATTR_RO(field)
  965. usb_intf_attr(bInterfaceNumber, "%02x\n");
  966. usb_intf_attr(bAlternateSetting, "%2d\n");
  967. usb_intf_attr(bNumEndpoints, "%02x\n");
  968. usb_intf_attr(bInterfaceClass, "%02x\n");
  969. usb_intf_attr(bInterfaceSubClass, "%02x\n");
  970. usb_intf_attr(bInterfaceProtocol, "%02x\n");
  971. static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
  972. char *buf)
  973. {
  974. struct usb_interface *intf;
  975. char *string;
  976. intf = to_usb_interface(dev);
  977. string = READ_ONCE(intf->cur_altsetting->string);
  978. if (!string)
  979. return 0;
  980. return sysfs_emit(buf, "%s\n", string);
  981. }
  982. static DEVICE_ATTR_RO(interface);
  983. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  984. char *buf)
  985. {
  986. struct usb_interface *intf;
  987. struct usb_device *udev;
  988. struct usb_host_interface *alt;
  989. intf = to_usb_interface(dev);
  990. udev = interface_to_usbdev(intf);
  991. alt = READ_ONCE(intf->cur_altsetting);
  992. return sysfs_emit(buf,
  993. "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  994. "ic%02Xisc%02Xip%02Xin%02X\n",
  995. le16_to_cpu(udev->descriptor.idVendor),
  996. le16_to_cpu(udev->descriptor.idProduct),
  997. le16_to_cpu(udev->descriptor.bcdDevice),
  998. udev->descriptor.bDeviceClass,
  999. udev->descriptor.bDeviceSubClass,
  1000. udev->descriptor.bDeviceProtocol,
  1001. alt->desc.bInterfaceClass,
  1002. alt->desc.bInterfaceSubClass,
  1003. alt->desc.bInterfaceProtocol,
  1004. alt->desc.bInterfaceNumber);
  1005. }
  1006. static DEVICE_ATTR_RO(modalias);
  1007. static ssize_t supports_autosuspend_show(struct device *dev,
  1008. struct device_attribute *attr,
  1009. char *buf)
  1010. {
  1011. int s;
  1012. s = device_lock_interruptible(dev);
  1013. if (s < 0)
  1014. return -EINTR;
  1015. /* Devices will be autosuspended even when an interface isn't claimed */
  1016. s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend);
  1017. device_unlock(dev);
  1018. return sysfs_emit(buf, "%u\n", s);
  1019. }
  1020. static DEVICE_ATTR_RO(supports_autosuspend);
  1021. /*
  1022. * interface_authorized_show - show authorization status of an USB interface
  1023. * 1 is authorized, 0 is deauthorized
  1024. */
  1025. static ssize_t interface_authorized_show(struct device *dev,
  1026. struct device_attribute *attr, char *buf)
  1027. {
  1028. struct usb_interface *intf = to_usb_interface(dev);
  1029. return sysfs_emit(buf, "%u\n", intf->authorized);
  1030. }
  1031. /*
  1032. * interface_authorized_store - authorize or deauthorize an USB interface
  1033. */
  1034. static ssize_t interface_authorized_store(struct device *dev,
  1035. struct device_attribute *attr, const char *buf, size_t count)
  1036. {
  1037. struct usb_interface *intf = to_usb_interface(dev);
  1038. bool val;
  1039. struct kernfs_node *kn;
  1040. if (kstrtobool(buf, &val) != 0)
  1041. return -EINVAL;
  1042. if (val) {
  1043. usb_authorize_interface(intf);
  1044. } else {
  1045. /*
  1046. * Prevent deadlock if another process is concurrently
  1047. * trying to unregister intf.
  1048. */
  1049. kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
  1050. if (kn) {
  1051. usb_deauthorize_interface(intf);
  1052. sysfs_unbreak_active_protection(kn);
  1053. }
  1054. }
  1055. return count;
  1056. }
  1057. static struct device_attribute dev_attr_interface_authorized =
  1058. __ATTR(authorized, S_IRUGO | S_IWUSR,
  1059. interface_authorized_show, interface_authorized_store);
  1060. static struct attribute *intf_attrs[] = {
  1061. &dev_attr_bInterfaceNumber.attr,
  1062. &dev_attr_bAlternateSetting.attr,
  1063. &dev_attr_bNumEndpoints.attr,
  1064. &dev_attr_bInterfaceClass.attr,
  1065. &dev_attr_bInterfaceSubClass.attr,
  1066. &dev_attr_bInterfaceProtocol.attr,
  1067. &dev_attr_modalias.attr,
  1068. &dev_attr_supports_autosuspend.attr,
  1069. &dev_attr_interface_authorized.attr,
  1070. NULL,
  1071. };
  1072. static const struct attribute_group intf_attr_grp = {
  1073. .attrs = intf_attrs,
  1074. };
  1075. static struct attribute *intf_assoc_attrs[] = {
  1076. &dev_attr_iad_bFirstInterface.attr,
  1077. &dev_attr_iad_bInterfaceCount.attr,
  1078. &dev_attr_iad_bFunctionClass.attr,
  1079. &dev_attr_iad_bFunctionSubClass.attr,
  1080. &dev_attr_iad_bFunctionProtocol.attr,
  1081. NULL,
  1082. };
  1083. static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
  1084. struct attribute *a, int n)
  1085. {
  1086. struct device *dev = kobj_to_dev(kobj);
  1087. struct usb_interface *intf = to_usb_interface(dev);
  1088. if (intf->intf_assoc == NULL)
  1089. return 0;
  1090. return a->mode;
  1091. }
  1092. static const struct attribute_group intf_assoc_attr_grp = {
  1093. .attrs = intf_assoc_attrs,
  1094. .is_visible = intf_assoc_attrs_are_visible,
  1095. };
  1096. static ssize_t wireless_status_show(struct device *dev,
  1097. struct device_attribute *attr, char *buf)
  1098. {
  1099. struct usb_interface *intf;
  1100. intf = to_usb_interface(dev);
  1101. if (intf->wireless_status == USB_WIRELESS_STATUS_DISCONNECTED)
  1102. return sysfs_emit(buf, "%s\n", "disconnected");
  1103. return sysfs_emit(buf, "%s\n", "connected");
  1104. }
  1105. static DEVICE_ATTR_RO(wireless_status);
  1106. static struct attribute *intf_wireless_status_attrs[] = {
  1107. &dev_attr_wireless_status.attr,
  1108. NULL
  1109. };
  1110. static umode_t intf_wireless_status_attr_is_visible(struct kobject *kobj,
  1111. struct attribute *a, int n)
  1112. {
  1113. struct device *dev = kobj_to_dev(kobj);
  1114. struct usb_interface *intf = to_usb_interface(dev);
  1115. if (a != &dev_attr_wireless_status.attr ||
  1116. intf->wireless_status != USB_WIRELESS_STATUS_NA)
  1117. return a->mode;
  1118. return 0;
  1119. }
  1120. static const struct attribute_group intf_wireless_status_attr_grp = {
  1121. .attrs = intf_wireless_status_attrs,
  1122. .is_visible = intf_wireless_status_attr_is_visible,
  1123. };
  1124. int usb_update_wireless_status_attr(struct usb_interface *intf)
  1125. {
  1126. struct device *dev = &intf->dev;
  1127. int ret;
  1128. ret = sysfs_update_group(&dev->kobj, &intf_wireless_status_attr_grp);
  1129. if (ret < 0)
  1130. return ret;
  1131. sysfs_notify(&dev->kobj, NULL, "wireless_status");
  1132. kobject_uevent(&dev->kobj, KOBJ_CHANGE);
  1133. return 0;
  1134. }
  1135. const struct attribute_group *usb_interface_groups[] = {
  1136. &intf_attr_grp,
  1137. &intf_assoc_attr_grp,
  1138. &intf_wireless_status_attr_grp,
  1139. NULL
  1140. };
  1141. void usb_create_sysfs_intf_files(struct usb_interface *intf)
  1142. {
  1143. struct usb_device *udev = interface_to_usbdev(intf);
  1144. struct usb_host_interface *alt = intf->cur_altsetting;
  1145. if (intf->sysfs_files_created || intf->unregistering)
  1146. return;
  1147. if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  1148. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  1149. if (alt->string && device_create_file(&intf->dev, &dev_attr_interface)) {
  1150. /* This is not a serious error */
  1151. dev_dbg(&intf->dev, "interface string descriptor file not created\n");
  1152. }
  1153. intf->sysfs_files_created = 1;
  1154. }
  1155. void usb_remove_sysfs_intf_files(struct usb_interface *intf)
  1156. {
  1157. if (!intf->sysfs_files_created)
  1158. return;
  1159. device_remove_file(&intf->dev, &dev_attr_interface);
  1160. intf->sysfs_files_created = 0;
  1161. }