w1_netlink.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * w1_netlink.c
  3. *
  4. * Copyright (c) 2003 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/skbuff.h>
  22. #include <linux/netlink.h>
  23. #include <linux/connector.h>
  24. #include "w1.h"
  25. #include "w1_log.h"
  26. #include "w1_netlink.h"
  27. #if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE)))
  28. void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
  29. {
  30. char buf[sizeof(struct cn_msg) + sizeof(struct w1_netlink_msg)];
  31. struct cn_msg *m = (struct cn_msg *)buf;
  32. struct w1_netlink_msg *w = (struct w1_netlink_msg *)(m+1);
  33. memset(buf, 0, sizeof(buf));
  34. m->id.idx = CN_W1_IDX;
  35. m->id.val = CN_W1_VAL;
  36. m->seq = dev->seq++;
  37. m->len = sizeof(struct w1_netlink_msg);
  38. memcpy(w, msg, sizeof(struct w1_netlink_msg));
  39. cn_netlink_send(m, 0, GFP_KERNEL);
  40. }
  41. static int w1_process_command_master(struct w1_master *dev, struct cn_msg *msg,
  42. struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
  43. {
  44. dev_dbg(&dev->dev, "%s: %s: cmd=%02x, len=%u.\n",
  45. __func__, dev->name, cmd->cmd, cmd->len);
  46. if (cmd->cmd != W1_CMD_SEARCH && cmd->cmd != W1_CMD_ALARM_SEARCH)
  47. return -EINVAL;
  48. w1_search_process(dev, (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH);
  49. return 0;
  50. }
  51. static int w1_send_read_reply(struct w1_slave *sl, struct cn_msg *msg,
  52. struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
  53. {
  54. void *data;
  55. struct w1_netlink_msg *h;
  56. struct w1_netlink_cmd *c;
  57. struct cn_msg *cm;
  58. int err;
  59. data = kzalloc(sizeof(struct cn_msg) +
  60. sizeof(struct w1_netlink_msg) +
  61. sizeof(struct w1_netlink_cmd) +
  62. cmd->len, GFP_KERNEL);
  63. if (!data)
  64. return -ENOMEM;
  65. cm = (struct cn_msg *)(data);
  66. h = (struct w1_netlink_msg *)(cm + 1);
  67. c = (struct w1_netlink_cmd *)(h + 1);
  68. memcpy(cm, msg, sizeof(struct cn_msg));
  69. memcpy(h, hdr, sizeof(struct w1_netlink_msg));
  70. memcpy(c, cmd, sizeof(struct w1_netlink_cmd));
  71. cm->ack = msg->seq+1;
  72. cm->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd) + cmd->len;
  73. h->len = sizeof(struct w1_netlink_cmd) + cmd->len;
  74. memcpy(c->data, cmd->data, c->len);
  75. err = cn_netlink_send(cm, 0, GFP_KERNEL);
  76. kfree(data);
  77. return err;
  78. }
  79. static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg,
  80. struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
  81. {
  82. int err = 0;
  83. dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n",
  84. __func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id, sl->reg_num.crc,
  85. cmd->cmd, cmd->len);
  86. switch (cmd->cmd) {
  87. case W1_CMD_READ:
  88. w1_read_block(sl->master, cmd->data, cmd->len);
  89. w1_send_read_reply(sl, msg, hdr, cmd);
  90. break;
  91. case W1_CMD_WRITE:
  92. w1_write_block(sl->master, cmd->data, cmd->len);
  93. break;
  94. case W1_CMD_SEARCH:
  95. case W1_CMD_ALARM_SEARCH:
  96. w1_search_process(sl->master,
  97. (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH);
  98. break;
  99. default:
  100. err = -1;
  101. break;
  102. }
  103. return err;
  104. }
  105. static int w1_process_command_root(struct cn_msg *msg, struct w1_netlink_msg *mcmd)
  106. {
  107. struct w1_master *m;
  108. struct cn_msg *cn;
  109. struct w1_netlink_msg *w;
  110. u32 *id;
  111. if (mcmd->type != W1_LIST_MASTERS) {
  112. printk(KERN_NOTICE "%s: msg: %x.%x, wrong type: %u, len: %u.\n",
  113. __func__, msg->id.idx, msg->id.val, mcmd->type, mcmd->len);
  114. return -EPROTO;
  115. }
  116. cn = kmalloc(PAGE_SIZE, GFP_KERNEL);
  117. if (!cn)
  118. return -ENOMEM;
  119. cn->id.idx = CN_W1_IDX;
  120. cn->id.val = CN_W1_VAL;
  121. cn->seq = msg->seq;
  122. cn->ack = 1;
  123. cn->len = sizeof(struct w1_netlink_msg);
  124. w = (struct w1_netlink_msg *)(cn + 1);
  125. w->type = W1_LIST_MASTERS;
  126. w->reserved = 0;
  127. w->len = 0;
  128. id = (u32 *)(w + 1);
  129. mutex_lock(&w1_mlock);
  130. list_for_each_entry(m, &w1_masters, w1_master_entry) {
  131. if (cn->len + sizeof(*id) > PAGE_SIZE - sizeof(struct cn_msg)) {
  132. cn_netlink_send(cn, 0, GFP_KERNEL);
  133. cn->ack++;
  134. cn->len = sizeof(struct w1_netlink_msg);
  135. w->len = 0;
  136. id = (u32 *)(w + 1);
  137. }
  138. *id = m->id;
  139. w->len += sizeof(*id);
  140. cn->len += sizeof(*id);
  141. id++;
  142. }
  143. cn->ack = 0;
  144. cn_netlink_send(cn, 0, GFP_KERNEL);
  145. mutex_unlock(&w1_mlock);
  146. kfree(cn);
  147. return 0;
  148. }
  149. static void w1_cn_callback(void *data)
  150. {
  151. struct cn_msg *msg = data;
  152. struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1);
  153. struct w1_netlink_cmd *cmd;
  154. struct w1_slave *sl;
  155. struct w1_master *dev;
  156. int err = 0;
  157. while (msg->len && !err) {
  158. struct w1_reg_num id;
  159. u16 mlen = m->len;
  160. u8 *cmd_data = m->data;
  161. dev = NULL;
  162. sl = NULL;
  163. memcpy(&id, m->id.id, sizeof(id));
  164. #if 0
  165. printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n",
  166. __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len);
  167. #endif
  168. if (m->len + sizeof(struct w1_netlink_msg) > msg->len) {
  169. err = -E2BIG;
  170. break;
  171. }
  172. if (!mlen)
  173. goto out_cont;
  174. if (m->type == W1_MASTER_CMD) {
  175. dev = w1_search_master_id(m->id.mst.id);
  176. } else if (m->type == W1_SLAVE_CMD) {
  177. sl = w1_search_slave(&id);
  178. if (sl)
  179. dev = sl->master;
  180. } else {
  181. err = w1_process_command_root(msg, m);
  182. goto out_cont;
  183. }
  184. if (!dev) {
  185. err = -ENODEV;
  186. goto out_cont;
  187. }
  188. mutex_lock(&dev->mutex);
  189. if (sl && w1_reset_select_slave(sl)) {
  190. err = -ENODEV;
  191. goto out_up;
  192. }
  193. while (mlen) {
  194. cmd = (struct w1_netlink_cmd *)cmd_data;
  195. if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) {
  196. err = -E2BIG;
  197. break;
  198. }
  199. if (sl)
  200. w1_process_command_slave(sl, msg, m, cmd);
  201. else
  202. w1_process_command_master(dev, msg, m, cmd);
  203. cmd_data += cmd->len + sizeof(struct w1_netlink_cmd);
  204. mlen -= cmd->len + sizeof(struct w1_netlink_cmd);
  205. }
  206. out_up:
  207. atomic_dec(&dev->refcnt);
  208. if (sl)
  209. atomic_dec(&sl->refcnt);
  210. mutex_unlock(&dev->mutex);
  211. out_cont:
  212. msg->len -= sizeof(struct w1_netlink_msg) + m->len;
  213. m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len);
  214. /*
  215. * Let's allow requests for nonexisting devices.
  216. */
  217. if (err == -ENODEV)
  218. err = 0;
  219. }
  220. #if 0
  221. if (err) {
  222. printk("%s: malformed message. Dropping.\n", __func__);
  223. }
  224. #endif
  225. }
  226. int w1_init_netlink(void)
  227. {
  228. struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
  229. return cn_add_callback(&w1_id, "w1", &w1_cn_callback);
  230. }
  231. void w1_fini_netlink(void)
  232. {
  233. struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
  234. cn_del_callback(&w1_id);
  235. }
  236. #else
  237. void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
  238. {
  239. }
  240. int w1_init_netlink(void)
  241. {
  242. return 0;
  243. }
  244. void w1_fini_netlink(void)
  245. {
  246. }
  247. #endif