asix_common.c 18 KB

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