asix_devices.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. #define PHY_MODE_MARVELL 0x0000
  24. #define MII_MARVELL_LED_CTRL 0x0018
  25. #define MII_MARVELL_STATUS 0x001b
  26. #define MII_MARVELL_CTRL 0x0014
  27. #define MARVELL_LED_MANUAL 0x0019
  28. #define MARVELL_STATUS_HWCFG 0x0004
  29. #define MARVELL_CTRL_TXDELAY 0x0002
  30. #define MARVELL_CTRL_RXDELAY 0x0080
  31. #define PHY_MODE_RTL8211CL 0x000C
  32. struct ax88172_int_data {
  33. __le16 res1;
  34. u8 link;
  35. __le16 res2;
  36. u8 status;
  37. __le16 res3;
  38. } __packed;
  39. static void asix_status(struct usbnet *dev, struct urb *urb)
  40. {
  41. struct ax88172_int_data *event;
  42. int link;
  43. if (urb->actual_length < 8)
  44. return;
  45. event = urb->transfer_buffer;
  46. link = event->link & 0x01;
  47. if (netif_carrier_ok(dev->net) != link) {
  48. if (link) {
  49. netif_carrier_on(dev->net);
  50. usbnet_defer_kevent (dev, EVENT_LINK_RESET );
  51. } else
  52. netif_carrier_off(dev->net);
  53. netdev_dbg(dev->net, "Link Status is: %d\n", link);
  54. }
  55. }
  56. static void asix_set_netdev_dev_addr(struct usbnet *dev, u8 *addr)
  57. {
  58. if (is_valid_ether_addr(addr)) {
  59. memcpy(dev->net->dev_addr, addr, ETH_ALEN);
  60. } else {
  61. netdev_info(dev->net, "invalid hw address, using random\n");
  62. eth_hw_addr_random(dev->net);
  63. }
  64. }
  65. /* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */
  66. static u32 asix_get_phyid(struct usbnet *dev)
  67. {
  68. int phy_reg;
  69. u32 phy_id;
  70. int i;
  71. /* Poll for the rare case the FW or phy isn't ready yet. */
  72. for (i = 0; i < 100; i++) {
  73. phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1);
  74. if (phy_reg != 0 && phy_reg != 0xFFFF)
  75. break;
  76. mdelay(1);
  77. }
  78. if (phy_reg <= 0 || phy_reg == 0xFFFF)
  79. return 0;
  80. phy_id = (phy_reg & 0xffff) << 16;
  81. phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID2);
  82. if (phy_reg < 0)
  83. return 0;
  84. phy_id |= (phy_reg & 0xffff);
  85. return phy_id;
  86. }
  87. static u32 asix_get_link(struct net_device *net)
  88. {
  89. struct usbnet *dev = netdev_priv(net);
  90. return mii_link_ok(&dev->mii);
  91. }
  92. static int asix_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
  93. {
  94. struct usbnet *dev = netdev_priv(net);
  95. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  96. }
  97. /* We need to override some ethtool_ops so we require our
  98. own structure so we don't interfere with other usbnet
  99. devices that may be connected at the same time. */
  100. static const struct ethtool_ops ax88172_ethtool_ops = {
  101. .get_drvinfo = asix_get_drvinfo,
  102. .get_link = asix_get_link,
  103. .get_msglevel = usbnet_get_msglevel,
  104. .set_msglevel = usbnet_set_msglevel,
  105. .get_wol = asix_get_wol,
  106. .set_wol = asix_set_wol,
  107. .get_eeprom_len = asix_get_eeprom_len,
  108. .get_eeprom = asix_get_eeprom,
  109. .set_eeprom = asix_set_eeprom,
  110. .get_settings = usbnet_get_settings,
  111. .set_settings = usbnet_set_settings,
  112. .nway_reset = usbnet_nway_reset,
  113. };
  114. static void ax88172_set_multicast(struct net_device *net)
  115. {
  116. struct usbnet *dev = netdev_priv(net);
  117. struct asix_data *data = (struct asix_data *)&dev->data;
  118. u8 rx_ctl = 0x8c;
  119. if (net->flags & IFF_PROMISC) {
  120. rx_ctl |= 0x01;
  121. } else if (net->flags & IFF_ALLMULTI ||
  122. netdev_mc_count(net) > AX_MAX_MCAST) {
  123. rx_ctl |= 0x02;
  124. } else if (netdev_mc_empty(net)) {
  125. /* just broadcast and directed */
  126. } else {
  127. /* We use the 20 byte dev->data
  128. * for our 8 byte filter buffer
  129. * to avoid allocating memory that
  130. * is tricky to free later */
  131. struct netdev_hw_addr *ha;
  132. u32 crc_bits;
  133. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  134. /* Build the multicast hash filter. */
  135. netdev_for_each_mc_addr(ha, net) {
  136. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  137. data->multi_filter[crc_bits >> 3] |=
  138. 1 << (crc_bits & 7);
  139. }
  140. asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  141. AX_MCAST_FILTER_SIZE, data->multi_filter);
  142. rx_ctl |= 0x10;
  143. }
  144. asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  145. }
  146. static int ax88172_link_reset(struct usbnet *dev)
  147. {
  148. u8 mode;
  149. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  150. mii_check_media(&dev->mii, 1, 1);
  151. mii_ethtool_gset(&dev->mii, &ecmd);
  152. mode = AX88172_MEDIUM_DEFAULT;
  153. if (ecmd.duplex != DUPLEX_FULL)
  154. mode |= ~AX88172_MEDIUM_FD;
  155. netdev_dbg(dev->net, "ax88172_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  156. ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
  157. asix_write_medium_mode(dev, mode);
  158. return 0;
  159. }
  160. static const struct net_device_ops ax88172_netdev_ops = {
  161. .ndo_open = usbnet_open,
  162. .ndo_stop = usbnet_stop,
  163. .ndo_start_xmit = usbnet_start_xmit,
  164. .ndo_tx_timeout = usbnet_tx_timeout,
  165. .ndo_change_mtu = usbnet_change_mtu,
  166. .ndo_set_mac_address = eth_mac_addr,
  167. .ndo_validate_addr = eth_validate_addr,
  168. .ndo_do_ioctl = asix_ioctl,
  169. .ndo_set_rx_mode = ax88172_set_multicast,
  170. };
  171. static int ax88172_bind(struct usbnet *dev, struct usb_interface *intf)
  172. {
  173. int ret = 0;
  174. u8 buf[ETH_ALEN];
  175. int i;
  176. unsigned long gpio_bits = dev->driver_info->data;
  177. usbnet_get_endpoints(dev,intf);
  178. /* Toggle the GPIOs in a manufacturer/model specific way */
  179. for (i = 2; i >= 0; i--) {
  180. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
  181. (gpio_bits >> (i * 8)) & 0xff, 0, 0, NULL);
  182. if (ret < 0)
  183. goto out;
  184. msleep(5);
  185. }
  186. ret = asix_write_rx_ctl(dev, 0x80);
  187. if (ret < 0)
  188. goto out;
  189. /* Get the MAC address */
  190. ret = asix_read_cmd(dev, AX88172_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  191. if (ret < 0) {
  192. netdev_dbg(dev->net, "read AX_CMD_READ_NODE_ID failed: %d\n",
  193. ret);
  194. goto out;
  195. }
  196. asix_set_netdev_dev_addr(dev, buf);
  197. /* Initialize MII structure */
  198. dev->mii.dev = dev->net;
  199. dev->mii.mdio_read = asix_mdio_read;
  200. dev->mii.mdio_write = asix_mdio_write;
  201. dev->mii.phy_id_mask = 0x3f;
  202. dev->mii.reg_num_mask = 0x1f;
  203. dev->mii.phy_id = asix_get_phy_addr(dev);
  204. dev->net->netdev_ops = &ax88172_netdev_ops;
  205. dev->net->ethtool_ops = &ax88172_ethtool_ops;
  206. dev->net->needed_headroom = 4; /* cf asix_tx_fixup() */
  207. dev->net->needed_tailroom = 4; /* cf asix_tx_fixup() */
  208. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  209. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  210. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  211. mii_nway_restart(&dev->mii);
  212. return 0;
  213. out:
  214. return ret;
  215. }
  216. static const struct ethtool_ops ax88772_ethtool_ops = {
  217. .get_drvinfo = asix_get_drvinfo,
  218. .get_link = asix_get_link,
  219. .get_msglevel = usbnet_get_msglevel,
  220. .set_msglevel = usbnet_set_msglevel,
  221. .get_wol = asix_get_wol,
  222. .set_wol = asix_set_wol,
  223. .get_eeprom_len = asix_get_eeprom_len,
  224. .get_eeprom = asix_get_eeprom,
  225. .set_eeprom = asix_set_eeprom,
  226. .get_settings = usbnet_get_settings,
  227. .set_settings = usbnet_set_settings,
  228. .nway_reset = usbnet_nway_reset,
  229. };
  230. static int ax88772_link_reset(struct usbnet *dev)
  231. {
  232. u16 mode;
  233. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  234. mii_check_media(&dev->mii, 1, 1);
  235. mii_ethtool_gset(&dev->mii, &ecmd);
  236. mode = AX88772_MEDIUM_DEFAULT;
  237. if (ethtool_cmd_speed(&ecmd) != SPEED_100)
  238. mode &= ~AX_MEDIUM_PS;
  239. if (ecmd.duplex != DUPLEX_FULL)
  240. mode &= ~AX_MEDIUM_FD;
  241. netdev_dbg(dev->net, "ax88772_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  242. ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
  243. asix_write_medium_mode(dev, mode);
  244. return 0;
  245. }
  246. static int ax88772_reset(struct usbnet *dev)
  247. {
  248. struct asix_data *data = (struct asix_data *)&dev->data;
  249. int ret, embd_phy;
  250. u16 rx_ctl;
  251. ret = asix_write_gpio(dev,
  252. AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5);
  253. if (ret < 0)
  254. goto out;
  255. embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
  256. ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
  257. if (ret < 0) {
  258. netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
  259. goto out;
  260. }
  261. ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL);
  262. if (ret < 0)
  263. goto out;
  264. msleep(150);
  265. ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
  266. if (ret < 0)
  267. goto out;
  268. msleep(150);
  269. if (embd_phy) {
  270. ret = asix_sw_reset(dev, AX_SWRESET_IPRL);
  271. if (ret < 0)
  272. goto out;
  273. } else {
  274. ret = asix_sw_reset(dev, AX_SWRESET_PRTE);
  275. if (ret < 0)
  276. goto out;
  277. }
  278. msleep(150);
  279. rx_ctl = asix_read_rx_ctl(dev);
  280. netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
  281. ret = asix_write_rx_ctl(dev, 0x0000);
  282. if (ret < 0)
  283. goto out;
  284. rx_ctl = asix_read_rx_ctl(dev);
  285. netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
  286. ret = asix_sw_reset(dev, AX_SWRESET_PRL);
  287. if (ret < 0)
  288. goto out;
  289. msleep(150);
  290. ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL);
  291. if (ret < 0)
  292. goto out;
  293. msleep(150);
  294. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  295. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  296. ADVERTISE_ALL | ADVERTISE_CSMA);
  297. mii_nway_restart(&dev->mii);
  298. ret = asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT);
  299. if (ret < 0)
  300. goto out;
  301. ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
  302. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  303. AX88772_IPG2_DEFAULT, 0, NULL);
  304. if (ret < 0) {
  305. netdev_dbg(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
  306. goto out;
  307. }
  308. /* Rewrite MAC address */
  309. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  310. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  311. data->mac_addr);
  312. if (ret < 0)
  313. goto out;
  314. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  315. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
  316. if (ret < 0)
  317. goto out;
  318. rx_ctl = asix_read_rx_ctl(dev);
  319. netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
  320. rx_ctl);
  321. rx_ctl = asix_read_medium_status(dev);
  322. netdev_dbg(dev->net,
  323. "Medium Status is 0x%04x after all initializations\n",
  324. rx_ctl);
  325. return 0;
  326. out:
  327. return ret;
  328. }
  329. static const struct net_device_ops ax88772_netdev_ops = {
  330. .ndo_open = usbnet_open,
  331. .ndo_stop = usbnet_stop,
  332. .ndo_start_xmit = usbnet_start_xmit,
  333. .ndo_tx_timeout = usbnet_tx_timeout,
  334. .ndo_change_mtu = usbnet_change_mtu,
  335. .ndo_set_mac_address = asix_set_mac_address,
  336. .ndo_validate_addr = eth_validate_addr,
  337. .ndo_do_ioctl = asix_ioctl,
  338. .ndo_set_rx_mode = asix_set_multicast,
  339. };
  340. static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
  341. {
  342. int ret, embd_phy, i;
  343. u8 buf[ETH_ALEN];
  344. u32 phyid;
  345. usbnet_get_endpoints(dev,intf);
  346. /* Get the MAC address */
  347. if (dev->driver_info->data & FLAG_EEPROM_MAC) {
  348. for (i = 0; i < (ETH_ALEN >> 1); i++) {
  349. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x04 + i,
  350. 0, 2, buf + i * 2);
  351. if (ret < 0)
  352. break;
  353. }
  354. } else {
  355. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID,
  356. 0, 0, ETH_ALEN, buf);
  357. }
  358. if (ret < 0) {
  359. netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret);
  360. return ret;
  361. }
  362. asix_set_netdev_dev_addr(dev, buf);
  363. /* Initialize MII structure */
  364. dev->mii.dev = dev->net;
  365. dev->mii.mdio_read = asix_mdio_read;
  366. dev->mii.mdio_write = asix_mdio_write;
  367. dev->mii.phy_id_mask = 0x1f;
  368. dev->mii.reg_num_mask = 0x1f;
  369. dev->mii.phy_id = asix_get_phy_addr(dev);
  370. dev->net->netdev_ops = &ax88772_netdev_ops;
  371. dev->net->ethtool_ops = &ax88772_ethtool_ops;
  372. dev->net->needed_headroom = 4; /* cf asix_tx_fixup() */
  373. dev->net->needed_tailroom = 4; /* cf asix_tx_fixup() */
  374. embd_phy = ((dev->mii.phy_id & 0x1f) == 0x10 ? 1 : 0);
  375. /* Reset the PHY to normal operation mode */
  376. ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
  377. if (ret < 0) {
  378. netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
  379. return ret;
  380. }
  381. ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL);
  382. if (ret < 0)
  383. return ret;
  384. msleep(150);
  385. ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
  386. if (ret < 0)
  387. return ret;
  388. msleep(150);
  389. ret = asix_sw_reset(dev, embd_phy ? AX_SWRESET_IPRL : AX_SWRESET_PRTE);
  390. /* Read PHYID register *AFTER* the PHY was reset properly */
  391. phyid = asix_get_phyid(dev);
  392. netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid);
  393. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  394. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  395. /* hard_mtu is still the default - the device does not support
  396. jumbo eth frames */
  397. dev->rx_urb_size = 2048;
  398. }
  399. dev->driver_priv = kzalloc(sizeof(struct asix_common_private), GFP_KERNEL);
  400. if (!dev->driver_priv)
  401. return -ENOMEM;
  402. return 0;
  403. }
  404. void ax88772_unbind(struct usbnet *dev, struct usb_interface *intf)
  405. {
  406. if (dev->driver_priv)
  407. kfree(dev->driver_priv);
  408. }
  409. static const struct ethtool_ops ax88178_ethtool_ops = {
  410. .get_drvinfo = asix_get_drvinfo,
  411. .get_link = asix_get_link,
  412. .get_msglevel = usbnet_get_msglevel,
  413. .set_msglevel = usbnet_set_msglevel,
  414. .get_wol = asix_get_wol,
  415. .set_wol = asix_set_wol,
  416. .get_eeprom_len = asix_get_eeprom_len,
  417. .get_eeprom = asix_get_eeprom,
  418. .set_eeprom = asix_set_eeprom,
  419. .get_settings = usbnet_get_settings,
  420. .set_settings = usbnet_set_settings,
  421. .nway_reset = usbnet_nway_reset,
  422. };
  423. static int marvell_phy_init(struct usbnet *dev)
  424. {
  425. struct asix_data *data = (struct asix_data *)&dev->data;
  426. u16 reg;
  427. netdev_dbg(dev->net, "marvell_phy_init()\n");
  428. reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_MARVELL_STATUS);
  429. netdev_dbg(dev->net, "MII_MARVELL_STATUS = 0x%04x\n", reg);
  430. asix_mdio_write(dev->net, dev->mii.phy_id, MII_MARVELL_CTRL,
  431. MARVELL_CTRL_RXDELAY | MARVELL_CTRL_TXDELAY);
  432. if (data->ledmode) {
  433. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  434. MII_MARVELL_LED_CTRL);
  435. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (1) = 0x%04x\n", reg);
  436. reg &= 0xf8ff;
  437. reg |= (1 + 0x0100);
  438. asix_mdio_write(dev->net, dev->mii.phy_id,
  439. MII_MARVELL_LED_CTRL, reg);
  440. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  441. MII_MARVELL_LED_CTRL);
  442. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (2) = 0x%04x\n", reg);
  443. reg &= 0xfc0f;
  444. }
  445. return 0;
  446. }
  447. static int rtl8211cl_phy_init(struct usbnet *dev)
  448. {
  449. struct asix_data *data = (struct asix_data *)&dev->data;
  450. netdev_dbg(dev->net, "rtl8211cl_phy_init()\n");
  451. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0005);
  452. asix_mdio_write (dev->net, dev->mii.phy_id, 0x0c, 0);
  453. asix_mdio_write (dev->net, dev->mii.phy_id, 0x01,
  454. asix_mdio_read (dev->net, dev->mii.phy_id, 0x01) | 0x0080);
  455. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  456. if (data->ledmode == 12) {
  457. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0002);
  458. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1a, 0x00cb);
  459. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  460. }
  461. return 0;
  462. }
  463. static int marvell_led_status(struct usbnet *dev, u16 speed)
  464. {
  465. u16 reg = asix_mdio_read(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL);
  466. netdev_dbg(dev->net, "marvell_led_status() read 0x%04x\n", reg);
  467. /* Clear out the center LED bits - 0x03F0 */
  468. reg &= 0xfc0f;
  469. switch (speed) {
  470. case SPEED_1000:
  471. reg |= 0x03e0;
  472. break;
  473. case SPEED_100:
  474. reg |= 0x03b0;
  475. break;
  476. default:
  477. reg |= 0x02f0;
  478. }
  479. netdev_dbg(dev->net, "marvell_led_status() writing 0x%04x\n", reg);
  480. asix_mdio_write(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL, reg);
  481. return 0;
  482. }
  483. static int ax88178_reset(struct usbnet *dev)
  484. {
  485. struct asix_data *data = (struct asix_data *)&dev->data;
  486. int ret;
  487. __le16 eeprom;
  488. u8 status;
  489. int gpio0 = 0;
  490. u32 phyid;
  491. asix_read_cmd(dev, AX_CMD_READ_GPIOS, 0, 0, 1, &status);
  492. netdev_dbg(dev->net, "GPIO Status: 0x%04x\n", status);
  493. asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0, 0, 0, NULL);
  494. asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2, &eeprom);
  495. asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0, 0, 0, NULL);
  496. netdev_dbg(dev->net, "EEPROM index 0x17 is 0x%04x\n", eeprom);
  497. if (eeprom == cpu_to_le16(0xffff)) {
  498. data->phymode = PHY_MODE_MARVELL;
  499. data->ledmode = 0;
  500. gpio0 = 1;
  501. } else {
  502. data->phymode = le16_to_cpu(eeprom) & 0x7F;
  503. data->ledmode = le16_to_cpu(eeprom) >> 8;
  504. gpio0 = (le16_to_cpu(eeprom) & 0x80) ? 0 : 1;
  505. }
  506. netdev_dbg(dev->net, "GPIO0: %d, PhyMode: %d\n", gpio0, data->phymode);
  507. /* Power up external GigaPHY through AX88178 GPIO pin */
  508. asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_1 | AX_GPIO_GPO1EN, 40);
  509. if ((le16_to_cpu(eeprom) >> 8) != 1) {
  510. asix_write_gpio(dev, 0x003c, 30);
  511. asix_write_gpio(dev, 0x001c, 300);
  512. asix_write_gpio(dev, 0x003c, 30);
  513. } else {
  514. netdev_dbg(dev->net, "gpio phymode == 1 path\n");
  515. asix_write_gpio(dev, AX_GPIO_GPO1EN, 30);
  516. asix_write_gpio(dev, AX_GPIO_GPO1EN | AX_GPIO_GPO_1, 30);
  517. }
  518. /* Read PHYID register *AFTER* powering up PHY */
  519. phyid = asix_get_phyid(dev);
  520. netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid);
  521. /* Set AX88178 to enable MII/GMII/RGMII interface for external PHY */
  522. asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 0, 0, 0, NULL);
  523. asix_sw_reset(dev, 0);
  524. msleep(150);
  525. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
  526. msleep(150);
  527. asix_write_rx_ctl(dev, 0);
  528. if (data->phymode == PHY_MODE_MARVELL) {
  529. marvell_phy_init(dev);
  530. msleep(60);
  531. } else if (data->phymode == PHY_MODE_RTL8211CL)
  532. rtl8211cl_phy_init(dev);
  533. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR,
  534. BMCR_RESET | BMCR_ANENABLE);
  535. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  536. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  537. asix_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
  538. ADVERTISE_1000FULL);
  539. mii_nway_restart(&dev->mii);
  540. ret = asix_write_medium_mode(dev, AX88178_MEDIUM_DEFAULT);
  541. if (ret < 0)
  542. return ret;
  543. /* Rewrite MAC address */
  544. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  545. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  546. data->mac_addr);
  547. if (ret < 0)
  548. return ret;
  549. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
  550. if (ret < 0)
  551. return ret;
  552. return 0;
  553. }
  554. static int ax88178_link_reset(struct usbnet *dev)
  555. {
  556. u16 mode;
  557. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  558. struct asix_data *data = (struct asix_data *)&dev->data;
  559. u32 speed;
  560. netdev_dbg(dev->net, "ax88178_link_reset()\n");
  561. mii_check_media(&dev->mii, 1, 1);
  562. mii_ethtool_gset(&dev->mii, &ecmd);
  563. mode = AX88178_MEDIUM_DEFAULT;
  564. speed = ethtool_cmd_speed(&ecmd);
  565. if (speed == SPEED_1000)
  566. mode |= AX_MEDIUM_GM;
  567. else if (speed == SPEED_100)
  568. mode |= AX_MEDIUM_PS;
  569. else
  570. mode &= ~(AX_MEDIUM_PS | AX_MEDIUM_GM);
  571. mode |= AX_MEDIUM_ENCK;
  572. if (ecmd.duplex == DUPLEX_FULL)
  573. mode |= AX_MEDIUM_FD;
  574. else
  575. mode &= ~AX_MEDIUM_FD;
  576. netdev_dbg(dev->net, "ax88178_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  577. speed, ecmd.duplex, mode);
  578. asix_write_medium_mode(dev, mode);
  579. if (data->phymode == PHY_MODE_MARVELL && data->ledmode)
  580. marvell_led_status(dev, speed);
  581. return 0;
  582. }
  583. static void ax88178_set_mfb(struct usbnet *dev)
  584. {
  585. u16 mfb = AX_RX_CTL_MFB_16384;
  586. u16 rxctl;
  587. u16 medium;
  588. int old_rx_urb_size = dev->rx_urb_size;
  589. if (dev->hard_mtu < 2048) {
  590. dev->rx_urb_size = 2048;
  591. mfb = AX_RX_CTL_MFB_2048;
  592. } else if (dev->hard_mtu < 4096) {
  593. dev->rx_urb_size = 4096;
  594. mfb = AX_RX_CTL_MFB_4096;
  595. } else if (dev->hard_mtu < 8192) {
  596. dev->rx_urb_size = 8192;
  597. mfb = AX_RX_CTL_MFB_8192;
  598. } else if (dev->hard_mtu < 16384) {
  599. dev->rx_urb_size = 16384;
  600. mfb = AX_RX_CTL_MFB_16384;
  601. }
  602. rxctl = asix_read_rx_ctl(dev);
  603. asix_write_rx_ctl(dev, (rxctl & ~AX_RX_CTL_MFB_16384) | mfb);
  604. medium = asix_read_medium_status(dev);
  605. if (dev->net->mtu > 1500)
  606. medium |= AX_MEDIUM_JFE;
  607. else
  608. medium &= ~AX_MEDIUM_JFE;
  609. asix_write_medium_mode(dev, medium);
  610. if (dev->rx_urb_size > old_rx_urb_size)
  611. usbnet_unlink_rx_urbs(dev);
  612. }
  613. static int ax88178_change_mtu(struct net_device *net, int new_mtu)
  614. {
  615. struct usbnet *dev = netdev_priv(net);
  616. int ll_mtu = new_mtu + net->hard_header_len + 4;
  617. netdev_dbg(dev->net, "ax88178_change_mtu() new_mtu=%d\n", new_mtu);
  618. if (new_mtu <= 0 || ll_mtu > 16384)
  619. return -EINVAL;
  620. if ((ll_mtu % dev->maxpacket) == 0)
  621. return -EDOM;
  622. net->mtu = new_mtu;
  623. dev->hard_mtu = net->mtu + net->hard_header_len;
  624. ax88178_set_mfb(dev);
  625. return 0;
  626. }
  627. static const struct net_device_ops ax88178_netdev_ops = {
  628. .ndo_open = usbnet_open,
  629. .ndo_stop = usbnet_stop,
  630. .ndo_start_xmit = usbnet_start_xmit,
  631. .ndo_tx_timeout = usbnet_tx_timeout,
  632. .ndo_set_mac_address = asix_set_mac_address,
  633. .ndo_validate_addr = eth_validate_addr,
  634. .ndo_set_rx_mode = asix_set_multicast,
  635. .ndo_do_ioctl = asix_ioctl,
  636. .ndo_change_mtu = ax88178_change_mtu,
  637. };
  638. static int ax88178_bind(struct usbnet *dev, struct usb_interface *intf)
  639. {
  640. int ret;
  641. u8 buf[ETH_ALEN];
  642. usbnet_get_endpoints(dev,intf);
  643. /* Get the MAC address */
  644. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  645. if (ret < 0) {
  646. netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret);
  647. return ret;
  648. }
  649. asix_set_netdev_dev_addr(dev, buf);
  650. /* Initialize MII structure */
  651. dev->mii.dev = dev->net;
  652. dev->mii.mdio_read = asix_mdio_read;
  653. dev->mii.mdio_write = asix_mdio_write;
  654. dev->mii.phy_id_mask = 0x1f;
  655. dev->mii.reg_num_mask = 0xff;
  656. dev->mii.supports_gmii = 1;
  657. dev->mii.phy_id = asix_get_phy_addr(dev);
  658. dev->net->netdev_ops = &ax88178_netdev_ops;
  659. dev->net->ethtool_ops = &ax88178_ethtool_ops;
  660. /* Blink LEDS so users know driver saw dongle */
  661. asix_sw_reset(dev, 0);
  662. msleep(150);
  663. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
  664. msleep(150);
  665. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  666. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  667. /* hard_mtu is still the default - the device does not support
  668. jumbo eth frames */
  669. dev->rx_urb_size = 2048;
  670. }
  671. dev->driver_priv = kzalloc(sizeof(struct asix_common_private), GFP_KERNEL);
  672. if (!dev->driver_priv)
  673. return -ENOMEM;
  674. return 0;
  675. }
  676. static const struct driver_info ax8817x_info = {
  677. .description = "ASIX AX8817x USB 2.0 Ethernet",
  678. .bind = ax88172_bind,
  679. .status = asix_status,
  680. .link_reset = ax88172_link_reset,
  681. .reset = ax88172_link_reset,
  682. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  683. .data = 0x00130103,
  684. };
  685. static const struct driver_info dlink_dub_e100_info = {
  686. .description = "DLink DUB-E100 USB Ethernet",
  687. .bind = ax88172_bind,
  688. .status = asix_status,
  689. .link_reset = ax88172_link_reset,
  690. .reset = ax88172_link_reset,
  691. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  692. .data = 0x009f9d9f,
  693. };
  694. static const struct driver_info netgear_fa120_info = {
  695. .description = "Netgear FA-120 USB Ethernet",
  696. .bind = ax88172_bind,
  697. .status = asix_status,
  698. .link_reset = ax88172_link_reset,
  699. .reset = ax88172_link_reset,
  700. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  701. .data = 0x00130103,
  702. };
  703. static const struct driver_info hawking_uf200_info = {
  704. .description = "Hawking UF200 USB Ethernet",
  705. .bind = ax88172_bind,
  706. .status = asix_status,
  707. .link_reset = ax88172_link_reset,
  708. .reset = ax88172_link_reset,
  709. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  710. .data = 0x001f1d1f,
  711. };
  712. static const struct driver_info ax88772_info = {
  713. .description = "ASIX AX88772 USB 2.0 Ethernet",
  714. .bind = ax88772_bind,
  715. .unbind = ax88772_unbind,
  716. .status = asix_status,
  717. .link_reset = ax88772_link_reset,
  718. .reset = ax88772_reset,
  719. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR | FLAG_MULTI_PACKET,
  720. .rx_fixup = asix_rx_fixup_common,
  721. .tx_fixup = asix_tx_fixup,
  722. };
  723. static const struct driver_info ax88772b_info = {
  724. .description = "ASIX AX88772B USB 2.0 Ethernet",
  725. .bind = ax88772_bind,
  726. .unbind = ax88772_unbind,
  727. .status = asix_status,
  728. .link_reset = ax88772_link_reset,
  729. .reset = ax88772_reset,
  730. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
  731. FLAG_MULTI_PACKET,
  732. .rx_fixup = asix_rx_fixup_common,
  733. .tx_fixup = asix_tx_fixup,
  734. .data = FLAG_EEPROM_MAC,
  735. };
  736. static const struct driver_info ax88178_info = {
  737. .description = "ASIX AX88178 USB 2.0 Ethernet",
  738. .bind = ax88178_bind,
  739. .unbind = ax88772_unbind,
  740. .status = asix_status,
  741. .link_reset = ax88178_link_reset,
  742. .reset = ax88178_reset,
  743. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR,
  744. .rx_fixup = asix_rx_fixup_common,
  745. .tx_fixup = asix_tx_fixup,
  746. };
  747. extern const struct driver_info ax88172a_info;
  748. static const struct usb_device_id products [] = {
  749. {
  750. // Linksys USB200M
  751. USB_DEVICE (0x077b, 0x2226),
  752. .driver_info = (unsigned long) &ax8817x_info,
  753. }, {
  754. // Netgear FA120
  755. USB_DEVICE (0x0846, 0x1040),
  756. .driver_info = (unsigned long) &netgear_fa120_info,
  757. }, {
  758. // DLink DUB-E100
  759. USB_DEVICE (0x2001, 0x1a00),
  760. .driver_info = (unsigned long) &dlink_dub_e100_info,
  761. }, {
  762. // Intellinet, ST Lab USB Ethernet
  763. USB_DEVICE (0x0b95, 0x1720),
  764. .driver_info = (unsigned long) &ax8817x_info,
  765. }, {
  766. // Hawking UF200, TrendNet TU2-ET100
  767. USB_DEVICE (0x07b8, 0x420a),
  768. .driver_info = (unsigned long) &hawking_uf200_info,
  769. }, {
  770. // Billionton Systems, USB2AR
  771. USB_DEVICE (0x08dd, 0x90ff),
  772. .driver_info = (unsigned long) &ax8817x_info,
  773. }, {
  774. // ATEN UC210T
  775. USB_DEVICE (0x0557, 0x2009),
  776. .driver_info = (unsigned long) &ax8817x_info,
  777. }, {
  778. // Buffalo LUA-U2-KTX
  779. USB_DEVICE (0x0411, 0x003d),
  780. .driver_info = (unsigned long) &ax8817x_info,
  781. }, {
  782. // Buffalo LUA-U2-GT 10/100/1000
  783. USB_DEVICE (0x0411, 0x006e),
  784. .driver_info = (unsigned long) &ax88178_info,
  785. }, {
  786. // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
  787. USB_DEVICE (0x6189, 0x182d),
  788. .driver_info = (unsigned long) &ax8817x_info,
  789. }, {
  790. // Sitecom LN-031 "USB 2.0 10/100/1000 Ethernet adapter"
  791. USB_DEVICE (0x0df6, 0x0056),
  792. .driver_info = (unsigned long) &ax88178_info,
  793. }, {
  794. // corega FEther USB2-TX
  795. USB_DEVICE (0x07aa, 0x0017),
  796. .driver_info = (unsigned long) &ax8817x_info,
  797. }, {
  798. // Surecom EP-1427X-2
  799. USB_DEVICE (0x1189, 0x0893),
  800. .driver_info = (unsigned long) &ax8817x_info,
  801. }, {
  802. // goodway corp usb gwusb2e
  803. USB_DEVICE (0x1631, 0x6200),
  804. .driver_info = (unsigned long) &ax8817x_info,
  805. }, {
  806. // JVC MP-PRX1 Port Replicator
  807. USB_DEVICE (0x04f1, 0x3008),
  808. .driver_info = (unsigned long) &ax8817x_info,
  809. }, {
  810. // Lenovo U2L100P 10/100
  811. USB_DEVICE (0x17ef, 0x7203),
  812. .driver_info = (unsigned long) &ax88772_info,
  813. }, {
  814. // ASIX AX88772B 10/100
  815. USB_DEVICE (0x0b95, 0x772b),
  816. .driver_info = (unsigned long) &ax88772b_info,
  817. }, {
  818. // ASIX AX88772 10/100
  819. USB_DEVICE (0x0b95, 0x7720),
  820. .driver_info = (unsigned long) &ax88772_info,
  821. }, {
  822. // ASIX AX88178 10/100/1000
  823. USB_DEVICE (0x0b95, 0x1780),
  824. .driver_info = (unsigned long) &ax88178_info,
  825. }, {
  826. // Logitec LAN-GTJ/U2A
  827. USB_DEVICE (0x0789, 0x0160),
  828. .driver_info = (unsigned long) &ax88178_info,
  829. }, {
  830. // Linksys USB200M Rev 2
  831. USB_DEVICE (0x13b1, 0x0018),
  832. .driver_info = (unsigned long) &ax88772_info,
  833. }, {
  834. // 0Q0 cable ethernet
  835. USB_DEVICE (0x1557, 0x7720),
  836. .driver_info = (unsigned long) &ax88772_info,
  837. }, {
  838. // DLink DUB-E100 H/W Ver B1
  839. USB_DEVICE (0x07d1, 0x3c05),
  840. .driver_info = (unsigned long) &ax88772_info,
  841. }, {
  842. // DLink DUB-E100 H/W Ver B1 Alternate
  843. USB_DEVICE (0x2001, 0x3c05),
  844. .driver_info = (unsigned long) &ax88772_info,
  845. }, {
  846. // DLink DUB-E100 H/W Ver C1
  847. USB_DEVICE (0x2001, 0x1a02),
  848. .driver_info = (unsigned long) &ax88772_info,
  849. }, {
  850. // Linksys USB1000
  851. USB_DEVICE (0x1737, 0x0039),
  852. .driver_info = (unsigned long) &ax88178_info,
  853. }, {
  854. // IO-DATA ETG-US2
  855. USB_DEVICE (0x04bb, 0x0930),
  856. .driver_info = (unsigned long) &ax88178_info,
  857. }, {
  858. // Belkin F5D5055
  859. USB_DEVICE(0x050d, 0x5055),
  860. .driver_info = (unsigned long) &ax88178_info,
  861. }, {
  862. // Apple USB Ethernet Adapter
  863. USB_DEVICE(0x05ac, 0x1402),
  864. .driver_info = (unsigned long) &ax88772_info,
  865. }, {
  866. // Cables-to-Go USB Ethernet Adapter
  867. USB_DEVICE(0x0b95, 0x772a),
  868. .driver_info = (unsigned long) &ax88772_info,
  869. }, {
  870. // ABOCOM for pci
  871. USB_DEVICE(0x14ea, 0xab11),
  872. .driver_info = (unsigned long) &ax88178_info,
  873. }, {
  874. // ASIX 88772a
  875. USB_DEVICE(0x0db0, 0xa877),
  876. .driver_info = (unsigned long) &ax88772_info,
  877. }, {
  878. // Asus USB Ethernet Adapter
  879. USB_DEVICE (0x0b95, 0x7e2b),
  880. .driver_info = (unsigned long) &ax88772_info,
  881. }, {
  882. /* ASIX 88172a demo board */
  883. USB_DEVICE(0x0b95, 0x172a),
  884. .driver_info = (unsigned long) &ax88172a_info,
  885. },
  886. { }, // END
  887. };
  888. MODULE_DEVICE_TABLE(usb, products);
  889. static struct usb_driver asix_driver = {
  890. .name = DRIVER_NAME,
  891. .id_table = products,
  892. .probe = usbnet_probe,
  893. .suspend = usbnet_suspend,
  894. .resume = usbnet_resume,
  895. .disconnect = usbnet_disconnect,
  896. .supports_autosuspend = 1,
  897. .disable_hub_initiated_lpm = 1,
  898. };
  899. module_usb_driver(asix_driver);
  900. MODULE_AUTHOR("David Hollis");
  901. MODULE_VERSION(DRIVER_VERSION);
  902. MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
  903. MODULE_LICENSE("GPL");