asix_common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ASIX AX8817X based USB 2.0 Ethernet Devices
  4. * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
  5. * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
  6. * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
  7. * Copyright (c) 2002-2003 TiVo Inc.
  8. */
  9. #include "asix.h"
  10. int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  11. u16 size, void *data, int in_pm)
  12. {
  13. int ret;
  14. int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
  15. BUG_ON(!dev);
  16. if (!in_pm)
  17. fn = usbnet_read_cmd;
  18. else
  19. fn = usbnet_read_cmd_nopm;
  20. ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  21. value, index, data, size);
  22. if (unlikely(ret < 0))
  23. netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n",
  24. index, ret);
  25. return ret;
  26. }
  27. int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  28. u16 size, void *data, int in_pm)
  29. {
  30. int ret;
  31. int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
  32. BUG_ON(!dev);
  33. if (!in_pm)
  34. fn = usbnet_write_cmd;
  35. else
  36. fn = usbnet_write_cmd_nopm;
  37. ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  38. value, index, data, size);
  39. if (unlikely(ret < 0))
  40. netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n",
  41. index, ret);
  42. return ret;
  43. }
  44. void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  45. u16 size, void *data)
  46. {
  47. usbnet_write_cmd_async(dev, cmd,
  48. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  49. value, index, data, size);
  50. }
  51. static void reset_asix_rx_fixup_info(struct asix_rx_fixup_info *rx)
  52. {
  53. /* Reset the variables that have a lifetime outside of
  54. * asix_rx_fixup_internal() so that future processing starts from a
  55. * known set of initial conditions.
  56. */
  57. if (rx->ax_skb) {
  58. /* Discard any incomplete Ethernet frame in the netdev buffer */
  59. kfree_skb(rx->ax_skb);
  60. rx->ax_skb = NULL;
  61. }
  62. /* Assume the Data header 32-bit word is at the start of the current
  63. * or next URB socket buffer so reset all the state variables.
  64. */
  65. rx->remaining = 0;
  66. rx->split_head = false;
  67. rx->header = 0;
  68. }
  69. int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
  70. struct asix_rx_fixup_info *rx)
  71. {
  72. int offset = 0;
  73. u16 size;
  74. /* When an Ethernet frame spans multiple URB socket buffers,
  75. * do a sanity test for the Data header synchronisation.
  76. * Attempt to detect the situation of the previous socket buffer having
  77. * been truncated or a socket buffer was missing. These situations
  78. * cause a discontinuity in the data stream and therefore need to avoid
  79. * appending bad data to the end of the current netdev socket buffer.
  80. * Also avoid unnecessarily discarding a good current netdev socket
  81. * buffer.
  82. */
  83. if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) {
  84. offset = ((rx->remaining + 1) & 0xfffe);
  85. rx->header = get_unaligned_le32(skb->data + offset);
  86. offset = 0;
  87. size = (u16)(rx->header & 0x7ff);
  88. if (size != ((~rx->header >> 16) & 0x7ff)) {
  89. netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n",
  90. rx->remaining);
  91. reset_asix_rx_fixup_info(rx);
  92. }
  93. }
  94. while (offset + sizeof(u16) <= skb->len) {
  95. u16 copy_length;
  96. if (!rx->remaining) {
  97. if (skb->len - offset == sizeof(u16)) {
  98. rx->header = get_unaligned_le16(
  99. skb->data + offset);
  100. rx->split_head = true;
  101. offset += sizeof(u16);
  102. break;
  103. }
  104. if (rx->split_head == true) {
  105. rx->header |= (get_unaligned_le16(
  106. skb->data + offset) << 16);
  107. rx->split_head = false;
  108. offset += sizeof(u16);
  109. } else {
  110. rx->header = get_unaligned_le32(skb->data +
  111. offset);
  112. offset += sizeof(u32);
  113. }
  114. /* take frame length from Data header 32-bit word */
  115. size = (u16)(rx->header & 0x7ff);
  116. if (size != ((~rx->header >> 16) & 0x7ff)) {
  117. netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
  118. rx->header, offset);
  119. reset_asix_rx_fixup_info(rx);
  120. return 0;
  121. }
  122. if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
  123. netdev_dbg(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
  124. size);
  125. reset_asix_rx_fixup_info(rx);
  126. return 0;
  127. }
  128. /* Sometimes may fail to get a netdev socket buffer but
  129. * continue to process the URB socket buffer so that
  130. * synchronisation of the Ethernet frame Data header
  131. * word is maintained.
  132. */
  133. rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
  134. rx->remaining = size;
  135. }
  136. if (rx->remaining > skb->len - offset) {
  137. copy_length = skb->len - offset;
  138. rx->remaining -= copy_length;
  139. } else {
  140. copy_length = rx->remaining;
  141. rx->remaining = 0;
  142. }
  143. if (rx->ax_skb) {
  144. skb_put_data(rx->ax_skb, skb->data + offset,
  145. copy_length);
  146. if (!rx->remaining) {
  147. usbnet_skb_return(dev, rx->ax_skb);
  148. rx->ax_skb = NULL;
  149. }
  150. }
  151. offset += (copy_length + 1) & 0xfffe;
  152. }
  153. if (skb->len != offset) {
  154. netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
  155. skb->len, offset);
  156. reset_asix_rx_fixup_info(rx);
  157. return 0;
  158. }
  159. return 1;
  160. }
  161. int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
  162. {
  163. struct asix_common_private *dp = dev->driver_priv;
  164. struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
  165. return asix_rx_fixup_internal(dev, skb, rx);
  166. }
  167. void asix_rx_fixup_common_free(struct asix_common_private *dp)
  168. {
  169. struct asix_rx_fixup_info *rx;
  170. if (!dp)
  171. return;
  172. rx = &dp->rx_fixup_info;
  173. if (rx->ax_skb) {
  174. kfree_skb(rx->ax_skb);
  175. rx->ax_skb = NULL;
  176. }
  177. }
  178. struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  179. gfp_t flags)
  180. {
  181. int padlen;
  182. int headroom = skb_headroom(skb);
  183. int tailroom = skb_tailroom(skb);
  184. u32 packet_len;
  185. u32 padbytes = 0xffff0000;
  186. void *ptr;
  187. padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
  188. /* We need to push 4 bytes in front of frame (packet_len)
  189. * and maybe add 4 bytes after the end (if padlen is 4)
  190. *
  191. * Avoid skb_copy_expand() expensive call, using following rules :
  192. * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
  193. * is false (and if we have 4 bytes of headroom)
  194. * - We are allowed to put 4 bytes at tail if skb_cloned()
  195. * is false (and if we have 4 bytes of tailroom)
  196. *
  197. * TCP packets for example are cloned, but __skb_header_release()
  198. * was called in tcp stack, allowing us to use headroom for our needs.
  199. */
  200. if (!skb_header_cloned(skb) &&
  201. !(padlen && skb_cloned(skb)) &&
  202. headroom + tailroom >= 4 + padlen) {
  203. /* following should not happen, but better be safe */
  204. if (headroom < 4 ||
  205. tailroom < padlen) {
  206. skb->data = memmove(skb->head + 4, skb->data, skb->len);
  207. skb_set_tail_pointer(skb, skb->len);
  208. }
  209. } else {
  210. struct sk_buff *skb2;
  211. skb2 = skb_copy_expand(skb, 4, padlen, flags);
  212. dev_kfree_skb_any(skb);
  213. skb = skb2;
  214. if (!skb)
  215. return NULL;
  216. }
  217. packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
  218. ptr = skb_push(skb, 4);
  219. put_unaligned_le32(packet_len, ptr);
  220. if (padlen) {
  221. put_unaligned_le32(padbytes, skb_tail_pointer(skb));
  222. skb_put(skb, sizeof(padbytes));
  223. }
  224. usbnet_set_skb_tx_stats(skb, 1, 0);
  225. return skb;
  226. }
  227. int asix_set_sw_mii(struct usbnet *dev, int in_pm)
  228. {
  229. int ret;
  230. ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL, in_pm);
  231. if (ret < 0)
  232. netdev_err(dev->net, "Failed to enable software MII access\n");
  233. return ret;
  234. }
  235. int asix_set_hw_mii(struct usbnet *dev, int in_pm)
  236. {
  237. int ret;
  238. ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL, in_pm);
  239. if (ret < 0)
  240. netdev_err(dev->net, "Failed to enable hardware MII access\n");
  241. return ret;
  242. }
  243. int asix_read_phy_addr(struct usbnet *dev, int internal)
  244. {
  245. int offset = (internal ? 1 : 0);
  246. u8 buf[2];
  247. int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf, 0);
  248. netdev_dbg(dev->net, "asix_get_phy_addr()\n");
  249. if (ret < 2) {
  250. netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
  251. goto out;
  252. }
  253. netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
  254. *((__le16 *)buf));
  255. ret = buf[offset];
  256. out:
  257. return ret;
  258. }
  259. int asix_get_phy_addr(struct usbnet *dev)
  260. {
  261. /* return the address of the internal phy */
  262. return asix_read_phy_addr(dev, 1);
  263. }
  264. int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm)
  265. {
  266. int ret;
  267. ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm);
  268. if (ret < 0)
  269. netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
  270. return ret;
  271. }
  272. u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm)
  273. {
  274. __le16 v;
  275. int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm);
  276. if (ret < 0) {
  277. netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
  278. goto out;
  279. }
  280. ret = le16_to_cpu(v);
  281. out:
  282. return ret;
  283. }
  284. int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm)
  285. {
  286. int ret;
  287. netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
  288. ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm);
  289. if (ret < 0)
  290. netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
  291. mode, ret);
  292. return ret;
  293. }
  294. u16 asix_read_medium_status(struct usbnet *dev, int in_pm)
  295. {
  296. __le16 v;
  297. int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS,
  298. 0, 0, 2, &v, in_pm);
  299. if (ret < 0) {
  300. netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
  301. ret);
  302. return ret; /* TODO: callers not checking for error ret */
  303. }
  304. return le16_to_cpu(v);
  305. }
  306. int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm)
  307. {
  308. int ret;
  309. netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
  310. ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
  311. mode, 0, 0, NULL, in_pm);
  312. if (ret < 0)
  313. netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
  314. mode, ret);
  315. return ret;
  316. }
  317. int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
  318. {
  319. int ret;
  320. netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
  321. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
  322. if (ret < 0)
  323. netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
  324. value, ret);
  325. if (sleep)
  326. msleep(sleep);
  327. return ret;
  328. }
  329. /*
  330. * AX88772 & AX88178 have a 16-bit RX_CTL value
  331. */
  332. void asix_set_multicast(struct net_device *net)
  333. {
  334. struct usbnet *dev = netdev_priv(net);
  335. struct asix_data *data = (struct asix_data *)&dev->data;
  336. u16 rx_ctl = AX_DEFAULT_RX_CTL;
  337. if (net->flags & IFF_PROMISC) {
  338. rx_ctl |= AX_RX_CTL_PRO;
  339. } else if (net->flags & IFF_ALLMULTI ||
  340. netdev_mc_count(net) > AX_MAX_MCAST) {
  341. rx_ctl |= AX_RX_CTL_AMALL;
  342. } else if (netdev_mc_empty(net)) {
  343. /* just broadcast and directed */
  344. } else {
  345. /* We use the 20 byte dev->data
  346. * for our 8 byte filter buffer
  347. * to avoid allocating memory that
  348. * is tricky to free later */
  349. struct netdev_hw_addr *ha;
  350. u32 crc_bits;
  351. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  352. /* Build the multicast hash filter. */
  353. netdev_for_each_mc_addr(ha, net) {
  354. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  355. data->multi_filter[crc_bits >> 3] |=
  356. 1 << (crc_bits & 7);
  357. }
  358. asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  359. AX_MCAST_FILTER_SIZE, data->multi_filter);
  360. rx_ctl |= AX_RX_CTL_AM;
  361. }
  362. asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  363. }
  364. int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
  365. {
  366. struct usbnet *dev = netdev_priv(netdev);
  367. __le16 res;
  368. u8 smsr;
  369. int i = 0;
  370. int ret;
  371. mutex_lock(&dev->phy_mutex);
  372. do {
  373. ret = asix_set_sw_mii(dev, 0);
  374. if (ret == -ENODEV || ret == -ETIMEDOUT)
  375. break;
  376. usleep_range(1000, 1100);
  377. ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
  378. 0, 0, 1, &smsr, 0);
  379. } while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
  380. if (ret == -ENODEV || ret == -ETIMEDOUT) {
  381. mutex_unlock(&dev->phy_mutex);
  382. return ret;
  383. }
  384. asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
  385. (__u16)loc, 2, &res, 0);
  386. asix_set_hw_mii(dev, 0);
  387. mutex_unlock(&dev->phy_mutex);
  388. netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
  389. phy_id, loc, le16_to_cpu(res));
  390. return le16_to_cpu(res);
  391. }
  392. void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
  393. {
  394. struct usbnet *dev = netdev_priv(netdev);
  395. __le16 res = cpu_to_le16(val);
  396. u8 smsr;
  397. int i = 0;
  398. int ret;
  399. netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
  400. phy_id, loc, val);
  401. mutex_lock(&dev->phy_mutex);
  402. do {
  403. ret = asix_set_sw_mii(dev, 0);
  404. if (ret == -ENODEV)
  405. break;
  406. usleep_range(1000, 1100);
  407. ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
  408. 0, 0, 1, &smsr, 0);
  409. } while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
  410. if (ret == -ENODEV) {
  411. mutex_unlock(&dev->phy_mutex);
  412. return;
  413. }
  414. asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
  415. (__u16)loc, 2, &res, 0);
  416. asix_set_hw_mii(dev, 0);
  417. mutex_unlock(&dev->phy_mutex);
  418. }
  419. int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
  420. {
  421. struct usbnet *dev = netdev_priv(netdev);
  422. __le16 res;
  423. u8 smsr;
  424. int i = 0;
  425. int ret;
  426. mutex_lock(&dev->phy_mutex);
  427. do {
  428. ret = asix_set_sw_mii(dev, 1);
  429. if (ret == -ENODEV || ret == -ETIMEDOUT)
  430. break;
  431. usleep_range(1000, 1100);
  432. ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
  433. 0, 0, 1, &smsr, 1);
  434. } while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
  435. if (ret == -ENODEV || ret == -ETIMEDOUT) {
  436. mutex_unlock(&dev->phy_mutex);
  437. return ret;
  438. }
  439. asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
  440. (__u16)loc, 2, &res, 1);
  441. asix_set_hw_mii(dev, 1);
  442. mutex_unlock(&dev->phy_mutex);
  443. netdev_dbg(dev->net, "asix_mdio_read_nopm() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
  444. phy_id, loc, le16_to_cpu(res));
  445. return le16_to_cpu(res);
  446. }
  447. void
  448. asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
  449. {
  450. struct usbnet *dev = netdev_priv(netdev);
  451. __le16 res = cpu_to_le16(val);
  452. u8 smsr;
  453. int i = 0;
  454. int ret;
  455. netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
  456. phy_id, loc, val);
  457. mutex_lock(&dev->phy_mutex);
  458. do {
  459. ret = asix_set_sw_mii(dev, 1);
  460. if (ret == -ENODEV)
  461. break;
  462. usleep_range(1000, 1100);
  463. ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
  464. 0, 0, 1, &smsr, 1);
  465. } while (!(smsr & AX_HOST_EN) && (i++ < 30) && (ret != -ENODEV));
  466. if (ret == -ENODEV) {
  467. mutex_unlock(&dev->phy_mutex);
  468. return;
  469. }
  470. asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
  471. (__u16)loc, 2, &res, 1);
  472. asix_set_hw_mii(dev, 1);
  473. mutex_unlock(&dev->phy_mutex);
  474. }
  475. void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  476. {
  477. struct usbnet *dev = netdev_priv(net);
  478. u8 opt;
  479. if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE,
  480. 0, 0, 1, &opt, 0) < 0) {
  481. wolinfo->supported = 0;
  482. wolinfo->wolopts = 0;
  483. return;
  484. }
  485. wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
  486. wolinfo->wolopts = 0;
  487. if (opt & AX_MONITOR_LINK)
  488. wolinfo->wolopts |= WAKE_PHY;
  489. if (opt & AX_MONITOR_MAGIC)
  490. wolinfo->wolopts |= WAKE_MAGIC;
  491. }
  492. int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  493. {
  494. struct usbnet *dev = netdev_priv(net);
  495. u8 opt = 0;
  496. if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
  497. return -EINVAL;
  498. if (wolinfo->wolopts & WAKE_PHY)
  499. opt |= AX_MONITOR_LINK;
  500. if (wolinfo->wolopts & WAKE_MAGIC)
  501. opt |= AX_MONITOR_MAGIC;
  502. if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
  503. opt, 0, 0, NULL, 0) < 0)
  504. return -EINVAL;
  505. return 0;
  506. }
  507. int asix_get_eeprom_len(struct net_device *net)
  508. {
  509. return AX_EEPROM_LEN;
  510. }
  511. int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
  512. u8 *data)
  513. {
  514. struct usbnet *dev = netdev_priv(net);
  515. u16 *eeprom_buff;
  516. int first_word, last_word;
  517. int i;
  518. if (eeprom->len == 0)
  519. return -EINVAL;
  520. eeprom->magic = AX_EEPROM_MAGIC;
  521. first_word = eeprom->offset >> 1;
  522. last_word = (eeprom->offset + eeprom->len - 1) >> 1;
  523. eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
  524. GFP_KERNEL);
  525. if (!eeprom_buff)
  526. return -ENOMEM;
  527. /* ax8817x returns 2 bytes from eeprom on read */
  528. for (i = first_word; i <= last_word; i++) {
  529. if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
  530. &eeprom_buff[i - first_word], 0) < 0) {
  531. kfree(eeprom_buff);
  532. return -EIO;
  533. }
  534. }
  535. memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
  536. kfree(eeprom_buff);
  537. return 0;
  538. }
  539. int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
  540. u8 *data)
  541. {
  542. struct usbnet *dev = netdev_priv(net);
  543. u16 *eeprom_buff;
  544. int first_word, last_word;
  545. int i;
  546. int ret;
  547. netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
  548. eeprom->len, eeprom->offset, eeprom->magic);
  549. if (eeprom->len == 0)
  550. return -EINVAL;
  551. if (eeprom->magic != AX_EEPROM_MAGIC)
  552. return -EINVAL;
  553. first_word = eeprom->offset >> 1;
  554. last_word = (eeprom->offset + eeprom->len - 1) >> 1;
  555. eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
  556. GFP_KERNEL);
  557. if (!eeprom_buff)
  558. return -ENOMEM;
  559. /* align data to 16 bit boundaries, read the missing data from
  560. the EEPROM */
  561. if (eeprom->offset & 1) {
  562. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
  563. &eeprom_buff[0], 0);
  564. if (ret < 0) {
  565. netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
  566. goto free;
  567. }
  568. }
  569. if ((eeprom->offset + eeprom->len) & 1) {
  570. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
  571. &eeprom_buff[last_word - first_word], 0);
  572. if (ret < 0) {
  573. netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
  574. goto free;
  575. }
  576. }
  577. memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
  578. /* write data to EEPROM */
  579. ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0);
  580. if (ret < 0) {
  581. netdev_err(net, "Failed to enable EEPROM write\n");
  582. goto free;
  583. }
  584. msleep(20);
  585. for (i = first_word; i <= last_word; i++) {
  586. netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
  587. i, eeprom_buff[i - first_word]);
  588. ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
  589. eeprom_buff[i - first_word], 0, NULL, 0);
  590. if (ret < 0) {
  591. netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
  592. i);
  593. goto free;
  594. }
  595. msleep(20);
  596. }
  597. ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0);
  598. if (ret < 0) {
  599. netdev_err(net, "Failed to disable EEPROM write\n");
  600. goto free;
  601. }
  602. ret = 0;
  603. free:
  604. kfree(eeprom_buff);
  605. return ret;
  606. }
  607. void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  608. {
  609. /* Inherit standard device info */
  610. usbnet_get_drvinfo(net, info);
  611. strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  612. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  613. }
  614. int asix_set_mac_address(struct net_device *net, void *p)
  615. {
  616. struct usbnet *dev = netdev_priv(net);
  617. struct asix_data *data = (struct asix_data *)&dev->data;
  618. struct sockaddr *addr = p;
  619. if (netif_running(net))
  620. return -EBUSY;
  621. if (!is_valid_ether_addr(addr->sa_data))
  622. return -EADDRNOTAVAIL;
  623. memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
  624. /* We use the 20 byte dev->data
  625. * for our 6 byte mac buffer
  626. * to avoid allocating memory that
  627. * is tricky to free later */
  628. memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
  629. asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  630. data->mac_addr);
  631. return 0;
  632. }