asix_common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. return skb;
  168. }
  169. int asix_set_sw_mii(struct usbnet *dev)
  170. {
  171. int ret;
  172. ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
  173. if (ret < 0)
  174. netdev_err(dev->net, "Failed to enable software MII access\n");
  175. return ret;
  176. }
  177. int asix_set_hw_mii(struct usbnet *dev)
  178. {
  179. int ret;
  180. ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
  181. if (ret < 0)
  182. netdev_err(dev->net, "Failed to enable hardware MII access\n");
  183. return ret;
  184. }
  185. int asix_read_phy_addr(struct usbnet *dev, int internal)
  186. {
  187. int offset = (internal ? 1 : 0);
  188. u8 buf[2];
  189. int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
  190. netdev_dbg(dev->net, "asix_get_phy_addr()\n");
  191. if (ret < 0) {
  192. netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
  193. goto out;
  194. }
  195. netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
  196. *((__le16 *)buf));
  197. ret = buf[offset];
  198. out:
  199. return ret;
  200. }
  201. int asix_get_phy_addr(struct usbnet *dev)
  202. {
  203. /* return the address of the internal phy */
  204. return asix_read_phy_addr(dev, 1);
  205. }
  206. int asix_sw_reset(struct usbnet *dev, u8 flags)
  207. {
  208. int ret;
  209. ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
  210. if (ret < 0)
  211. netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
  212. return ret;
  213. }
  214. u16 asix_read_rx_ctl(struct usbnet *dev)
  215. {
  216. __le16 v;
  217. int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
  218. if (ret < 0) {
  219. netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
  220. goto out;
  221. }
  222. ret = le16_to_cpu(v);
  223. out:
  224. return ret;
  225. }
  226. int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
  227. {
  228. int ret;
  229. netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
  230. ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
  231. if (ret < 0)
  232. netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
  233. mode, ret);
  234. return ret;
  235. }
  236. u16 asix_read_medium_status(struct usbnet *dev)
  237. {
  238. __le16 v;
  239. int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
  240. if (ret < 0) {
  241. netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
  242. ret);
  243. return ret; /* TODO: callers not checking for error ret */
  244. }
  245. return le16_to_cpu(v);
  246. }
  247. int asix_write_medium_mode(struct usbnet *dev, u16 mode)
  248. {
  249. int ret;
  250. netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
  251. ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
  252. if (ret < 0)
  253. netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
  254. mode, ret);
  255. return ret;
  256. }
  257. int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
  258. {
  259. int ret;
  260. netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
  261. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
  262. if (ret < 0)
  263. netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
  264. value, ret);
  265. if (sleep)
  266. msleep(sleep);
  267. return ret;
  268. }
  269. /*
  270. * AX88772 & AX88178 have a 16-bit RX_CTL value
  271. */
  272. void asix_set_multicast(struct net_device *net)
  273. {
  274. struct usbnet *dev = netdev_priv(net);
  275. struct asix_data *data = (struct asix_data *)&dev->data;
  276. u16 rx_ctl = AX_DEFAULT_RX_CTL;
  277. if (net->flags & IFF_PROMISC) {
  278. rx_ctl |= AX_RX_CTL_PRO;
  279. } else if (net->flags & IFF_ALLMULTI ||
  280. netdev_mc_count(net) > AX_MAX_MCAST) {
  281. rx_ctl |= AX_RX_CTL_AMALL;
  282. } else if (netdev_mc_empty(net)) {
  283. /* just broadcast and directed */
  284. } else {
  285. /* We use the 20 byte dev->data
  286. * for our 8 byte filter buffer
  287. * to avoid allocating memory that
  288. * is tricky to free later */
  289. struct netdev_hw_addr *ha;
  290. u32 crc_bits;
  291. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  292. /* Build the multicast hash filter. */
  293. netdev_for_each_mc_addr(ha, net) {
  294. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  295. data->multi_filter[crc_bits >> 3] |=
  296. 1 << (crc_bits & 7);
  297. }
  298. asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  299. AX_MCAST_FILTER_SIZE, data->multi_filter);
  300. rx_ctl |= AX_RX_CTL_AM;
  301. }
  302. asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  303. }
  304. int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
  305. {
  306. struct usbnet *dev = netdev_priv(netdev);
  307. __le16 res;
  308. mutex_lock(&dev->phy_mutex);
  309. asix_set_sw_mii(dev);
  310. asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
  311. (__u16)loc, 2, &res);
  312. asix_set_hw_mii(dev);
  313. mutex_unlock(&dev->phy_mutex);
  314. netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
  315. phy_id, loc, le16_to_cpu(res));
  316. return le16_to_cpu(res);
  317. }
  318. void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
  319. {
  320. struct usbnet *dev = netdev_priv(netdev);
  321. __le16 res = cpu_to_le16(val);
  322. netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
  323. phy_id, loc, val);
  324. mutex_lock(&dev->phy_mutex);
  325. asix_set_sw_mii(dev);
  326. asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
  327. asix_set_hw_mii(dev);
  328. mutex_unlock(&dev->phy_mutex);
  329. }
  330. void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  331. {
  332. struct usbnet *dev = netdev_priv(net);
  333. u8 opt;
  334. if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
  335. wolinfo->supported = 0;
  336. wolinfo->wolopts = 0;
  337. return;
  338. }
  339. wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
  340. wolinfo->wolopts = 0;
  341. if (opt & AX_MONITOR_LINK)
  342. wolinfo->wolopts |= WAKE_PHY;
  343. if (opt & AX_MONITOR_MAGIC)
  344. wolinfo->wolopts |= WAKE_MAGIC;
  345. }
  346. int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  347. {
  348. struct usbnet *dev = netdev_priv(net);
  349. u8 opt = 0;
  350. if (wolinfo->wolopts & WAKE_PHY)
  351. opt |= AX_MONITOR_LINK;
  352. if (wolinfo->wolopts & WAKE_MAGIC)
  353. opt |= AX_MONITOR_MAGIC;
  354. if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
  355. opt, 0, 0, NULL) < 0)
  356. return -EINVAL;
  357. return 0;
  358. }
  359. int asix_get_eeprom_len(struct net_device *net)
  360. {
  361. return AX_EEPROM_LEN;
  362. }
  363. int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
  364. u8 *data)
  365. {
  366. struct usbnet *dev = netdev_priv(net);
  367. u16 *eeprom_buff;
  368. int first_word, last_word;
  369. int i;
  370. if (eeprom->len == 0)
  371. return -EINVAL;
  372. eeprom->magic = AX_EEPROM_MAGIC;
  373. first_word = eeprom->offset >> 1;
  374. last_word = (eeprom->offset + eeprom->len - 1) >> 1;
  375. eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
  376. GFP_KERNEL);
  377. if (!eeprom_buff)
  378. return -ENOMEM;
  379. /* ax8817x returns 2 bytes from eeprom on read */
  380. for (i = first_word; i <= last_word; i++) {
  381. if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
  382. &(eeprom_buff[i - first_word])) < 0) {
  383. kfree(eeprom_buff);
  384. return -EIO;
  385. }
  386. }
  387. memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
  388. kfree(eeprom_buff);
  389. return 0;
  390. }
  391. int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
  392. u8 *data)
  393. {
  394. struct usbnet *dev = netdev_priv(net);
  395. u16 *eeprom_buff;
  396. int first_word, last_word;
  397. int i;
  398. int ret;
  399. netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
  400. eeprom->len, eeprom->offset, eeprom->magic);
  401. if (eeprom->len == 0)
  402. return -EINVAL;
  403. if (eeprom->magic != AX_EEPROM_MAGIC)
  404. return -EINVAL;
  405. first_word = eeprom->offset >> 1;
  406. last_word = (eeprom->offset + eeprom->len - 1) >> 1;
  407. eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
  408. GFP_KERNEL);
  409. if (!eeprom_buff)
  410. return -ENOMEM;
  411. /* align data to 16 bit boundaries, read the missing data from
  412. the EEPROM */
  413. if (eeprom->offset & 1) {
  414. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
  415. &(eeprom_buff[0]));
  416. if (ret < 0) {
  417. netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
  418. goto free;
  419. }
  420. }
  421. if ((eeprom->offset + eeprom->len) & 1) {
  422. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
  423. &(eeprom_buff[last_word - first_word]));
  424. if (ret < 0) {
  425. netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
  426. goto free;
  427. }
  428. }
  429. memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
  430. /* write data to EEPROM */
  431. ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL);
  432. if (ret < 0) {
  433. netdev_err(net, "Failed to enable EEPROM write\n");
  434. goto free;
  435. }
  436. msleep(20);
  437. for (i = first_word; i <= last_word; i++) {
  438. netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
  439. i, eeprom_buff[i - first_word]);
  440. ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
  441. eeprom_buff[i - first_word], 0, NULL);
  442. if (ret < 0) {
  443. netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
  444. i);
  445. goto free;
  446. }
  447. msleep(20);
  448. }
  449. ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL);
  450. if (ret < 0) {
  451. netdev_err(net, "Failed to disable EEPROM write\n");
  452. goto free;
  453. }
  454. ret = 0;
  455. free:
  456. kfree(eeprom_buff);
  457. return ret;
  458. }
  459. void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  460. {
  461. /* Inherit standard device info */
  462. usbnet_get_drvinfo(net, info);
  463. strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  464. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  465. info->eedump_len = AX_EEPROM_LEN;
  466. }
  467. int asix_set_mac_address(struct net_device *net, void *p)
  468. {
  469. struct usbnet *dev = netdev_priv(net);
  470. struct asix_data *data = (struct asix_data *)&dev->data;
  471. struct sockaddr *addr = p;
  472. if (netif_running(net))
  473. return -EBUSY;
  474. if (!is_valid_ether_addr(addr->sa_data))
  475. return -EADDRNOTAVAIL;
  476. memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
  477. /* We use the 20 byte dev->data
  478. * for our 6 byte mac buffer
  479. * to avoid allocating memory that
  480. * is tricky to free later */
  481. memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
  482. asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  483. data->mac_addr);
  484. return 0;
  485. }