asix_common.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. netdev_dbg(dev->net, "%s PHY address 0x%x\n",
  280. internal ? "internal" : "external", ret);
  281. return ret;
  282. error:
  283. netdev_err(dev->net, "Error reading PHY_ID register: %02x\n", ret);
  284. return ret;
  285. }
  286. int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm)
  287. {
  288. int ret;
  289. ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm);
  290. if (ret < 0)
  291. netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
  292. return ret;
  293. }
  294. u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm)
  295. {
  296. __le16 v;
  297. int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm);
  298. if (ret < 0) {
  299. netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
  300. goto out;
  301. }
  302. ret = le16_to_cpu(v);
  303. out:
  304. return ret;
  305. }
  306. int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm)
  307. {
  308. int ret;
  309. netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
  310. ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm);
  311. if (ret < 0)
  312. netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
  313. mode, ret);
  314. return ret;
  315. }
  316. u16 asix_read_medium_status(struct usbnet *dev, int in_pm)
  317. {
  318. __le16 v;
  319. int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS,
  320. 0, 0, 2, &v, in_pm);
  321. if (ret < 0) {
  322. netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
  323. ret);
  324. return ret; /* TODO: callers not checking for error ret */
  325. }
  326. return le16_to_cpu(v);
  327. }
  328. int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm)
  329. {
  330. int ret;
  331. netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
  332. ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
  333. mode, 0, 0, NULL, in_pm);
  334. if (ret < 0)
  335. netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
  336. mode, ret);
  337. return ret;
  338. }
  339. int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
  340. {
  341. int ret;
  342. netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
  343. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
  344. if (ret < 0)
  345. netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
  346. value, ret);
  347. if (sleep)
  348. msleep(sleep);
  349. return ret;
  350. }
  351. /*
  352. * AX88772 & AX88178 have a 16-bit RX_CTL value
  353. */
  354. void asix_set_multicast(struct net_device *net)
  355. {
  356. struct usbnet *dev = netdev_priv(net);
  357. struct asix_data *data = (struct asix_data *)&dev->data;
  358. u16 rx_ctl = AX_DEFAULT_RX_CTL;
  359. if (net->flags & IFF_PROMISC) {
  360. rx_ctl |= AX_RX_CTL_PRO;
  361. } else if (net->flags & IFF_ALLMULTI ||
  362. netdev_mc_count(net) > AX_MAX_MCAST) {
  363. rx_ctl |= AX_RX_CTL_AMALL;
  364. } else if (netdev_mc_empty(net)) {
  365. /* just broadcast and directed */
  366. } else {
  367. /* We use the 20 byte dev->data
  368. * for our 8 byte filter buffer
  369. * to avoid allocating memory that
  370. * is tricky to free later */
  371. struct netdev_hw_addr *ha;
  372. u32 crc_bits;
  373. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  374. /* Build the multicast hash filter. */
  375. netdev_for_each_mc_addr(ha, net) {
  376. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  377. data->multi_filter[crc_bits >> 3] |=
  378. 1 << (crc_bits & 7);
  379. }
  380. asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  381. AX_MCAST_FILTER_SIZE, data->multi_filter);
  382. rx_ctl |= AX_RX_CTL_AM;
  383. }
  384. asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  385. }
  386. static int __asix_mdio_read(struct net_device *netdev, int phy_id, int loc,
  387. bool in_pm)
  388. {
  389. struct usbnet *dev = netdev_priv(netdev);
  390. __le16 res;
  391. int ret;
  392. mutex_lock(&dev->phy_mutex);
  393. ret = asix_check_host_enable(dev, in_pm);
  394. if (ret == -ENODEV || ret == -ETIMEDOUT) {
  395. mutex_unlock(&dev->phy_mutex);
  396. return ret;
  397. }
  398. ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2,
  399. &res, in_pm);
  400. if (ret < 0)
  401. goto out;
  402. ret = asix_set_hw_mii(dev, in_pm);
  403. out:
  404. mutex_unlock(&dev->phy_mutex);
  405. netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
  406. phy_id, loc, le16_to_cpu(res));
  407. return ret < 0 ? ret : le16_to_cpu(res);
  408. }
  409. int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
  410. {
  411. return __asix_mdio_read(netdev, phy_id, loc, false);
  412. }
  413. static int __asix_mdio_write(struct net_device *netdev, int phy_id, int loc,
  414. int val, bool in_pm)
  415. {
  416. struct usbnet *dev = netdev_priv(netdev);
  417. __le16 res = cpu_to_le16(val);
  418. int ret;
  419. netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
  420. phy_id, loc, val);
  421. mutex_lock(&dev->phy_mutex);
  422. ret = asix_check_host_enable(dev, in_pm);
  423. if (ret == -ENODEV)
  424. goto out;
  425. ret = asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2,
  426. &res, in_pm);
  427. if (ret < 0)
  428. goto out;
  429. ret = asix_set_hw_mii(dev, in_pm);
  430. out:
  431. mutex_unlock(&dev->phy_mutex);
  432. return ret < 0 ? ret : 0;
  433. }
  434. void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
  435. {
  436. __asix_mdio_write(netdev, phy_id, loc, val, false);
  437. }
  438. /* MDIO read and write wrappers for phylib */
  439. int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum)
  440. {
  441. struct usbnet *priv = bus->priv;
  442. return __asix_mdio_read(priv->net, phy_id, regnum, false);
  443. }
  444. int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum, u16 val)
  445. {
  446. struct usbnet *priv = bus->priv;
  447. return __asix_mdio_write(priv->net, phy_id, regnum, val, false);
  448. }
  449. int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
  450. {
  451. return __asix_mdio_read(netdev, phy_id, loc, true);
  452. }
  453. void
  454. asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
  455. {
  456. __asix_mdio_write(netdev, phy_id, loc, val, true);
  457. }
  458. void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  459. {
  460. struct usbnet *dev = netdev_priv(net);
  461. u8 opt;
  462. if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE,
  463. 0, 0, 1, &opt, 0) < 0) {
  464. wolinfo->supported = 0;
  465. wolinfo->wolopts = 0;
  466. return;
  467. }
  468. wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
  469. wolinfo->wolopts = 0;
  470. if (opt & AX_MONITOR_LINK)
  471. wolinfo->wolopts |= WAKE_PHY;
  472. if (opt & AX_MONITOR_MAGIC)
  473. wolinfo->wolopts |= WAKE_MAGIC;
  474. }
  475. int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  476. {
  477. struct usbnet *dev = netdev_priv(net);
  478. u8 opt = 0;
  479. if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
  480. return -EINVAL;
  481. if (wolinfo->wolopts & WAKE_PHY)
  482. opt |= AX_MONITOR_LINK;
  483. if (wolinfo->wolopts & WAKE_MAGIC)
  484. opt |= AX_MONITOR_MAGIC;
  485. if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
  486. opt, 0, 0, NULL, 0) < 0)
  487. return -EINVAL;
  488. return 0;
  489. }
  490. int asix_get_eeprom_len(struct net_device *net)
  491. {
  492. return AX_EEPROM_LEN;
  493. }
  494. int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
  495. u8 *data)
  496. {
  497. struct usbnet *dev = netdev_priv(net);
  498. u16 *eeprom_buff;
  499. int first_word, last_word;
  500. int i;
  501. if (eeprom->len == 0)
  502. return -EINVAL;
  503. eeprom->magic = AX_EEPROM_MAGIC;
  504. first_word = eeprom->offset >> 1;
  505. last_word = (eeprom->offset + eeprom->len - 1) >> 1;
  506. eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
  507. GFP_KERNEL);
  508. if (!eeprom_buff)
  509. return -ENOMEM;
  510. /* ax8817x returns 2 bytes from eeprom on read */
  511. for (i = first_word; i <= last_word; i++) {
  512. if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
  513. &eeprom_buff[i - first_word], 0) < 0) {
  514. kfree(eeprom_buff);
  515. return -EIO;
  516. }
  517. }
  518. memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
  519. kfree(eeprom_buff);
  520. return 0;
  521. }
  522. int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
  523. u8 *data)
  524. {
  525. struct usbnet *dev = netdev_priv(net);
  526. u16 *eeprom_buff;
  527. int first_word, last_word;
  528. int i;
  529. int ret;
  530. netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
  531. eeprom->len, eeprom->offset, eeprom->magic);
  532. if (eeprom->len == 0)
  533. return -EINVAL;
  534. if (eeprom->magic != AX_EEPROM_MAGIC)
  535. return -EINVAL;
  536. first_word = eeprom->offset >> 1;
  537. last_word = (eeprom->offset + eeprom->len - 1) >> 1;
  538. eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
  539. GFP_KERNEL);
  540. if (!eeprom_buff)
  541. return -ENOMEM;
  542. /* align data to 16 bit boundaries, read the missing data from
  543. the EEPROM */
  544. if (eeprom->offset & 1) {
  545. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
  546. &eeprom_buff[0], 0);
  547. if (ret < 0) {
  548. netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
  549. goto free;
  550. }
  551. }
  552. if ((eeprom->offset + eeprom->len) & 1) {
  553. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
  554. &eeprom_buff[last_word - first_word], 0);
  555. if (ret < 0) {
  556. netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
  557. goto free;
  558. }
  559. }
  560. memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
  561. /* write data to EEPROM */
  562. ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0);
  563. if (ret < 0) {
  564. netdev_err(net, "Failed to enable EEPROM write\n");
  565. goto free;
  566. }
  567. msleep(20);
  568. for (i = first_word; i <= last_word; i++) {
  569. netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
  570. i, eeprom_buff[i - first_word]);
  571. ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
  572. eeprom_buff[i - first_word], 0, NULL, 0);
  573. if (ret < 0) {
  574. netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
  575. i);
  576. goto free;
  577. }
  578. msleep(20);
  579. }
  580. ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0);
  581. if (ret < 0) {
  582. netdev_err(net, "Failed to disable EEPROM write\n");
  583. goto free;
  584. }
  585. ret = 0;
  586. free:
  587. kfree(eeprom_buff);
  588. return ret;
  589. }
  590. void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  591. {
  592. /* Inherit standard device info */
  593. usbnet_get_drvinfo(net, info);
  594. strscpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  595. strscpy(info->version, DRIVER_VERSION, sizeof(info->version));
  596. }
  597. int asix_set_mac_address(struct net_device *net, void *p)
  598. {
  599. struct usbnet *dev = netdev_priv(net);
  600. struct asix_data *data = (struct asix_data *)&dev->data;
  601. struct sockaddr *addr = p;
  602. if (netif_running(net))
  603. return -EBUSY;
  604. if (!is_valid_ether_addr(addr->sa_data))
  605. return -EADDRNOTAVAIL;
  606. eth_hw_addr_set(net, addr->sa_data);
  607. /* We use the 20 byte dev->data
  608. * for our 6 byte mac buffer
  609. * to avoid allocating memory that
  610. * is tricky to free later */
  611. memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
  612. asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  613. data->mac_addr);
  614. return 0;
  615. }