asix_common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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 <[email protected]>
  5. * Copyright (C) 2005 Phil Chang <[email protected]>
  6. * Copyright (C) 2006 James Painter <[email protected]>
  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. /* set MAC link settings according to information from phylib */
  340. void asix_adjust_link(struct net_device *netdev)
  341. {
  342. struct phy_device *phydev = netdev->phydev;
  343. struct usbnet *dev = netdev_priv(netdev);
  344. u16 mode = 0;
  345. if (phydev->link) {
  346. mode = AX88772_MEDIUM_DEFAULT;
  347. if (phydev->duplex == DUPLEX_HALF)
  348. mode &= ~AX_MEDIUM_FD;
  349. if (phydev->speed != SPEED_100)
  350. mode &= ~AX_MEDIUM_PS;
  351. }
  352. asix_write_medium_mode(dev, mode, 0);
  353. phy_print_status(phydev);
  354. usbnet_link_change(dev, phydev->link, 0);
  355. }
  356. int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
  357. {
  358. int ret;
  359. netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
  360. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
  361. if (ret < 0)
  362. netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
  363. value, ret);
  364. if (sleep)
  365. msleep(sleep);
  366. return ret;
  367. }
  368. /*
  369. * AX88772 & AX88178 have a 16-bit RX_CTL value
  370. */
  371. void asix_set_multicast(struct net_device *net)
  372. {
  373. struct usbnet *dev = netdev_priv(net);
  374. struct asix_data *data = (struct asix_data *)&dev->data;
  375. u16 rx_ctl = AX_DEFAULT_RX_CTL;
  376. if (net->flags & IFF_PROMISC) {
  377. rx_ctl |= AX_RX_CTL_PRO;
  378. } else if (net->flags & IFF_ALLMULTI ||
  379. netdev_mc_count(net) > AX_MAX_MCAST) {
  380. rx_ctl |= AX_RX_CTL_AMALL;
  381. } else if (netdev_mc_empty(net)) {
  382. /* just broadcast and directed */
  383. } else {
  384. /* We use the 20 byte dev->data
  385. * for our 8 byte filter buffer
  386. * to avoid allocating memory that
  387. * is tricky to free later */
  388. struct netdev_hw_addr *ha;
  389. u32 crc_bits;
  390. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  391. /* Build the multicast hash filter. */
  392. netdev_for_each_mc_addr(ha, net) {
  393. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  394. data->multi_filter[crc_bits >> 3] |=
  395. 1 << (crc_bits & 7);
  396. }
  397. asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  398. AX_MCAST_FILTER_SIZE, data->multi_filter);
  399. rx_ctl |= AX_RX_CTL_AM;
  400. }
  401. asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  402. }
  403. static int __asix_mdio_read(struct net_device *netdev, int phy_id, int loc,
  404. bool in_pm)
  405. {
  406. struct usbnet *dev = netdev_priv(netdev);
  407. __le16 res;
  408. int ret;
  409. mutex_lock(&dev->phy_mutex);
  410. ret = asix_check_host_enable(dev, in_pm);
  411. if (ret == -ENODEV || ret == -ETIMEDOUT) {
  412. mutex_unlock(&dev->phy_mutex);
  413. return ret;
  414. }
  415. ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2,
  416. &res, in_pm);
  417. if (ret < 0)
  418. goto out;
  419. ret = asix_set_hw_mii(dev, in_pm);
  420. out:
  421. mutex_unlock(&dev->phy_mutex);
  422. netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
  423. phy_id, loc, le16_to_cpu(res));
  424. return ret < 0 ? ret : le16_to_cpu(res);
  425. }
  426. int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
  427. {
  428. return __asix_mdio_read(netdev, phy_id, loc, false);
  429. }
  430. static int __asix_mdio_write(struct net_device *netdev, int phy_id, int loc,
  431. int val, bool in_pm)
  432. {
  433. struct usbnet *dev = netdev_priv(netdev);
  434. __le16 res = cpu_to_le16(val);
  435. int ret;
  436. netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
  437. phy_id, loc, val);
  438. mutex_lock(&dev->phy_mutex);
  439. ret = asix_check_host_enable(dev, in_pm);
  440. if (ret == -ENODEV)
  441. goto out;
  442. ret = asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2,
  443. &res, in_pm);
  444. if (ret < 0)
  445. goto out;
  446. ret = asix_set_hw_mii(dev, in_pm);
  447. out:
  448. mutex_unlock(&dev->phy_mutex);
  449. return ret < 0 ? ret : 0;
  450. }
  451. void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
  452. {
  453. __asix_mdio_write(netdev, phy_id, loc, val, false);
  454. }
  455. /* MDIO read and write wrappers for phylib */
  456. int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum)
  457. {
  458. struct usbnet *priv = bus->priv;
  459. return __asix_mdio_read(priv->net, phy_id, regnum, false);
  460. }
  461. int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum, u16 val)
  462. {
  463. struct usbnet *priv = bus->priv;
  464. return __asix_mdio_write(priv->net, phy_id, regnum, val, false);
  465. }
  466. int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
  467. {
  468. return __asix_mdio_read(netdev, phy_id, loc, true);
  469. }
  470. void
  471. asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
  472. {
  473. __asix_mdio_write(netdev, phy_id, loc, val, true);
  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. strscpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  612. strscpy(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. eth_hw_addr_set(net, addr->sa_data);
  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. }