sysfs.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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 show_##field(struct device *dev, \
  19. struct device_attribute *attr, char *buf) \
  20. { \
  21. struct usb_device *udev; \
  22. struct usb_host_config *actconfig; \
  23. \
  24. udev = to_usb_device(dev); \
  25. actconfig = udev->actconfig; \
  26. if (actconfig) \
  27. return sprintf(buf, format_string, \
  28. actconfig->desc.field); \
  29. else \
  30. return 0; \
  31. } \
  32. #define usb_actconfig_attr(field, format_string) \
  33. usb_actconfig_show(field, format_string) \
  34. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  35. usb_actconfig_attr(bNumInterfaces, "%2d\n")
  36. usb_actconfig_attr(bmAttributes, "%2x\n")
  37. static ssize_t show_bMaxPower(struct device *dev,
  38. struct device_attribute *attr, char *buf)
  39. {
  40. struct usb_device *udev;
  41. struct usb_host_config *actconfig;
  42. udev = to_usb_device(dev);
  43. actconfig = udev->actconfig;
  44. if (!actconfig)
  45. return 0;
  46. return sprintf(buf, "%dmA\n", usb_get_max_power(udev, actconfig));
  47. }
  48. static DEVICE_ATTR(bMaxPower, S_IRUGO, show_bMaxPower, NULL);
  49. static ssize_t show_configuration_string(struct device *dev,
  50. struct device_attribute *attr, char *buf)
  51. {
  52. struct usb_device *udev;
  53. struct usb_host_config *actconfig;
  54. udev = to_usb_device(dev);
  55. actconfig = udev->actconfig;
  56. if ((!actconfig) || (!actconfig->string))
  57. return 0;
  58. return sprintf(buf, "%s\n", actconfig->string);
  59. }
  60. static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
  61. /* configuration value is always present, and r/w */
  62. usb_actconfig_show(bConfigurationValue, "%u\n");
  63. static ssize_t
  64. set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
  65. const char *buf, size_t count)
  66. {
  67. struct usb_device *udev = to_usb_device(dev);
  68. int config, value;
  69. if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
  70. return -EINVAL;
  71. usb_lock_device(udev);
  72. value = usb_set_configuration(udev, config);
  73. usb_unlock_device(udev);
  74. return (value < 0) ? value : count;
  75. }
  76. static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,
  77. show_bConfigurationValue, set_bConfigurationValue);
  78. /* String fields */
  79. #define usb_string_attr(name) \
  80. static ssize_t show_##name(struct device *dev, \
  81. struct device_attribute *attr, char *buf) \
  82. { \
  83. struct usb_device *udev; \
  84. int retval; \
  85. \
  86. udev = to_usb_device(dev); \
  87. usb_lock_device(udev); \
  88. retval = sprintf(buf, "%s\n", udev->name); \
  89. usb_unlock_device(udev); \
  90. return retval; \
  91. } \
  92. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  93. usb_string_attr(product);
  94. usb_string_attr(manufacturer);
  95. usb_string_attr(serial);
  96. static ssize_t
  97. show_speed(struct device *dev, struct device_attribute *attr, char *buf)
  98. {
  99. struct usb_device *udev;
  100. char *speed;
  101. udev = to_usb_device(dev);
  102. switch (udev->speed) {
  103. case USB_SPEED_LOW:
  104. speed = "1.5";
  105. break;
  106. case USB_SPEED_UNKNOWN:
  107. case USB_SPEED_FULL:
  108. speed = "12";
  109. break;
  110. case USB_SPEED_HIGH:
  111. speed = "480";
  112. break;
  113. case USB_SPEED_WIRELESS:
  114. speed = "480";
  115. break;
  116. case USB_SPEED_SUPER:
  117. speed = "5000";
  118. break;
  119. default:
  120. speed = "unknown";
  121. }
  122. return sprintf(buf, "%s\n", speed);
  123. }
  124. static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
  125. static ssize_t
  126. show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
  127. {
  128. struct usb_device *udev;
  129. udev = to_usb_device(dev);
  130. return sprintf(buf, "%d\n", udev->bus->busnum);
  131. }
  132. static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
  133. static ssize_t
  134. show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
  135. {
  136. struct usb_device *udev;
  137. udev = to_usb_device(dev);
  138. return sprintf(buf, "%d\n", udev->devnum);
  139. }
  140. static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
  141. static ssize_t
  142. show_devpath(struct device *dev, struct device_attribute *attr, char *buf)
  143. {
  144. struct usb_device *udev;
  145. udev = to_usb_device(dev);
  146. return sprintf(buf, "%s\n", udev->devpath);
  147. }
  148. static DEVICE_ATTR(devpath, S_IRUGO, show_devpath, NULL);
  149. static ssize_t
  150. show_version(struct device *dev, struct device_attribute *attr, char *buf)
  151. {
  152. struct usb_device *udev;
  153. u16 bcdUSB;
  154. udev = to_usb_device(dev);
  155. bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
  156. return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
  157. }
  158. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  159. static ssize_t
  160. show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
  161. {
  162. struct usb_device *udev;
  163. udev = to_usb_device(dev);
  164. return sprintf(buf, "%d\n", udev->maxchild);
  165. }
  166. static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
  167. static ssize_t
  168. show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
  169. {
  170. struct usb_device *udev;
  171. udev = to_usb_device(dev);
  172. return sprintf(buf, "0x%x\n", udev->quirks);
  173. }
  174. static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
  175. static ssize_t
  176. show_avoid_reset_quirk(struct device *dev, struct device_attribute *attr, char *buf)
  177. {
  178. struct usb_device *udev;
  179. udev = to_usb_device(dev);
  180. return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
  181. }
  182. static ssize_t
  183. set_avoid_reset_quirk(struct device *dev, struct device_attribute *attr,
  184. const char *buf, size_t count)
  185. {
  186. struct usb_device *udev = to_usb_device(dev);
  187. int val;
  188. if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1)
  189. return -EINVAL;
  190. usb_lock_device(udev);
  191. if (val)
  192. udev->quirks |= USB_QUIRK_RESET;
  193. else
  194. udev->quirks &= ~USB_QUIRK_RESET;
  195. usb_unlock_device(udev);
  196. return count;
  197. }
  198. static DEVICE_ATTR(avoid_reset_quirk, S_IRUGO | S_IWUSR,
  199. show_avoid_reset_quirk, set_avoid_reset_quirk);
  200. static ssize_t
  201. show_urbnum(struct device *dev, struct device_attribute *attr, char *buf)
  202. {
  203. struct usb_device *udev;
  204. udev = to_usb_device(dev);
  205. return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
  206. }
  207. static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL);
  208. static ssize_t
  209. show_removable(struct device *dev, struct device_attribute *attr, char *buf)
  210. {
  211. struct usb_device *udev;
  212. char *state;
  213. udev = to_usb_device(dev);
  214. switch (udev->removable) {
  215. case USB_DEVICE_REMOVABLE:
  216. state = "removable";
  217. break;
  218. case USB_DEVICE_FIXED:
  219. state = "fixed";
  220. break;
  221. default:
  222. state = "unknown";
  223. }
  224. return sprintf(buf, "%s\n", state);
  225. }
  226. static DEVICE_ATTR(removable, S_IRUGO, show_removable, NULL);
  227. static ssize_t
  228. show_ltm_capable(struct device *dev, struct device_attribute *attr, char *buf)
  229. {
  230. if (usb_device_supports_ltm(to_usb_device(dev)))
  231. return sprintf(buf, "%s\n", "yes");
  232. return sprintf(buf, "%s\n", "no");
  233. }
  234. static DEVICE_ATTR(ltm_capable, S_IRUGO, show_ltm_capable, NULL);
  235. #ifdef CONFIG_PM
  236. static ssize_t
  237. show_persist(struct device *dev, struct device_attribute *attr, char *buf)
  238. {
  239. struct usb_device *udev = to_usb_device(dev);
  240. return sprintf(buf, "%d\n", udev->persist_enabled);
  241. }
  242. static ssize_t
  243. set_persist(struct device *dev, struct device_attribute *attr,
  244. const char *buf, size_t count)
  245. {
  246. struct usb_device *udev = to_usb_device(dev);
  247. int value;
  248. /* Hubs are always enabled for USB_PERSIST */
  249. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  250. return -EPERM;
  251. if (sscanf(buf, "%d", &value) != 1)
  252. return -EINVAL;
  253. usb_lock_device(udev);
  254. udev->persist_enabled = !!value;
  255. usb_unlock_device(udev);
  256. return count;
  257. }
  258. static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
  259. static int add_persist_attributes(struct device *dev)
  260. {
  261. int rc = 0;
  262. if (is_usb_device(dev)) {
  263. struct usb_device *udev = to_usb_device(dev);
  264. /* Hubs are automatically enabled for USB_PERSIST,
  265. * no point in creating the attribute file.
  266. */
  267. if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
  268. rc = sysfs_add_file_to_group(&dev->kobj,
  269. &dev_attr_persist.attr,
  270. power_group_name);
  271. }
  272. return rc;
  273. }
  274. static void remove_persist_attributes(struct device *dev)
  275. {
  276. sysfs_remove_file_from_group(&dev->kobj,
  277. &dev_attr_persist.attr,
  278. power_group_name);
  279. }
  280. #else
  281. #define add_persist_attributes(dev) 0
  282. #define remove_persist_attributes(dev) do {} while (0)
  283. #endif /* CONFIG_PM */
  284. #ifdef CONFIG_PM_RUNTIME
  285. static ssize_t
  286. show_connected_duration(struct device *dev, struct device_attribute *attr,
  287. 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(connected_duration, S_IRUGO, show_connected_duration, NULL);
  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
  302. show_active_duration(struct device *dev, struct device_attribute *attr,
  303. char *buf)
  304. {
  305. struct usb_device *udev = to_usb_device(dev);
  306. int duration;
  307. if (udev->state != USB_STATE_SUSPENDED)
  308. duration = jiffies_to_msecs(jiffies + udev->active_duration);
  309. else
  310. duration = jiffies_to_msecs(udev->active_duration);
  311. return sprintf(buf, "%u\n", duration);
  312. }
  313. static DEVICE_ATTR(active_duration, S_IRUGO, show_active_duration, NULL);
  314. static ssize_t
  315. show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
  316. {
  317. return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);
  318. }
  319. static ssize_t
  320. set_autosuspend(struct device *dev, struct device_attribute *attr,
  321. const char *buf, size_t count)
  322. {
  323. int value;
  324. if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
  325. value <= -INT_MAX/1000)
  326. return -EINVAL;
  327. pm_runtime_set_autosuspend_delay(dev, value * 1000);
  328. return count;
  329. }
  330. static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
  331. show_autosuspend, set_autosuspend);
  332. static const char on_string[] = "on";
  333. static const char auto_string[] = "auto";
  334. static void warn_level(void) {
  335. static int level_warned;
  336. if (!level_warned) {
  337. level_warned = 1;
  338. printk(KERN_WARNING "WARNING! power/level is deprecated; "
  339. "use power/control instead\n");
  340. }
  341. }
  342. static ssize_t
  343. show_level(struct device *dev, struct device_attribute *attr, char *buf)
  344. {
  345. struct usb_device *udev = to_usb_device(dev);
  346. const char *p = auto_string;
  347. warn_level();
  348. if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
  349. p = on_string;
  350. return sprintf(buf, "%s\n", p);
  351. }
  352. static ssize_t
  353. set_level(struct device *dev, struct device_attribute *attr,
  354. const char *buf, size_t count)
  355. {
  356. struct usb_device *udev = to_usb_device(dev);
  357. int len = count;
  358. char *cp;
  359. int rc = count;
  360. warn_level();
  361. cp = memchr(buf, '\n', count);
  362. if (cp)
  363. len = cp - buf;
  364. usb_lock_device(udev);
  365. if (len == sizeof on_string - 1 &&
  366. strncmp(buf, on_string, len) == 0)
  367. usb_disable_autosuspend(udev);
  368. else if (len == sizeof auto_string - 1 &&
  369. strncmp(buf, auto_string, len) == 0)
  370. usb_enable_autosuspend(udev);
  371. else
  372. rc = -EINVAL;
  373. usb_unlock_device(udev);
  374. return rc;
  375. }
  376. static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
  377. static ssize_t
  378. show_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,
  379. char *buf)
  380. {
  381. struct usb_device *udev = to_usb_device(dev);
  382. const char *p;
  383. if (udev->usb2_hw_lpm_enabled == 1)
  384. p = "enabled";
  385. else
  386. p = "disabled";
  387. return sprintf(buf, "%s\n", p);
  388. }
  389. static ssize_t
  390. set_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,
  391. const char *buf, size_t count)
  392. {
  393. struct usb_device *udev = to_usb_device(dev);
  394. bool value;
  395. int ret;
  396. usb_lock_device(udev);
  397. ret = strtobool(buf, &value);
  398. if (!ret)
  399. ret = usb_set_usb2_hardware_lpm(udev, value);
  400. usb_unlock_device(udev);
  401. if (!ret)
  402. return count;
  403. return ret;
  404. }
  405. static DEVICE_ATTR(usb2_hardware_lpm, S_IRUGO | S_IWUSR, show_usb2_hardware_lpm,
  406. set_usb2_hardware_lpm);
  407. static ssize_t
  408. show_usb2_lpm_l1_timeout(struct device *dev, struct device_attribute *attr,
  409. char *buf)
  410. {
  411. struct usb_device *udev = to_usb_device(dev);
  412. return sprintf(buf, "%d\n", udev->l1_params.timeout);
  413. }
  414. static ssize_t
  415. set_usb2_lpm_l1_timeout(struct device *dev, struct device_attribute *attr,
  416. const char *buf, size_t count)
  417. {
  418. struct usb_device *udev = to_usb_device(dev);
  419. u16 timeout;
  420. if (kstrtou16(buf, 0, &timeout))
  421. return -EINVAL;
  422. udev->l1_params.timeout = timeout;
  423. return count;
  424. }
  425. static DEVICE_ATTR(usb2_lpm_l1_timeout, S_IRUGO | S_IWUSR,
  426. show_usb2_lpm_l1_timeout, set_usb2_lpm_l1_timeout);
  427. static ssize_t
  428. show_usb2_lpm_besl(struct device *dev, struct device_attribute *attr,
  429. char *buf)
  430. {
  431. struct usb_device *udev = to_usb_device(dev);
  432. return sprintf(buf, "%d\n", udev->l1_params.besl);
  433. }
  434. static ssize_t
  435. set_usb2_lpm_besl(struct device *dev, struct device_attribute *attr,
  436. const char *buf, size_t count)
  437. {
  438. struct usb_device *udev = to_usb_device(dev);
  439. u8 besl;
  440. if (kstrtou8(buf, 0, &besl) || besl > 15)
  441. return -EINVAL;
  442. udev->l1_params.besl = besl;
  443. return count;
  444. }
  445. static DEVICE_ATTR(usb2_lpm_besl, S_IRUGO | S_IWUSR,
  446. show_usb2_lpm_besl, set_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. show_##field(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(field, S_IRUGO, show_##field, NULL);
  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. show_##field(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(field, S_IRUGO, show_##field, NULL);
  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 usb_dev_authorized_show(struct device *dev,
  523. struct device_attribute *attr,
  524. char *buf)
  525. {
  526. struct usb_device *usb_dev = to_usb_device(dev);
  527. return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
  528. }
  529. /*
  530. * Authorize a device to be used in the system
  531. *
  532. * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
  533. */
  534. static ssize_t usb_dev_authorized_store(struct device *dev,
  535. struct device_attribute *attr,
  536. const char *buf, size_t size)
  537. {
  538. ssize_t result;
  539. struct usb_device *usb_dev = to_usb_device(dev);
  540. unsigned val;
  541. result = sscanf(buf, "%u\n", &val);
  542. if (result != 1)
  543. result = -EINVAL;
  544. else if (val == 0)
  545. result = usb_deauthorize_device(usb_dev);
  546. else
  547. result = usb_authorize_device(usb_dev);
  548. return result < 0? result : size;
  549. }
  550. static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, 0644,
  551. usb_dev_authorized_show, usb_dev_authorized_store);
  552. /* "Safely remove a device" */
  553. static ssize_t usb_remove_store(struct device *dev,
  554. struct device_attribute *attr,
  555. const char *buf, size_t count)
  556. {
  557. struct usb_device *udev = to_usb_device(dev);
  558. int rc = 0;
  559. usb_lock_device(udev);
  560. if (udev->state != USB_STATE_NOTATTACHED) {
  561. /* To avoid races, first unconfigure and then remove */
  562. usb_set_configuration(udev, -1);
  563. rc = usb_remove_device(udev);
  564. }
  565. if (rc == 0)
  566. rc = count;
  567. usb_unlock_device(udev);
  568. return rc;
  569. }
  570. static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0200, NULL, usb_remove_store);
  571. static struct attribute *dev_attrs[] = {
  572. /* current configuration's attributes */
  573. &dev_attr_configuration.attr,
  574. &dev_attr_bNumInterfaces.attr,
  575. &dev_attr_bConfigurationValue.attr,
  576. &dev_attr_bmAttributes.attr,
  577. &dev_attr_bMaxPower.attr,
  578. /* device attributes */
  579. &dev_attr_urbnum.attr,
  580. &dev_attr_idVendor.attr,
  581. &dev_attr_idProduct.attr,
  582. &dev_attr_bcdDevice.attr,
  583. &dev_attr_bDeviceClass.attr,
  584. &dev_attr_bDeviceSubClass.attr,
  585. &dev_attr_bDeviceProtocol.attr,
  586. &dev_attr_bNumConfigurations.attr,
  587. &dev_attr_bMaxPacketSize0.attr,
  588. &dev_attr_speed.attr,
  589. &dev_attr_busnum.attr,
  590. &dev_attr_devnum.attr,
  591. &dev_attr_devpath.attr,
  592. &dev_attr_version.attr,
  593. &dev_attr_maxchild.attr,
  594. &dev_attr_quirks.attr,
  595. &dev_attr_avoid_reset_quirk.attr,
  596. &dev_attr_authorized.attr,
  597. &dev_attr_remove.attr,
  598. &dev_attr_removable.attr,
  599. &dev_attr_ltm_capable.attr,
  600. NULL,
  601. };
  602. static struct attribute_group dev_attr_grp = {
  603. .attrs = dev_attrs,
  604. };
  605. /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
  606. * accordingly.
  607. */
  608. static struct attribute *dev_string_attrs[] = {
  609. &dev_attr_manufacturer.attr,
  610. &dev_attr_product.attr,
  611. &dev_attr_serial.attr,
  612. NULL
  613. };
  614. static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
  615. struct attribute *a, int n)
  616. {
  617. struct device *dev = container_of(kobj, struct device, kobj);
  618. struct usb_device *udev = to_usb_device(dev);
  619. if (a == &dev_attr_manufacturer.attr) {
  620. if (udev->manufacturer == NULL)
  621. return 0;
  622. } else if (a == &dev_attr_product.attr) {
  623. if (udev->product == NULL)
  624. return 0;
  625. } else if (a == &dev_attr_serial.attr) {
  626. if (udev->serial == NULL)
  627. return 0;
  628. }
  629. return a->mode;
  630. }
  631. static struct attribute_group dev_string_attr_grp = {
  632. .attrs = dev_string_attrs,
  633. .is_visible = dev_string_attrs_are_visible,
  634. };
  635. const struct attribute_group *usb_device_groups[] = {
  636. &dev_attr_grp,
  637. &dev_string_attr_grp,
  638. NULL
  639. };
  640. /* Binary descriptors */
  641. static ssize_t
  642. read_descriptors(struct file *filp, struct kobject *kobj,
  643. struct bin_attribute *attr,
  644. char *buf, loff_t off, size_t count)
  645. {
  646. struct device *dev = container_of(kobj, struct device, kobj);
  647. struct usb_device *udev = to_usb_device(dev);
  648. size_t nleft = count;
  649. size_t srclen, n;
  650. int cfgno;
  651. void *src;
  652. /* The binary attribute begins with the device descriptor.
  653. * Following that are the raw descriptor entries for all the
  654. * configurations (config plus subsidiary descriptors).
  655. */
  656. for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
  657. nleft > 0; ++cfgno) {
  658. if (cfgno < 0) {
  659. src = &udev->descriptor;
  660. srclen = sizeof(struct usb_device_descriptor);
  661. } else {
  662. src = udev->rawdescriptors[cfgno];
  663. srclen = __le16_to_cpu(udev->config[cfgno].desc.
  664. wTotalLength);
  665. }
  666. if (off < srclen) {
  667. n = min(nleft, srclen - (size_t) off);
  668. memcpy(buf, src + off, n);
  669. nleft -= n;
  670. buf += n;
  671. off = 0;
  672. } else {
  673. off -= srclen;
  674. }
  675. }
  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. show_iad_##field(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(iad_##field, S_IRUGO, show_iad_##field, NULL);
  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. show_##field(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(field, S_IRUGO, show_##field, NULL);
  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 show_interface_string(struct device *dev,
  744. struct device_attribute *attr, char *buf)
  745. {
  746. struct usb_interface *intf;
  747. char *string;
  748. intf = to_usb_interface(dev);
  749. string = intf->cur_altsetting->string;
  750. barrier(); /* The altsetting might change! */
  751. if (!string)
  752. return 0;
  753. return sprintf(buf, "%s\n", string);
  754. }
  755. static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
  756. static ssize_t show_modalias(struct device *dev,
  757. struct device_attribute *attr, char *buf)
  758. {
  759. struct usb_interface *intf;
  760. struct usb_device *udev;
  761. struct usb_host_interface *alt;
  762. intf = to_usb_interface(dev);
  763. udev = interface_to_usbdev(intf);
  764. alt = intf->cur_altsetting;
  765. return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  766. "ic%02Xisc%02Xip%02Xin%02X\n",
  767. le16_to_cpu(udev->descriptor.idVendor),
  768. le16_to_cpu(udev->descriptor.idProduct),
  769. le16_to_cpu(udev->descriptor.bcdDevice),
  770. udev->descriptor.bDeviceClass,
  771. udev->descriptor.bDeviceSubClass,
  772. udev->descriptor.bDeviceProtocol,
  773. alt->desc.bInterfaceClass,
  774. alt->desc.bInterfaceSubClass,
  775. alt->desc.bInterfaceProtocol,
  776. alt->desc.bInterfaceNumber);
  777. }
  778. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  779. static ssize_t show_supports_autosuspend(struct device *dev,
  780. struct device_attribute *attr, char *buf)
  781. {
  782. struct usb_interface *intf;
  783. struct usb_device *udev;
  784. int ret;
  785. intf = to_usb_interface(dev);
  786. udev = interface_to_usbdev(intf);
  787. usb_lock_device(udev);
  788. /* Devices will be autosuspended even when an interface isn't claimed */
  789. if (!intf->dev.driver ||
  790. to_usb_driver(intf->dev.driver)->supports_autosuspend)
  791. ret = sprintf(buf, "%u\n", 1);
  792. else
  793. ret = sprintf(buf, "%u\n", 0);
  794. usb_unlock_device(udev);
  795. return ret;
  796. }
  797. static DEVICE_ATTR(supports_autosuspend, S_IRUGO, show_supports_autosuspend, NULL);
  798. static struct attribute *intf_attrs[] = {
  799. &dev_attr_bInterfaceNumber.attr,
  800. &dev_attr_bAlternateSetting.attr,
  801. &dev_attr_bNumEndpoints.attr,
  802. &dev_attr_bInterfaceClass.attr,
  803. &dev_attr_bInterfaceSubClass.attr,
  804. &dev_attr_bInterfaceProtocol.attr,
  805. &dev_attr_modalias.attr,
  806. &dev_attr_supports_autosuspend.attr,
  807. NULL,
  808. };
  809. static struct attribute_group intf_attr_grp = {
  810. .attrs = intf_attrs,
  811. };
  812. static struct attribute *intf_assoc_attrs[] = {
  813. &dev_attr_iad_bFirstInterface.attr,
  814. &dev_attr_iad_bInterfaceCount.attr,
  815. &dev_attr_iad_bFunctionClass.attr,
  816. &dev_attr_iad_bFunctionSubClass.attr,
  817. &dev_attr_iad_bFunctionProtocol.attr,
  818. NULL,
  819. };
  820. static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
  821. struct attribute *a, int n)
  822. {
  823. struct device *dev = container_of(kobj, struct device, kobj);
  824. struct usb_interface *intf = to_usb_interface(dev);
  825. if (intf->intf_assoc == NULL)
  826. return 0;
  827. return a->mode;
  828. }
  829. static struct attribute_group intf_assoc_attr_grp = {
  830. .attrs = intf_assoc_attrs,
  831. .is_visible = intf_assoc_attrs_are_visible,
  832. };
  833. const struct attribute_group *usb_interface_groups[] = {
  834. &intf_attr_grp,
  835. &intf_assoc_attr_grp,
  836. NULL
  837. };
  838. void usb_create_sysfs_intf_files(struct usb_interface *intf)
  839. {
  840. struct usb_device *udev = interface_to_usbdev(intf);
  841. struct usb_host_interface *alt = intf->cur_altsetting;
  842. if (intf->sysfs_files_created || intf->unregistering)
  843. return;
  844. if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  845. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  846. if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
  847. ; /* We don't actually care if the function fails. */
  848. intf->sysfs_files_created = 1;
  849. }
  850. void usb_remove_sysfs_intf_files(struct usb_interface *intf)
  851. {
  852. if (!intf->sysfs_files_created)
  853. return;
  854. device_remove_file(&intf->dev, &dev_attr_interface);
  855. intf->sysfs_files_created = 0;
  856. }