asix_common.c 20 KB

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