sysfs.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * drivers/usb/core/sysfs.c
  3. *
  4. * (C) Copyright 2002 David Brownell
  5. * (C) Copyright 2002,2004 Greg Kroah-Hartman
  6. * (C) Copyright 2002,2004 IBM Corp.
  7. *
  8. * All of the sysfs file attributes for usb devices and interfaces.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/string.h>
  13. #include <linux/usb.h>
  14. #include <linux/usb/quirks.h>
  15. #include "usb.h"
  16. /* Active configuration fields */
  17. #define usb_actconfig_show(field, format_string) \
  18. static ssize_t field##_show(struct device *dev, \
  19. struct device_attribute *attr, char *buf) \
  20. { \
  21. struct usb_device *udev; \
  22. struct usb_host_config *actconfig; \
  23. ssize_t rc = 0; \
  24. \
  25. udev = to_usb_device(dev); \
  26. usb_lock_device(udev); \
  27. actconfig = udev->actconfig; \
  28. if (actconfig) \
  29. rc = sprintf(buf, format_string, \
  30. actconfig->desc.field); \
  31. usb_unlock_device(udev); \
  32. return rc; \
  33. } \
  34. #define usb_actconfig_attr(field, format_string) \
  35. usb_actconfig_show(field, format_string) \
  36. static DEVICE_ATTR_RO(field)
  37. usb_actconfig_attr(bNumInterfaces, "%2d\n");
  38. usb_actconfig_attr(bmAttributes, "%2x\n");
  39. static ssize_t bMaxPower_show(struct device *dev,
  40. struct device_attribute *attr, char *buf)
  41. {
  42. struct usb_device *udev;
  43. struct usb_host_config *actconfig;
  44. ssize_t rc = 0;
  45. udev = to_usb_device(dev);
  46. usb_lock_device(udev);
  47. actconfig = udev->actconfig;
  48. if (actconfig)
  49. rc = sprintf(buf, "%dmA\n", usb_get_max_power(udev, actconfig));
  50. usb_unlock_device(udev);
  51. return rc;
  52. }
  53. static DEVICE_ATTR_RO(bMaxPower);
  54. static ssize_t configuration_show(struct device *dev,
  55. struct device_attribute *attr, char *buf)
  56. {
  57. struct usb_device *udev;
  58. struct usb_host_config *actconfig;
  59. ssize_t rc = 0;
  60. udev = to_usb_device(dev);
  61. usb_lock_device(udev);
  62. actconfig = udev->actconfig;
  63. if (actconfig && actconfig->string)
  64. rc = sprintf(buf, "%s\n", actconfig->string);
  65. usb_unlock_device(udev);
  66. return rc;
  67. }
  68. static DEVICE_ATTR_RO(configuration);
  69. /* configuration value is always present, and r/w */
  70. usb_actconfig_show(bConfigurationValue, "%u\n");
  71. static ssize_t bConfigurationValue_store(struct device *dev,
  72. struct device_attribute *attr,
  73. const char *buf, size_t count)
  74. {
  75. struct usb_device *udev = to_usb_device(dev);
  76. int config, value;
  77. if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
  78. return -EINVAL;
  79. usb_lock_device(udev);
  80. value = usb_set_configuration(udev, config);
  81. usb_unlock_device(udev);
  82. return (value < 0) ? value : count;
  83. }
  84. static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,
  85. bConfigurationValue_show, bConfigurationValue_store);
  86. /* String fields */
  87. #define usb_string_attr(name) \
  88. static ssize_t name##_show(struct device *dev, \
  89. struct device_attribute *attr, char *buf) \
  90. { \
  91. struct usb_device *udev; \
  92. int retval; \
  93. \
  94. udev = to_usb_device(dev); \
  95. usb_lock_device(udev); \
  96. retval = sprintf(buf, "%s\n", udev->name); \
  97. usb_unlock_device(udev); \
  98. return retval; \
  99. } \
  100. static DEVICE_ATTR_RO(name)
  101. usb_string_attr(product);
  102. usb_string_attr(manufacturer);
  103. usb_string_attr(serial);
  104. static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
  105. char *buf)
  106. {
  107. struct usb_device *udev;
  108. char *speed;
  109. udev = to_usb_device(dev);
  110. switch (udev->speed) {
  111. case USB_SPEED_LOW:
  112. speed = "1.5";
  113. break;
  114. case USB_SPEED_UNKNOWN:
  115. case USB_SPEED_FULL:
  116. speed = "12";
  117. break;
  118. case USB_SPEED_HIGH:
  119. speed = "480";
  120. break;
  121. case USB_SPEED_WIRELESS:
  122. speed = "480";
  123. break;
  124. case USB_SPEED_SUPER:
  125. speed = "5000";
  126. break;
  127. default:
  128. speed = "unknown";
  129. }
  130. return sprintf(buf, "%s\n", speed);
  131. }
  132. static DEVICE_ATTR_RO(speed);
  133. static ssize_t busnum_show(struct device *dev, struct device_attribute *attr,
  134. char *buf)
  135. {
  136. struct usb_device *udev;
  137. udev = to_usb_device(dev);
  138. return sprintf(buf, "%d\n", udev->bus->busnum);
  139. }
  140. static DEVICE_ATTR_RO(busnum);
  141. static ssize_t devnum_show(struct device *dev, struct device_attribute *attr,
  142. char *buf)
  143. {
  144. struct usb_device *udev;
  145. udev = to_usb_device(dev);
  146. return sprintf(buf, "%d\n", udev->devnum);
  147. }
  148. static DEVICE_ATTR_RO(devnum);
  149. static ssize_t devpath_show(struct device *dev, struct device_attribute *attr,
  150. char *buf)
  151. {
  152. struct usb_device *udev;
  153. udev = to_usb_device(dev);
  154. return sprintf(buf, "%s\n", udev->devpath);
  155. }
  156. static DEVICE_ATTR_RO(devpath);
  157. static ssize_t version_show(struct device *dev, struct device_attribute *attr,
  158. char *buf)
  159. {
  160. struct usb_device *udev;
  161. u16 bcdUSB;
  162. udev = to_usb_device(dev);
  163. bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
  164. return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
  165. }
  166. static DEVICE_ATTR_RO(version);
  167. static ssize_t maxchild_show(struct device *dev, struct device_attribute *attr,
  168. char *buf)
  169. {
  170. struct usb_device *udev;
  171. udev = to_usb_device(dev);
  172. return sprintf(buf, "%d\n", udev->maxchild);
  173. }
  174. static DEVICE_ATTR_RO(maxchild);
  175. static ssize_t quirks_show(struct device *dev, struct device_attribute *attr,
  176. char *buf)
  177. {
  178. struct usb_device *udev;
  179. udev = to_usb_device(dev);
  180. return sprintf(buf, "0x%x\n", udev->quirks);
  181. }
  182. static DEVICE_ATTR_RO(quirks);
  183. static ssize_t avoid_reset_quirk_show(struct device *dev,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. struct usb_device *udev;
  187. udev = to_usb_device(dev);
  188. return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
  189. }
  190. static ssize_t avoid_reset_quirk_store(struct device *dev,
  191. struct device_attribute *attr,
  192. const char *buf, size_t count)
  193. {
  194. struct usb_device *udev = to_usb_device(dev);
  195. int val;
  196. if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1)
  197. return -EINVAL;
  198. usb_lock_device(udev);
  199. if (val)
  200. udev->quirks |= USB_QUIRK_RESET;
  201. else
  202. udev->quirks &= ~USB_QUIRK_RESET;
  203. usb_unlock_device(udev);
  204. return count;
  205. }
  206. static DEVICE_ATTR_RW(avoid_reset_quirk);
  207. static ssize_t urbnum_show(struct device *dev, struct device_attribute *attr,
  208. char *buf)
  209. {
  210. struct usb_device *udev;
  211. udev = to_usb_device(dev);
  212. return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
  213. }
  214. static DEVICE_ATTR_RO(urbnum);
  215. static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
  216. char *buf)
  217. {
  218. struct usb_device *udev;
  219. char *state;
  220. udev = to_usb_device(dev);
  221. switch (udev->removable) {
  222. case USB_DEVICE_REMOVABLE:
  223. state = "removable";
  224. break;
  225. case USB_DEVICE_FIXED:
  226. state = "fixed";
  227. break;
  228. default:
  229. state = "unknown";
  230. }
  231. return sprintf(buf, "%s\n", state);
  232. }
  233. static DEVICE_ATTR_RO(removable);
  234. static ssize_t ltm_capable_show(struct device *dev,
  235. struct device_attribute *attr, char *buf)
  236. {
  237. if (usb_device_supports_ltm(to_usb_device(dev)))
  238. return sprintf(buf, "%s\n", "yes");
  239. return sprintf(buf, "%s\n", "no");
  240. }
  241. static DEVICE_ATTR_RO(ltm_capable);
  242. #ifdef CONFIG_PM
  243. static ssize_t persist_show(struct device *dev, struct device_attribute *attr,
  244. char *buf)
  245. {
  246. struct usb_device *udev = to_usb_device(dev);
  247. return sprintf(buf, "%d\n", udev->persist_enabled);
  248. }
  249. static ssize_t persist_store(struct device *dev, struct device_attribute *attr,
  250. const char *buf, size_t count)
  251. {
  252. struct usb_device *udev = to_usb_device(dev);
  253. int value;
  254. /* Hubs are always enabled for USB_PERSIST */
  255. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  256. return -EPERM;
  257. if (sscanf(buf, "%d", &value) != 1)
  258. return -EINVAL;
  259. usb_lock_device(udev);
  260. udev->persist_enabled = !!value;
  261. usb_unlock_device(udev);
  262. return count;
  263. }
  264. static DEVICE_ATTR_RW(persist);
  265. static int add_persist_attributes(struct device *dev)
  266. {
  267. int rc = 0;
  268. if (is_usb_device(dev)) {
  269. struct usb_device *udev = to_usb_device(dev);
  270. /* Hubs are automatically enabled for USB_PERSIST,
  271. * no point in creating the attribute file.
  272. */
  273. if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
  274. rc = sysfs_add_file_to_group(&dev->kobj,
  275. &dev_attr_persist.attr,
  276. power_group_name);
  277. }
  278. return rc;
  279. }
  280. static void remove_persist_attributes(struct device *dev)
  281. {
  282. sysfs_remove_file_from_group(&dev->kobj,
  283. &dev_attr_persist.attr,
  284. power_group_name);
  285. }
  286. static ssize_t connected_duration_show(struct device *dev,
  287. struct device_attribute *attr, char *buf)
  288. {
  289. struct usb_device *udev = to_usb_device(dev);
  290. return sprintf(buf, "%u\n",
  291. jiffies_to_msecs(jiffies - udev->connect_time));
  292. }
  293. static DEVICE_ATTR_RO(connected_duration);
  294. /*
  295. * If the device is resumed, the last time the device was suspended has
  296. * been pre-subtracted from active_duration. We add the current time to
  297. * get the duration that the device was actually active.
  298. *
  299. * If the device is suspended, the active_duration is up-to-date.
  300. */
  301. static ssize_t active_duration_show(struct device *dev,
  302. struct device_attribute *attr, char *buf)
  303. {
  304. struct usb_device *udev = to_usb_device(dev);
  305. int duration;
  306. if (udev->state != USB_STATE_SUSPENDED)
  307. duration = jiffies_to_msecs(jiffies + udev->active_duration);
  308. else
  309. duration = jiffies_to_msecs(udev->active_duration);
  310. return sprintf(buf, "%u\n", duration);
  311. }
  312. static DEVICE_ATTR_RO(active_duration);
  313. static ssize_t autosuspend_show(struct device *dev,
  314. struct device_attribute *attr, char *buf)
  315. {
  316. return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);
  317. }
  318. static ssize_t autosuspend_store(struct device *dev,
  319. struct device_attribute *attr, const char *buf,
  320. size_t count)
  321. {
  322. int value;
  323. if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
  324. value <= -INT_MAX/1000)
  325. return -EINVAL;
  326. pm_runtime_set_autosuspend_delay(dev, value * 1000);
  327. return count;
  328. }
  329. static DEVICE_ATTR_RW(autosuspend);
  330. static const char on_string[] = "on";
  331. static const char auto_string[] = "auto";
  332. static void warn_level(void)
  333. {
  334. static int level_warned;
  335. if (!level_warned) {
  336. level_warned = 1;
  337. printk(KERN_WARNING "WARNING! power/level is deprecated; "
  338. "use power/control instead\n");
  339. }
  340. }
  341. static ssize_t level_show(struct device *dev, struct device_attribute *attr,
  342. char *buf)
  343. {
  344. struct usb_device *udev = to_usb_device(dev);
  345. const char *p = auto_string;
  346. warn_level();
  347. if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
  348. p = on_string;
  349. return sprintf(buf, "%s\n", p);
  350. }
  351. static ssize_t level_store(struct device *dev, struct device_attribute *attr,
  352. const char *buf, size_t count)
  353. {
  354. struct usb_device *udev = to_usb_device(dev);
  355. int len = count;
  356. char *cp;
  357. int rc = count;
  358. warn_level();
  359. cp = memchr(buf, '\n', count);
  360. if (cp)
  361. len = cp - buf;
  362. usb_lock_device(udev);
  363. if (len == sizeof on_string - 1 &&
  364. strncmp(buf, on_string, len) == 0)
  365. usb_disable_autosuspend(udev);
  366. else if (len == sizeof auto_string - 1 &&
  367. strncmp(buf, auto_string, len) == 0)
  368. usb_enable_autosuspend(udev);
  369. else
  370. rc = -EINVAL;
  371. usb_unlock_device(udev);
  372. return rc;
  373. }
  374. static DEVICE_ATTR_RW(level);
  375. static ssize_t usb2_hardware_lpm_show(struct device *dev,
  376. struct device_attribute *attr, char *buf)
  377. {
  378. struct usb_device *udev = to_usb_device(dev);
  379. const char *p;
  380. if (udev->usb2_hw_lpm_allowed == 1)
  381. p = "enabled";
  382. else
  383. p = "disabled";
  384. return sprintf(buf, "%s\n", p);
  385. }
  386. static ssize_t usb2_hardware_lpm_store(struct device *dev,
  387. struct device_attribute *attr,
  388. const char *buf, size_t count)
  389. {
  390. struct usb_device *udev = to_usb_device(dev);
  391. bool value;
  392. int ret;
  393. usb_lock_device(udev);
  394. ret = strtobool(buf, &value);
  395. if (!ret) {
  396. udev->usb2_hw_lpm_allowed = value;
  397. ret = usb_set_usb2_hardware_lpm(udev, value);
  398. }
  399. usb_unlock_device(udev);
  400. if (!ret)
  401. return count;
  402. return ret;
  403. }
  404. static DEVICE_ATTR_RW(usb2_hardware_lpm);
  405. static ssize_t usb2_lpm_l1_timeout_show(struct device *dev,
  406. struct device_attribute *attr,
  407. char *buf)
  408. {
  409. struct usb_device *udev = to_usb_device(dev);
  410. return sprintf(buf, "%d\n", udev->l1_params.timeout);
  411. }
  412. static ssize_t usb2_lpm_l1_timeout_store(struct device *dev,
  413. struct device_attribute *attr,
  414. const char *buf, size_t count)
  415. {
  416. struct usb_device *udev = to_usb_device(dev);
  417. u16 timeout;
  418. if (kstrtou16(buf, 0, &timeout))
  419. return -EINVAL;
  420. udev->l1_params.timeout = timeout;
  421. return count;
  422. }
  423. static DEVICE_ATTR_RW(usb2_lpm_l1_timeout);
  424. static ssize_t usb2_lpm_besl_show(struct device *dev,
  425. struct device_attribute *attr, char *buf)
  426. {
  427. struct usb_device *udev = to_usb_device(dev);
  428. return sprintf(buf, "%d\n", udev->l1_params.besl);
  429. }
  430. static ssize_t usb2_lpm_besl_store(struct device *dev,
  431. struct device_attribute *attr,
  432. const char *buf, size_t count)
  433. {
  434. struct usb_device *udev = to_usb_device(dev);
  435. u8 besl;
  436. if (kstrtou8(buf, 0, &besl) || besl > 15)
  437. return -EINVAL;
  438. udev->l1_params.besl = besl;
  439. return count;
  440. }
  441. static DEVICE_ATTR_RW(usb2_lpm_besl);
  442. static ssize_t usb3_hardware_lpm_show(struct device *dev,
  443. struct device_attribute *attr, char *buf)
  444. {
  445. struct usb_device *udev = to_usb_device(dev);
  446. const char *p;
  447. usb_lock_device(udev);
  448. if (udev->usb3_lpm_enabled)
  449. p = "enabled";
  450. else
  451. p = "disabled";
  452. usb_unlock_device(udev);
  453. return sprintf(buf, "%s\n", p);
  454. }
  455. static DEVICE_ATTR_RO(usb3_hardware_lpm);
  456. static struct attribute *usb2_hardware_lpm_attr[] = {
  457. &dev_attr_usb2_hardware_lpm.attr,
  458. &dev_attr_usb2_lpm_l1_timeout.attr,
  459. &dev_attr_usb2_lpm_besl.attr,
  460. NULL,
  461. };
  462. static struct attribute_group usb2_hardware_lpm_attr_group = {
  463. .name = power_group_name,
  464. .attrs = usb2_hardware_lpm_attr,
  465. };
  466. static struct attribute *usb3_hardware_lpm_attr[] = {
  467. &dev_attr_usb3_hardware_lpm.attr,
  468. NULL,
  469. };
  470. static struct attribute_group usb3_hardware_lpm_attr_group = {
  471. .name = power_group_name,
  472. .attrs = usb3_hardware_lpm_attr,
  473. };
  474. static struct attribute *power_attrs[] = {
  475. &dev_attr_autosuspend.attr,
  476. &dev_attr_level.attr,
  477. &dev_attr_connected_duration.attr,
  478. &dev_attr_active_duration.attr,
  479. NULL,
  480. };
  481. static struct attribute_group power_attr_group = {
  482. .name = power_group_name,
  483. .attrs = power_attrs,
  484. };
  485. static int add_power_attributes(struct device *dev)
  486. {
  487. int rc = 0;
  488. if (is_usb_device(dev)) {
  489. struct usb_device *udev = to_usb_device(dev);
  490. rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
  491. if (udev->usb2_hw_lpm_capable == 1)
  492. rc = sysfs_merge_group(&dev->kobj,
  493. &usb2_hardware_lpm_attr_group);
  494. if (udev->lpm_capable == 1)
  495. rc = sysfs_merge_group(&dev->kobj,
  496. &usb3_hardware_lpm_attr_group);
  497. }
  498. return rc;
  499. }
  500. static void remove_power_attributes(struct device *dev)
  501. {
  502. sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
  503. sysfs_unmerge_group(&dev->kobj, &power_attr_group);
  504. }
  505. #else
  506. #define add_persist_attributes(dev) 0
  507. #define remove_persist_attributes(dev) do {} while (0)
  508. #define add_power_attributes(dev) 0
  509. #define remove_power_attributes(dev) do {} while (0)
  510. #endif /* CONFIG_PM */
  511. /* Descriptor fields */
  512. #define usb_descriptor_attr_le16(field, format_string) \
  513. static ssize_t \
  514. field##_show(struct device *dev, struct device_attribute *attr, \
  515. char *buf) \
  516. { \
  517. struct usb_device *udev; \
  518. \
  519. udev = to_usb_device(dev); \
  520. return sprintf(buf, format_string, \
  521. le16_to_cpu(udev->descriptor.field)); \
  522. } \
  523. static DEVICE_ATTR_RO(field)
  524. usb_descriptor_attr_le16(idVendor, "%04x\n");
  525. usb_descriptor_attr_le16(idProduct, "%04x\n");
  526. usb_descriptor_attr_le16(bcdDevice, "%04x\n");
  527. #define usb_descriptor_attr(field, format_string) \
  528. static ssize_t \
  529. field##_show(struct device *dev, struct device_attribute *attr, \
  530. char *buf) \
  531. { \
  532. struct usb_device *udev; \
  533. \
  534. udev = to_usb_device(dev); \
  535. return sprintf(buf, format_string, udev->descriptor.field); \
  536. } \
  537. static DEVICE_ATTR_RO(field)
  538. usb_descriptor_attr(bDeviceClass, "%02x\n");
  539. usb_descriptor_attr(bDeviceSubClass, "%02x\n");
  540. usb_descriptor_attr(bDeviceProtocol, "%02x\n");
  541. usb_descriptor_attr(bNumConfigurations, "%d\n");
  542. usb_descriptor_attr(bMaxPacketSize0, "%d\n");
  543. /* show if the device is authorized (1) or not (0) */
  544. static ssize_t authorized_show(struct device *dev,
  545. struct device_attribute *attr, char *buf)
  546. {
  547. struct usb_device *usb_dev = to_usb_device(dev);
  548. return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
  549. }
  550. /*
  551. * Authorize a device to be used in the system
  552. *
  553. * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
  554. */
  555. static ssize_t authorized_store(struct device *dev,
  556. struct device_attribute *attr, const char *buf,
  557. size_t size)
  558. {
  559. ssize_t result;
  560. struct usb_device *usb_dev = to_usb_device(dev);
  561. unsigned val;
  562. result = sscanf(buf, "%u\n", &val);
  563. if (result != 1)
  564. result = -EINVAL;
  565. else if (val == 0)
  566. result = usb_deauthorize_device(usb_dev);
  567. else
  568. result = usb_authorize_device(usb_dev);
  569. return result < 0 ? result : size;
  570. }
  571. static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
  572. authorized_show, authorized_store);
  573. /* "Safely remove a device" */
  574. static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
  575. const char *buf, size_t count)
  576. {
  577. struct usb_device *udev = to_usb_device(dev);
  578. int rc = 0;
  579. usb_lock_device(udev);
  580. if (udev->state != USB_STATE_NOTATTACHED) {
  581. /* To avoid races, first unconfigure and then remove */
  582. usb_set_configuration(udev, -1);
  583. rc = usb_remove_device(udev);
  584. }
  585. if (rc == 0)
  586. rc = count;
  587. usb_unlock_device(udev);
  588. return rc;
  589. }
  590. static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store);
  591. static struct attribute *dev_attrs[] = {
  592. /* current configuration's attributes */
  593. &dev_attr_configuration.attr,
  594. &dev_attr_bNumInterfaces.attr,
  595. &dev_attr_bConfigurationValue.attr,
  596. &dev_attr_bmAttributes.attr,
  597. &dev_attr_bMaxPower.attr,
  598. /* device attributes */
  599. &dev_attr_urbnum.attr,
  600. &dev_attr_idVendor.attr,
  601. &dev_attr_idProduct.attr,
  602. &dev_attr_bcdDevice.attr,
  603. &dev_attr_bDeviceClass.attr,
  604. &dev_attr_bDeviceSubClass.attr,
  605. &dev_attr_bDeviceProtocol.attr,
  606. &dev_attr_bNumConfigurations.attr,
  607. &dev_attr_bMaxPacketSize0.attr,
  608. &dev_attr_speed.attr,
  609. &dev_attr_busnum.attr,
  610. &dev_attr_devnum.attr,
  611. &dev_attr_devpath.attr,
  612. &dev_attr_version.attr,
  613. &dev_attr_maxchild.attr,
  614. &dev_attr_quirks.attr,
  615. &dev_attr_avoid_reset_quirk.attr,
  616. &dev_attr_authorized.attr,
  617. &dev_attr_remove.attr,
  618. &dev_attr_removable.attr,
  619. &dev_attr_ltm_capable.attr,
  620. NULL,
  621. };
  622. static struct attribute_group dev_attr_grp = {
  623. .attrs = dev_attrs,
  624. };
  625. /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
  626. * accordingly.
  627. */
  628. static struct attribute *dev_string_attrs[] = {
  629. &dev_attr_manufacturer.attr,
  630. &dev_attr_product.attr,
  631. &dev_attr_serial.attr,
  632. NULL
  633. };
  634. static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
  635. struct attribute *a, int n)
  636. {
  637. struct device *dev = container_of(kobj, struct device, kobj);
  638. struct usb_device *udev = to_usb_device(dev);
  639. if (a == &dev_attr_manufacturer.attr) {
  640. if (udev->manufacturer == NULL)
  641. return 0;
  642. } else if (a == &dev_attr_product.attr) {
  643. if (udev->product == NULL)
  644. return 0;
  645. } else if (a == &dev_attr_serial.attr) {
  646. if (udev->serial == NULL)
  647. return 0;
  648. }
  649. return a->mode;
  650. }
  651. static struct attribute_group dev_string_attr_grp = {
  652. .attrs = dev_string_attrs,
  653. .is_visible = dev_string_attrs_are_visible,
  654. };
  655. const struct attribute_group *usb_device_groups[] = {
  656. &dev_attr_grp,
  657. &dev_string_attr_grp,
  658. NULL
  659. };
  660. /* Binary descriptors */
  661. static ssize_t
  662. read_descriptors(struct file *filp, struct kobject *kobj,
  663. struct bin_attribute *attr,
  664. char *buf, loff_t off, size_t count)
  665. {
  666. struct device *dev = container_of(kobj, struct device, kobj);
  667. struct usb_device *udev = to_usb_device(dev);
  668. size_t nleft = count;
  669. size_t srclen, n;
  670. int cfgno;
  671. void *src;
  672. /* The binary attribute begins with the device descriptor.
  673. * Following that are the raw descriptor entries for all the
  674. * configurations (config plus subsidiary descriptors).
  675. */
  676. usb_lock_device(udev);
  677. for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
  678. nleft > 0; ++cfgno) {
  679. if (cfgno < 0) {
  680. src = &udev->descriptor;
  681. srclen = sizeof(struct usb_device_descriptor);
  682. } else {
  683. src = udev->rawdescriptors[cfgno];
  684. srclen = __le16_to_cpu(udev->config[cfgno].desc.
  685. wTotalLength);
  686. }
  687. if (off < srclen) {
  688. n = min(nleft, srclen - (size_t) off);
  689. memcpy(buf, src + off, n);
  690. nleft -= n;
  691. buf += n;
  692. off = 0;
  693. } else {
  694. off -= srclen;
  695. }
  696. }
  697. usb_unlock_device(udev);
  698. return count - nleft;
  699. }
  700. static struct bin_attribute dev_bin_attr_descriptors = {
  701. .attr = {.name = "descriptors", .mode = 0444},
  702. .read = read_descriptors,
  703. .size = 18 + 65535, /* dev descr + max-size raw descriptor */
  704. };
  705. int usb_create_sysfs_dev_files(struct usb_device *udev)
  706. {
  707. struct device *dev = &udev->dev;
  708. int retval;
  709. retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
  710. if (retval)
  711. goto error;
  712. retval = add_persist_attributes(dev);
  713. if (retval)
  714. goto error;
  715. retval = add_power_attributes(dev);
  716. if (retval)
  717. goto error;
  718. return retval;
  719. error:
  720. usb_remove_sysfs_dev_files(udev);
  721. return retval;
  722. }
  723. void usb_remove_sysfs_dev_files(struct usb_device *udev)
  724. {
  725. struct device *dev = &udev->dev;
  726. remove_power_attributes(dev);
  727. remove_persist_attributes(dev);
  728. device_remove_bin_file(dev, &dev_bin_attr_descriptors);
  729. }
  730. /* Interface Association Descriptor fields */
  731. #define usb_intf_assoc_attr(field, format_string) \
  732. static ssize_t \
  733. iad_##field##_show(struct device *dev, struct device_attribute *attr, \
  734. char *buf) \
  735. { \
  736. struct usb_interface *intf = to_usb_interface(dev); \
  737. \
  738. return sprintf(buf, format_string, \
  739. intf->intf_assoc->field); \
  740. } \
  741. static DEVICE_ATTR_RO(iad_##field)
  742. usb_intf_assoc_attr(bFirstInterface, "%02x\n");
  743. usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
  744. usb_intf_assoc_attr(bFunctionClass, "%02x\n");
  745. usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
  746. usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
  747. /* Interface fields */
  748. #define usb_intf_attr(field, format_string) \
  749. static ssize_t \
  750. field##_show(struct device *dev, struct device_attribute *attr, \
  751. char *buf) \
  752. { \
  753. struct usb_interface *intf = to_usb_interface(dev); \
  754. \
  755. return sprintf(buf, format_string, \
  756. intf->cur_altsetting->desc.field); \
  757. } \
  758. static DEVICE_ATTR_RO(field)
  759. usb_intf_attr(bInterfaceNumber, "%02x\n");
  760. usb_intf_attr(bAlternateSetting, "%2d\n");
  761. usb_intf_attr(bNumEndpoints, "%02x\n");
  762. usb_intf_attr(bInterfaceClass, "%02x\n");
  763. usb_intf_attr(bInterfaceSubClass, "%02x\n");
  764. usb_intf_attr(bInterfaceProtocol, "%02x\n");
  765. static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
  766. char *buf)
  767. {
  768. struct usb_interface *intf;
  769. char *string;
  770. intf = to_usb_interface(dev);
  771. string = ACCESS_ONCE(intf->cur_altsetting->string);
  772. if (!string)
  773. return 0;
  774. return sprintf(buf, "%s\n", string);
  775. }
  776. static DEVICE_ATTR_RO(interface);
  777. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  778. char *buf)
  779. {
  780. struct usb_interface *intf;
  781. struct usb_device *udev;
  782. struct usb_host_interface *alt;
  783. intf = to_usb_interface(dev);
  784. udev = interface_to_usbdev(intf);
  785. alt = ACCESS_ONCE(intf->cur_altsetting);
  786. return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  787. "ic%02Xisc%02Xip%02Xin%02X\n",
  788. le16_to_cpu(udev->descriptor.idVendor),
  789. le16_to_cpu(udev->descriptor.idProduct),
  790. le16_to_cpu(udev->descriptor.bcdDevice),
  791. udev->descriptor.bDeviceClass,
  792. udev->descriptor.bDeviceSubClass,
  793. udev->descriptor.bDeviceProtocol,
  794. alt->desc.bInterfaceClass,
  795. alt->desc.bInterfaceSubClass,
  796. alt->desc.bInterfaceProtocol,
  797. alt->desc.bInterfaceNumber);
  798. }
  799. static DEVICE_ATTR_RO(modalias);
  800. static ssize_t supports_autosuspend_show(struct device *dev,
  801. struct device_attribute *attr,
  802. char *buf)
  803. {
  804. int s;
  805. device_lock(dev);
  806. /* Devices will be autosuspended even when an interface isn't claimed */
  807. s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend);
  808. device_unlock(dev);
  809. return sprintf(buf, "%u\n", s);
  810. }
  811. static DEVICE_ATTR_RO(supports_autosuspend);
  812. static struct attribute *intf_attrs[] = {
  813. &dev_attr_bInterfaceNumber.attr,
  814. &dev_attr_bAlternateSetting.attr,
  815. &dev_attr_bNumEndpoints.attr,
  816. &dev_attr_bInterfaceClass.attr,
  817. &dev_attr_bInterfaceSubClass.attr,
  818. &dev_attr_bInterfaceProtocol.attr,
  819. &dev_attr_modalias.attr,
  820. &dev_attr_supports_autosuspend.attr,
  821. NULL,
  822. };
  823. static struct attribute_group intf_attr_grp = {
  824. .attrs = intf_attrs,
  825. };
  826. static struct attribute *intf_assoc_attrs[] = {
  827. &dev_attr_iad_bFirstInterface.attr,
  828. &dev_attr_iad_bInterfaceCount.attr,
  829. &dev_attr_iad_bFunctionClass.attr,
  830. &dev_attr_iad_bFunctionSubClass.attr,
  831. &dev_attr_iad_bFunctionProtocol.attr,
  832. NULL,
  833. };
  834. static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
  835. struct attribute *a, int n)
  836. {
  837. struct device *dev = container_of(kobj, struct device, kobj);
  838. struct usb_interface *intf = to_usb_interface(dev);
  839. if (intf->intf_assoc == NULL)
  840. return 0;
  841. return a->mode;
  842. }
  843. static struct attribute_group intf_assoc_attr_grp = {
  844. .attrs = intf_assoc_attrs,
  845. .is_visible = intf_assoc_attrs_are_visible,
  846. };
  847. const struct attribute_group *usb_interface_groups[] = {
  848. &intf_attr_grp,
  849. &intf_assoc_attr_grp,
  850. NULL
  851. };
  852. void usb_create_sysfs_intf_files(struct usb_interface *intf)
  853. {
  854. struct usb_device *udev = interface_to_usbdev(intf);
  855. struct usb_host_interface *alt = intf->cur_altsetting;
  856. if (intf->sysfs_files_created || intf->unregistering)
  857. return;
  858. if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  859. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  860. if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
  861. ; /* We don't actually care if the function fails. */
  862. intf->sysfs_files_created = 1;
  863. }
  864. void usb_remove_sysfs_intf_files(struct usb_interface *intf)
  865. {
  866. if (!intf->sysfs_files_created)
  867. return;
  868. device_remove_file(&intf->dev, &dev_attr_interface);
  869. intf->sysfs_files_created = 0;
  870. }