asix_common.c 15 KB

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