plcmdio16.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * plcmdio.c - Qualcomm Atheros 16-bit MDIO Register Editor
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. /*====================================================================*
  17. * system header files;
  18. *--------------------------------------------------------------------*/
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. /*====================================================================*
  24. * custom header files;
  25. *--------------------------------------------------------------------*/
  26. #include "../tools/getoptv.h"
  27. #include "../tools/putoptv.h"
  28. #include "../tools/memory.h"
  29. #include "../tools/number.h"
  30. #include "../tools/symbol.h"
  31. #include "../tools/types.h"
  32. #include "../tools/flags.h"
  33. #include "../tools/files.h"
  34. #include "../tools/error.h"
  35. #include "../plc/plc.h"
  36. #include "../ram/nvram.h"
  37. #include "../ram/sdram.h"
  38. #include "../nvm/nvm.h"
  39. #include "../pib/pib.h"
  40. #include "../mme/mme.h"
  41. /*====================================================================*
  42. * custom source files;
  43. *--------------------------------------------------------------------*/
  44. #ifndef MAKEFILE
  45. #include "../plc/Devices.c"
  46. #include "../plc/Display.c"
  47. #include "../mme/UnwantedMessage.c"
  48. #endif
  49. #ifndef MAKEFILE
  50. #include "../tools/getoptv.c"
  51. #include "../tools/putoptv.c"
  52. #include "../tools/version.c"
  53. #include "../tools/uintspec.c"
  54. #include "../tools/hexencode.c"
  55. #include "../tools/hexdecode.c"
  56. #include "../tools/todigit.c"
  57. #include "../tools/hexdump.c"
  58. #include "../tools/hexview.c"
  59. #include "../tools/regview32.c"
  60. #include "../tools/synonym.c"
  61. #include "../tools/error.c"
  62. #endif
  63. #ifndef MAKEFILE
  64. #include "../ether/channel.c"
  65. #include "../ether/openchannel.c"
  66. #include "../ether/closechannel.c"
  67. #include "../ether/readpacket.c"
  68. #include "../ether/sendpacket.c"
  69. #endif
  70. #ifndef MAKEFILE
  71. #include "../mme/EthernetHeader.c"
  72. #include "../mme/QualcommHeader.c"
  73. #include "../mme/MMECode.c"
  74. #endif
  75. /*====================================================================*
  76. * program constants;
  77. *--------------------------------------------------------------------*/
  78. #define INT6KMDIO_RELATIVE (1 << 0)
  79. #define INT6KMDIO_ABSOLUTE (1 << 1)
  80. #define INT6KMDIO_REGISTER (1 << 2)
  81. #define INT6KMDIO_HEADINGS (1 << 3)
  82. #define INT6KMDIO_WRITE (1 << 4)
  83. /*====================================================================*
  84. *
  85. * void function (struct channel * channel, uint32_t offset, uint32_t length, flag_t flags);
  86. *
  87. *--------------------------------------------------------------------*/
  88. static void function (struct channel * channel, uint8_t mode, uint8_t phy, uint8_t reg, uint16_t data)
  89. {
  90. struct message message;
  91. signed packetsize;
  92. #ifndef __GNUC__
  93. #pragma pack (push,1)
  94. #endif
  95. struct __packed vs_mdio_command_request
  96. {
  97. struct ethernet_hdr ethernet;
  98. struct qualcomm_hdr qualcomm;
  99. uint8_t OPERATION;
  100. uint8_t PHY;
  101. uint8_t REG;
  102. uint16_t DATA;
  103. }
  104. * request = (struct vs_mdio_command_request *) (& message);
  105. struct __packed vs_mdio_command_confirm
  106. {
  107. struct ethernet_hdr ethernet;
  108. struct qualcomm_hdr qualcomm;
  109. uint8_t MSTATUS;
  110. uint16_t DATA;
  111. uint8_t PHY;
  112. uint8_t REG;
  113. }
  114. * confirm = (struct vs_mdio_command_confirm *) (& message);
  115. #ifndef __GNUC__
  116. #pragma pack (pop)
  117. #endif
  118. memset (& message, 0, sizeof (message));
  119. EthernetHeader (& request->ethernet, channel->peer, channel->host, channel->type);
  120. QualcommHeader (& request->qualcomm, 0, (VS_MDIO_COMMAND | MMTYPE_REQ));
  121. request->OPERATION = mode;
  122. request->PHY = phy;
  123. request->REG = reg;
  124. request->DATA = HTOLE16 (data);
  125. if (sendpacket (channel, & message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) == -1)
  126. {
  127. error (1, errno, CHANNEL_CANTSEND);
  128. }
  129. while ((packetsize = readpacket (channel, & message, sizeof (message))) > 0)
  130. {
  131. if (UnwantedMessage (& message, packetsize, 0, (VS_MDIO_COMMAND | MMTYPE_CNF)))
  132. {
  133. continue;
  134. }
  135. if (confirm->MSTATUS)
  136. {
  137. error (0, 0, "%s (%0X): %s", MMECode (confirm->qualcomm.MMTYPE, confirm->MSTATUS), confirm->MSTATUS, PLC_WONTDOIT);
  138. continue;
  139. }
  140. printf (" phy 0x%02X", confirm->PHY);
  141. printf (" reg 0x%02X", confirm->REG);
  142. printf (" data 0x%04X", confirm->DATA);
  143. printf ("\n");
  144. }
  145. return;
  146. }
  147. /*====================================================================*
  148. *
  149. * int main (int argc, char const * argv []);
  150. *
  151. *--------------------------------------------------------------------*/
  152. int main (int argc, char const * argv [])
  153. {
  154. extern struct channel channel;
  155. static char const * optv [] =
  156. {
  157. "d:ei:p:qr:v",
  158. "[device] [...]",
  159. "Qualcomm Atheros 16-bit MDIO Register Editor",
  160. "d n\tdata value is (n) [0x0000]",
  161. "e\tredirect stderr to stdout",
  162. #if defined (WINPCAP) || defined (LIBPCAP)
  163. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  164. #else
  165. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  166. #endif
  167. "p n\tPHY number (n) [0xFF]",
  168. "q\tquiet mode",
  169. "r n\tregister number (n) [0xFF]",
  170. "v\tverbose mode",
  171. (char const *) (0)
  172. };
  173. uint8_t mode = 0;
  174. uint8_t phy = 0;
  175. uint8_t reg = 0;
  176. uint16_t data = 0;
  177. signed c;
  178. if (getenv (PLCDEVICE))
  179. {
  180. #if defined (WINPCAP) || defined (LIBPCAP)
  181. channel.ifindex = atoi (getenv (PLCDEVICE));
  182. #else
  183. channel.ifname = strdup (getenv (PLCDEVICE));
  184. #endif
  185. }
  186. optind = 1;
  187. while (~ (c = getoptv (argc, argv, optv)))
  188. {
  189. switch (c)
  190. {
  191. case 'd':
  192. mode = 1;
  193. data = (uint16_t) (uintspec (optarg, 0, 0xFFFF));
  194. break;
  195. case 'e':
  196. dup2 (STDOUT_FILENO, STDERR_FILENO);
  197. break;
  198. case 'i':
  199. #if defined (WINPCAP) || defined (LIBPCAP)
  200. channel.ifindex = atoi (optarg);
  201. #else
  202. channel.ifname = optarg;
  203. #endif
  204. break;
  205. case 'p':
  206. phy = (uint8_t) (uintspec (optarg, 0, 0x1F));
  207. break;
  208. case 'q':
  209. _setbits (channel.flags, CHANNEL_SILENCE);
  210. break;
  211. case 'r':
  212. reg = (uint8_t) (uintspec (optarg, 0, 0x1F));
  213. break;
  214. case 'v':
  215. _setbits (channel.flags, CHANNEL_VERBOSE);
  216. break;
  217. default:
  218. break;
  219. }
  220. }
  221. argc -= optind;
  222. argv += optind;
  223. openchannel (& channel);
  224. if (! argc)
  225. {
  226. function (& channel, mode, phy, reg, data);
  227. }
  228. while ((argc) && (* argv))
  229. {
  230. if (! hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  231. {
  232. error (1, errno, PLC_BAD_MAC, * argv);
  233. }
  234. function (& channel, mode, phy, reg, data);
  235. argv++;
  236. argc--;
  237. }
  238. closechannel (& channel);
  239. return (0);
  240. }