asix_common.c 15 KB

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