asix_common.c 19 KB

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