w1_netlink.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 void w1_cn_callback(void *data)
  106. {
  107. struct cn_msg *msg = data;
  108. struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1);
  109. struct w1_netlink_cmd *cmd;
  110. struct w1_slave *sl;
  111. struct w1_master *dev;
  112. int err = 0;
  113. while (msg->len && !err) {
  114. struct w1_reg_num id;
  115. u16 mlen = m->len;
  116. u8 *cmd_data = m->data;
  117. dev = NULL;
  118. sl = NULL;
  119. memcpy(&id, m->id.id, sizeof(id));
  120. #if 0
  121. printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n",
  122. __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len);
  123. #endif
  124. if (m->len + sizeof(struct w1_netlink_msg) > msg->len) {
  125. err = -E2BIG;
  126. break;
  127. }
  128. if (!mlen)
  129. goto out_cont;
  130. if (m->type == W1_MASTER_CMD) {
  131. dev = w1_search_master_id(m->id.mst.id);
  132. } else if (m->type == W1_SLAVE_CMD) {
  133. sl = w1_search_slave(&id);
  134. if (sl)
  135. dev = sl->master;
  136. }
  137. if (!dev) {
  138. err = -ENODEV;
  139. goto out_cont;
  140. }
  141. mutex_lock(&dev->mutex);
  142. if (sl && w1_reset_select_slave(sl)) {
  143. err = -ENODEV;
  144. goto out_up;
  145. }
  146. while (mlen) {
  147. cmd = (struct w1_netlink_cmd *)cmd_data;
  148. if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) {
  149. err = -E2BIG;
  150. break;
  151. }
  152. if (sl)
  153. w1_process_command_slave(sl, msg, m, cmd);
  154. else
  155. w1_process_command_master(dev, msg, m, cmd);
  156. cmd_data += cmd->len + sizeof(struct w1_netlink_cmd);
  157. mlen -= cmd->len + sizeof(struct w1_netlink_cmd);
  158. }
  159. out_up:
  160. atomic_dec(&dev->refcnt);
  161. if (sl)
  162. atomic_dec(&sl->refcnt);
  163. mutex_unlock(&dev->mutex);
  164. out_cont:
  165. msg->len -= sizeof(struct w1_netlink_msg) + m->len;
  166. m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len);
  167. /*
  168. * Let's allow requests for nonexisting devices.
  169. */
  170. if (err == -ENODEV)
  171. err = 0;
  172. }
  173. #if 0
  174. if (err) {
  175. printk("%s: malformed message. Dropping.\n", __func__);
  176. }
  177. #endif
  178. }
  179. int w1_init_netlink(void)
  180. {
  181. struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
  182. return cn_add_callback(&w1_id, "w1", &w1_cn_callback);
  183. }
  184. void w1_fini_netlink(void)
  185. {
  186. struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
  187. cn_del_callback(&w1_id);
  188. }
  189. #else
  190. void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
  191. {
  192. }
  193. int w1_init_netlink(void)
  194. {
  195. return 0;
  196. }
  197. void w1_fini_netlink(void)
  198. {
  199. }
  200. #endif