plcmdio16.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * plcmdio16.c - Qualcomm Atheros 16-bit MDIO Register Editor
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. *
  49. *--------------------------------------------------------------------*/
  50. /*====================================================================*"
  51. * system header files;
  52. *--------------------------------------------------------------------*/
  53. #include <unistd.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <ctype.h>
  57. /*====================================================================*
  58. * custom header files;
  59. *--------------------------------------------------------------------*/
  60. #include "../tools/getoptv.h"
  61. #include "../tools/putoptv.h"
  62. #include "../tools/memory.h"
  63. #include "../tools/number.h"
  64. #include "../tools/symbol.h"
  65. #include "../tools/types.h"
  66. #include "../tools/flags.h"
  67. #include "../tools/files.h"
  68. #include "../tools/error.h"
  69. #include "../plc/plc.h"
  70. #include "../ram/nvram.h"
  71. #include "../ram/sdram.h"
  72. #include "../nvm/nvm.h"
  73. #include "../pib/pib.h"
  74. #include "../mme/mme.h"
  75. /*====================================================================*
  76. * custom source files;
  77. *--------------------------------------------------------------------*/
  78. #ifndef MAKEFILE
  79. #include "../plc/Devices.c"
  80. #include "../plc/Display.c"
  81. #include "../mme/UnwantedMessage.c"
  82. #endif
  83. #ifndef MAKEFILE
  84. #include "../tools/getoptv.c"
  85. #include "../tools/putoptv.c"
  86. #include "../tools/version.c"
  87. #include "../tools/uintspec.c"
  88. #include "../tools/hexencode.c"
  89. #include "../tools/hexdecode.c"
  90. #include "../tools/todigit.c"
  91. #include "../tools/hexdump.c"
  92. #include "../tools/hexview.c"
  93. #include "../tools/regview32.c"
  94. #include "../tools/synonym.c"
  95. #include "../tools/error.c"
  96. #endif
  97. #ifndef MAKEFILE
  98. #include "../ether/channel.c"
  99. #include "../ether/openchannel.c"
  100. #include "../ether/closechannel.c"
  101. #include "../ether/readpacket.c"
  102. #include "../ether/sendpacket.c"
  103. #endif
  104. #ifndef MAKEFILE
  105. #include "../mme/EthernetHeader.c"
  106. #include "../mme/QualcommHeader.c"
  107. #include "../mme/MMECode.c"
  108. #endif
  109. /*====================================================================*
  110. * program constants;
  111. *--------------------------------------------------------------------*/
  112. #define INT6KMDIO_RELATIVE (1 << 0)
  113. #define INT6KMDIO_ABSOLUTE (1 << 1)
  114. #define INT6KMDIO_REGISTER (1 << 2)
  115. #define INT6KMDIO_HEADINGS (1 << 3)
  116. #define INT6KMDIO_WRITE (1 << 4)
  117. /*====================================================================*
  118. *
  119. * void function (struct channel * channel, uint32_t offset, uint32_t length, flag_t flags);
  120. *
  121. *
  122. *
  123. *--------------------------------------------------------------------*/
  124. static void function (struct channel * channel, uint8_t mode, uint8_t phy, uint8_t reg, uint16_t data)
  125. {
  126. struct message message;
  127. signed packetsize;
  128. #ifndef __GNUC__
  129. #pragma pack (push,1)
  130. #endif
  131. struct __packed vs_mdio_command_request
  132. {
  133. struct ethernet_hdr ethernet;
  134. struct qualcomm_hdr qualcomm;
  135. uint8_t OPERATION;
  136. uint8_t PHY;
  137. uint8_t REG;
  138. uint16_t DATA;
  139. }
  140. * request = (struct vs_mdio_command_request *)(&message);
  141. struct __packed vs_mdio_command_confirm
  142. {
  143. struct ethernet_hdr ethernet;
  144. struct qualcomm_hdr qualcomm;
  145. uint8_t MSTATUS;
  146. uint16_t DATA;
  147. uint8_t PHY;
  148. uint8_t REG;
  149. }
  150. * confirm = (struct vs_mdio_command_confirm *)(&message);
  151. #ifndef __GNUC__
  152. #pragma pack (pop)
  153. #endif
  154. memset (&message, 0, sizeof (message));
  155. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  156. QualcommHeader (&request->qualcomm, 0, (VS_MDIO_COMMAND | MMTYPE_REQ));
  157. request->OPERATION = mode;
  158. request->PHY = phy;
  159. request->REG = reg;
  160. request->DATA = HTOLE16 (data);
  161. if (sendpacket (channel, &message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) == -1)
  162. {
  163. error (1, errno, CHANNEL_CANTSEND);
  164. }
  165. while ((packetsize = readpacket (channel, &message, sizeof (message))) > 0)
  166. {
  167. if (UnwantedMessage (&message, packetsize, 0, (VS_MDIO_COMMAND | MMTYPE_CNF)))
  168. {
  169. continue;
  170. }
  171. if (confirm->MSTATUS)
  172. {
  173. error (0, 0, "%s (%0X): %s", MMECode (confirm->qualcomm.MMTYPE, confirm->MSTATUS), confirm->MSTATUS, PLC_WONTDOIT);
  174. continue;
  175. }
  176. printf (" phy 0x%02X", confirm->PHY);
  177. printf (" reg 0x%02X", confirm->REG);
  178. printf (" data 0x%04X", confirm->DATA);
  179. printf ("\n");
  180. }
  181. return;
  182. }
  183. /*====================================================================*
  184. *
  185. * int main (int argc, char const * argv []);
  186. *
  187. *
  188. *
  189. *--------------------------------------------------------------------*/
  190. int main (int argc, char const * argv [])
  191. {
  192. extern struct channel channel;
  193. static char const * optv [] =
  194. {
  195. "d:ei:p:qr:v",
  196. "[device] [...]",
  197. "Qualcomm Atheros 16-bit MDIO Register Editor",
  198. "d n\tdata value is (n) [0x0000]",
  199. "e\tredirect stderr to stdout",
  200. #if defined (WINPCAP) || defined (LIBPCAP)
  201. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  202. #else
  203. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  204. #endif
  205. "p n\tPHY number (n) [0xFF]",
  206. "q\tquiet mode",
  207. "r n\tregister number (n) [0xFF]",
  208. "v\tverbose mode",
  209. (char const *) (0)
  210. };
  211. uint8_t mode = 0;
  212. uint8_t phy = 0;
  213. uint8_t reg = 0;
  214. uint16_t data = 0;
  215. signed c;
  216. if (getenv (PLCDEVICE))
  217. {
  218. #if defined (WINPCAP) || defined (LIBPCAP)
  219. channel.ifindex = atoi (getenv (PLCDEVICE));
  220. #else
  221. channel.ifname = strdup (getenv (PLCDEVICE));
  222. #endif
  223. }
  224. optind = 1;
  225. while ((c = getoptv (argc, argv, optv)) != -1)
  226. {
  227. switch (c)
  228. {
  229. case 'd':
  230. mode = 1;
  231. data = (uint16_t)(uintspec (optarg, 0, 0xFFFF));
  232. break;
  233. case 'e':
  234. dup2 (STDOUT_FILENO, STDERR_FILENO);
  235. break;
  236. case 'i':
  237. #if defined (WINPCAP) || defined (LIBPCAP)
  238. channel.ifindex = atoi (optarg);
  239. #else
  240. channel.ifname = optarg;
  241. #endif
  242. break;
  243. case 'p':
  244. phy = (uint8_t)(uintspec (optarg, 0, 0x1F));
  245. break;
  246. case 'q':
  247. _setbits (channel.flags, CHANNEL_SILENCE);
  248. break;
  249. case 'r':
  250. reg = (uint8_t)(uintspec (optarg, 0, 0x1F));
  251. break;
  252. case 'v':
  253. _setbits (channel.flags, CHANNEL_VERBOSE);
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. argc -= optind;
  260. argv += optind;
  261. openchannel (&channel);
  262. if (!argc)
  263. {
  264. function (&channel, mode, phy, reg, data);
  265. }
  266. while ((argc) && (* argv))
  267. {
  268. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  269. {
  270. error (1, errno, PLC_BAD_MAC, * argv);
  271. }
  272. function (&channel, mode, phy, reg, data);
  273. argv++;
  274. argc--;
  275. }
  276. closechannel (&channel);
  277. return (0);
  278. }