asix_common.c 15 KB

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