1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
| kobject çå±æ§å¯å¨æ件系ç»ä¸ä»¥æ®éæ件çå½¢å¼å¯¼åºãSysfs 为å±æ§å®ä¹ äºé¢åæ件 I/O æä½çæ¹æ³ï¼ä»¥æä¾å¯¹å
æ ¸å±æ§ç读åã
å±æ§åºä¸º ASCII ç ææ¬æ件ã以ä¸ä¸ªæ件åªåå¨ä¸ä¸ªå±æ§å¼ä¸ºå®ãä½ä¸ä¸ª æ件åªå
å«ä¸ä¸ªå±æ§å¼å¯è½å½±åæçï¼æ以ä¸ä¸ªå
å«ç¸åæ°æ®ç±»åçå±æ§å¼ æ°ç»ä¹è¢«å¹¿æ³å°æ¥åã
æ··åç±»åã表达å¤è¡æ°æ®ä»¥åä¸äºæªå¼çæ°æ®æ ¼å¼ä¼éå°å¼ºçå对ãè¿æ ·åæ¯ å¾ä¸¢è¸ç,èä¸å
¶ä»£ç ä¼å¨æªéç¥ä½è
çæ
åµä¸è¢«éåã
ä¸ä¸ªç®åçå±æ§ç»æå®ä¹å¦ä¸:
struct attribute { char * name; struct module *owner; umode_t mode; };
int sysfs_create_file(struct kobject * kobj, const struct attribute * attr); void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr);
ä¸ä¸ªåç¬çå±æ§ç»æ并ä¸å
å«è¯»åå
¶å±æ§å¼çæ¹æ³ãåç³»ç»æ好为å¢å ç¹å® 对象类åçå±æ§å®ä¹èªå·±çå±æ§ç»æä½åå°è£
å½æ°ã
ä¾å¦:驱å¨ç¨åºæ¨¡åå®ä¹ç device_attribute ç»æä½å¦ä¸:
struct device_attribute { struct attribute attr; ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf); ssize_t (*store)(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); };
int device_create_file(struct device *, const struct device_attribute *); void device_remove_file(struct device *, const struct device_attribute *);
为äºå®ä¹è®¾å¤å±æ§ï¼åæ¶å®ä¹äºä¸ä¸è¾
å©å®:
#define DEVICE_ATTR(_name, _mode, _show, _store) \ struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
ä¾å¦:声æ
static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo);
çåäºå¦ä¸ä»£ç ï¼
static struct device_attribute dev_attr_foo = { .attr = { .name = "foo", .mode = S_IWUSR | S_IRUGO, .show = show_foo, .store = store_foo, }, };
åç³»ç»ç¹æçåè°å½æ° ~~~~~~~~~~~~~~~~~~~
å½ä¸ä¸ªåç³»ç»å®ä¹ä¸ä¸ªæ°çå±æ§ç±»åæ¶ï¼å¿
é¡»å®ç°ä¸ç³»åç sysfs æä½ï¼ 以帮å©è¯»åè°ç¨å®ç°å±æ§ææè
çæ¾ç¤ºåå¨åæ¹æ³ã
struct sysfs_ops { ssize_t (*show)(struct kobject *, struct attribute *, char *); ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); };
[åç³»ç»åºå·²ç»å®ä¹äºä¸ä¸ª struct kobj_type ç»æä½ä½ä¸ºè¿ä¸ªç±»åç æ述符ï¼å¹¶å¨æ¤ä¿å sysfs_ops çæéãæ´å¤çä¿¡æ¯åè§ kobject ç ææ¡£]
sysfs ä¼ä¸ºè¿ä¸ªç±»åè°ç¨éå½çæ¹æ³ãå½ä¸ä¸ªæ件被读åæ¶ï¼è¿ä¸ªæ¹æ³ä¼ å°ä¸è¬çkobject å attribute ç»æä½æé转æ¢ä¸ºéå½çæéç±»åå è°ç¨ç¸å
³èçå½æ°ã
示ä¾:
#define to_dev(obj) container_of(obj, struct device, kobj) #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) { struct device_attribute *dev_attr = to_dev_attr(attr); struct device *dev = to_dev(kobj); ssize_t ret = -EIO;
if (dev_attr->show) ret = dev_attr->show(dev, dev_attr, buf); if (ret >= (ssize_t)PAGE_SIZE) { printk("dev_attr_show: %pS returned bad count\n", dev_attr->show); } return ret; }
读åå±æ§æ°æ® ~~~~~~~~~~~~
å¨å£°æå±æ§æ¶ï¼å¿
é¡»æå® show() æ store() æ¹æ³ï¼ä»¥å®ç°å±æ§ç 读æåãè¿äºæ¹æ³çç±»ååºè¯¥å以ä¸ç设å¤å±æ§å®ä¹ä¸æ ·ç®åã
ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf); ssize_t (*store)(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);
ä¹å°±æ¯è¯´,ä»ä»¬åºåªä»¥ä¸ä¸ªå¤ç对象ãä¸ä¸ªå±æ§åä¸ä¸ªç¼å²æéä½ä¸ºåæ°ã
sysfs ä¼åé
ä¸ä¸ªå¤§å°ä¸º (PAGE_SIZE) çç¼å²åºå¹¶ä¼ éç»è¿ä¸ªæ¹æ³ã Sysfs å°ä¼ä¸ºæ¯æ¬¡è¯»åæä½è°ç¨ä¸æ¬¡è¿ä¸ªæ¹æ³ãè¿ä½¿å¾è¿äºæ¹æ³å¨æ§è¡æ¶ ä¼åºç°ä»¥ä¸çè¡ä¸º:
- å¨è¯»æ¹é¢ï¼read(2)ï¼ï¼show() æ¹æ³åºè¯¥å¡«å
æ´ä¸ªç¼å²åºãåæ³å±æ§ åºåªå¯¼åºäºä¸ä¸ªå±æ§å¼ææ¯ä¸ä¸ªåç±»åå±æ§å¼çæ°ç»ï¼æ以è¿ä¸ªä»£ä»·å° ä¸ä¼ä¸å¤ªé«ã
è¿ä½¿å¾ç¨æ·ç©ºé´å¯ä»¥å±é¨å°è¯»åä»»æçååæç´¢æ´ä¸ªæ件ãå¦æç¨æ·ç©ºé´ ååæç´¢å°é¶æ使ç¨â0âå移æ§è¡ä¸ä¸ªpread(2)æä½ï¼show()æ¹æ³å° å次被è°ç¨ï¼ä»¥éæ°å¡«å
ç¼åã
- å¨åæ¹é¢ï¼write(2)ï¼ï¼sysfs å¸æå¨ç¬¬ä¸æ¬¡åæä½æ¶å¾å°æ´ä¸ªç¼å²åºã ä¹å Sysfs ä¼ éæ´ä¸ªç¼å²åºç» store() æ¹æ³ã
å½è¦å sysfs æ件æ¶ï¼ç¨æ·ç©ºé´è¿ç¨åºé¦å
读åæ´ä¸ªæ件ï¼ä¿®è¯¥æ³è¦ æ¹åçå¼ï¼ç¶åååæ´ä¸ªç¼å²åºã
å¨è¯»åå±æ§å¼æ¶ï¼å±æ§æ¹æ³çæ§è¡åºæä½ç¸åçç¼å²åºã
注记:
- åæä½å¯¼è´ç show() æ¹æ³éè½½ï¼ä¼å¿½ç¥å½åæ件ä½ç½®ã
- ç¼å²åºåºæ»æ¯ PAGE_SIZE 大å°ã对äºi386ï¼è¿ä¸ªå¼ä¸º4096ã
- show() æ¹æ³åºè¯¥è¿ååå
¥ç¼å²åºçåèæ°ï¼ä¹å°±æ¯ snprintf()ç è¿åå¼ã
- show() åºå§ç»ä½¿ç¨ snprintf()ã
- store() åºè¿åç¼å²åºçå·²ç¨åèæ°ãå¦ææ´ä¸ªç¼åé½å·²å¡«æ»¡ï¼åªéè¿å count åæ°ã
- show() æ store() å¯ä»¥è¿åé误å¼ãå½å¾å°ä¸ä¸ªéæ³å¼ï¼å¿
é¡»è¿åä¸ä¸ª é误å¼ã
- ä¸ä¸ªä¼ éç»æ¹æ³ç对象å°ä¼éè¿ sysfs è°ç¨å¯¹è±¡å
åµçå¼ç¨è®¡æ°åºå®å¨ å
åä¸ã尽管å¦æ¤ï¼å¯¹è±¡ä»£è¡¨çç©çå®ä½(å¦è®¾å¤)å¯è½å·²ä¸åå¨ãå¦æå¿
è¦ï¼ åºè¯¥å®ç°ä¸ä¸ªæ£æµæºå¶ã
ä¸ä¸ªç®åç(æªç»å®éªè¯å®ç)设å¤å±æ§å®ç°å¦ä¸ï¼
static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf) { return scnprintf(buf, PAGE_SIZE, "%s\n", dev->name); }
static ssize_t store_name(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { snprintf(dev->name, sizeof(dev->name), "%.*s", (int)min(count, sizeof(dev->name) - 1), buf); return count; }
static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
ï¼æ³¨æï¼çæ£çå®ç°ä¸å
许ç¨æ·ç©ºé´è®¾ç½®è®¾å¤åãï¼
顶å±ç®å½å¸å± ~~~~~~~~~~~~
sysfs ç®å½çå®ææ¾ç¤ºäºå
æ ¸æ°æ®ç»æä¹é´çå
³ç³»ã
é¡¶å± sysfs ç®å½å¦ä¸:
block/ bus/ class/ dev/ devices/ firmware/ net/ fs/
devices/ å
å«äºä¸ä¸ªè®¾å¤æ çæ件系ç»è¡¨ç¤ºãä»ç´æ¥æ å°äºå
é¨çå
æ ¸ 设å¤æ ï¼åæ äºè®¾å¤çå±æ¬¡ç»æã
bus/ å
å«äºå
æ ¸ä¸åç§æ»çº¿ç±»åçå¹³é¢ç®å½å¸å±ãæ¯ä¸ªæ»çº¿ç®å½å
å«ä¸¤ä¸ª åç®å½:
devices/ drivers/
devices/ å
å«äºç³»ç»ä¸åºç°çæ¯ä¸ªè®¾å¤ç符å·é¾æ¥ï¼ä»ä»¬æå root/ ä¸ç 设å¤ç®å½ã
drivers/ å
å«äºæ¯ä¸ªå·²ä¸ºç¹å®æ»çº¿ä¸ç设å¤èæè½½ç驱å¨ç¨åºçç®å½(è¿é åå®é©±å¨æ²¡æè·¨è¶å¤ä¸ªæ»çº¿ç±»å)ã
fs/ å
å«äºä¸ä¸ªä¸ºæ件系ç»è®¾ç«çç®å½ãç°å¨æ¯ä¸ªæ³è¦å¯¼åºå±æ§çæ件系ç»å¿
é¡» å¨ fs/ ä¸å建èªå·±çå±æ¬¡ç»æ(åè§Documentation/filesystems/fuse.txt)ã
dev/ å
å«ä¸¤ä¸ªåç®å½ï¼ char/ å block/ãå¨è¿ä¸¤ä¸ªåç®å½ä¸ï¼æ以 <major>:<minor> æ ¼å¼å½åç符å·é¾æ¥ãè¿äºç¬¦å·é¾æ¥æå sysfs ç®å½ ä¸ç¸åºç设å¤ã/sys/dev æä¾ä¸ä¸ªéè¿ä¸ä¸ª stat(2) æä½ç»æï¼æ¥æ¾ è®¾å¤ sysfs æ¥å£å¿«æ·çæ¹æ³ã
æ´å¤æå
³ driver-model çç¹æ§ä¿¡æ¯å¯ä»¥å¨ Documentation/driver-model/ ä¸æ¾å°ã
TODO: å®æè¿ä¸èã
å½åæ¥å£ ~~~~~~~~
以ä¸çæ¥å£å±æ®éåå¨äºå½åçsysfsä¸:
- è®¾å¤ (include/linux/device.h) ---------------------------------- ç»æä½:
struct device_attribute { struct attribute attr; ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf); ssize_t (*store)(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); };
声æ:
DEVICE_ATTR(_name, _mode, _show, _store);
å¢/å å±æ§:
int device_create_file(struct device *dev, const struct device_attribute * attr); void device_remove_file(struct device *dev, const struct device_attribute * attr);
- æ»çº¿é©±å¨ç¨åº (include/linux/device.h) -------------------------------------- ç»æä½:
struct bus_attribute { struct attribute attr; ssize_t (*show)(struct bus_type *, char * buf); ssize_t (*store)(struct bus_type *, const char * buf, size_t count); };
声æ:
BUS_ATTR(_name, _mode, _show, _store)
å¢/å å±æ§:
int bus_create_file(struct bus_type *, struct bus_attribute *); void bus_remove_file(struct bus_type *, struct bus_attribute *);
- 设å¤é©±å¨ç¨åº (include/linux/device.h) -----------------------------------------
ç»æä½:
struct driver_attribute { struct attribute attr; ssize_t (*show)(struct device_driver *, char * buf); ssize_t (*store)(struct device_driver *, const char * buf, size_t count); };
声æ:
DRIVER_ATTR(_name, _mode, _show, _store)
å¢/å å±æ§ï¼
int driver_create_file(struct device_driver *, const struct driver_attribute *); void driver_remove_file(struct device_driver *, const struct driver_attribute *);
ææ¡£
|