sysfs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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. #else
  287. #define add_persist_attributes(dev) 0
  288. #define remove_persist_attributes(dev) do {} while (0)
  289. #endif /* CONFIG_PM */
  290. #ifdef CONFIG_PM_RUNTIME
  291. static ssize_t connected_duration_show(struct device *dev,
  292. struct device_attribute *attr, char *buf)
  293. {
  294. struct usb_device *udev = to_usb_device(dev);
  295. return sprintf(buf, "%u\n",
  296. jiffies_to_msecs(jiffies - udev->connect_time));
  297. }
  298. static DEVICE_ATTR_RO(connected_duration);
  299. /*
  300. * If the device is resumed, the last time the device was suspended has
  301. * been pre-subtracted from active_duration. We add the current time to
  302. * get the duration that the device was actually active.
  303. *
  304. * If the device is suspended, the active_duration is up-to-date.
  305. */
  306. static ssize_t active_duration_show(struct device *dev,
  307. struct device_attribute *attr, char *buf)
  308. {
  309. struct usb_device *udev = to_usb_device(dev);
  310. int duration;
  311. if (udev->state != USB_STATE_SUSPENDED)
  312. duration = jiffies_to_msecs(jiffies + udev->active_duration);
  313. else
  314. duration = jiffies_to_msecs(udev->active_duration);
  315. return sprintf(buf, "%u\n", duration);
  316. }
  317. static DEVICE_ATTR_RO(active_duration);
  318. static ssize_t autosuspend_show(struct device *dev,
  319. struct device_attribute *attr, char *buf)
  320. {
  321. return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);
  322. }
  323. static ssize_t autosuspend_store(struct device *dev,
  324. struct device_attribute *attr, const char *buf,
  325. size_t count)
  326. {
  327. int value;
  328. if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
  329. value <= -INT_MAX/1000)
  330. return -EINVAL;
  331. pm_runtime_set_autosuspend_delay(dev, value * 1000);
  332. return count;
  333. }
  334. static DEVICE_ATTR_RW(autosuspend);
  335. static const char on_string[] = "on";
  336. static const char auto_string[] = "auto";
  337. static void warn_level(void)
  338. {
  339. static int level_warned;
  340. if (!level_warned) {
  341. level_warned = 1;
  342. printk(KERN_WARNING "WARNING! power/level is deprecated; "
  343. "use power/control instead\n");
  344. }
  345. }
  346. static ssize_t level_show(struct device *dev, struct device_attribute *attr,
  347. char *buf)
  348. {
  349. struct usb_device *udev = to_usb_device(dev);
  350. const char *p = auto_string;
  351. warn_level();
  352. if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
  353. p = on_string;
  354. return sprintf(buf, "%s\n", p);
  355. }
  356. static ssize_t level_store(struct device *dev, struct device_attribute *attr,
  357. const char *buf, size_t count)
  358. {
  359. struct usb_device *udev = to_usb_device(dev);
  360. int len = count;
  361. char *cp;
  362. int rc = count;
  363. warn_level();
  364. cp = memchr(buf, '\n', count);
  365. if (cp)
  366. len = cp - buf;
  367. usb_lock_device(udev);
  368. if (len == sizeof on_string - 1 &&
  369. strncmp(buf, on_string, len) == 0)
  370. usb_disable_autosuspend(udev);
  371. else if (len == sizeof auto_string - 1 &&
  372. strncmp(buf, auto_string, len) == 0)
  373. usb_enable_autosuspend(udev);
  374. else
  375. rc = -EINVAL;
  376. usb_unlock_device(udev);
  377. return rc;
  378. }
  379. static DEVICE_ATTR_RW(level);
  380. static ssize_t usb2_hardware_lpm_show(struct device *dev,
  381. struct device_attribute *attr, char *buf)
  382. {
  383. struct usb_device *udev = to_usb_device(dev);
  384. const char *p;
  385. if (udev->usb2_hw_lpm_allowed == 1)
  386. p = "enabled";
  387. else
  388. p = "disabled";
  389. return sprintf(buf, "%s\n", p);
  390. }
  391. static ssize_t usb2_hardware_lpm_store(struct device *dev,
  392. struct device_attribute *attr,
  393. const char *buf, size_t count)
  394. {
  395. struct usb_device *udev = to_usb_device(dev);
  396. bool value;
  397. int ret;
  398. usb_lock_device(udev);
  399. ret = strtobool(buf, &value);
  400. if (!ret) {
  401. udev->usb2_hw_lpm_allowed = value;
  402. ret = usb_set_usb2_hardware_lpm(udev, value);
  403. }
  404. usb_unlock_device(udev);
  405. if (!ret)
  406. return count;
  407. return ret;
  408. }
  409. static DEVICE_ATTR_RW(usb2_hardware_lpm);
  410. static ssize_t usb2_lpm_l1_timeout_show(struct device *dev,
  411. struct device_attribute *attr,
  412. char *buf)
  413. {
  414. struct usb_device *udev = to_usb_device(dev);
  415. return sprintf(buf, "%d\n", udev->l1_params.timeout);
  416. }
  417. static ssize_t usb2_lpm_l1_timeout_store(struct device *dev,
  418. struct device_attribute *attr,
  419. const char *buf, size_t count)
  420. {
  421. struct usb_device *udev = to_usb_device(dev);
  422. u16 timeout;
  423. if (kstrtou16(buf, 0, &timeout))
  424. return -EINVAL;
  425. udev->l1_params.timeout = timeout;
  426. return count;
  427. }
  428. static DEVICE_ATTR_RW(usb2_lpm_l1_timeout);
  429. static ssize_t usb2_lpm_besl_show(struct device *dev,
  430. struct device_attribute *attr, char *buf)
  431. {
  432. struct usb_device *udev = to_usb_device(dev);
  433. return sprintf(buf, "%d\n", udev->l1_params.besl);
  434. }
  435. static ssize_t usb2_lpm_besl_store(struct device *dev,
  436. struct device_attribute *attr,
  437. const char *buf, size_t count)
  438. {
  439. struct usb_device *udev = to_usb_device(dev);
  440. u8 besl;
  441. if (kstrtou8(buf, 0, &besl) || besl > 15)
  442. return -EINVAL;
  443. udev->l1_params.besl = besl;
  444. return count;
  445. }
  446. static DEVICE_ATTR_RW(usb2_lpm_besl);
  447. static struct attribute *usb2_hardware_lpm_attr[] = {
  448. &dev_attr_usb2_hardware_lpm.attr,
  449. &dev_attr_usb2_lpm_l1_timeout.attr,
  450. &dev_attr_usb2_lpm_besl.attr,
  451. NULL,
  452. };
  453. static struct attribute_group usb2_hardware_lpm_attr_group = {
  454. .name = power_group_name,
  455. .attrs = usb2_hardware_lpm_attr,
  456. };
  457. static struct attribute *power_attrs[] = {
  458. &dev_attr_autosuspend.attr,
  459. &dev_attr_level.attr,
  460. &dev_attr_connected_duration.attr,
  461. &dev_attr_active_duration.attr,
  462. NULL,
  463. };
  464. static struct attribute_group power_attr_group = {
  465. .name = power_group_name,
  466. .attrs = power_attrs,
  467. };
  468. static int add_power_attributes(struct device *dev)
  469. {
  470. int rc = 0;
  471. if (is_usb_device(dev)) {
  472. struct usb_device *udev = to_usb_device(dev);
  473. rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
  474. if (udev->usb2_hw_lpm_capable == 1)
  475. rc = sysfs_merge_group(&dev->kobj,
  476. &usb2_hardware_lpm_attr_group);
  477. }
  478. return rc;
  479. }
  480. static void remove_power_attributes(struct device *dev)
  481. {
  482. sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
  483. sysfs_unmerge_group(&dev->kobj, &power_attr_group);
  484. }
  485. #else
  486. #define add_power_attributes(dev) 0
  487. #define remove_power_attributes(dev) do {} while (0)
  488. #endif /* CONFIG_PM_RUNTIME */
  489. /* Descriptor fields */
  490. #define usb_descriptor_attr_le16(field, format_string) \
  491. static ssize_t \
  492. field##_show(struct device *dev, struct device_attribute *attr, \
  493. char *buf) \
  494. { \
  495. struct usb_device *udev; \
  496. \
  497. udev = to_usb_device(dev); \
  498. return sprintf(buf, format_string, \
  499. le16_to_cpu(udev->descriptor.field)); \
  500. } \
  501. static DEVICE_ATTR_RO(field)
  502. usb_descriptor_attr_le16(idVendor, "%04x\n");
  503. usb_descriptor_attr_le16(idProduct, "%04x\n");
  504. usb_descriptor_attr_le16(bcdDevice, "%04x\n");
  505. #define usb_descriptor_attr(field, format_string) \
  506. static ssize_t \
  507. field##_show(struct device *dev, struct device_attribute *attr, \
  508. char *buf) \
  509. { \
  510. struct usb_device *udev; \
  511. \
  512. udev = to_usb_device(dev); \
  513. return sprintf(buf, format_string, udev->descriptor.field); \
  514. } \
  515. static DEVICE_ATTR_RO(field)
  516. usb_descriptor_attr(bDeviceClass, "%02x\n");
  517. usb_descriptor_attr(bDeviceSubClass, "%02x\n");
  518. usb_descriptor_attr(bDeviceProtocol, "%02x\n");
  519. usb_descriptor_attr(bNumConfigurations, "%d\n");
  520. usb_descriptor_attr(bMaxPacketSize0, "%d\n");
  521. /* show if the device is authorized (1) or not (0) */
  522. static ssize_t authorized_show(struct device *dev,
  523. struct device_attribute *attr, char *buf)
  524. {
  525. struct usb_device *usb_dev = to_usb_device(dev);
  526. return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
  527. }
  528. /*
  529. * Authorize a device to be used in the system
  530. *
  531. * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
  532. */
  533. static ssize_t authorized_store(struct device *dev,
  534. struct device_attribute *attr, const char *buf,
  535. size_t size)
  536. {
  537. ssize_t result;
  538. struct usb_device *usb_dev = to_usb_device(dev);
  539. unsigned val;
  540. result = sscanf(buf, "%u\n", &val);
  541. if (result != 1)
  542. result = -EINVAL;
  543. else if (val == 0)
  544. result = usb_deauthorize_device(usb_dev);
  545. else
  546. result = usb_authorize_device(usb_dev);
  547. return result < 0 ? result : size;
  548. }
  549. static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
  550. authorized_show, authorized_store);
  551. /* "Safely remove a device" */
  552. static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
  553. const char *buf, size_t count)
  554. {
  555. struct usb_device *udev = to_usb_device(dev);
  556. int rc = 0;
  557. usb_lock_device(udev);
  558. if (udev->state != USB_STATE_NOTATTACHED) {
  559. /* To avoid races, first unconfigure and then remove */
  560. usb_set_configuration(udev, -1);
  561. rc = usb_remove_device(udev);
  562. }
  563. if (rc == 0)
  564. rc = count;
  565. usb_unlock_device(udev);
  566. return rc;
  567. }
  568. static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store);
  569. static struct attribute *dev_attrs[] = {
  570. /* current configuration's attributes */
  571. &dev_attr_configuration.attr,
  572. &dev_attr_bNumInterfaces.attr,
  573. &dev_attr_bConfigurationValue.attr,
  574. &dev_attr_bmAttributes.attr,
  575. &dev_attr_bMaxPower.attr,
  576. /* device attributes */
  577. &dev_attr_urbnum.attr,
  578. &dev_attr_idVendor.attr,
  579. &dev_attr_idProduct.attr,
  580. &dev_attr_bcdDevice.attr,
  581. &dev_attr_bDeviceClass.attr,
  582. &dev_attr_bDeviceSubClass.attr,
  583. &dev_attr_bDeviceProtocol.attr,
  584. &dev_attr_bNumConfigurations.attr,
  585. &dev_attr_bMaxPacketSize0.attr,
  586. &dev_attr_speed.attr,
  587. &dev_attr_busnum.attr,
  588. &dev_attr_devnum.attr,
  589. &dev_attr_devpath.attr,
  590. &dev_attr_version.attr,
  591. &dev_attr_maxchild.attr,
  592. &dev_attr_quirks.attr,
  593. &dev_attr_avoid_reset_quirk.attr,
  594. &dev_attr_authorized.attr,
  595. &dev_attr_remove.attr,
  596. &dev_attr_removable.attr,
  597. &dev_attr_ltm_capable.attr,
  598. NULL,
  599. };
  600. static struct attribute_group dev_attr_grp = {
  601. .attrs = dev_attrs,
  602. };
  603. /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
  604. * accordingly.
  605. */
  606. static struct attribute *dev_string_attrs[] = {
  607. &dev_attr_manufacturer.attr,
  608. &dev_attr_product.attr,
  609. &dev_attr_serial.attr,
  610. NULL
  611. };
  612. static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
  613. struct attribute *a, int n)
  614. {
  615. struct device *dev = container_of(kobj, struct device, kobj);
  616. struct usb_device *udev = to_usb_device(dev);
  617. if (a == &dev_attr_manufacturer.attr) {
  618. if (udev->manufacturer == NULL)
  619. return 0;
  620. } else if (a == &dev_attr_product.attr) {
  621. if (udev->product == NULL)
  622. return 0;
  623. } else if (a == &dev_attr_serial.attr) {
  624. if (udev->serial == NULL)
  625. return 0;
  626. }
  627. return a->mode;
  628. }
  629. static struct attribute_group dev_string_attr_grp = {
  630. .attrs = dev_string_attrs,
  631. .is_visible = dev_string_attrs_are_visible,
  632. };
  633. const struct attribute_group *usb_device_groups[] = {
  634. &dev_attr_grp,
  635. &dev_string_attr_grp,
  636. NULL
  637. };
  638. /* Binary descriptors */
  639. static ssize_t
  640. read_descriptors(struct file *filp, struct kobject *kobj,
  641. struct bin_attribute *attr,
  642. char *buf, loff_t off, size_t count)
  643. {
  644. struct device *dev = container_of(kobj, struct device, kobj);
  645. struct usb_device *udev = to_usb_device(dev);
  646. size_t nleft = count;
  647. size_t srclen, n;
  648. int cfgno;
  649. void *src;
  650. /* The binary attribute begins with the device descriptor.
  651. * Following that are the raw descriptor entries for all the
  652. * configurations (config plus subsidiary descriptors).
  653. */
  654. usb_lock_device(udev);
  655. for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
  656. nleft > 0; ++cfgno) {
  657. if (cfgno < 0) {
  658. src = &udev->descriptor;
  659. srclen = sizeof(struct usb_device_descriptor);
  660. } else {
  661. src = udev->rawdescriptors[cfgno];
  662. srclen = __le16_to_cpu(udev->config[cfgno].desc.
  663. wTotalLength);
  664. }
  665. if (off < srclen) {
  666. n = min(nleft, srclen - (size_t) off);
  667. memcpy(buf, src + off, n);
  668. nleft -= n;
  669. buf += n;
  670. off = 0;
  671. } else {
  672. off -= srclen;
  673. }
  674. }
  675. usb_unlock_device(udev);
  676. return count - nleft;
  677. }
  678. static struct bin_attribute dev_bin_attr_descriptors = {
  679. .attr = {.name = "descriptors", .mode = 0444},
  680. .read = read_descriptors,
  681. .size = 18 + 65535, /* dev descr + max-size raw descriptor */
  682. };
  683. int usb_create_sysfs_dev_files(struct usb_device *udev)
  684. {
  685. struct device *dev = &udev->dev;
  686. int retval;
  687. retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
  688. if (retval)
  689. goto error;
  690. retval = add_persist_attributes(dev);
  691. if (retval)
  692. goto error;
  693. retval = add_power_attributes(dev);
  694. if (retval)
  695. goto error;
  696. return retval;
  697. error:
  698. usb_remove_sysfs_dev_files(udev);
  699. return retval;
  700. }
  701. void usb_remove_sysfs_dev_files(struct usb_device *udev)
  702. {
  703. struct device *dev = &udev->dev;
  704. remove_power_attributes(dev);
  705. remove_persist_attributes(dev);
  706. device_remove_bin_file(dev, &dev_bin_attr_descriptors);
  707. }
  708. /* Interface Accociation Descriptor fields */
  709. #define usb_intf_assoc_attr(field, format_string) \
  710. static ssize_t \
  711. iad_##field##_show(struct device *dev, struct device_attribute *attr, \
  712. char *buf) \
  713. { \
  714. struct usb_interface *intf = to_usb_interface(dev); \
  715. \
  716. return sprintf(buf, format_string, \
  717. intf->intf_assoc->field); \
  718. } \
  719. static DEVICE_ATTR_RO(iad_##field)
  720. usb_intf_assoc_attr(bFirstInterface, "%02x\n");
  721. usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
  722. usb_intf_assoc_attr(bFunctionClass, "%02x\n");
  723. usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
  724. usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
  725. /* Interface fields */
  726. #define usb_intf_attr(field, format_string) \
  727. static ssize_t \
  728. field##_show(struct device *dev, struct device_attribute *attr, \
  729. char *buf) \
  730. { \
  731. struct usb_interface *intf = to_usb_interface(dev); \
  732. \
  733. return sprintf(buf, format_string, \
  734. intf->cur_altsetting->desc.field); \
  735. } \
  736. static DEVICE_ATTR_RO(field)
  737. usb_intf_attr(bInterfaceNumber, "%02x\n");
  738. usb_intf_attr(bAlternateSetting, "%2d\n");
  739. usb_intf_attr(bNumEndpoints, "%02x\n");
  740. usb_intf_attr(bInterfaceClass, "%02x\n");
  741. usb_intf_attr(bInterfaceSubClass, "%02x\n");
  742. usb_intf_attr(bInterfaceProtocol, "%02x\n");
  743. static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
  744. char *buf)
  745. {
  746. struct usb_interface *intf;
  747. char *string;
  748. intf = to_usb_interface(dev);
  749. string = ACCESS_ONCE(intf->cur_altsetting->string);
  750. if (!string)
  751. return 0;
  752. return sprintf(buf, "%s\n", string);
  753. }
  754. static DEVICE_ATTR_RO(interface);
  755. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  756. char *buf)
  757. {
  758. struct usb_interface *intf;
  759. struct usb_device *udev;
  760. struct usb_host_interface *alt;
  761. intf = to_usb_interface(dev);
  762. udev = interface_to_usbdev(intf);
  763. alt = ACCESS_ONCE(intf->cur_altsetting);
  764. return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  765. "ic%02Xisc%02Xip%02Xin%02X\n",
  766. le16_to_cpu(udev->descriptor.idVendor),
  767. le16_to_cpu(udev->descriptor.idProduct),
  768. le16_to_cpu(udev->descriptor.bcdDevice),
  769. udev->descriptor.bDeviceClass,
  770. udev->descriptor.bDeviceSubClass,
  771. udev->descriptor.bDeviceProtocol,
  772. alt->desc.bInterfaceClass,
  773. alt->desc.bInterfaceSubClass,
  774. alt->desc.bInterfaceProtocol,
  775. alt->desc.bInterfaceNumber);
  776. }
  777. static DEVICE_ATTR_RO(modalias);
  778. static ssize_t supports_autosuspend_show(struct device *dev,
  779. struct device_attribute *attr,
  780. char *buf)
  781. {
  782. int s;
  783. device_lock(dev);
  784. /* Devices will be autosuspended even when an interface isn't claimed */
  785. s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend);
  786. device_unlock(dev);
  787. return sprintf(buf, "%u\n", s);
  788. }
  789. static DEVICE_ATTR_RO(supports_autosuspend);
  790. static struct attribute *intf_attrs[] = {
  791. &dev_attr_bInterfaceNumber.attr,
  792. &dev_attr_bAlternateSetting.attr,
  793. &dev_attr_bNumEndpoints.attr,
  794. &dev_attr_bInterfaceClass.attr,
  795. &dev_attr_bInterfaceSubClass.attr,
  796. &dev_attr_bInterfaceProtocol.attr,
  797. &dev_attr_modalias.attr,
  798. &dev_attr_supports_autosuspend.attr,
  799. NULL,
  800. };
  801. static struct attribute_group intf_attr_grp = {
  802. .attrs = intf_attrs,
  803. };
  804. static struct attribute *intf_assoc_attrs[] = {
  805. &dev_attr_iad_bFirstInterface.attr,
  806. &dev_attr_iad_bInterfaceCount.attr,
  807. &dev_attr_iad_bFunctionClass.attr,
  808. &dev_attr_iad_bFunctionSubClass.attr,
  809. &dev_attr_iad_bFunctionProtocol.attr,
  810. NULL,
  811. };
  812. static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
  813. struct attribute *a, int n)
  814. {
  815. struct device *dev = container_of(kobj, struct device, kobj);
  816. struct usb_interface *intf = to_usb_interface(dev);
  817. if (intf->intf_assoc == NULL)
  818. return 0;
  819. return a->mode;
  820. }
  821. static struct attribute_group intf_assoc_attr_grp = {
  822. .attrs = intf_assoc_attrs,
  823. .is_visible = intf_assoc_attrs_are_visible,
  824. };
  825. const struct attribute_group *usb_interface_groups[] = {
  826. &intf_attr_grp,
  827. &intf_assoc_attr_grp,
  828. NULL
  829. };
  830. void usb_create_sysfs_intf_files(struct usb_interface *intf)
  831. {
  832. struct usb_device *udev = interface_to_usbdev(intf);
  833. struct usb_host_interface *alt = intf->cur_altsetting;
  834. if (intf->sysfs_files_created || intf->unregistering)
  835. return;
  836. if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  837. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  838. if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
  839. ; /* We don't actually care if the function fails. */
  840. intf->sysfs_files_created = 1;
  841. }
  842. void usb_remove_sysfs_intf_files(struct usb_interface *intf)
  843. {
  844. if (!intf->sysfs_files_created)
  845. return;
  846. device_remove_file(&intf->dev, &dev_attr_interface);
  847. intf->sysfs_files_created = 0;
  848. }