asix_common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include "asix.h"
  23. int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  24. u16 size, void *data)
  25. {
  26. int ret;
  27. ret = usbnet_read_cmd(dev, cmd,
  28. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  29. value, index, data, size);
  30. if (ret != size && ret >= 0)
  31. return -EINVAL;
  32. return ret;
  33. }
  34. int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  35. u16 size, void *data)
  36. {
  37. return usbnet_write_cmd(dev, cmd,
  38. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  39. value, index, data, size);
  40. }
  41. void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  42. u16 size, void *data)
  43. {
  44. usbnet_write_cmd_async(dev, cmd,
  45. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  46. value, index, data, size);
  47. }
  48. int asix_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  49. {
  50. int offset = 0;
  51. while (offset + sizeof(u32) < skb->len) {
  52. struct sk_buff *ax_skb;
  53. u16 size;
  54. u32 header = get_unaligned_le32(skb->data + offset);
  55. offset += sizeof(u32);
  56. /* get the packet length */
  57. size = (u16) (header & 0x7ff);
  58. if (size != ((~header >> 16) & 0x07ff)) {
  59. netdev_err(dev->net, "asix_rx_fixup() Bad Header Length\n");
  60. return 0;
  61. }
  62. if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) ||
  63. (size + offset > skb->len)) {
  64. netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
  65. size);
  66. return 0;
  67. }
  68. ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
  69. if (!ax_skb)
  70. return 0;
  71. skb_put(ax_skb, size);
  72. memcpy(ax_skb->data, skb->data + offset, size);
  73. usbnet_skb_return(dev, ax_skb);
  74. offset += (size + 1) & 0xfffe;
  75. }
  76. if (skb->len != offset) {
  77. netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d\n",
  78. skb->len);
  79. return 0;
  80. }
  81. return 1;
  82. }
  83. struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  84. gfp_t flags)
  85. {
  86. int padlen;
  87. int headroom = skb_headroom(skb);
  88. int tailroom = skb_tailroom(skb);
  89. u32 packet_len;
  90. u32 padbytes = 0xffff0000;
  91. padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
  92. /* We need to push 4 bytes in front of frame (packet_len)
  93. * and maybe add 4 bytes after the end (if padlen is 4)
  94. *
  95. * Avoid skb_copy_expand() expensive call, using following rules :
  96. * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
  97. * is false (and if we have 4 bytes of headroom)
  98. * - We are allowed to put 4 bytes at tail if skb_cloned()
  99. * is false (and if we have 4 bytes of tailroom)
  100. *
  101. * TCP packets for example are cloned, but skb_header_release()
  102. * was called in tcp stack, allowing us to use headroom for our needs.
  103. */
  104. if (!skb_header_cloned(skb) &&
  105. !(padlen && skb_cloned(skb)) &&
  106. headroom + tailroom >= 4 + padlen) {
  107. /* following should not happen, but better be safe */
  108. if (headroom < 4 ||
  109. tailroom < padlen) {
  110. skb->data = memmove(skb->head + 4, skb->data, skb->len);
  111. skb_set_tail_pointer(skb, skb->len);
  112. }
  113. } else {
  114. struct sk_buff *skb2;
  115. skb2 = skb_copy_expand(skb, 4, padlen, flags);
  116. dev_kfree_skb_any(skb);
  117. skb = skb2;
  118. if (!skb)
  119. return NULL;
  120. }
  121. packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
  122. skb_push(skb, 4);
  123. cpu_to_le32s(&packet_len);
  124. skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
  125. if (padlen) {
  126. cpu_to_le32s(&padbytes);
  127. memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
  128. skb_put(skb, sizeof(padbytes));
  129. }
  130. return skb;
  131. }
  132. int asix_set_sw_mii(struct usbnet *dev)
  133. {
  134. int ret;
  135. ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
  136. if (ret < 0)
  137. netdev_err(dev->net, "Failed to enable software MII access\n");
  138. return ret;
  139. }
  140. int asix_set_hw_mii(struct usbnet *dev)
  141. {
  142. int ret;
  143. ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
  144. if (ret < 0)
  145. netdev_err(dev->net, "Failed to enable hardware MII access\n");
  146. return ret;
  147. }
  148. int asix_read_phy_addr(struct usbnet *dev, int internal)
  149. {
  150. int offset = (internal ? 1 : 0);
  151. u8 buf[2];
  152. int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
  153. netdev_dbg(dev->net, "asix_get_phy_addr()\n");
  154. if (ret < 0) {
  155. netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
  156. goto out;
  157. }
  158. netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
  159. *((__le16 *)buf));
  160. ret = buf[offset];
  161. out:
  162. return ret;
  163. }
  164. int asix_get_phy_addr(struct usbnet *dev)
  165. {
  166. /* return the address of the internal phy */
  167. return asix_read_phy_addr(dev, 1);
  168. }
  169. int asix_sw_reset(struct usbnet *dev, u8 flags)
  170. {
  171. int ret;
  172. ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
  173. if (ret < 0)
  174. netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
  175. return ret;
  176. }
  177. u16 asix_read_rx_ctl(struct usbnet *dev)
  178. {
  179. __le16 v;
  180. int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
  181. if (ret < 0) {
  182. netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
  183. goto out;
  184. }
  185. ret = le16_to_cpu(v);
  186. out:
  187. return ret;
  188. }
  189. int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
  190. {
  191. int ret;
  192. netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
  193. ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
  194. if (ret < 0)
  195. netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
  196. mode, ret);
  197. return ret;
  198. }
  199. u16 asix_read_medium_status(struct usbnet *dev)
  200. {
  201. __le16 v;
  202. int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
  203. if (ret < 0) {
  204. netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
  205. ret);
  206. return ret; /* TODO: callers not checking for error ret */
  207. }
  208. return le16_to_cpu(v);
  209. }
  210. int asix_write_medium_mode(struct usbnet *dev, u16 mode)
  211. {
  212. int ret;
  213. netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
  214. ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
  215. if (ret < 0)
  216. netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
  217. mode, ret);
  218. return ret;
  219. }
  220. int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
  221. {
  222. int ret;
  223. netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
  224. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
  225. if (ret < 0)
  226. netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
  227. value, ret);
  228. if (sleep)
  229. msleep(sleep);
  230. return ret;
  231. }
  232. /*
  233. * AX88772 & AX88178 have a 16-bit RX_CTL value
  234. */
  235. void asix_set_multicast(struct net_device *net)
  236. {
  237. struct usbnet *dev = netdev_priv(net);
  238. struct asix_data *data = (struct asix_data *)&dev->data;
  239. u16 rx_ctl = AX_DEFAULT_RX_CTL;
  240. if (net->flags & IFF_PROMISC) {
  241. rx_ctl |= AX_RX_CTL_PRO;
  242. } else if (net->flags & IFF_ALLMULTI ||
  243. netdev_mc_count(net) > AX_MAX_MCAST) {
  244. rx_ctl |= AX_RX_CTL_AMALL;
  245. } else if (netdev_mc_empty(net)) {
  246. /* just broadcast and directed */
  247. } else {
  248. /* We use the 20 byte dev->data
  249. * for our 8 byte filter buffer
  250. * to avoid allocating memory that
  251. * is tricky to free later */
  252. struct netdev_hw_addr *ha;
  253. u32 crc_bits;
  254. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  255. /* Build the multicast hash filter. */
  256. netdev_for_each_mc_addr(ha, net) {
  257. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  258. data->multi_filter[crc_bits >> 3] |=
  259. 1 << (crc_bits & 7);
  260. }
  261. asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  262. AX_MCAST_FILTER_SIZE, data->multi_filter);
  263. rx_ctl |= AX_RX_CTL_AM;
  264. }
  265. asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  266. }
  267. int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
  268. {
  269. struct usbnet *dev = netdev_priv(netdev);
  270. __le16 res;
  271. mutex_lock(&dev->phy_mutex);
  272. asix_set_sw_mii(dev);
  273. asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
  274. (__u16)loc, 2, &res);
  275. asix_set_hw_mii(dev);
  276. mutex_unlock(&dev->phy_mutex);
  277. netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
  278. phy_id, loc, le16_to_cpu(res));
  279. return le16_to_cpu(res);
  280. }
  281. void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
  282. {
  283. struct usbnet *dev = netdev_priv(netdev);
  284. __le16 res = cpu_to_le16(val);
  285. netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
  286. phy_id, loc, val);
  287. mutex_lock(&dev->phy_mutex);
  288. asix_set_sw_mii(dev);
  289. asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
  290. asix_set_hw_mii(dev);
  291. mutex_unlock(&dev->phy_mutex);
  292. }
  293. void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  294. {
  295. struct usbnet *dev = netdev_priv(net);
  296. u8 opt;
  297. if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
  298. wolinfo->supported = 0;
  299. wolinfo->wolopts = 0;
  300. return;
  301. }
  302. wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
  303. wolinfo->wolopts = 0;
  304. if (opt & AX_MONITOR_LINK)
  305. wolinfo->wolopts |= WAKE_PHY;
  306. if (opt & AX_MONITOR_MAGIC)
  307. wolinfo->wolopts |= WAKE_MAGIC;
  308. }
  309. int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  310. {
  311. struct usbnet *dev = netdev_priv(net);
  312. u8 opt = 0;
  313. if (wolinfo->wolopts & WAKE_PHY)
  314. opt |= AX_MONITOR_LINK;
  315. if (wolinfo->wolopts & WAKE_MAGIC)
  316. opt |= AX_MONITOR_MAGIC;
  317. if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
  318. opt, 0, 0, NULL) < 0)
  319. return -EINVAL;
  320. return 0;
  321. }
  322. int asix_get_eeprom_len(struct net_device *net)
  323. {
  324. return AX_EEPROM_LEN;
  325. }
  326. int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
  327. u8 *data)
  328. {
  329. struct usbnet *dev = netdev_priv(net);
  330. u16 *eeprom_buff;
  331. int first_word, last_word;
  332. int i;
  333. if (eeprom->len == 0)
  334. return -EINVAL;
  335. eeprom->magic = AX_EEPROM_MAGIC;
  336. first_word = eeprom->offset >> 1;
  337. last_word = (eeprom->offset + eeprom->len - 1) >> 1;
  338. eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
  339. GFP_KERNEL);
  340. if (!eeprom_buff)
  341. return -ENOMEM;
  342. /* ax8817x returns 2 bytes from eeprom on read */
  343. for (i = first_word; i <= last_word; i++) {
  344. if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
  345. &(eeprom_buff[i - first_word])) < 0) {
  346. kfree(eeprom_buff);
  347. return -EIO;
  348. }
  349. }
  350. memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
  351. kfree(eeprom_buff);
  352. return 0;
  353. }
  354. int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
  355. u8 *data)
  356. {
  357. struct usbnet *dev = netdev_priv(net);
  358. u16 *eeprom_buff;
  359. int first_word, last_word;
  360. int i;
  361. int ret;
  362. netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
  363. eeprom->len, eeprom->offset, eeprom->magic);
  364. if (eeprom->len == 0)
  365. return -EINVAL;
  366. if (eeprom->magic != AX_EEPROM_MAGIC)
  367. return -EINVAL;
  368. first_word = eeprom->offset >> 1;
  369. last_word = (eeprom->offset + eeprom->len - 1) >> 1;
  370. eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
  371. GFP_KERNEL);
  372. if (!eeprom_buff)
  373. return -ENOMEM;
  374. /* align data to 16 bit boundaries, read the missing data from
  375. the EEPROM */
  376. if (eeprom->offset & 1) {
  377. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
  378. &(eeprom_buff[0]));
  379. if (ret < 0) {
  380. netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
  381. goto free;
  382. }
  383. }
  384. if ((eeprom->offset + eeprom->len) & 1) {
  385. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
  386. &(eeprom_buff[last_word - first_word]));
  387. if (ret < 0) {
  388. netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
  389. goto free;
  390. }
  391. }
  392. memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
  393. /* write data to EEPROM */
  394. ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL);
  395. if (ret < 0) {
  396. netdev_err(net, "Failed to enable EEPROM write\n");
  397. goto free;
  398. }
  399. msleep(20);
  400. for (i = first_word; i <= last_word; i++) {
  401. netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
  402. i, eeprom_buff[i - first_word]);
  403. ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
  404. eeprom_buff[i - first_word], 0, NULL);
  405. if (ret < 0) {
  406. netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
  407. i);
  408. goto free;
  409. }
  410. msleep(20);
  411. }
  412. ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL);
  413. if (ret < 0) {
  414. netdev_err(net, "Failed to disable EEPROM write\n");
  415. goto free;
  416. }
  417. ret = 0;
  418. free:
  419. kfree(eeprom_buff);
  420. return ret;
  421. }
  422. void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  423. {
  424. /* Inherit standard device info */
  425. usbnet_get_drvinfo(net, info);
  426. strncpy (info->driver, DRIVER_NAME, sizeof info->driver);
  427. strncpy (info->version, DRIVER_VERSION, sizeof info->version);
  428. info->eedump_len = AX_EEPROM_LEN;
  429. }
  430. int asix_set_mac_address(struct net_device *net, void *p)
  431. {
  432. struct usbnet *dev = netdev_priv(net);
  433. struct asix_data *data = (struct asix_data *)&dev->data;
  434. struct sockaddr *addr = p;
  435. if (netif_running(net))
  436. return -EBUSY;
  437. if (!is_valid_ether_addr(addr->sa_data))
  438. return -EADDRNOTAVAIL;
  439. memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
  440. /* We use the 20 byte dev->data
  441. * for our 6 byte mac buffer
  442. * to avoid allocating memory that
  443. * is tricky to free later */
  444. memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
  445. asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  446. data->mac_addr);
  447. return 0;
  448. }