bat_sysfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "bat_sysfs.h"
  23. #include "translation-table.h"
  24. #include "originator.h"
  25. #include "hard-interface.h"
  26. #include "gateway_common.h"
  27. #include "gateway_client.h"
  28. #include "vis.h"
  29. static struct net_device *kobj_to_netdev(struct kobject *obj)
  30. {
  31. struct device *dev = container_of(obj->parent, struct device, kobj);
  32. return to_net_dev(dev);
  33. }
  34. static struct bat_priv *kobj_to_batpriv(struct kobject *obj)
  35. {
  36. struct net_device *net_dev = kobj_to_netdev(obj);
  37. return netdev_priv(net_dev);
  38. }
  39. #define UEV_TYPE_VAR "BATTYPE="
  40. #define UEV_ACTION_VAR "BATACTION="
  41. #define UEV_DATA_VAR "BATDATA="
  42. static char *uev_action_str[] = {
  43. "add",
  44. "del",
  45. "change"
  46. };
  47. static char *uev_type_str[] = {
  48. "gw"
  49. };
  50. /* Use this, if you have customized show and store functions */
  51. #define BAT_ATTR(_name, _mode, _show, _store) \
  52. struct bat_attribute bat_attr_##_name = { \
  53. .attr = {.name = __stringify(_name), \
  54. .mode = _mode }, \
  55. .show = _show, \
  56. .store = _store, \
  57. };
  58. #define BAT_ATTR_STORE_BOOL(_name, _post_func) \
  59. ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
  60. char *buff, size_t count) \
  61. { \
  62. struct net_device *net_dev = kobj_to_netdev(kobj); \
  63. struct bat_priv *bat_priv = netdev_priv(net_dev); \
  64. return __store_bool_attr(buff, count, _post_func, attr, \
  65. &bat_priv->_name, net_dev); \
  66. }
  67. #define BAT_ATTR_SHOW_BOOL(_name) \
  68. ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
  69. char *buff) \
  70. { \
  71. struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
  72. return sprintf(buff, "%s\n", \
  73. atomic_read(&bat_priv->_name) == 0 ? \
  74. "disabled" : "enabled"); \
  75. } \
  76. /* Use this, if you are going to turn a [name] in bat_priv on or off */
  77. #define BAT_ATTR_BOOL(_name, _mode, _post_func) \
  78. static BAT_ATTR_STORE_BOOL(_name, _post_func) \
  79. static BAT_ATTR_SHOW_BOOL(_name) \
  80. static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
  81. #define BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func) \
  82. ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
  83. char *buff, size_t count) \
  84. { \
  85. struct net_device *net_dev = kobj_to_netdev(kobj); \
  86. struct bat_priv *bat_priv = netdev_priv(net_dev); \
  87. return __store_uint_attr(buff, count, _min, _max, _post_func, \
  88. attr, &bat_priv->_name, net_dev); \
  89. }
  90. #define BAT_ATTR_SHOW_UINT(_name) \
  91. ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
  92. char *buff) \
  93. { \
  94. struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
  95. return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
  96. } \
  97. /* Use this, if you are going to set [name] in bat_priv to unsigned integer
  98. * values only */
  99. #define BAT_ATTR_UINT(_name, _mode, _min, _max, _post_func) \
  100. static BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func) \
  101. static BAT_ATTR_SHOW_UINT(_name) \
  102. static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
  103. static int store_bool_attr(char *buff, size_t count,
  104. struct net_device *net_dev,
  105. const char *attr_name, atomic_t *attr)
  106. {
  107. int enabled = -1;
  108. if (buff[count - 1] == '\n')
  109. buff[count - 1] = '\0';
  110. if ((strncmp(buff, "1", 2) == 0) ||
  111. (strncmp(buff, "enable", 7) == 0) ||
  112. (strncmp(buff, "enabled", 8) == 0))
  113. enabled = 1;
  114. if ((strncmp(buff, "0", 2) == 0) ||
  115. (strncmp(buff, "disable", 8) == 0) ||
  116. (strncmp(buff, "disabled", 9) == 0))
  117. enabled = 0;
  118. if (enabled < 0) {
  119. bat_info(net_dev,
  120. "%s: Invalid parameter received: %s\n",
  121. attr_name, buff);
  122. return -EINVAL;
  123. }
  124. if (atomic_read(attr) == enabled)
  125. return count;
  126. bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
  127. atomic_read(attr) == 1 ? "enabled" : "disabled",
  128. enabled == 1 ? "enabled" : "disabled");
  129. atomic_set(attr, (unsigned int)enabled);
  130. return count;
  131. }
  132. static inline ssize_t __store_bool_attr(char *buff, size_t count,
  133. void (*post_func)(struct net_device *),
  134. struct attribute *attr,
  135. atomic_t *attr_store, struct net_device *net_dev)
  136. {
  137. int ret;
  138. ret = store_bool_attr(buff, count, net_dev, attr->name, attr_store);
  139. if (post_func && ret)
  140. post_func(net_dev);
  141. return ret;
  142. }
  143. static int store_uint_attr(const char *buff, size_t count,
  144. struct net_device *net_dev, const char *attr_name,
  145. unsigned int min, unsigned int max, atomic_t *attr)
  146. {
  147. unsigned long uint_val;
  148. int ret;
  149. ret = kstrtoul(buff, 10, &uint_val);
  150. if (ret) {
  151. bat_info(net_dev,
  152. "%s: Invalid parameter received: %s\n",
  153. attr_name, buff);
  154. return -EINVAL;
  155. }
  156. if (uint_val < min) {
  157. bat_info(net_dev, "%s: Value is too small: %lu min: %u\n",
  158. attr_name, uint_val, min);
  159. return -EINVAL;
  160. }
  161. if (uint_val > max) {
  162. bat_info(net_dev, "%s: Value is too big: %lu max: %u\n",
  163. attr_name, uint_val, max);
  164. return -EINVAL;
  165. }
  166. if (atomic_read(attr) == uint_val)
  167. return count;
  168. bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
  169. attr_name, atomic_read(attr), uint_val);
  170. atomic_set(attr, uint_val);
  171. return count;
  172. }
  173. static inline ssize_t __store_uint_attr(const char *buff, size_t count,
  174. int min, int max,
  175. void (*post_func)(struct net_device *),
  176. const struct attribute *attr,
  177. atomic_t *attr_store, struct net_device *net_dev)
  178. {
  179. int ret;
  180. ret = store_uint_attr(buff, count, net_dev, attr->name,
  181. min, max, attr_store);
  182. if (post_func && ret)
  183. post_func(net_dev);
  184. return ret;
  185. }
  186. static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
  187. char *buff)
  188. {
  189. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  190. int vis_mode = atomic_read(&bat_priv->vis_mode);
  191. return sprintf(buff, "%s\n",
  192. vis_mode == VIS_TYPE_CLIENT_UPDATE ?
  193. "client" : "server");
  194. }
  195. static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
  196. char *buff, size_t count)
  197. {
  198. struct net_device *net_dev = kobj_to_netdev(kobj);
  199. struct bat_priv *bat_priv = netdev_priv(net_dev);
  200. unsigned long val;
  201. int ret, vis_mode_tmp = -1;
  202. ret = kstrtoul(buff, 10, &val);
  203. if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
  204. (strncmp(buff, "client", 6) == 0) ||
  205. (strncmp(buff, "off", 3) == 0))
  206. vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
  207. if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
  208. (strncmp(buff, "server", 6) == 0))
  209. vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
  210. if (vis_mode_tmp < 0) {
  211. if (buff[count - 1] == '\n')
  212. buff[count - 1] = '\0';
  213. bat_info(net_dev,
  214. "Invalid parameter for 'vis mode' setting received: %s\n",
  215. buff);
  216. return -EINVAL;
  217. }
  218. if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
  219. return count;
  220. bat_info(net_dev, "Changing vis mode from: %s to: %s\n",
  221. atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ?
  222. "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ?
  223. "client" : "server");
  224. atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
  225. return count;
  226. }
  227. static ssize_t show_bat_algo(struct kobject *kobj, struct attribute *attr,
  228. char *buff)
  229. {
  230. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  231. return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
  232. }
  233. static void post_gw_deselect(struct net_device *net_dev)
  234. {
  235. struct bat_priv *bat_priv = netdev_priv(net_dev);
  236. gw_deselect(bat_priv);
  237. }
  238. static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr,
  239. char *buff)
  240. {
  241. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  242. int bytes_written;
  243. switch (atomic_read(&bat_priv->gw_mode)) {
  244. case GW_MODE_CLIENT:
  245. bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME);
  246. break;
  247. case GW_MODE_SERVER:
  248. bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
  249. break;
  250. default:
  251. bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
  252. break;
  253. }
  254. return bytes_written;
  255. }
  256. static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr,
  257. char *buff, size_t count)
  258. {
  259. struct net_device *net_dev = kobj_to_netdev(kobj);
  260. struct bat_priv *bat_priv = netdev_priv(net_dev);
  261. char *curr_gw_mode_str;
  262. int gw_mode_tmp = -1;
  263. if (buff[count - 1] == '\n')
  264. buff[count - 1] = '\0';
  265. if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
  266. gw_mode_tmp = GW_MODE_OFF;
  267. if (strncmp(buff, GW_MODE_CLIENT_NAME,
  268. strlen(GW_MODE_CLIENT_NAME)) == 0)
  269. gw_mode_tmp = GW_MODE_CLIENT;
  270. if (strncmp(buff, GW_MODE_SERVER_NAME,
  271. strlen(GW_MODE_SERVER_NAME)) == 0)
  272. gw_mode_tmp = GW_MODE_SERVER;
  273. if (gw_mode_tmp < 0) {
  274. bat_info(net_dev,
  275. "Invalid parameter for 'gw mode' setting received: %s\n",
  276. buff);
  277. return -EINVAL;
  278. }
  279. if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
  280. return count;
  281. switch (atomic_read(&bat_priv->gw_mode)) {
  282. case GW_MODE_CLIENT:
  283. curr_gw_mode_str = GW_MODE_CLIENT_NAME;
  284. break;
  285. case GW_MODE_SERVER:
  286. curr_gw_mode_str = GW_MODE_SERVER_NAME;
  287. break;
  288. default:
  289. curr_gw_mode_str = GW_MODE_OFF_NAME;
  290. break;
  291. }
  292. bat_info(net_dev, "Changing gw mode from: %s to: %s\n",
  293. curr_gw_mode_str, buff);
  294. gw_deselect(bat_priv);
  295. atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
  296. return count;
  297. }
  298. static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr,
  299. char *buff)
  300. {
  301. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  302. int down, up;
  303. int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
  304. gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
  305. return sprintf(buff, "%i%s/%i%s\n",
  306. (down > 2048 ? down / 1024 : down),
  307. (down > 2048 ? "MBit" : "KBit"),
  308. (up > 2048 ? up / 1024 : up),
  309. (up > 2048 ? "MBit" : "KBit"));
  310. }
  311. static ssize_t store_gw_bwidth(struct kobject *kobj, struct attribute *attr,
  312. char *buff, size_t count)
  313. {
  314. struct net_device *net_dev = kobj_to_netdev(kobj);
  315. if (buff[count - 1] == '\n')
  316. buff[count - 1] = '\0';
  317. return gw_bandwidth_set(net_dev, buff, count);
  318. }
  319. BAT_ATTR_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
  320. BAT_ATTR_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
  321. BAT_ATTR_BOOL(fragmentation, S_IRUGO | S_IWUSR, update_min_mtu);
  322. BAT_ATTR_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
  323. static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode);
  324. static BAT_ATTR(routing_algo, S_IRUGO, show_bat_algo, NULL);
  325. static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, show_gw_mode, store_gw_mode);
  326. BAT_ATTR_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
  327. BAT_ATTR_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
  328. BAT_ATTR_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
  329. post_gw_deselect);
  330. static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, show_gw_bwidth,
  331. store_gw_bwidth);
  332. #ifdef CONFIG_BATMAN_ADV_DEBUG
  333. BAT_ATTR_UINT(log_level, S_IRUGO | S_IWUSR, 0, 7, NULL);
  334. #endif
  335. static struct bat_attribute *mesh_attrs[] = {
  336. &bat_attr_aggregated_ogms,
  337. &bat_attr_bonding,
  338. &bat_attr_fragmentation,
  339. &bat_attr_ap_isolation,
  340. &bat_attr_vis_mode,
  341. &bat_attr_routing_algo,
  342. &bat_attr_gw_mode,
  343. &bat_attr_orig_interval,
  344. &bat_attr_hop_penalty,
  345. &bat_attr_gw_sel_class,
  346. &bat_attr_gw_bandwidth,
  347. #ifdef CONFIG_BATMAN_ADV_DEBUG
  348. &bat_attr_log_level,
  349. #endif
  350. NULL,
  351. };
  352. int sysfs_add_meshif(struct net_device *dev)
  353. {
  354. struct kobject *batif_kobject = &dev->dev.kobj;
  355. struct bat_priv *bat_priv = netdev_priv(dev);
  356. struct bat_attribute **bat_attr;
  357. int err;
  358. bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
  359. batif_kobject);
  360. if (!bat_priv->mesh_obj) {
  361. bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  362. SYSFS_IF_MESH_SUBDIR);
  363. goto out;
  364. }
  365. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) {
  366. err = sysfs_create_file(bat_priv->mesh_obj,
  367. &((*bat_attr)->attr));
  368. if (err) {
  369. bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  370. dev->name, SYSFS_IF_MESH_SUBDIR,
  371. ((*bat_attr)->attr).name);
  372. goto rem_attr;
  373. }
  374. }
  375. return 0;
  376. rem_attr:
  377. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
  378. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  379. kobject_put(bat_priv->mesh_obj);
  380. bat_priv->mesh_obj = NULL;
  381. out:
  382. return -ENOMEM;
  383. }
  384. void sysfs_del_meshif(struct net_device *dev)
  385. {
  386. struct bat_priv *bat_priv = netdev_priv(dev);
  387. struct bat_attribute **bat_attr;
  388. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
  389. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  390. kobject_put(bat_priv->mesh_obj);
  391. bat_priv->mesh_obj = NULL;
  392. }
  393. static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
  394. char *buff)
  395. {
  396. struct net_device *net_dev = kobj_to_netdev(kobj);
  397. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  398. ssize_t length;
  399. if (!hard_iface)
  400. return 0;
  401. length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
  402. "none" : hard_iface->soft_iface->name);
  403. hardif_free_ref(hard_iface);
  404. return length;
  405. }
  406. static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
  407. char *buff, size_t count)
  408. {
  409. struct net_device *net_dev = kobj_to_netdev(kobj);
  410. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  411. int status_tmp = -1;
  412. int ret = count;
  413. if (!hard_iface)
  414. return count;
  415. if (buff[count - 1] == '\n')
  416. buff[count - 1] = '\0';
  417. if (strlen(buff) >= IFNAMSIZ) {
  418. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  419. buff);
  420. hardif_free_ref(hard_iface);
  421. return -EINVAL;
  422. }
  423. if (strncmp(buff, "none", 4) == 0)
  424. status_tmp = IF_NOT_IN_USE;
  425. else
  426. status_tmp = IF_I_WANT_YOU;
  427. if (hard_iface->if_status == status_tmp)
  428. goto out;
  429. if ((hard_iface->soft_iface) &&
  430. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  431. goto out;
  432. if (!rtnl_trylock()) {
  433. ret = -ERESTARTSYS;
  434. goto out;
  435. }
  436. if (status_tmp == IF_NOT_IN_USE) {
  437. hardif_disable_interface(hard_iface);
  438. goto unlock;
  439. }
  440. /* if the interface already is in use */
  441. if (hard_iface->if_status != IF_NOT_IN_USE)
  442. hardif_disable_interface(hard_iface);
  443. ret = hardif_enable_interface(hard_iface, buff);
  444. unlock:
  445. rtnl_unlock();
  446. out:
  447. hardif_free_ref(hard_iface);
  448. return ret;
  449. }
  450. static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
  451. char *buff)
  452. {
  453. struct net_device *net_dev = kobj_to_netdev(kobj);
  454. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  455. ssize_t length;
  456. if (!hard_iface)
  457. return 0;
  458. switch (hard_iface->if_status) {
  459. case IF_TO_BE_REMOVED:
  460. length = sprintf(buff, "disabling\n");
  461. break;
  462. case IF_INACTIVE:
  463. length = sprintf(buff, "inactive\n");
  464. break;
  465. case IF_ACTIVE:
  466. length = sprintf(buff, "active\n");
  467. break;
  468. case IF_TO_BE_ACTIVATED:
  469. length = sprintf(buff, "enabling\n");
  470. break;
  471. case IF_NOT_IN_USE:
  472. default:
  473. length = sprintf(buff, "not in use\n");
  474. break;
  475. }
  476. hardif_free_ref(hard_iface);
  477. return length;
  478. }
  479. static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
  480. show_mesh_iface, store_mesh_iface);
  481. static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL);
  482. static struct bat_attribute *batman_attrs[] = {
  483. &bat_attr_mesh_iface,
  484. &bat_attr_iface_status,
  485. NULL,
  486. };
  487. int sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  488. {
  489. struct kobject *hardif_kobject = &dev->dev.kobj;
  490. struct bat_attribute **bat_attr;
  491. int err;
  492. *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
  493. hardif_kobject);
  494. if (!*hardif_obj) {
  495. bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  496. SYSFS_IF_BAT_SUBDIR);
  497. goto out;
  498. }
  499. for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
  500. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  501. if (err) {
  502. bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  503. dev->name, SYSFS_IF_BAT_SUBDIR,
  504. ((*bat_attr)->attr).name);
  505. goto rem_attr;
  506. }
  507. }
  508. return 0;
  509. rem_attr:
  510. for (bat_attr = batman_attrs; *bat_attr; ++bat_attr)
  511. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  512. out:
  513. return -ENOMEM;
  514. }
  515. void sysfs_del_hardif(struct kobject **hardif_obj)
  516. {
  517. kobject_put(*hardif_obj);
  518. *hardif_obj = NULL;
  519. }
  520. int throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
  521. enum uev_action action, const char *data)
  522. {
  523. int ret = -1;
  524. struct hard_iface *primary_if = NULL;
  525. struct kobject *bat_kobj;
  526. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  527. primary_if = primary_if_get_selected(bat_priv);
  528. if (!primary_if)
  529. goto out;
  530. bat_kobj = &primary_if->soft_iface->dev.kobj;
  531. uevent_env[0] = kmalloc(strlen(UEV_TYPE_VAR) +
  532. strlen(uev_type_str[type]) + 1,
  533. GFP_ATOMIC);
  534. if (!uevent_env[0])
  535. goto out;
  536. sprintf(uevent_env[0], "%s%s", UEV_TYPE_VAR, uev_type_str[type]);
  537. uevent_env[1] = kmalloc(strlen(UEV_ACTION_VAR) +
  538. strlen(uev_action_str[action]) + 1,
  539. GFP_ATOMIC);
  540. if (!uevent_env[1])
  541. goto out;
  542. sprintf(uevent_env[1], "%s%s", UEV_ACTION_VAR, uev_action_str[action]);
  543. /* If the event is DEL, ignore the data field */
  544. if (action != UEV_DEL) {
  545. uevent_env[2] = kmalloc(strlen(UEV_DATA_VAR) +
  546. strlen(data) + 1, GFP_ATOMIC);
  547. if (!uevent_env[2])
  548. goto out;
  549. sprintf(uevent_env[2], "%s%s", UEV_DATA_VAR, data);
  550. }
  551. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  552. out:
  553. kfree(uevent_env[0]);
  554. kfree(uevent_env[1]);
  555. kfree(uevent_env[2]);
  556. if (primary_if)
  557. hardif_free_ref(primary_if);
  558. if (ret)
  559. bat_dbg(DBG_BATMAN, bat_priv,
  560. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  561. uev_type_str[type], uev_action_str[action],
  562. (action == UEV_DEL ? "NULL" : data), ret);
  563. return ret;
  564. }