Chinaunix首页 | 论坛 | 博客
  • 博客访问: 88206
  • 博文数量: 60
  • 博客积分: 4002
  • 博客等级: 中校
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 18:11
文章分类

全部博文(60)

文章存档

2011年(60)

我的朋友

分类: LINUX

2011-01-05 15:25:05

  1. System Utilities sysfs Library - libsysfs
  2. =========================================

  3. Version: 0.4.0
  4. December 16, 2003

  5. Contents
  6. --------
  7. 1. Introduction
  8. 2. Requirements
  9. 3. Definitions
  10. 4. Overview
  11. 5. Data Structures
  12. 5.1 Directory and Attribute Data Structures
  13. 5.1.1 Attribute Structure
  14. 5.1.2 Link Structure
  15. 5.1.3 Directory Structure
  16. 5.2 Bus Data Structure
  17. 5.3 Class Data Structures
  18. 5.4 Root Device Data Structure
  19. 5.5 Device Data Structure
  20. 5.6 Driver Data Structure
  21. 6. Functions
  22. 6.1 Utility Functions
  23. 6.2 Filesystem Functions
  24. 6.2.1 Attribute Functions
  25. 6.2.2 Directory Link Functions
  26. 6.2.3 Directory Functions
  27. 6.3 Bus Functions
  28. 6.4 Class Functions
  29. 6.5 Device Functions
  30. 6.6 Driver Functions
  31. 7. Navigating a dlist
  32. 8. Usage
  33. 9. Conclusion


  34. 1. Introduction
  35. ---------------

  36. Libsysfs' purpose is to provide a consistent and stable interface for
  37. querying system device information exposed through the sysfs filesystem.
  38. The library implements functions for querying filesystem information,
  39. such as reading directories and files. It also contains routines for
  40. working with buses, classes, and the device tree.


  41. 2. Requirements
  42. ---------------

  43. The library must satisfy the following requirements:

  44. - It must provide a stable programming interfaces that applications can
  45. be built upon.

  46. - It must provide functions to retrieve device Vital Product Data (VPD)
  47. information for Error Log Analysis (ELA) support. ELA will provide
  48. device driver and device bus address information.

  49. - It must provide access to all system devices and information exposed
  50. by sysfs.

  51. - It must provide a function to find sysfs' current mount point.

  52. - It must provide a function for udev to retrieve a device's major and
  53. minor numbers.


  54. 3. Definitions
  55. --------------

  56. - sysfs: Sysfs is a virtual filesystem in 2.5+ Linux kernels that
  57. presents a hierarchical representation of all system physical and
  58. virtual devices. It presents system devices by bus, by class, and
  59. by topology. Callbacks to device drivers are exposed as files in
  60. device directories. Sysfs, for all purposes, is our tree of system
  61. devices. For more information, please see:


  62. - udev: Udev is Greg Kroah-Hartman's User Space implementation of devfs.
  63. Udev creates /dev nodes for devices upon Hotplug events. The Hotplug
  64. event provides udev with a sysfs directory location of the device. Udev
  65. must use that directory to grab device's major and minor number, which it
  66. will use to create the /dev node. For more information, please see:



  67. 4. Overview
  68. -----------

  69. Libsysfs grew from a common need. There are several applications under
  70. development that need access to sysfs and system devices. Udev, on a
  71. hotplug event, must take a sysfs device path and create a /dev node. Our
  72. diagnostic client needs to list all system devices. Finally, our Error
  73. Log Analysis piece is required to retrieve VPD information for a
  74. failing device. We divided to create a single library interface rather
  75. than having these separate applications create their own accesses to
  76. sysfs involving reading directories and files.

  77. Libsysfs will also provide stability for applications to be built upon. Sysfs
  78. currently doesn't enforce any standards for callback or file names. File
  79. names change depending on bus or class. Sysfs is also changing, it is
  80. currently being developed. Libsysfs will provide a stable interface to
  81. applications while allowing sysfs to change underneath it.

  82. Like sysfs, the library will provide devices to applications by bus, by
  83. class, and by topology. The library will function similar to directories
  84. and files that lie underneath it. To query a device on a PCI bus, one would
  85. "open" the bus to scan or read devices and "close" the bus when
  86. completed. Besides supplying functions to retrieve devices, the library
  87. will also provide some utility functions like getting sysfs mount point.


  88. 5. Data Structures
  89. ------------------

  90. Libsysfs will classify system devices following sysfs' example, dividing
  91. them by bus, class, and devices. The library presents this information
  92. generically. It doesn't, for example, differentiate between PCI and USB
  93. buses. Device attributes are presented with values as they are exposed
  94. by sysfs, the values are not formatted.

  95. The library will provide standard definitions for working with sysfs
  96. and devices, here's some examples:

  97. #define SYSFS_FSTYPE_NAME "sysfs"
  98. #define SYSFS_PROC_MNTS "/proc/mounts"
  99. #define SYSFS_BUS_DIR "/bus"
  100. #define SYSFS_BUS_NAME "bus"
  101. #define SYSFS_CLASS_DIR "/class"
  102. #define SYSFS_CLASS_NAME "class"
  103. #define SYSFS_BLOCK_DIR "/block"
  104. #define SYSFS_BLOCK_NAME "block"
  105. #define SYSFS_DEVICES_DIR "/devices"
  106. #define SYSFS_DEVICES_NAME "devices"
  107. #define SYSFS_DRIVERS_DIR "/drivers"
  108. #define SYSFS_DRIVERS_NAME "drivers"
  109. #define SYSFS_NAME_ATTRIBUTE "name"

  110. The library uses some definitions to mark maximum size of a sysfs name or
  111. path length:

  112. #define SYSFS_PATH_MAX 255
  113. #define SYSFS_NAME_LEN 50
  114. #define SYSFS_BUS_ID_SIZE 20


  115. NOTE:
  116. As of release 0.4.0 of libsysfs, a number of changes have been made
  117. so that the dlists and "directory" references in all libsysfs's
  118. structures are not populated until such time that it is absolutely
  119. necessary. Hence, these elements may not contain valid data at all
  120. times (as was the case before).

  121. 5.1 Directory and Attribute Data Structures
  122. -------------------------------------------

  123. The library implements structures to represent sysfs directories, links,
  124. and files.


  125. 5.1.1 Attribute Structure
  126. -------------------------

  127. A file in sysfs represents a device or driver attribute. Attributes can be
  128. read only, write only, or read and write. File data can be ASCII and
  129. binary. The library has the following structure to represent files:

  130. struct sysfs_attribute {
  131. unsigned char *value;
  132. unsigned short len; /* value length */
  133. unsigned short method; /* show and store */
  134. unsigned char name[SYSFS_NAME_LEN];
  135. unsigned char path[SYSFS_PATH_MAX];
  136. };

  137. Path represents the file/attribute's full path. Value is used when reading
  138. from or writing to an attribute. "len" is the length of data in "value".
  139. Method is a bitmask for defining if the attribute supports show(read)
  140. and/or store(write).


  141. 5.1.2 Link Structure
  142. --------------------

  143. Symbolic links are used in sysfs to link bus or class views with
  144. particular devices.

  145. struct sysfs_link {
  146. unsigned char name[SYSFS_NAME_LEN];
  147. unsigned char path[SYSFS_PATH_MAX];
  148. unsigned char target[SYSFS_PATH_MAX];
  149. };

  150. Link's name is stored in "name' and it's target stored in "target". Absolute
  151. path to the link is stored in "path".


  152. 5.1.3 Directory Structure
  153. -------------------------

  154. The directory structure represents a sysfs directory:

  155. struct sysfs_directory {
  156. struct dlist *subdirs;
  157. struct dlist *links;
  158. struct dlist *attributes;
  159. unsigned char name[SYSFS_NAME_LEN];
  160. unsigned char path[SYSFS_PATH_MAX];
  161. };

  162. The sysfs_directory structure includes the list of subdirectories, links and
  163. attributes. The "name" and absolute "path" are also stored in the structure.
  164. The sysfs_directory structure is intended for use internal to the library.
  165. Applications needing access to attributes and links from the directory
  166. will need to make appropriate calls (described below) to get the same.


  167. 5.2 Bus Data Structure
  168. ----------------------

  169. All buses look similar in sysfs including lists of devices and drivers,
  170. therefore we use the following structure to represent all sysfs buses:

  171. struct sysfs_bus {
  172. unsigned char name[SYSFS_NAME_LEN];
  173. unsigned char path[SYSFS_PATH_MAX];

  174. /* internal use only */
  175. struct dlist *drivers;
  176. struct dlist *devices;
  177. struct sysfs_directory *directory;
  178. };

  179. The sysfs_bus structure contains the bus "name", while the "path" to bus
  180. directory is also stored. It also contains lists of devices on the bus
  181. and drivers that are registered on it. The bus' directory is represented
  182. by the sysfs_directory structure and it contains references to all the
  183. subdirectories, links, and attributes. The sysfs_directory structure
  184. is for internal use only. The following functions may be used by
  185. applications to retrieve data from the sysfs_directory structure:

  186. struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus)
  187. struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
  188. unsigned char *attrname)


  189. 5.3 Class Data Structures
  190. -------------------------

  191. The library uses two data structures to represent classes in sysfs. Sysfs
  192. classes contains a class directory like "net" or "scsi_host" and then
  193. class devices like "eth0", "lo", or "eth1" for the "net" class.

  194. struct sysfs_class {
  195. unsigned char name[SYSFS_NAME_LEN];
  196. unsigned char path[SYSFS_PATH_MAX];

  197. /* for internal use only */
  198. struct dlist *devices;
  199. struct sysfs_directory *directory;
  200. };

  201. The sysfs_class represents device classes in sysfs like "net". It contains
  202. the class "name", "path" to the class, a list of class devices and the
  203. directory representation (for internal use only).

  204. struct sysfs_class_device {
  205. unsigned char name[SYSFS_NAME_LEN];
  206. unsigned char classname[SYSFS_NAME_LEN];
  207. unsigned char path[SYSFS_PATH_MAX];

  208. /* for internal use only */
  209. struct sysfs_class_device *parent;
  210. struct sysfs_device *sysdevice; /* NULL if virtual */
  211. struct sysfs_driver *driver; /* NULL if not implemented */
  212. struct sysfs_directory *directory;
  213. };

  214. A class device isn't the same as a sysfs_device, it's specific to the class in
  215. which it belongs. The class device structure contains the name of the class
  216. the class device belongs to, its sysfs_device reference and that device's
  217. driver reference (if any). It also contains the name of the class device
  218. - like "eth0", its parent point (if present) and its sysfs directory
  219. information including links and attributes (for internal use only).
  220. The following function may be used by applications to retrieve data
  221. from the sysfs_directory structure:

  222. struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev);


  223. 5.4 Root Device Data Structure
  224. ------------------------------

  225. Device hierarchies in sysfs are represented under the /sys/devices directory
  226. structure. Sysfs devices typically spawn off from base devices which are
  227. represented by a sysfs_root_device.

  228. struct sysfs_root_device {
  229. unsigned char name[SYSFS_NAME_LEN];
  230. unsigned char path[SYSFS_PATH_MAX];

  231. /* for internal use only */
  232. struct dlist *devices;
  233. struct sysfs_directory *directory;
  234. };

  235. The sysfs_root_device structure contains a list of "devices" that spawn off it.
  236. The name of the root device as represented under /sys/devices is read into
  237. "name" and the absolute path into "path" and its sysfs_directory information
  238. intended to be used internal to the library.


  239. 5.5 Device Data Structure
  240. -------------------------

  241. The sysfs_device structure represents a system device that's exposed
  242. in sysfs under the /sys/devices directory structure.

  243. struct sysfs_device {
  244. unsigned char name[SYSFS_NAME_LEN];
  245. unsigned char bus_id[SYSFS_NAME_LEN];
  246. unsigned char bus[SYSFS_NAME_LEN];
  247. unsigned char driver_name[SYSFS_NAME_LEN];
  248. unsigned char path[SYSFS_PATH_MAX];

  249. /* for internal use only */
  250. struct sysfs_device *parent;
  251. struct dlist *children;
  252. struct sysfs_directory *directory;
  253. };

  254. The sysfs_device structure contains a "parent" pointer, a list of child
  255. devices, if any, device's directory, its bus id - which is the name of
  256. device's directory, the bus name on which this device is registered and
  257. its driver name. The device structure also contains the absolute path
  258. to the device and a directory structure, which contains a list of the
  259. device's attributes (for internal use only). The following functions
  260. may be used to obtain information from sysfs_directory structure:

  261. struct sysfs_attribute *sysfs_get_device_attribute(struct sysfs_device *dev,
  262. const unsigned char *name)
  263. struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)


  264. 5.6 Driver Data Structure
  265. -------------------------

  266. The sysfs_driver structure represents a device driver.

  267. struct sysfs_driver {
  268. unsigned char name[SYSFS_NAME_LEN];
  269. unsigned char path[SYSFS_PATH_MAX];

  270. /* for internal use only */
  271. struct dlist *devices;
  272. struct sysfs_directory *directory;
  273. };

  274. The sysfs_driver structure contains a list of devices that use this driver,
  275. the name of the driver, its path, and its directory information, which
  276. includes the driver's attributes (for internal use only). The following
  277. function may be used to retrieve driver attribute information from the
  278. sysfs_directory structure:

  279. struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver)


  280. 6. Functions
  281. ------------

  282. Libsysfs will provide functions to access system devices by bus, by class,
  283. and by device. Functions will act like accessing directories and files,
  284. using "open" and "close". Open returns a structure and close is used
  285. to clean that structure up.


  286. 6.1 Utility Functions
  287. ---------------------

  288. The library will provide a few utility functions for working with sysfs.

  289. -------------------------------------------------------------------------------
  290. Name: sysfs_get_mnt_path

  291. Description: Function finds the mount path for filesystem type "sysfs".

  292. Arguments: unsigned char *mnt_path Mount path buffer
  293. size_t len Size of mount path buffer

  294. Returns: Zero with success.
  295. -1 with error. Errno will be set with error:
  296. - EINVAL for invalid argument, if buffer is NULL.

  297. Prototype: sysfs_get_mnt_path(unsigned char *mnt_path, size_t len);
  298. -------------------------------------------------------------------------------

  299. -------------------------------------------------------------------------------
  300. Name: sysfs_get_name_from_path

  301. Description: Function returns the last directory or file name from the
  302. included path.

  303. Arguments: const unsigned char *path Path to parse name from
  304. unsigned char *name Buffer to put parsed name into
  305. size_t *len Size of name buffer

  306. Returns: 0 with success.
  307. -1 on Error. Errno will be set with error, returning
  308. - EINVAL for invalid arguments

  309. Prototype: int sysfs_get_name_from_path(const unsigned char *path,
  310. unsigned char *name, size_t *len)
  311. -------------------------------------------------------------------------------

  312. -------------------------------------------------------------------------------
  313. Name: sysfs_get_link

  314. Description: Sysfs readlink function, reads the link at supplied path
  315. and returns its target path.

  316. Arguments: const unsigned char *path Link's path
  317. unsigned char *target Buffer to place link's target path
  318. size_t len Size of target buffer

  319. Returns: 0 with success
  320. -1 with error. Errno will be set with error, returning
  321. - EINVAL for invalid arguments

  322. Prototype: int sysfs_get_link(const unsigned char *path,
  323. unsigned char *target, size_t len)
  324. -------------------------------------------------------------------------------

  325. -------------------------------------------------------------------------------
  326. Name: sysfs_open_subsystem_list

  327. Description: Function returns the list of entries for the given subsystem. If
  328. the argument is "bus", this function will return a list of buses
  329. ("pci", "scsi", etc) supported on the system.

  330. sysfs_close_list() has to be called to free the list obtained
  331. from this call.

  332. Arguments: unsigned char *name Subsystem to open, like "bus"..

  333. Returns: dlist of entries for the subsystem on success
  334. NULL with error indicating the "name" subsystem is invalid.

  335. Prototype: struct dlist *sysfs_open_subsystem_list(unsigned char *name)
  336. -------------------------------------------------------------------------------

  337. -------------------------------------------------------------------------------
  338. Name: sysfs_open_bus_devices_list

  339. Description: Function returns the list of devices on the given bus.

  340. sysfs_close_list() has to be called to free the list obtained
  341. from this call.

  342. Arguments: unsigned char *name Bus name to open "pci"/"scsi"/"usb"..

  343. Returns: dlist of device names for the given bus on success
  344. NULL with error indicating the bus is not supported.

  345. Prototype: struct dlist *sysfs_open_bus_devices_list(unsigned char *name)
  346. -------------------------------------------------------------------------------

  347. -------------------------------------------------------------------------------
  348. Name: sysfs_close_list

  349. Description: Closes a given dlist. This can be used as a generic list close
  350. routine.

  351. Arguments: struct dlist *list List to be closed

  352. Prototype: void sysfs_close_list(struct dlist *list)
  353. -------------------------------------------------------------------------------

  354. -------------------------------------------------------------------------------
  355. Name: sysfs_path_is_dir

  356. Description: Utility function to verify if a given path is to a directory.

  357. Arguments: unsigned char *path Path to verify

  358. Returns: 0 on success, 1 on error
  359. - EINVAL for invalid arguments

  360. Prototype: int sysfs_path_is_dir(unsigned char *path)
  361. -------------------------------------------------------------------------------

  362. -------------------------------------------------------------------------------
  363. Name: sysfs_path_is_file

  364. Description: Utility function to verify if a given path is to a file.

  365. Arguments: unsigned char *path Path to verify

  366. Returns: 0 on success, 1 on error
  367. - EINVAL for invalid arguments

  368. Prototype: int sysfs_path_is_file(unsigned char *path)
  369. -------------------------------------------------------------------------------

  370. -------------------------------------------------------------------------------
  371. Name: sysfs_path_is_link

  372. Description: Utility function to verify if a given path is to a link.

  373. Arguments: unsigned char *path Path to verify

  374. Returns: 0 on success, 1 on error
  375. - EINVAL for invalid arguments

  376. Prototype: int sysfs_path_is_link(unsigned char *path)
  377. -------------------------------------------------------------------------------

  378. 6.2 Filesystem Functions
  379. ------------------------

  380. Libsysfs provides a set of functions to open, read, and close directories
  381. and attributes/files in sysfs. These functions mirror their filesystem
  382. function counterparts.

  383. 6.2.1 Attribute Functions
  384. -------------------------

  385. Along with the usual open, read, and close functions, libsysfs provides
  386. a couple other functions for accessing attribute values.

  387. -------------------------------------------------------------------------------
  388. Name: sysfs_open_attribute

  389. Description: Opens up a file in sysfs and creates a sysfs_attribute
  390. structure. File isn't read with this function.

  391. Arguments: const unsigned char *path File/Attribute's path

  392. Returns: struct sysfs_attribute * with success.
  393. NULL with error. Errno will be set with error, returning
  394. - EINVAL for invalid arguments

  395. Prototype: struct sysfs_attribute *sysfs_open_attribute
  396. (const unsigned char *path)
  397. -------------------------------------------------------------------------------

  398. -------------------------------------------------------------------------------
  399. Name: sysfs_close_attribute

  400. Description: Cleans up and closes sysfs_attribute structure.

  401. Arguments: struct sysfs_attribute *sysattr Attribute to close

  402. Prototype: void sysfs_close_attribute(struct sysfs_attribute *sysattr)
  403. -------------------------------------------------------------------------------

  404. -------------------------------------------------------------------------------
  405. Name: sysfs_read_dir_attributes

  406. Description: Reads the given sysfs_directory to create a list of attributes.

  407. Arguments: struct sysfs_directory *sysdir sysfs_directory whose
  408. attributes to read

  409. Returns: struct dlist * of attributes on success
  410. NULL with error. Errno will be set on error, returning EINVAL
  411. for invalid arguments

  412. Prototype: struct dlist *sysfs_read_dir_attributes
  413. (struct sysfs_directory *sysdir)
  414. -------------------------------------------------------------------------------

  415. -------------------------------------------------------------------------------
  416. Name: sysfs_refresh_attributes

  417. Description: Given a list of attributes, this function refreshes the values
  418. of attributes in the list.

  419. Arguments: struct dlist *attrlist list of attributes to refresh

  420. Returns: 0 with success.
  421. 1 with error. Errno will be set on error, returning EINVAL
  422. for invalid arguments

  423. Prototype: int sysfs_refresh_attributes(struct dlist *attrlist)
  424. -------------------------------------------------------------------------------

  425. -------------------------------------------------------------------------------
  426. Name: sysfs_read_attribute

  427. Description: Reads the supplied attribute. Since the maximum transfer
  428. from a sysfs attribute is a pagesize, function reads in
  429. up to a page from the file and stores it in the "value"
  430. field in the attribute.

  431. Arguments: struct sysfs_attribute *sysattr Attribute to read

  432. Returns: 0 with success.
  433. -1 with error. Errno will be set with error, returning
  434. - EINVAL for invalid arguments

  435. Prototype: int sysfs_read_attribute(struct sysfs_attribute *sysattr)
  436. -------------------------------------------------------------------------------

  437. -------------------------------------------------------------------------------
  438. Name: sysfs_write_attribute

  439. Description: Writes to the supplied attribute. Function validates if attribute
  440. is writable, and writes the new value to the attribute. Value to
  441. write as well as its length is user supplied. In case the length
  442. written is not equal to the length requested to be written, the
  443. original value is restored and an error is returned.

  444. Arguments: struct sysfs_attribute *sysattr Attribute to write to
  445. const unsigned char *new_value New value for the attribute
  446. size_t len Length of "new_value"

  447. Returns: 0 with success.
  448. -1 with error. Errno will be set with error, returning
  449. - EINVAL for invalid arguments

  450. Prototype: int sysfs_write_attribute(struct sysfs_attribute *sysattr,
  451. const unsigned char *new_value, size_t len)
  452. -------------------------------------------------------------------------------

  453. -------------------------------------------------------------------------------
  454. Name: sysfs_read_attribute_value

  455. Description: Given a path to a specific attribute, function reads and
  456. returns its value to the supplied value buffer.

  457. Arguments: const unsigned char *attrpath Attribute path to read
  458. unsigned char *value Buffer to place attribute's value
  459. size_t vsize Size of buffer

  460. Returns: 0 with success.
  461. -1 with error. Errno will be set with error, returning
  462. - EINVAL for invalid arguments

  463. Prototype: int sysfs_read_attribute_value(const unsigned char *attrpath,
  464. unsigned char *value, size_t vsize)
  465. -------------------------------------------------------------------------------

  466. -------------------------------------------------------------------------------
  467. Name: sysfs_get_value_from_attributes

  468. Description: Function takes a single or linked list of sysfs attribute
  469. structures and returns the value of the specified attribute
  470. name.

  471. Arguments: struct sysfs_attribute *attr
  472. Attribute list to search through
  473. const unsigned char *name Name of attribute to return value

  474. Returns: unsigned char * attribute value with success.
  475. NULL with error. Errno will be set with error, returning
  476. - EINVAL for invalid arguments

  477. Prototype: unsigned char *sysfs_get_value_from_attributes
  478. (struct sysfs_attribute *attr, const unsigned char * name)
  479. -------------------------------------------------------------------------------

  480. -------------------------------------------------------------------------------
  481. Name: sysfs_get_directory_attribute

  482. Description: Function walks the list of attributes for the given sysfs
  483. directory and returns the sysfs_attribute structure for
  484. the specified attribute name.

  485. Arguments: struct sysfs_directory *dir Directory in which to search
  486. unsigned char *attrname Attribute name to look for

  487. Returns: struct sysfs_attribute on success.
  488. NULL with error. Errno will be set with error, returning
  489. - EINVAL for invalid arguments

  490. Prototype: struct sysfs_attribute *sysfs_get_directory_attribute
  491. (struct sysfs_directory *dir, unsigned char *attrname)
  492. -------------------------------------------------------------------------------


  493. 6.2.2 Link Functions
  494. --------------------

  495. Sysfs contains many symbolic links, like bus links to bus devices. Libsysfs
  496. treats links differently than directories due to processing differences. A
  497. link in the /sys/bus/"busname"/devices/ directory indicates a device in the
  498. /sys/devices directory. Through links we give the functionality to know
  499. what is and what isn't a link and the ability to query the links target.

  500. -------------------------------------------------------------------------------
  501. Name: sysfs_open_link

  502. Description: Opens a directory link.

  503. Arguments: const unsigned char *linkpath Path to link

  504. Returns: struct sysfs_link * with success.
  505. NULL with error. Errno will be set with error, returning
  506. - EINVAL for invalid arguments

  507. Prototype: struct sysfs_link *sysfs_open_link
  508. (const unsigned char *linkpath)
  509. -------------------------------------------------------------------------------

  510. -------------------------------------------------------------------------------
  511. Name: sysfs_close_link

  512. Description: Closes a directory link structure.

  513. Arguments: struct sysfs_link *ln Link to close

  514. Prototype: void sysfs_close_link(struct sysfs_link *ln)
  515. -------------------------------------------------------------------------------

  516. -------------------------------------------------------------------------------
  517. Name: sysfs_read_dir_links

  518. Description: Reads the given sysfs_directory to create a list of links.

  519. Arguments: struct sysfs_directory *sysdir sysfs_directory whose
  520. links to read

  521. Returns: struct dlist * of links with success
  522. NULL with error. Errno will be set on error, returning EINVAL
  523. for invalid arguments

  524. Prototype: struct dlist *sysfs_read_dir_links
  525. (struct sysfs_directory *sysdir)
  526. -------------------------------------------------------------------------------

  527. -------------------------------------------------------------------------------
  528. Name: sysfs_get_directory_link

  529. Description: Function walks the list of links for the given sysfs directory
  530. and returns the sysfs_link structure for the specified link
  531. name.

  532. Arguments: struct sysfs_directory *dir Directory in which to search
  533. unsigned char *linkname Link name to look for

  534. Returns: struct sysfs_link * with success.
  535. NULL with error. Errno will be set with error, returning
  536. - EINVAL for invalid arguments

  537. Prototype: struct sysfs_link *sysfs_get_directory_link
  538. (struct sysfs_directory *dir, unsigned char *linkname)
  539. -------------------------------------------------------------------------------

  540. -------------------------------------------------------------------------------
  541. Name: sysfs_get_subdirectory_link

  542. Description: Function walks the list of links for the given sysfs directory
  543. and its subdirectories returns the sysfs_link structure for
  544. the specified link name.

  545. Arguments: struct sysfs_directory *dir Directory in which to search
  546. unsigned char *linkname Link name to look for

  547. Returns: struct sysfs_link * with success.
  548. NULL with error. Errno will be set with error, returning
  549. - EINVAL for invalid arguments

  550. Prototype: struct sysfs_link *sysfs_get_subdirectory_link
  551. (struct sysfs_directory *dir, unsigned char *linkname)
  552. -------------------------------------------------------------------------------


  553. 6.2.3 Directory Functions
  554. -------------------------

  555. Sysfs directories can represent every directory under sysfs. The structures
  556. keep track of subdirectories, links, and files. Like opendir, readdir, and
  557. closedir, libsysfs provides open, read, and close functions for working with
  558. sysfs directories. Open creates the sysfs_directory structure. Read reads in
  559. its contents - like subdirectories, links, and files. Close cleans it all
  560. up.

  561. -------------------------------------------------------------------------------
  562. Name: sysfs_open_directory

  563. Description: Opens a sysfs directory at a specific path

  564. Arguments: const unsigned char *path Directory path to open

  565. Returns: struct sysfs_directory * with success.
  566. NULL with error. Errno will be set with error, returning
  567. - EINVAL for invalid arguments

  568. Prototype: struct sysfs_directory *sysfs_open_directory
  569. (const unsigned char *path)
  570. -------------------------------------------------------------------------------

  571. -------------------------------------------------------------------------------
  572. Name: sysfs_close_directory

  573. Description: Closes specific directory, its subdirectories, links, and
  574. files.

  575. Arguments: struct sysfs_directory *sysdir Directory to close

  576. Prototype: void sysfs_close_directory(struct sysfs_directory *sysdir)
  577. -------------------------------------------------------------------------------

  578. -------------------------------------------------------------------------------
  579. Name: sysfs_read_dir_subdirs

  580. Description: Reads the given sysfs_directory to create a list of subdirs.

  581. Arguments: struct sysfs_directory *sysdir sysfs_directory whose
  582. subdirs have to be read

  583. Returns: struct dlist * of links with success
  584. NULL with error. Errno will be set on error, returning EINVAL
  585. for invalid arguments

  586. Prototype: struct dlist *sysfs_read_dir_subdirs
  587. (struct sysfs_directory *sysdir)
  588. -------------------------------------------------------------------------------

  589. -------------------------------------------------------------------------------
  590. Name: sysfs_read_directory

  591. Description: Read the supplied directory. Reading fills in the directory's
  592. contents like subdirectories, links, and attributes.

  593. Arguments: struct sysfs_directory *sysdir Directory to read

  594. Returns: 0 with success.
  595. -1 with error. Errno will be set with error, returning
  596. - EINVAL for invalid arguments

  597. Prototype: int sysfs_read_directory(struct sysfs_directory *sysdir)
  598. -------------------------------------------------------------------------------

  599. -------------------------------------------------------------------------------
  600. Name: sysfs_read_all_subdirs

  601. Description: Reads all subdirs under a given supplied directory.

  602. Arguments: struct sysfs_directory *sysdir Directory to read

  603. Returns: 0 with success.
  604. -1 with error. Errno will be set with error, returning
  605. - EINVAL for invalid arguments

  606. Prototype: int sysfs_read_all_subdirs(struct sysfs_directory *sysdir)
  607. -------------------------------------------------------------------------------

  608. -------------------------------------------------------------------------------
  609. Name: sysfs_get_subdirectory

  610. Description: Function walks the directory tree for the given directory and
  611. returns a sysfs_directory structure for the specified directory
  612. name.

  613. Arguments: struct sysfs_directory *dir Directory in which to search
  614. unsigned char *subname Name of directory to look for

  615. Returns: struct sysfs_directory with success.
  616. NULL with error. Errno will be set with error, returning
  617. - EINVAL for invalid arguments
  618. -------------------------------------------------------------------------------


  619. 6.3 Bus Functions
  620. -----------------

  621. The library provides a functions for viewing buses represented in sysfs.
  622. The sysfs_open_bus opens a bus in the /sys/bus directory, such as "pci",
  623. "usb", or "scsi". The open command returns a sysfs_bus structure that
  624. contains a list of the bus' devices. The sysfs_close_bus function is
  625. used to clean up the bus structure. Given a device or a driver,
  626. functions are provided to determine what bus they are on.

  627. -------------------------------------------------------------------------------
  628. Name: sysfs_open_bus

  629. Description: Function opens up one of the buses represented in sysfs in
  630. the /sys/bus directory. It returns a sysfs_bus structure
  631. that includes a list of bus devices and drivers.

  632. Arguments: const unsigned char *name Bus name to open, like "pci"...

  633. Returns: struct sysfs_bus * with success
  634. NULL with error. Errno will be set with error, returning
  635. - EINVAL for invalid arguments

  636. Prototype: struct sysfs_bus *sysfs_open_bus(const unsigned char *name)
  637. -------------------------------------------------------------------------------

  638. -------------------------------------------------------------------------------
  639. Name: sysfs_close_bus

  640. Description: Function closes up the sysfs_bus structure including its
  641. devices, drivers, and directory.

  642. Arguments: sysfs_bus *bus Bus structure to close

  643. Prototype: void sysfs_close_bus(struct sysfs_bus *bus)
  644. -------------------------------------------------------------------------------

  645. -------------------------------------------------------------------------------
  646. Name: sysfs_get_bus_devices

  647. Description: Function returns a list of devices that are registered with
  648. this bus.

  649. Arguments: struct sysfs_bus *bus Bus whose devices list to return

  650. Returns: struct dlist * of sysfs_devices on success
  651. NULL with error. Errno will be sent with error, returning
  652. - EINVAL for invalid arguments

  653. Prototype: struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus)
  654. -------------------------------------------------------------------------------

  655. -------------------------------------------------------------------------------
  656. Name: sysfs_get_bus_drivers

  657. Description: Function returns a list of drivers that are registered with
  658. this bus.

  659. Arguments: struct sysfs_bus *bus Bus whose drivers list to return

  660. Returns: struct dlist * of sysfs_drivers on success
  661. NULL with error. Errno will be sent with error, returning
  662. - EINVAL for invalid arguments

  663. Prototype: struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus)
  664. -------------------------------------------------------------------------------

  665. -------------------------------------------------------------------------------
  666. Name: sysfs_get_bus_device

  667. Description: Function takes a sysfs_bus structure(obtained on a successful
  668. return from a sysfs_open_bus() call) and looks for the given
  669. device on this bus. On success, it returns a sysfs_device
  670. structure corresponding to the device.

  671. Arguments: struct sysfs_bus *bus Bus structure on which to search
  672. unsigned char *id Device to look for

  673. Returns: struct sysfs_device * with success
  674. NULL with error. Errno will be set with error, returning
  675. - EINVAL for invalid arguments

  676. Prototype: struct sysfs_device *sysfs_get_bus_device
  677. (struct sysfs_bus *bus, unsigned char *id)
  678. -------------------------------------------------------------------------------

  679. -------------------------------------------------------------------------------
  680. Name: sysfs_get_bus_driver

  681. Description: Function takes a sysfs_bus structure (obtained on a successful
  682. return from a sysfs_open_bus() call) and looks for the given
  683. driver on this bus. On success, it returns a sysfs_driver
  684. structure corresponding to the driver.

  685. Arguments: struct sysfs_bus *bus Bus structure on which to search
  686. unsigned char *drvname Driver to look for

  687. Returns: struct sysfs_driver * with success
  688. NULL with error. Errno will be set with error, returning
  689. - EINVAL for invalid arguments

  690. Prototype: struct sysfs_device *sysfs_get_bus_driver
  691. (struct sysfs_bus *bus, unsigned char *drvname)
  692. -------------------------------------------------------------------------------

  693. -------------------------------------------------------------------------------
  694. Name: sysfs_get_bus_attributes

  695. Description: Function takes a sysfs_bus structure and returns a list of
  696. attributes for the bus.

  697. Arguments: struct sysfs_bus *bus Bus for which attributes are required

  698. Returns: struct dlist * of attributes with success
  699. NULL with error. Errno will be set with error, returning
  700. - EINVAL for invalid arguments

  701. Prototype: struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus)
  702. -------------------------------------------------------------------------------

  703. -------------------------------------------------------------------------------
  704. Name: sysfs_get_bus_attribute

  705. Description: Function takes a sysfs_bus structure and looks for the required
  706. attribute on the bus. On success, it returns a sysfs_attribute
  707. structure corresponding to the given attribute.

  708. Arguments: struct sysfs_bus *bus Bus structure on which to search
  709. unsigned char *attrname Attribute to look for

  710. Returns: struct sysfs_attribute * with success
  711. NULL with error. Errno will be set with error, returning
  712. - EINVAL for invalid arguments

  713. Prototype: struct sysfs_attribute *sysfs_get_bus_attribute
  714. (struct sysfs_bus *bus, unsigned char *attrname)
  715. -------------------------------------------------------------------------------

  716. -------------------------------------------------------------------------------
  717. Name: sysfs_open_bus_device

  718. Description: Given the name of the bus on which to look for, this function
  719. locates a given device on the bus and returns a sysfs_device
  720. structure corresponding to the requested device.

  721. NOTE:
  722. 1. The sysfs_device structure obtained upon successful return
  723. from this function has to be closed by calling
  724. sysfs_close_device().

  725. Arguments: unsigned char *busname Bus on which to search
  726. unsigned char *dev_id Name of the device to look for

  727. Returns: struct sysfs_device * on success
  728. NULL with error. Errno will be set with error, returning
  729. - EINVAL for invalid arguments

  730. Prototype: struct sysfs_device *sysfs_open_bus_device
  731. (unsigned char *busname, unsigned char *dev_id)
  732. -------------------------------------------------------------------------------

  733. -------------------------------------------------------------------------------
  734. Name: sysfs_find_driver_bus

  735. Description: Given the name of a driver, this function finds the name of the
  736. bus the driver is on

  737. Arguments: const unsigned char *driver Name of the driver to look for
  738. unsigned char *busname Buffer to return the bus name
  739. size_t bsize Size of the "busname" buffer

  740. Returns: 0 with success.
  741. -1 with error. Errno will be set with error, returning
  742. - EINVAL for invalid arguments

  743. Prototype: int sysfs_find_driver_bus(const unsigned char *driver,
  744. unsigned char *busname, size_t bsize)
  745. -------------------------------------------------------------------------------


  746. 6.4 Class Functions
  747. -------------------

  748. Libsysfs provides functions to open sysfs classes and their class devices.
  749. These functions too operate with open and close, close must be called to
  750. clean up the class structures. Given a class device name, functions are
  751. provided to determine what class they belong to. Once a class device
  752. name and the class it belongs to is known, a function to open the
  753. class device is provided. This method can be used when details of
  754. a single class device is required.

  755. -------------------------------------------------------------------------------
  756. Name: sysfs_open_class

  757. Description: Function opens up one of the classes represented in sysfs in
  758. the /sys/class directory. It returns a sysfs_class structure
  759. that includes a list of class devices.

  760. Arguments: const unsigned char *name Class name to open, like "net"..

  761. Returns: struct sysfs_class * with success
  762. NULL with error. Errno will be set with error, returning
  763. - EINVAL for invalid arguments

  764. Prototype: struct sysfs_class *sysfs_open_class(const unsigned char *name)
  765. -------------------------------------------------------------------------------

  766. -------------------------------------------------------------------------------
  767. Name: sysfs_close_class

  768. Description: Function closes up the sysfs_class structure including its
  769. class devices.

  770. Arguments: sysfs_class *class Class structure to close

  771. Prototype: void sysfs_close_class(struct sysfs_class *class);
  772. -------------------------------------------------------------------------------

  773. -------------------------------------------------------------------------------
  774. Name: sysfs_open_class_device_path

  775. Description: Function opens up one of the class devices represented in
  776. sysfs in sysfs/class/"class"/ directory. It returns a
  777. sysfs_class_device structure.

  778. Arguments: const unsigned char *path Path to class device

  779. Returns: struct sysfs_class_device * with success
  780. NULL with error. Errno will be set with error, returning
  781. - EINVAL for invalid arguments

  782. Prototype: struct sysfs_class_device *sysfs_open_class_device_path
  783. (const unsigned char *path)
  784. -------------------------------------------------------------------------------

  785. -------------------------------------------------------------------------------
  786. Name: sysfs_close_class_device

  787. Description: Function closes up the sysfs_class_device structure.

  788. Arguments: sysfs_class_device *dev Class device structure to close

  789. Prototype: void sysfs_close_class_device(struct sysfs_class_device *dev)
  790. -------------------------------------------------------------------------------

  791. -------------------------------------------------------------------------------
  792. Name: sysfs_get_class_device

  793. Description: Function takes a sysfs_class structure(obtained on a successful
  794. return from a sysfs_open_class() call) and looks for the given
  795. device in this class. On success, it returns a sysfs_class_device
  796. structure corresponding to the class device.

  797. Arguments: struct sysfs_class *class Class on which to search
  798. unsigned_char *name Class device "name" to look for

  799. Returns: struct sysfs_class_device * with success
  800. NULL with error. Errno will be set with error, returning
  801. - EINVAL for invalid arguments

  802. Prototype: struct sysfs_class_device *sysfs_get_class_device
  803. (struct sysfs_class *class, unsigned char *name)
  804. -------------------------------------------------------------------------------

  805. -------------------------------------------------------------------------------
  806. Name: sysfs_get_class_devices

  807. Description: Function returns a list of class devices for the given class.

  808. Arguments: struct sysfs_class *cls Class whose class device list
  809. is required

  810. Returns: struct dlist * of sysfs_class_devices on success
  811. NULL with error. Errno will be set with error, returning
  812. - EINVAL for invalid arguments

  813. Prototype: struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
  814. -------------------------------------------------------------------------------

  815. -------------------------------------------------------------------------------
  816. Name: sysfs_open_class_device

  817. Description: Given the name of the class on which to look for, this function
  818. locates a given class device and returns a sysfs_class_device
  819. structure corresponding to the requested class device.

  820. NOTE:
  821. 1. The sysfs_class_device structure obtained upon successful
  822. return from this function has to be closed by calling
  823. sysfs_close_class_device().
  824. 2. Class this device belongs to must be known prior to calling
  825. this function.

  826. Arguments: const unsigned char *classname Class on which to search
  827. unsigned char *name Class device "name" to open

  828. Returns: struct sysfs_class_device * with success
  829. NULL with error. Errno will be set with error, returning
  830. - EINVAL for invalid arguments

  831. Prototype: struct sysfs_class_device *sysfs_open_class_device
  832. (const unsigned char *classname, unsigned char *class)
  833. -------------------------------------------------------------------------------

  834. -------------------------------------------------------------------------------
  835. Name: sysfs_get_classdev_device

  836. Description: Function returns the sysfs_device reference (if present) for the
  837. given class device.

  838. Arguments: struct sysfs_class_device *clsdev Class device whose
  839. sysfs_device reference
  840. is required

  841. Returns: struct sysfs_device * on success
  842. NULL with error. Errno will be set with error, returning
  843. - EINVAL for invalid arguments

  844. Prototype: struct sysfs_device *sysfs_get_classdev_device
  845. (struct sysfs_class_device *clsdev)
  846. -------------------------------------------------------------------------------

  847. -------------------------------------------------------------------------------
  848. Name: sysfs_get_classdev_driver

  849. Description: Function returns the sysfs_driver reference (if present) for the
  850. given class device.

  851. Arguments: struct sysfs_class_device *clsdev Class device whose
  852. sysfs_driver reference
  853. is required

  854. Returns: struct sysfs_driver * on success
  855. NULL with error. Errno will be set with error, returning
  856. - EINVAL for invalid arguments

  857. Prototype: struct sysfs_driver *sysfs_get_classdev_driver
  858. (struct sysfs_class_device *clsdev)
  859. -------------------------------------------------------------------------------

  860. -------------------------------------------------------------------------------
  861. Name: sysfs_get_classdev_parent

  862. Description: Function returns the sysfs_class_device reference for the parent
  863. (if present) of the given class device.

  864. Arguments: struct sysfs_class_device *clsdev Class device whose
  865. parent reference
  866. is required

  867. Returns: struct sysfs_class_device * on success
  868. NULL with error. Errno will be set with error, returning
  869. - EINVAL for invalid arguments

  870. Prototype: struct sysfs_class_device *sysfs_get_classdev_parent
  871. (struct sysfs_class_device *clsdev)
  872. -------------------------------------------------------------------------------

  873. -------------------------------------------------------------------------------
  874. Name: sysfs_get_classdev_attributes

  875. Description: Function takes a sysfs_class_device structure and returns a list
  876. of attributes for the class device.

  877. Arguments: struct sysfs_class_device *cdev Class device for which
  878. attributes are required

  879. Returns: struct dlist * of attributes with success
  880. NULL with error. Errno will be set with error, returning
  881. - EINVAL for invalid arguments

  882. Prototype: struct dlist *sysfs_get_classdev_attributes
  883. (struct sysfs_class_device *cdev)
  884. -------------------------------------------------------------------------------

  885. -------------------------------------------------------------------------------
  886. Name: sysfs_get_classdev_attr

  887. Description: Searches supplied class device's attributes by name and returns
  888. the attribute.

  889. Arguments: struct sysfs_class_device *clsdev Device to search
  890. const unsigned char *name Attribute name to find

  891. Returns: struct sysfs_attribute * with success
  892. NULL with error. Errno will be set with error, returning
  893. - EINVAL for invalid arguments

  894. Prototype: struct sysfs_attribute *sysfs_get_classdev_attr
  895. (struct sysfs_class_device *clsdev, const unsigned char *name)
  896. -------------------------------------------------------------------------------

  897. -------------------------------------------------------------------------------
  898. Name: sysfs_open_classdev_attr

  899. Description: Function takes as arguments, a the name of the class, the class
  900. device name and the name of the required attribute.

  901. NOTE:
  902. 1. The struct sysfs_attribute * obtained upon successful
  903. return from this function has to be closed by making
  904. a call to sysfs_close_attribute()

  905. Arguments: unsigned char *classname Class name on which to search
  906. unsigned char *dev Name of the class device
  907. unsigned char *attrib Attribute to open

  908. Returns: struct sysfs_attribute * with success.
  909. NULL with error. Errno will be set with error, returning
  910. - EINVAL for invalid arguments

  911. Prototype: struct sysfs_attribute *sysfs_write_classdev_attr
  912. (const unsigned char *classname, const unsigned char *dev,
  913. const unsigned char *attrib)
  914. -------------------------------------------------------------------------------


  915. 6.5 Device Functions
  916. --------------------

  917. Devices represent everything in sysfs under /sys/devices, which is a
  918. hierarchical view of system devices. Besides the expected open and
  919. close functions, libsysfs provides open and close functions for
  920. root devices. These functions recursively open or close a device
  921. and all of its children.

  922. -------------------------------------------------------------------------------
  923. Name: sysfs_open_device_path

  924. Description: Opens up a device at a specific path. It opens the device's
  925. directory, reads the directory, and returns a sysfs_device
  926. structure.

  927. Arguments: const unsigned char *path Path to device

  928. Returns: struct sysfs_device * with success
  929. NULL with error. Errno will be set with error, returning
  930. - EINVAL for invalid arguments

  931. Prototype: struct sysfs_device *sysfs_open_device_path
  932. (const unsigned char *path)
  933. -------------------------------------------------------------------------------

  934. -------------------------------------------------------------------------------
  935. Name: sysfs_close_device

  936. Description: Function closes up the sysfs_device structure.

  937. Arguments: sysfs_device *dev Device structure to close

  938. Prototype: void sysfs_close_device(struct sysfs_device *dev)
  939. -------------------------------------------------------------------------------

  940. -------------------------------------------------------------------------------
  941. Name: sysfs_open_root_device

  942. Description: Function opens up one of the root devices represented in sysfs
  943. in the /sys/devices directory. It returns a sysfs_root_device
  944. structure that includes a list of devices in the tree.

  945. Arguments: const unsigned char *name Name of the root device to open

  946. Returns: struct sysfs_root_device * with success
  947. NULL with error. Errno will be set with error, returning
  948. - EINVAL for invalid arguments

  949. Prototype: struct sysfs_device *sysfs_open_root_device
  950. (const unsigned char *name)
  951. -------------------------------------------------------------------------------

  952. -------------------------------------------------------------------------------
  953. Name: sysfs_close_root_device

  954. Description: Function closes up the sysfs_root_device structure including the
  955. devices in the root device tree.

  956. Arguments: sysfs_device *root Root device structure to close

  957. Prototype: void sysfs_close_root_device(struct sysfs_root_device *root)
  958. -------------------------------------------------------------------------------

  959. -------------------------------------------------------------------------------
  960. Name: sysfs_get_device_parent

  961. Description: Function returns the sysfs_device reference for the parent
  962. (if present) of the given sysfs_device.

  963. Arguments: struct sysfs_device *dev sysfs_device whose parent
  964. reference is required

  965. Returns: struct sysfs_device * on success
  966. NULL with error. Errno will be set with error, returning
  967. - EINVAL for invalid arguments

  968. Prototype: struct sysfs_device *sysfs_get_device_parent
  969. (struct sysfs_device *dev)
  970. -------------------------------------------------------------------------------

  971. -------------------------------------------------------------------------------
  972. Name: sysfs_get_root_devices

  973. Description: Function returns a list of devices under the given root device.

  974. Arguments: struct sysfs_root_device *root sysfs_root_device whose devices
  975. list is required

  976. Returns: struct dlist * of sysfs_devices on success
  977. NULL with error. Errno will be set with error, returning
  978. - EINVAL for invalid arguments

  979. Prototype: struct dlist *sysfs_get_root_devices
  980. (struct sysfs_root_device *root)
  981. -------------------------------------------------------------------------------

  982. -------------------------------------------------------------------------------
  983. Name: sysfs_get_device_attr

  984. Description: Searches supplied device's attributes by name and returns
  985. the attribute.

  986. Arguments: struct sysfs_device *dev Device to search
  987. const unsigned char *name Attribute name to find

  988. Returns: struct sysfs_attribute * with success
  989. NULL with error. Errno will be set with error, returning
  990. - EINVAL for invalid arguments

  991. Prototype: struct sysfs_attribute *sysfs_get_device_attr
  992. (struct sysfs_device *dev, const unsigned char *name)
  993. -------------------------------------------------------------------------------

  994. -------------------------------------------------------------------------------
  995. Name: sysfs_get_device_attributes

  996. Description: Function takes a sysfs_device structure and returns a list
  997. of attributes for the device.

  998. Arguments: struct sysfs_device *device Device for which
  999. attributes are required

  1000. Returns: struct dlist * of attributes with success
  1001. NULL with error. Errno will be set with error, returning
  1002. - EINVAL for invalid arguments

  1003. Prototype: struct dlist *sysfs_get_device_attributes
  1004. (struct sysfs_device *device)
  1005. -------------------------------------------------------------------------------

  1006. -------------------------------------------------------------------------------
  1007. Name: sysfs_open_device

  1008. Description: Given the name of the bus on which to look for, this function
  1009. locates a given device and returns a sysfs_device structure
  1010. corresponding to the requested device.

  1011. Arguments: const unsigned char *bus_id Device to look for
  1012. const unsigned char *bus Bus on which to search

  1013. Returns: struct sysfs_device * with success
  1014. NULL with error. Errno will be set with error, returning
  1015. - EINVAL for invalid arguments

  1016. Prototype: struct sysfs_device *sysfs_open_device
  1017. (const unsigned char *bus_id, const unsigned char *bus)
  1018. -------------------------------------------------------------------------------

  1019. -------------------------------------------------------------------------------
  1020. Name: sysfs_open_device_attr

  1021. Description: Function takes as arguments, the bus on which to search for a
  1022. device, and an attribute of the device to open.

  1023. NOTE:
  1024. 1. The struct sysfs_attribute * obtained upon successful
  1025. return from this function has to be closed by making
  1026. a call to sysfs_close_attribute()

  1027. Arguments: unsigned char *bus Bus on which to search
  1028. unsigned char *bus_id Device to look for
  1029. unsigned char *attrib Name of the attribute to open

  1030. Returns: struct sysfs_attribute * with success.
  1031. NULL with error. Errno will be set with error, returning
  1032. - EINVAL for invalid arguments

  1033. Prototype: struct sysfs_attribute *sysfs_open_device_attr
  1034. (const unsigned char *bus, const unsigned char *bus_id,
  1035. const unsigned char *attrib)
  1036. -------------------------------------------------------------------------------


  1037. 6.6 Driver Functions
  1038. --------------------

  1039. Drivers are represented in sysfs under the /sys/bus/xxx/drivers (xxx being
  1040. the bus type, such as "pci", "usb, and so on). Functions are provided to
  1041. open and close drivers.

  1042. -------------------------------------------------------------------------------
  1043. Name: sysfs_open_driver_path

  1044. Description: Opens driver at specific path.

  1045. Arguments: const unsigned char *path Path to driver

  1046. Returns: struct sysfs_driver * with success
  1047. NULL with error. Errno will be set with error, returning
  1048. - EINVAL for invalid arguments

  1049. Prototype: struct sysfs_driver *sysfs_open_driver_path
  1050. (const unsigned char *path)
  1051. -------------------------------------------------------------------------------

  1052. -------------------------------------------------------------------------------
  1053. Name: sysfs_close_driver

  1054. Description: Closes and cleans up sysfs_driver structure.

  1055. Arguments: sysfs_driver *driver Driver structure to close

  1056. Prototype: void sysfs_close_driver(struct sysfs_driver *driver)
  1057. -------------------------------------------------------------------------------

  1058. -------------------------------------------------------------------------------
  1059. Name: sysfs_get_driver_devices

  1060. Description: Function returns a list of devices that use this driver.

  1061. Arguments: struct sysfs_driver *driver Driver whose devices list is
  1062. required

  1063. Returns: struct dlist * of sysfs_devices on success
  1064. NULL with error. Errno will be set with error, returning
  1065. - EINVAL for invalid arguments

  1066. Prototype: struct dlist *sysfs_get_driver_devices
  1067. (struct sysfs_driver *driver)
  1068. -------------------------------------------------------------------------------

  1069. -------------------------------------------------------------------------------
  1070. Name: sysfs_get_driver_device

  1071. Description: Function returns a sysfs_device reference for the device with
  1072. "name" that uses this driver

  1073. Arguments: struct sysfs_driver *driver Driver on which to search
  1074. const unsigned char *name Name of the device to look for

  1075. Returns: struct sysfs_device * corresponding to "name" on success
  1076. NULL with error. Errno will be set with error, returning
  1077. - EINVAL for invalid arguments

  1078. Prototype: struct dlist *sysfs_get_driver_device
  1079. (struct sysfs_driver *driver, const unsigned char *name)
  1080. -------------------------------------------------------------------------------

  1081. -------------------------------------------------------------------------------
  1082. Name: sysfs_get_driver_attr

  1083. Description: Searches supplied driver's attributes by name and returns
  1084. the attribute.

  1085. Arguments: struct sysfs_driver *drv Driver to search
  1086. const unsigned char *name Attribute name to find

  1087. Returns: struct sysfs_attribute * with success
  1088. NULL with error. Errno will be set with error, returning
  1089. - EINVAL for invalid arguments

  1090. Prototype: struct sysfs_attribute *sysfs_get_driver_attr
  1091. (struct sysfs_driver *drv, const unsigned char *name)
  1092. -------------------------------------------------------------------------------

  1093. -------------------------------------------------------------------------------
  1094. Name: sysfs_get_driver_attributes

  1095. Description: Function takes a sysfs_driver structure and returns a list
  1096. of attributes for the driver.

  1097. Arguments: struct sysfs_driver *driver Driver for which
  1098. attributes are required

  1099. Returns: struct dlist * of attributes with success
  1100. NULL with error. Errno will be set with error, returning
  1101. - EINVAL for invalid arguments

  1102. Prototype: struct dlist *sysfs_get_driver_attributes
  1103. (struct sysfs_driver *driver)
  1104. -------------------------------------------------------------------------------

  1105. -------------------------------------------------------------------------------
  1106. Name: sysfs_open_driver

  1107. Description: Given the name of the bus on which to look for, this function
  1108. locates a given driver and returns a sysfs_driver structure
  1109. corresponding to the requested device.

  1110. NOTE:
  1111. 1. The sysfs_driver structure obtained upon successful return
  1112. from this function has to be closed by calling
  1113. sysfs_close_driver_by_name().
  1114. 2. Bus on which to look for this driver should be known prior
  1115. to calling this function. Use sysfs_find_driver_bus()
  1116. to determine this.

  1117. Arguments: const unsigned char *drv_name Driver to look for
  1118. const unsigned char *bus Bus on which to search
  1119. size_t bsize Size of "bus"

  1120. Returns: struct sysfs_driver * with success
  1121. NULL with error. Errno will be set with error, returning
  1122. - EINVAL for invalid arguments

  1123. Prototype: struct sysfs_driver *sysfs_open_driver
  1124. (const unsigned char *drv_name,
  1125. const unsigned char *bus, size_t bsize)
  1126. -------------------------------------------------------------------------------

  1127. -------------------------------------------------------------------------------
  1128. Name: sysfs_get_driver_links

  1129. Description: Function returns a list of links for a given driver

  1130. Arguments: struct sysfs_driver *driver Driver to get links from

  1131. Returns: struct dlist * of links on success
  1132. NULL with error

  1133. Prototype: struct dlist *sysfs_get_driver_links
  1134. (struct sysfs_driver *driver)
  1135. -------------------------------------------------------------------------------

  1136. -------------------------------------------------------------------------------
  1137. Name: sysfs_open_driver_attr

  1138. Description: Function takes as arguments, the bus the driver is registered
  1139. on, the driver name and the name of the attribute to open.

  1140. NOTE:
  1141. 1. The struct sysfs_attribute * obtained upon successful
  1142. return from this function has to be closed by making
  1143. a call to sysfs_close_attribute()

  1144. Arguments: unsigned char *bus Bus on which driver is present
  1145. unsigned char *drv Driver to look for
  1146. unsigned char *attrib Name of the attribute to open

  1147. Returns: struct sysfs_attribute * with success.
  1148. NULL with error. Errno will be set with error, returning
  1149. - EINVAL for invalid arguments

  1150. Prototype: struct sysfs_attribute *sysfs_open_driver_attr
  1151. (const unsigned char *bus, const unsigned char *drv,
  1152. const unsigned char *attrib)
  1153. -------------------------------------------------------------------------------


  1154. 7. Navigating a dlist
  1155. ---------------------

  1156. Libsysfs uses (yet another) list implementation thanks to Eric J Bohm.

  1157. Some library functions return a dlist of devices/drivers/attributes, etc.
  1158. To navigate the list returned the macro "dlist_for_each_data" is to be used.

  1159. ------------------------------------------------------------------------------
  1160. Function/Macro name: dlist_for_each_data

  1161. Description: Walk the given list, returning a known data type/
  1162. structure in each iteration.

  1163. Arguments: struct dlist *list List pointer
  1164. data_iterator Data type/structure variable
  1165. contained in the list
  1166. datatype Data type/structure contained
  1167. in the list

  1168. Returns: On each iteration, "data_iterator" will contain a list
  1169. element of "datatype"

  1170. Usage example: The function sysfs_get_classdev_attributes() returns a
  1171. dlist of attributes. To navigate the list:

  1172. struct sysfs_attribute *attr = NULL;
  1173. struct dlist *attrlist = NULL;
  1174. .
  1175. .
  1176. .
  1177. attrlist = sysfs_get_classdev_attributes
  1178. (struct sysfs_class_device *cdev)
  1179. if (attrlist != NULL) {
  1180. dlist_for_each_data(attrlist, attr,
  1181. struct sysfs_attribute) {
  1182. .
  1183. .
  1184. .
  1185. }
  1186. }
  1187. -------------------------------------------------------------------------------


  1188. 8. Usage
  1189. --------

  1190. Accessing devices through libsysfs is supposed to mirror accessing devices
  1191. in the filesystem it represents. Here's a typical order of operation:

  1192. - get sysfs mount point
  1193. - "open" sysfs category, ie. bus, class, or device
  1194. - work with category
  1195. - "close" sysfs category


  1196. 9. Conclusion
  1197. -------------

  1198. Libsysfs is meant to provide a stable application programming interface to
  1199. sysfs. Applications can depend upon the library to access system devices
  1200. and functions exposed through sysfs.
阅读(1267) | 评论(0) | 转发(0) |
0

上一篇:c之贪心法

下一篇:C++:Types and Declarations

给主人留下些什么吧!~~