asix.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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. // #define DEBUG // error path messages, extra info
  23. // #define VERBOSE // more; success messages
  24. #include <linux/module.h>
  25. #include <linux/kmod.h>
  26. #include <linux/init.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/mii.h>
  32. #include <linux/usb.h>
  33. #include <linux/crc32.h>
  34. #include <linux/usb/usbnet.h>
  35. #include <linux/slab.h>
  36. #define DRIVER_VERSION "22-Dec-2011"
  37. #define DRIVER_NAME "asix"
  38. /* ASIX AX8817X based USB 2.0 Ethernet Devices */
  39. #define AX_CMD_SET_SW_MII 0x06
  40. #define AX_CMD_READ_MII_REG 0x07
  41. #define AX_CMD_WRITE_MII_REG 0x08
  42. #define AX_CMD_SET_HW_MII 0x0a
  43. #define AX_CMD_READ_EEPROM 0x0b
  44. #define AX_CMD_WRITE_EEPROM 0x0c
  45. #define AX_CMD_WRITE_ENABLE 0x0d
  46. #define AX_CMD_WRITE_DISABLE 0x0e
  47. #define AX_CMD_READ_RX_CTL 0x0f
  48. #define AX_CMD_WRITE_RX_CTL 0x10
  49. #define AX_CMD_READ_IPG012 0x11
  50. #define AX_CMD_WRITE_IPG0 0x12
  51. #define AX_CMD_WRITE_IPG1 0x13
  52. #define AX_CMD_READ_NODE_ID 0x13
  53. #define AX_CMD_WRITE_NODE_ID 0x14
  54. #define AX_CMD_WRITE_IPG2 0x14
  55. #define AX_CMD_WRITE_MULTI_FILTER 0x16
  56. #define AX88172_CMD_READ_NODE_ID 0x17
  57. #define AX_CMD_READ_PHY_ID 0x19
  58. #define AX_CMD_READ_MEDIUM_STATUS 0x1a
  59. #define AX_CMD_WRITE_MEDIUM_MODE 0x1b
  60. #define AX_CMD_READ_MONITOR_MODE 0x1c
  61. #define AX_CMD_WRITE_MONITOR_MODE 0x1d
  62. #define AX_CMD_READ_GPIOS 0x1e
  63. #define AX_CMD_WRITE_GPIOS 0x1f
  64. #define AX_CMD_SW_RESET 0x20
  65. #define AX_CMD_SW_PHY_STATUS 0x21
  66. #define AX_CMD_SW_PHY_SELECT 0x22
  67. #define AX_MONITOR_MODE 0x01
  68. #define AX_MONITOR_LINK 0x02
  69. #define AX_MONITOR_MAGIC 0x04
  70. #define AX_MONITOR_HSFS 0x10
  71. /* AX88172 Medium Status Register values */
  72. #define AX88172_MEDIUM_FD 0x02
  73. #define AX88172_MEDIUM_TX 0x04
  74. #define AX88172_MEDIUM_FC 0x10
  75. #define AX88172_MEDIUM_DEFAULT \
  76. ( AX88172_MEDIUM_FD | AX88172_MEDIUM_TX | AX88172_MEDIUM_FC )
  77. #define AX_MCAST_FILTER_SIZE 8
  78. #define AX_MAX_MCAST 64
  79. #define AX_SWRESET_CLEAR 0x00
  80. #define AX_SWRESET_RR 0x01
  81. #define AX_SWRESET_RT 0x02
  82. #define AX_SWRESET_PRTE 0x04
  83. #define AX_SWRESET_PRL 0x08
  84. #define AX_SWRESET_BZ 0x10
  85. #define AX_SWRESET_IPRL 0x20
  86. #define AX_SWRESET_IPPD 0x40
  87. #define AX88772_IPG0_DEFAULT 0x15
  88. #define AX88772_IPG1_DEFAULT 0x0c
  89. #define AX88772_IPG2_DEFAULT 0x12
  90. /* AX88772 & AX88178 Medium Mode Register */
  91. #define AX_MEDIUM_PF 0x0080
  92. #define AX_MEDIUM_JFE 0x0040
  93. #define AX_MEDIUM_TFC 0x0020
  94. #define AX_MEDIUM_RFC 0x0010
  95. #define AX_MEDIUM_ENCK 0x0008
  96. #define AX_MEDIUM_AC 0x0004
  97. #define AX_MEDIUM_FD 0x0002
  98. #define AX_MEDIUM_GM 0x0001
  99. #define AX_MEDIUM_SM 0x1000
  100. #define AX_MEDIUM_SBP 0x0800
  101. #define AX_MEDIUM_PS 0x0200
  102. #define AX_MEDIUM_RE 0x0100
  103. #define AX88178_MEDIUM_DEFAULT \
  104. (AX_MEDIUM_PS | AX_MEDIUM_FD | AX_MEDIUM_AC | \
  105. AX_MEDIUM_RFC | AX_MEDIUM_TFC | AX_MEDIUM_JFE | \
  106. AX_MEDIUM_RE)
  107. #define AX88772_MEDIUM_DEFAULT \
  108. (AX_MEDIUM_FD | AX_MEDIUM_RFC | \
  109. AX_MEDIUM_TFC | AX_MEDIUM_PS | \
  110. AX_MEDIUM_AC | AX_MEDIUM_RE)
  111. /* AX88772 & AX88178 RX_CTL values */
  112. #define AX_RX_CTL_SO 0x0080
  113. #define AX_RX_CTL_AP 0x0020
  114. #define AX_RX_CTL_AM 0x0010
  115. #define AX_RX_CTL_AB 0x0008
  116. #define AX_RX_CTL_SEP 0x0004
  117. #define AX_RX_CTL_AMALL 0x0002
  118. #define AX_RX_CTL_PRO 0x0001
  119. #define AX_RX_CTL_MFB_2048 0x0000
  120. #define AX_RX_CTL_MFB_4096 0x0100
  121. #define AX_RX_CTL_MFB_8192 0x0200
  122. #define AX_RX_CTL_MFB_16384 0x0300
  123. #define AX_DEFAULT_RX_CTL (AX_RX_CTL_SO | AX_RX_CTL_AB)
  124. /* GPIO 0 .. 2 toggles */
  125. #define AX_GPIO_GPO0EN 0x01 /* GPIO0 Output enable */
  126. #define AX_GPIO_GPO_0 0x02 /* GPIO0 Output value */
  127. #define AX_GPIO_GPO1EN 0x04 /* GPIO1 Output enable */
  128. #define AX_GPIO_GPO_1 0x08 /* GPIO1 Output value */
  129. #define AX_GPIO_GPO2EN 0x10 /* GPIO2 Output enable */
  130. #define AX_GPIO_GPO_2 0x20 /* GPIO2 Output value */
  131. #define AX_GPIO_RESERVED 0x40 /* Reserved */
  132. #define AX_GPIO_RSE 0x80 /* Reload serial EEPROM */
  133. #define AX_EEPROM_MAGIC 0xdeadbeef
  134. #define AX88172_EEPROM_LEN 0x40
  135. #define AX88772_EEPROM_LEN 0xff
  136. #define PHY_MODE_MARVELL 0x0000
  137. #define MII_MARVELL_LED_CTRL 0x0018
  138. #define MII_MARVELL_STATUS 0x001b
  139. #define MII_MARVELL_CTRL 0x0014
  140. #define MARVELL_LED_MANUAL 0x0019
  141. #define MARVELL_STATUS_HWCFG 0x0004
  142. #define MARVELL_CTRL_TXDELAY 0x0002
  143. #define MARVELL_CTRL_RXDELAY 0x0080
  144. #define PHY_MODE_RTL8211CL 0x000C
  145. /* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */
  146. struct asix_data {
  147. u8 multi_filter[AX_MCAST_FILTER_SIZE];
  148. u8 mac_addr[ETH_ALEN];
  149. u8 phymode;
  150. u8 ledmode;
  151. u8 eeprom_len;
  152. };
  153. struct ax88172_int_data {
  154. __le16 res1;
  155. u8 link;
  156. __le16 res2;
  157. u8 status;
  158. __le16 res3;
  159. } __packed;
  160. static int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  161. u16 size, void *data)
  162. {
  163. void *buf;
  164. int err = -ENOMEM;
  165. netdev_dbg(dev->net, "asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
  166. cmd, value, index, size);
  167. buf = kmalloc(size, GFP_KERNEL);
  168. if (!buf)
  169. goto out;
  170. err = usb_control_msg(
  171. dev->udev,
  172. usb_rcvctrlpipe(dev->udev, 0),
  173. cmd,
  174. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  175. value,
  176. index,
  177. buf,
  178. size,
  179. USB_CTRL_GET_TIMEOUT);
  180. if (err == size)
  181. memcpy(data, buf, size);
  182. else if (err >= 0)
  183. err = -EINVAL;
  184. kfree(buf);
  185. out:
  186. return err;
  187. }
  188. static int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  189. u16 size, void *data)
  190. {
  191. void *buf = NULL;
  192. int err = -ENOMEM;
  193. netdev_dbg(dev->net, "asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
  194. cmd, value, index, size);
  195. if (data) {
  196. buf = kmemdup(data, size, GFP_KERNEL);
  197. if (!buf)
  198. goto out;
  199. }
  200. err = usb_control_msg(
  201. dev->udev,
  202. usb_sndctrlpipe(dev->udev, 0),
  203. cmd,
  204. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  205. value,
  206. index,
  207. buf,
  208. size,
  209. USB_CTRL_SET_TIMEOUT);
  210. kfree(buf);
  211. out:
  212. return err;
  213. }
  214. static void asix_async_cmd_callback(struct urb *urb)
  215. {
  216. struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
  217. int status = urb->status;
  218. if (status < 0)
  219. printk(KERN_DEBUG "asix_async_cmd_callback() failed with %d",
  220. status);
  221. kfree(req);
  222. usb_free_urb(urb);
  223. }
  224. static void
  225. asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  226. u16 size, void *data)
  227. {
  228. struct usb_ctrlrequest *req;
  229. int status;
  230. struct urb *urb;
  231. netdev_dbg(dev->net, "asix_write_cmd_async() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
  232. cmd, value, index, size);
  233. urb = usb_alloc_urb(0, GFP_ATOMIC);
  234. if (!urb) {
  235. netdev_err(dev->net, "Error allocating URB in write_cmd_async!\n");
  236. return;
  237. }
  238. req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
  239. if (!req) {
  240. netdev_err(dev->net, "Failed to allocate memory for control request\n");
  241. usb_free_urb(urb);
  242. return;
  243. }
  244. req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
  245. req->bRequest = cmd;
  246. req->wValue = cpu_to_le16(value);
  247. req->wIndex = cpu_to_le16(index);
  248. req->wLength = cpu_to_le16(size);
  249. usb_fill_control_urb(urb, dev->udev,
  250. usb_sndctrlpipe(dev->udev, 0),
  251. (void *)req, data, size,
  252. asix_async_cmd_callback, req);
  253. status = usb_submit_urb(urb, GFP_ATOMIC);
  254. if (status < 0) {
  255. netdev_err(dev->net, "Error submitting the control message: status=%d\n",
  256. status);
  257. kfree(req);
  258. usb_free_urb(urb);
  259. }
  260. }
  261. static int asix_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  262. {
  263. int offset = 0;
  264. while (offset + sizeof(u32) < skb->len) {
  265. struct sk_buff *ax_skb;
  266. u16 size;
  267. u32 header = get_unaligned_le32(skb->data + offset);
  268. offset += sizeof(u32);
  269. /* get the packet length */
  270. size = (u16) (header & 0x7ff);
  271. if (size != ((~header >> 16) & 0x07ff)) {
  272. netdev_err(dev->net, "asix_rx_fixup() Bad Header Length\n");
  273. return 0;
  274. }
  275. if ((size > dev->net->mtu + ETH_HLEN) ||
  276. (size + offset > skb->len)) {
  277. netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
  278. size);
  279. return 0;
  280. }
  281. ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
  282. if (!ax_skb)
  283. return 0;
  284. skb_put(ax_skb, size);
  285. memcpy(ax_skb->data, skb->data + offset, size);
  286. usbnet_skb_return(dev, ax_skb);
  287. offset += (size + 1) & 0xfffe;
  288. }
  289. if (skb->len != offset) {
  290. netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d\n",
  291. skb->len);
  292. return 0;
  293. }
  294. return 1;
  295. }
  296. static struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  297. gfp_t flags)
  298. {
  299. int padlen;
  300. int headroom = skb_headroom(skb);
  301. int tailroom = skb_tailroom(skb);
  302. u32 packet_len;
  303. u32 padbytes = 0xffff0000;
  304. padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
  305. if ((!skb_cloned(skb)) &&
  306. ((headroom + tailroom) >= (4 + padlen))) {
  307. if ((headroom < 4) || (tailroom < padlen)) {
  308. skb->data = memmove(skb->head + 4, skb->data, skb->len);
  309. skb_set_tail_pointer(skb, skb->len);
  310. }
  311. } else {
  312. struct sk_buff *skb2;
  313. skb2 = skb_copy_expand(skb, 4, padlen, flags);
  314. dev_kfree_skb_any(skb);
  315. skb = skb2;
  316. if (!skb)
  317. return NULL;
  318. }
  319. skb_push(skb, 4);
  320. packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
  321. cpu_to_le32s(&packet_len);
  322. skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
  323. if (padlen) {
  324. cpu_to_le32s(&padbytes);
  325. memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
  326. skb_put(skb, sizeof(padbytes));
  327. }
  328. return skb;
  329. }
  330. static void asix_status(struct usbnet *dev, struct urb *urb)
  331. {
  332. struct ax88172_int_data *event;
  333. int link;
  334. if (urb->actual_length < 8)
  335. return;
  336. event = urb->transfer_buffer;
  337. link = event->link & 0x01;
  338. if (netif_carrier_ok(dev->net) != link) {
  339. if (link) {
  340. netif_carrier_on(dev->net);
  341. usbnet_defer_kevent (dev, EVENT_LINK_RESET );
  342. } else
  343. netif_carrier_off(dev->net);
  344. netdev_dbg(dev->net, "Link Status is: %d\n", link);
  345. }
  346. }
  347. static inline int asix_set_sw_mii(struct usbnet *dev)
  348. {
  349. int ret;
  350. ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
  351. if (ret < 0)
  352. netdev_err(dev->net, "Failed to enable software MII access\n");
  353. return ret;
  354. }
  355. static inline int asix_set_hw_mii(struct usbnet *dev)
  356. {
  357. int ret;
  358. ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
  359. if (ret < 0)
  360. netdev_err(dev->net, "Failed to enable hardware MII access\n");
  361. return ret;
  362. }
  363. static inline int asix_get_phy_addr(struct usbnet *dev)
  364. {
  365. u8 buf[2];
  366. int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
  367. netdev_dbg(dev->net, "asix_get_phy_addr()\n");
  368. if (ret < 0) {
  369. netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
  370. goto out;
  371. }
  372. netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
  373. *((__le16 *)buf));
  374. ret = buf[1];
  375. out:
  376. return ret;
  377. }
  378. static int asix_sw_reset(struct usbnet *dev, u8 flags)
  379. {
  380. int ret;
  381. ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
  382. if (ret < 0)
  383. netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
  384. return ret;
  385. }
  386. static u16 asix_read_rx_ctl(struct usbnet *dev)
  387. {
  388. __le16 v;
  389. int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
  390. if (ret < 0) {
  391. netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
  392. goto out;
  393. }
  394. ret = le16_to_cpu(v);
  395. out:
  396. return ret;
  397. }
  398. static int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
  399. {
  400. int ret;
  401. netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
  402. ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
  403. if (ret < 0)
  404. netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
  405. mode, ret);
  406. return ret;
  407. }
  408. static u16 asix_read_medium_status(struct usbnet *dev)
  409. {
  410. __le16 v;
  411. int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
  412. if (ret < 0) {
  413. netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
  414. ret);
  415. return ret; /* TODO: callers not checking for error ret */
  416. }
  417. return le16_to_cpu(v);
  418. }
  419. static int asix_write_medium_mode(struct usbnet *dev, u16 mode)
  420. {
  421. int ret;
  422. netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
  423. ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
  424. if (ret < 0)
  425. netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
  426. mode, ret);
  427. return ret;
  428. }
  429. static int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
  430. {
  431. int ret;
  432. netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
  433. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
  434. if (ret < 0)
  435. netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
  436. value, ret);
  437. if (sleep)
  438. msleep(sleep);
  439. return ret;
  440. }
  441. /*
  442. * AX88772 & AX88178 have a 16-bit RX_CTL value
  443. */
  444. static void asix_set_multicast(struct net_device *net)
  445. {
  446. struct usbnet *dev = netdev_priv(net);
  447. struct asix_data *data = (struct asix_data *)&dev->data;
  448. u16 rx_ctl = AX_DEFAULT_RX_CTL;
  449. if (net->flags & IFF_PROMISC) {
  450. rx_ctl |= AX_RX_CTL_PRO;
  451. } else if (net->flags & IFF_ALLMULTI ||
  452. netdev_mc_count(net) > AX_MAX_MCAST) {
  453. rx_ctl |= AX_RX_CTL_AMALL;
  454. } else if (netdev_mc_empty(net)) {
  455. /* just broadcast and directed */
  456. } else {
  457. /* We use the 20 byte dev->data
  458. * for our 8 byte filter buffer
  459. * to avoid allocating memory that
  460. * is tricky to free later */
  461. struct netdev_hw_addr *ha;
  462. u32 crc_bits;
  463. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  464. /* Build the multicast hash filter. */
  465. netdev_for_each_mc_addr(ha, net) {
  466. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  467. data->multi_filter[crc_bits >> 3] |=
  468. 1 << (crc_bits & 7);
  469. }
  470. asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  471. AX_MCAST_FILTER_SIZE, data->multi_filter);
  472. rx_ctl |= AX_RX_CTL_AM;
  473. }
  474. asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  475. }
  476. static int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
  477. {
  478. struct usbnet *dev = netdev_priv(netdev);
  479. __le16 res;
  480. mutex_lock(&dev->phy_mutex);
  481. asix_set_sw_mii(dev);
  482. asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
  483. (__u16)loc, 2, &res);
  484. asix_set_hw_mii(dev);
  485. mutex_unlock(&dev->phy_mutex);
  486. netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
  487. phy_id, loc, le16_to_cpu(res));
  488. return le16_to_cpu(res);
  489. }
  490. static void
  491. asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
  492. {
  493. struct usbnet *dev = netdev_priv(netdev);
  494. __le16 res = cpu_to_le16(val);
  495. netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
  496. phy_id, loc, val);
  497. mutex_lock(&dev->phy_mutex);
  498. asix_set_sw_mii(dev);
  499. asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
  500. asix_set_hw_mii(dev);
  501. mutex_unlock(&dev->phy_mutex);
  502. }
  503. /* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */
  504. static u32 asix_get_phyid(struct usbnet *dev)
  505. {
  506. int phy_reg;
  507. u32 phy_id;
  508. int i;
  509. /* Poll for the rare case the FW or phy isn't ready yet. */
  510. for (i = 0; i < 100; i++) {
  511. phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1);
  512. if (phy_reg != 0 && phy_reg != 0xFFFF)
  513. break;
  514. mdelay(1);
  515. }
  516. if (phy_reg <= 0 || phy_reg == 0xFFFF)
  517. return 0;
  518. phy_id = (phy_reg & 0xffff) << 16;
  519. phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID2);
  520. if (phy_reg < 0)
  521. return 0;
  522. phy_id |= (phy_reg & 0xffff);
  523. return phy_id;
  524. }
  525. static void
  526. asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  527. {
  528. struct usbnet *dev = netdev_priv(net);
  529. u8 opt;
  530. if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
  531. wolinfo->supported = 0;
  532. wolinfo->wolopts = 0;
  533. return;
  534. }
  535. wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
  536. wolinfo->wolopts = 0;
  537. if (opt & AX_MONITOR_LINK)
  538. wolinfo->wolopts |= WAKE_PHY;
  539. if (opt & AX_MONITOR_MAGIC)
  540. wolinfo->wolopts |= WAKE_MAGIC;
  541. }
  542. static int
  543. asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  544. {
  545. struct usbnet *dev = netdev_priv(net);
  546. u8 opt = 0;
  547. if (wolinfo->wolopts & WAKE_PHY)
  548. opt |= AX_MONITOR_LINK;
  549. if (wolinfo->wolopts & WAKE_MAGIC)
  550. opt |= AX_MONITOR_MAGIC;
  551. if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
  552. opt, 0, 0, NULL) < 0)
  553. return -EINVAL;
  554. return 0;
  555. }
  556. static int asix_get_eeprom_len(struct net_device *net)
  557. {
  558. struct usbnet *dev = netdev_priv(net);
  559. struct asix_data *data = (struct asix_data *)&dev->data;
  560. return data->eeprom_len;
  561. }
  562. static int asix_get_eeprom(struct net_device *net,
  563. struct ethtool_eeprom *eeprom, u8 *data)
  564. {
  565. struct usbnet *dev = netdev_priv(net);
  566. __le16 *ebuf = (__le16 *)data;
  567. int i;
  568. /* Crude hack to ensure that we don't overwrite memory
  569. * if an odd length is supplied
  570. */
  571. if (eeprom->len % 2)
  572. return -EINVAL;
  573. eeprom->magic = AX_EEPROM_MAGIC;
  574. /* ax8817x returns 2 bytes from eeprom on read */
  575. for (i=0; i < eeprom->len / 2; i++) {
  576. if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
  577. eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
  578. return -EINVAL;
  579. }
  580. return 0;
  581. }
  582. static void asix_get_drvinfo (struct net_device *net,
  583. struct ethtool_drvinfo *info)
  584. {
  585. struct usbnet *dev = netdev_priv(net);
  586. struct asix_data *data = (struct asix_data *)&dev->data;
  587. /* Inherit standard device info */
  588. usbnet_get_drvinfo(net, info);
  589. strncpy (info->driver, DRIVER_NAME, sizeof info->driver);
  590. strncpy (info->version, DRIVER_VERSION, sizeof info->version);
  591. info->eedump_len = data->eeprom_len;
  592. }
  593. static u32 asix_get_link(struct net_device *net)
  594. {
  595. struct usbnet *dev = netdev_priv(net);
  596. return mii_link_ok(&dev->mii);
  597. }
  598. static int asix_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
  599. {
  600. struct usbnet *dev = netdev_priv(net);
  601. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  602. }
  603. static int asix_set_mac_address(struct net_device *net, void *p)
  604. {
  605. struct usbnet *dev = netdev_priv(net);
  606. struct asix_data *data = (struct asix_data *)&dev->data;
  607. struct sockaddr *addr = p;
  608. if (netif_running(net))
  609. return -EBUSY;
  610. if (!is_valid_ether_addr(addr->sa_data))
  611. return -EADDRNOTAVAIL;
  612. memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
  613. /* We use the 20 byte dev->data
  614. * for our 6 byte mac buffer
  615. * to avoid allocating memory that
  616. * is tricky to free later */
  617. memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
  618. asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  619. data->mac_addr);
  620. return 0;
  621. }
  622. /* We need to override some ethtool_ops so we require our
  623. own structure so we don't interfere with other usbnet
  624. devices that may be connected at the same time. */
  625. static const struct ethtool_ops ax88172_ethtool_ops = {
  626. .get_drvinfo = asix_get_drvinfo,
  627. .get_link = asix_get_link,
  628. .get_msglevel = usbnet_get_msglevel,
  629. .set_msglevel = usbnet_set_msglevel,
  630. .get_wol = asix_get_wol,
  631. .set_wol = asix_set_wol,
  632. .get_eeprom_len = asix_get_eeprom_len,
  633. .get_eeprom = asix_get_eeprom,
  634. .get_settings = usbnet_get_settings,
  635. .set_settings = usbnet_set_settings,
  636. .nway_reset = usbnet_nway_reset,
  637. };
  638. static void ax88172_set_multicast(struct net_device *net)
  639. {
  640. struct usbnet *dev = netdev_priv(net);
  641. struct asix_data *data = (struct asix_data *)&dev->data;
  642. u8 rx_ctl = 0x8c;
  643. if (net->flags & IFF_PROMISC) {
  644. rx_ctl |= 0x01;
  645. } else if (net->flags & IFF_ALLMULTI ||
  646. netdev_mc_count(net) > AX_MAX_MCAST) {
  647. rx_ctl |= 0x02;
  648. } else if (netdev_mc_empty(net)) {
  649. /* just broadcast and directed */
  650. } else {
  651. /* We use the 20 byte dev->data
  652. * for our 8 byte filter buffer
  653. * to avoid allocating memory that
  654. * is tricky to free later */
  655. struct netdev_hw_addr *ha;
  656. u32 crc_bits;
  657. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  658. /* Build the multicast hash filter. */
  659. netdev_for_each_mc_addr(ha, net) {
  660. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  661. data->multi_filter[crc_bits >> 3] |=
  662. 1 << (crc_bits & 7);
  663. }
  664. asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  665. AX_MCAST_FILTER_SIZE, data->multi_filter);
  666. rx_ctl |= 0x10;
  667. }
  668. asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  669. }
  670. static int ax88172_link_reset(struct usbnet *dev)
  671. {
  672. u8 mode;
  673. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  674. mii_check_media(&dev->mii, 1, 1);
  675. mii_ethtool_gset(&dev->mii, &ecmd);
  676. mode = AX88172_MEDIUM_DEFAULT;
  677. if (ecmd.duplex != DUPLEX_FULL)
  678. mode |= ~AX88172_MEDIUM_FD;
  679. netdev_dbg(dev->net, "ax88172_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  680. ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
  681. asix_write_medium_mode(dev, mode);
  682. return 0;
  683. }
  684. static const struct net_device_ops ax88172_netdev_ops = {
  685. .ndo_open = usbnet_open,
  686. .ndo_stop = usbnet_stop,
  687. .ndo_start_xmit = usbnet_start_xmit,
  688. .ndo_tx_timeout = usbnet_tx_timeout,
  689. .ndo_change_mtu = usbnet_change_mtu,
  690. .ndo_set_mac_address = eth_mac_addr,
  691. .ndo_validate_addr = eth_validate_addr,
  692. .ndo_do_ioctl = asix_ioctl,
  693. .ndo_set_rx_mode = ax88172_set_multicast,
  694. };
  695. static int ax88172_bind(struct usbnet *dev, struct usb_interface *intf)
  696. {
  697. int ret = 0;
  698. u8 buf[ETH_ALEN];
  699. int i;
  700. unsigned long gpio_bits = dev->driver_info->data;
  701. struct asix_data *data = (struct asix_data *)&dev->data;
  702. data->eeprom_len = AX88172_EEPROM_LEN;
  703. usbnet_get_endpoints(dev,intf);
  704. /* Toggle the GPIOs in a manufacturer/model specific way */
  705. for (i = 2; i >= 0; i--) {
  706. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
  707. (gpio_bits >> (i * 8)) & 0xff, 0, 0, NULL);
  708. if (ret < 0)
  709. goto out;
  710. msleep(5);
  711. }
  712. ret = asix_write_rx_ctl(dev, 0x80);
  713. if (ret < 0)
  714. goto out;
  715. /* Get the MAC address */
  716. ret = asix_read_cmd(dev, AX88172_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  717. if (ret < 0) {
  718. dbg("read AX_CMD_READ_NODE_ID failed: %d", ret);
  719. goto out;
  720. }
  721. memcpy(dev->net->dev_addr, buf, ETH_ALEN);
  722. /* Initialize MII structure */
  723. dev->mii.dev = dev->net;
  724. dev->mii.mdio_read = asix_mdio_read;
  725. dev->mii.mdio_write = asix_mdio_write;
  726. dev->mii.phy_id_mask = 0x3f;
  727. dev->mii.reg_num_mask = 0x1f;
  728. dev->mii.phy_id = asix_get_phy_addr(dev);
  729. dev->net->netdev_ops = &ax88172_netdev_ops;
  730. dev->net->ethtool_ops = &ax88172_ethtool_ops;
  731. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  732. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  733. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  734. mii_nway_restart(&dev->mii);
  735. return 0;
  736. out:
  737. return ret;
  738. }
  739. static const struct ethtool_ops ax88772_ethtool_ops = {
  740. .get_drvinfo = asix_get_drvinfo,
  741. .get_link = asix_get_link,
  742. .get_msglevel = usbnet_get_msglevel,
  743. .set_msglevel = usbnet_set_msglevel,
  744. .get_wol = asix_get_wol,
  745. .set_wol = asix_set_wol,
  746. .get_eeprom_len = asix_get_eeprom_len,
  747. .get_eeprom = asix_get_eeprom,
  748. .get_settings = usbnet_get_settings,
  749. .set_settings = usbnet_set_settings,
  750. .nway_reset = usbnet_nway_reset,
  751. };
  752. static int ax88772_link_reset(struct usbnet *dev)
  753. {
  754. u16 mode;
  755. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  756. mii_check_media(&dev->mii, 1, 1);
  757. mii_ethtool_gset(&dev->mii, &ecmd);
  758. mode = AX88772_MEDIUM_DEFAULT;
  759. if (ethtool_cmd_speed(&ecmd) != SPEED_100)
  760. mode &= ~AX_MEDIUM_PS;
  761. if (ecmd.duplex != DUPLEX_FULL)
  762. mode &= ~AX_MEDIUM_FD;
  763. netdev_dbg(dev->net, "ax88772_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  764. ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
  765. asix_write_medium_mode(dev, mode);
  766. return 0;
  767. }
  768. static int ax88772_reset(struct usbnet *dev)
  769. {
  770. struct asix_data *data = (struct asix_data *)&dev->data;
  771. int ret, embd_phy;
  772. u16 rx_ctl;
  773. ret = asix_write_gpio(dev,
  774. AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5);
  775. if (ret < 0)
  776. goto out;
  777. embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
  778. ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
  779. if (ret < 0) {
  780. dbg("Select PHY #1 failed: %d", ret);
  781. goto out;
  782. }
  783. ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL);
  784. if (ret < 0)
  785. goto out;
  786. msleep(150);
  787. ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
  788. if (ret < 0)
  789. goto out;
  790. msleep(150);
  791. if (embd_phy) {
  792. ret = asix_sw_reset(dev, AX_SWRESET_IPRL);
  793. if (ret < 0)
  794. goto out;
  795. } else {
  796. ret = asix_sw_reset(dev, AX_SWRESET_PRTE);
  797. if (ret < 0)
  798. goto out;
  799. }
  800. msleep(150);
  801. rx_ctl = asix_read_rx_ctl(dev);
  802. dbg("RX_CTL is 0x%04x after software reset", rx_ctl);
  803. ret = asix_write_rx_ctl(dev, 0x0000);
  804. if (ret < 0)
  805. goto out;
  806. rx_ctl = asix_read_rx_ctl(dev);
  807. dbg("RX_CTL is 0x%04x setting to 0x0000", rx_ctl);
  808. ret = asix_sw_reset(dev, AX_SWRESET_PRL);
  809. if (ret < 0)
  810. goto out;
  811. msleep(150);
  812. ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL);
  813. if (ret < 0)
  814. goto out;
  815. msleep(150);
  816. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  817. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  818. ADVERTISE_ALL | ADVERTISE_CSMA);
  819. mii_nway_restart(&dev->mii);
  820. ret = asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT);
  821. if (ret < 0)
  822. goto out;
  823. ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
  824. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  825. AX88772_IPG2_DEFAULT, 0, NULL);
  826. if (ret < 0) {
  827. dbg("Write IPG,IPG1,IPG2 failed: %d", ret);
  828. goto out;
  829. }
  830. /* Rewrite MAC address */
  831. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  832. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  833. data->mac_addr);
  834. if (ret < 0)
  835. goto out;
  836. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  837. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
  838. if (ret < 0)
  839. goto out;
  840. rx_ctl = asix_read_rx_ctl(dev);
  841. dbg("RX_CTL is 0x%04x after all initializations", rx_ctl);
  842. rx_ctl = asix_read_medium_status(dev);
  843. dbg("Medium Status is 0x%04x after all initializations", rx_ctl);
  844. return 0;
  845. out:
  846. return ret;
  847. }
  848. static const struct net_device_ops ax88772_netdev_ops = {
  849. .ndo_open = usbnet_open,
  850. .ndo_stop = usbnet_stop,
  851. .ndo_start_xmit = usbnet_start_xmit,
  852. .ndo_tx_timeout = usbnet_tx_timeout,
  853. .ndo_change_mtu = usbnet_change_mtu,
  854. .ndo_set_mac_address = asix_set_mac_address,
  855. .ndo_validate_addr = eth_validate_addr,
  856. .ndo_do_ioctl = asix_ioctl,
  857. .ndo_set_rx_mode = asix_set_multicast,
  858. };
  859. static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
  860. {
  861. int ret, embd_phy;
  862. struct asix_data *data = (struct asix_data *)&dev->data;
  863. u8 buf[ETH_ALEN];
  864. u32 phyid;
  865. data->eeprom_len = AX88772_EEPROM_LEN;
  866. usbnet_get_endpoints(dev,intf);
  867. /* Get the MAC address */
  868. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  869. if (ret < 0) {
  870. dbg("Failed to read MAC address: %d", ret);
  871. return ret;
  872. }
  873. memcpy(dev->net->dev_addr, buf, ETH_ALEN);
  874. /* Initialize MII structure */
  875. dev->mii.dev = dev->net;
  876. dev->mii.mdio_read = asix_mdio_read;
  877. dev->mii.mdio_write = asix_mdio_write;
  878. dev->mii.phy_id_mask = 0x1f;
  879. dev->mii.reg_num_mask = 0x1f;
  880. dev->mii.phy_id = asix_get_phy_addr(dev);
  881. dev->net->netdev_ops = &ax88772_netdev_ops;
  882. dev->net->ethtool_ops = &ax88772_ethtool_ops;
  883. embd_phy = ((dev->mii.phy_id & 0x1f) == 0x10 ? 1 : 0);
  884. /* Reset the PHY to normal operation mode */
  885. ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
  886. if (ret < 0) {
  887. dbg("Select PHY #1 failed: %d", ret);
  888. return ret;
  889. }
  890. ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL);
  891. if (ret < 0)
  892. return ret;
  893. msleep(150);
  894. ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
  895. if (ret < 0)
  896. return ret;
  897. msleep(150);
  898. ret = asix_sw_reset(dev, embd_phy ? AX_SWRESET_IPRL : AX_SWRESET_PRTE);
  899. /* Read PHYID register *AFTER* the PHY was reset properly */
  900. phyid = asix_get_phyid(dev);
  901. dbg("PHYID=0x%08x", phyid);
  902. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  903. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  904. /* hard_mtu is still the default - the device does not support
  905. jumbo eth frames */
  906. dev->rx_urb_size = 2048;
  907. }
  908. return 0;
  909. }
  910. static const struct ethtool_ops ax88178_ethtool_ops = {
  911. .get_drvinfo = asix_get_drvinfo,
  912. .get_link = asix_get_link,
  913. .get_msglevel = usbnet_get_msglevel,
  914. .set_msglevel = usbnet_set_msglevel,
  915. .get_wol = asix_get_wol,
  916. .set_wol = asix_set_wol,
  917. .get_eeprom_len = asix_get_eeprom_len,
  918. .get_eeprom = asix_get_eeprom,
  919. .get_settings = usbnet_get_settings,
  920. .set_settings = usbnet_set_settings,
  921. .nway_reset = usbnet_nway_reset,
  922. };
  923. static int marvell_phy_init(struct usbnet *dev)
  924. {
  925. struct asix_data *data = (struct asix_data *)&dev->data;
  926. u16 reg;
  927. netdev_dbg(dev->net, "marvell_phy_init()\n");
  928. reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_MARVELL_STATUS);
  929. netdev_dbg(dev->net, "MII_MARVELL_STATUS = 0x%04x\n", reg);
  930. asix_mdio_write(dev->net, dev->mii.phy_id, MII_MARVELL_CTRL,
  931. MARVELL_CTRL_RXDELAY | MARVELL_CTRL_TXDELAY);
  932. if (data->ledmode) {
  933. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  934. MII_MARVELL_LED_CTRL);
  935. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (1) = 0x%04x\n", reg);
  936. reg &= 0xf8ff;
  937. reg |= (1 + 0x0100);
  938. asix_mdio_write(dev->net, dev->mii.phy_id,
  939. MII_MARVELL_LED_CTRL, reg);
  940. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  941. MII_MARVELL_LED_CTRL);
  942. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (2) = 0x%04x\n", reg);
  943. reg &= 0xfc0f;
  944. }
  945. return 0;
  946. }
  947. static int rtl8211cl_phy_init(struct usbnet *dev)
  948. {
  949. struct asix_data *data = (struct asix_data *)&dev->data;
  950. netdev_dbg(dev->net, "rtl8211cl_phy_init()\n");
  951. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0005);
  952. asix_mdio_write (dev->net, dev->mii.phy_id, 0x0c, 0);
  953. asix_mdio_write (dev->net, dev->mii.phy_id, 0x01,
  954. asix_mdio_read (dev->net, dev->mii.phy_id, 0x01) | 0x0080);
  955. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  956. if (data->ledmode == 12) {
  957. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0002);
  958. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1a, 0x00cb);
  959. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  960. }
  961. return 0;
  962. }
  963. static int marvell_led_status(struct usbnet *dev, u16 speed)
  964. {
  965. u16 reg = asix_mdio_read(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL);
  966. netdev_dbg(dev->net, "marvell_led_status() read 0x%04x\n", reg);
  967. /* Clear out the center LED bits - 0x03F0 */
  968. reg &= 0xfc0f;
  969. switch (speed) {
  970. case SPEED_1000:
  971. reg |= 0x03e0;
  972. break;
  973. case SPEED_100:
  974. reg |= 0x03b0;
  975. break;
  976. default:
  977. reg |= 0x02f0;
  978. }
  979. netdev_dbg(dev->net, "marvell_led_status() writing 0x%04x\n", reg);
  980. asix_mdio_write(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL, reg);
  981. return 0;
  982. }
  983. static int ax88178_reset(struct usbnet *dev)
  984. {
  985. struct asix_data *data = (struct asix_data *)&dev->data;
  986. int ret;
  987. __le16 eeprom;
  988. u8 status;
  989. int gpio0 = 0;
  990. u32 phyid;
  991. asix_read_cmd(dev, AX_CMD_READ_GPIOS, 0, 0, 1, &status);
  992. dbg("GPIO Status: 0x%04x", status);
  993. asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0, 0, 0, NULL);
  994. asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2, &eeprom);
  995. asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0, 0, 0, NULL);
  996. dbg("EEPROM index 0x17 is 0x%04x", eeprom);
  997. if (eeprom == cpu_to_le16(0xffff)) {
  998. data->phymode = PHY_MODE_MARVELL;
  999. data->ledmode = 0;
  1000. gpio0 = 1;
  1001. } else {
  1002. data->phymode = le16_to_cpu(eeprom) & 0x7F;
  1003. data->ledmode = le16_to_cpu(eeprom) >> 8;
  1004. gpio0 = (le16_to_cpu(eeprom) & 0x80) ? 0 : 1;
  1005. }
  1006. dbg("GPIO0: %d, PhyMode: %d", gpio0, data->phymode);
  1007. /* Power up external GigaPHY through AX88178 GPIO pin */
  1008. asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_1 | AX_GPIO_GPO1EN, 40);
  1009. if ((le16_to_cpu(eeprom) >> 8) != 1) {
  1010. asix_write_gpio(dev, 0x003c, 30);
  1011. asix_write_gpio(dev, 0x001c, 300);
  1012. asix_write_gpio(dev, 0x003c, 30);
  1013. } else {
  1014. dbg("gpio phymode == 1 path");
  1015. asix_write_gpio(dev, AX_GPIO_GPO1EN, 30);
  1016. asix_write_gpio(dev, AX_GPIO_GPO1EN | AX_GPIO_GPO_1, 30);
  1017. }
  1018. /* Read PHYID register *AFTER* powering up PHY */
  1019. phyid = asix_get_phyid(dev);
  1020. dbg("PHYID=0x%08x", phyid);
  1021. /* Set AX88178 to enable MII/GMII/RGMII interface for external PHY */
  1022. asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 0, 0, 0, NULL);
  1023. asix_sw_reset(dev, 0);
  1024. msleep(150);
  1025. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
  1026. msleep(150);
  1027. asix_write_rx_ctl(dev, 0);
  1028. if (data->phymode == PHY_MODE_MARVELL) {
  1029. marvell_phy_init(dev);
  1030. msleep(60);
  1031. } else if (data->phymode == PHY_MODE_RTL8211CL)
  1032. rtl8211cl_phy_init(dev);
  1033. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR,
  1034. BMCR_RESET | BMCR_ANENABLE);
  1035. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  1036. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  1037. asix_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
  1038. ADVERTISE_1000FULL);
  1039. mii_nway_restart(&dev->mii);
  1040. ret = asix_write_medium_mode(dev, AX88178_MEDIUM_DEFAULT);
  1041. if (ret < 0)
  1042. return ret;
  1043. /* Rewrite MAC address */
  1044. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  1045. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  1046. data->mac_addr);
  1047. if (ret < 0)
  1048. return ret;
  1049. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
  1050. if (ret < 0)
  1051. return ret;
  1052. return 0;
  1053. }
  1054. static int ax88178_link_reset(struct usbnet *dev)
  1055. {
  1056. u16 mode;
  1057. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  1058. struct asix_data *data = (struct asix_data *)&dev->data;
  1059. u32 speed;
  1060. netdev_dbg(dev->net, "ax88178_link_reset()\n");
  1061. mii_check_media(&dev->mii, 1, 1);
  1062. mii_ethtool_gset(&dev->mii, &ecmd);
  1063. mode = AX88178_MEDIUM_DEFAULT;
  1064. speed = ethtool_cmd_speed(&ecmd);
  1065. if (speed == SPEED_1000)
  1066. mode |= AX_MEDIUM_GM;
  1067. else if (speed == SPEED_100)
  1068. mode |= AX_MEDIUM_PS;
  1069. else
  1070. mode &= ~(AX_MEDIUM_PS | AX_MEDIUM_GM);
  1071. mode |= AX_MEDIUM_ENCK;
  1072. if (ecmd.duplex == DUPLEX_FULL)
  1073. mode |= AX_MEDIUM_FD;
  1074. else
  1075. mode &= ~AX_MEDIUM_FD;
  1076. netdev_dbg(dev->net, "ax88178_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  1077. speed, ecmd.duplex, mode);
  1078. asix_write_medium_mode(dev, mode);
  1079. if (data->phymode == PHY_MODE_MARVELL && data->ledmode)
  1080. marvell_led_status(dev, speed);
  1081. return 0;
  1082. }
  1083. static void ax88178_set_mfb(struct usbnet *dev)
  1084. {
  1085. u16 mfb = AX_RX_CTL_MFB_16384;
  1086. u16 rxctl;
  1087. u16 medium;
  1088. int old_rx_urb_size = dev->rx_urb_size;
  1089. if (dev->hard_mtu < 2048) {
  1090. dev->rx_urb_size = 2048;
  1091. mfb = AX_RX_CTL_MFB_2048;
  1092. } else if (dev->hard_mtu < 4096) {
  1093. dev->rx_urb_size = 4096;
  1094. mfb = AX_RX_CTL_MFB_4096;
  1095. } else if (dev->hard_mtu < 8192) {
  1096. dev->rx_urb_size = 8192;
  1097. mfb = AX_RX_CTL_MFB_8192;
  1098. } else if (dev->hard_mtu < 16384) {
  1099. dev->rx_urb_size = 16384;
  1100. mfb = AX_RX_CTL_MFB_16384;
  1101. }
  1102. rxctl = asix_read_rx_ctl(dev);
  1103. asix_write_rx_ctl(dev, (rxctl & ~AX_RX_CTL_MFB_16384) | mfb);
  1104. medium = asix_read_medium_status(dev);
  1105. if (dev->net->mtu > 1500)
  1106. medium |= AX_MEDIUM_JFE;
  1107. else
  1108. medium &= ~AX_MEDIUM_JFE;
  1109. asix_write_medium_mode(dev, medium);
  1110. if (dev->rx_urb_size > old_rx_urb_size)
  1111. usbnet_unlink_rx_urbs(dev);
  1112. }
  1113. static int ax88178_change_mtu(struct net_device *net, int new_mtu)
  1114. {
  1115. struct usbnet *dev = netdev_priv(net);
  1116. int ll_mtu = new_mtu + net->hard_header_len + 4;
  1117. netdev_dbg(dev->net, "ax88178_change_mtu() new_mtu=%d\n", new_mtu);
  1118. if (new_mtu <= 0 || ll_mtu > 16384)
  1119. return -EINVAL;
  1120. if ((ll_mtu % dev->maxpacket) == 0)
  1121. return -EDOM;
  1122. net->mtu = new_mtu;
  1123. dev->hard_mtu = net->mtu + net->hard_header_len;
  1124. ax88178_set_mfb(dev);
  1125. return 0;
  1126. }
  1127. static const struct net_device_ops ax88178_netdev_ops = {
  1128. .ndo_open = usbnet_open,
  1129. .ndo_stop = usbnet_stop,
  1130. .ndo_start_xmit = usbnet_start_xmit,
  1131. .ndo_tx_timeout = usbnet_tx_timeout,
  1132. .ndo_set_mac_address = asix_set_mac_address,
  1133. .ndo_validate_addr = eth_validate_addr,
  1134. .ndo_set_rx_mode = asix_set_multicast,
  1135. .ndo_do_ioctl = asix_ioctl,
  1136. .ndo_change_mtu = ax88178_change_mtu,
  1137. };
  1138. static int ax88178_bind(struct usbnet *dev, struct usb_interface *intf)
  1139. {
  1140. int ret;
  1141. u8 buf[ETH_ALEN];
  1142. struct asix_data *data = (struct asix_data *)&dev->data;
  1143. data->eeprom_len = AX88772_EEPROM_LEN;
  1144. usbnet_get_endpoints(dev,intf);
  1145. /* Get the MAC address */
  1146. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  1147. if (ret < 0) {
  1148. dbg("Failed to read MAC address: %d", ret);
  1149. return ret;
  1150. }
  1151. memcpy(dev->net->dev_addr, buf, ETH_ALEN);
  1152. /* Initialize MII structure */
  1153. dev->mii.dev = dev->net;
  1154. dev->mii.mdio_read = asix_mdio_read;
  1155. dev->mii.mdio_write = asix_mdio_write;
  1156. dev->mii.phy_id_mask = 0x1f;
  1157. dev->mii.reg_num_mask = 0xff;
  1158. dev->mii.supports_gmii = 1;
  1159. dev->mii.phy_id = asix_get_phy_addr(dev);
  1160. dev->net->netdev_ops = &ax88178_netdev_ops;
  1161. dev->net->ethtool_ops = &ax88178_ethtool_ops;
  1162. /* Blink LEDS so users know driver saw dongle */
  1163. asix_sw_reset(dev, 0);
  1164. msleep(150);
  1165. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
  1166. msleep(150);
  1167. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  1168. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  1169. /* hard_mtu is still the default - the device does not support
  1170. jumbo eth frames */
  1171. dev->rx_urb_size = 2048;
  1172. }
  1173. return 0;
  1174. }
  1175. static const struct driver_info ax8817x_info = {
  1176. .description = "ASIX AX8817x USB 2.0 Ethernet",
  1177. .bind = ax88172_bind,
  1178. .status = asix_status,
  1179. .link_reset = ax88172_link_reset,
  1180. .reset = ax88172_link_reset,
  1181. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1182. .data = 0x00130103,
  1183. };
  1184. static const struct driver_info dlink_dub_e100_info = {
  1185. .description = "DLink DUB-E100 USB Ethernet",
  1186. .bind = ax88172_bind,
  1187. .status = asix_status,
  1188. .link_reset = ax88172_link_reset,
  1189. .reset = ax88172_link_reset,
  1190. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1191. .data = 0x009f9d9f,
  1192. };
  1193. static const struct driver_info netgear_fa120_info = {
  1194. .description = "Netgear FA-120 USB Ethernet",
  1195. .bind = ax88172_bind,
  1196. .status = asix_status,
  1197. .link_reset = ax88172_link_reset,
  1198. .reset = ax88172_link_reset,
  1199. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1200. .data = 0x00130103,
  1201. };
  1202. static const struct driver_info hawking_uf200_info = {
  1203. .description = "Hawking UF200 USB Ethernet",
  1204. .bind = ax88172_bind,
  1205. .status = asix_status,
  1206. .link_reset = ax88172_link_reset,
  1207. .reset = ax88172_link_reset,
  1208. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1209. .data = 0x001f1d1f,
  1210. };
  1211. static const struct driver_info ax88772_info = {
  1212. .description = "ASIX AX88772 USB 2.0 Ethernet",
  1213. .bind = ax88772_bind,
  1214. .status = asix_status,
  1215. .link_reset = ax88772_link_reset,
  1216. .reset = ax88772_reset,
  1217. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR | FLAG_MULTI_PACKET,
  1218. .rx_fixup = asix_rx_fixup,
  1219. .tx_fixup = asix_tx_fixup,
  1220. };
  1221. static const struct driver_info ax88178_info = {
  1222. .description = "ASIX AX88178 USB 2.0 Ethernet",
  1223. .bind = ax88178_bind,
  1224. .status = asix_status,
  1225. .link_reset = ax88178_link_reset,
  1226. .reset = ax88178_reset,
  1227. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR,
  1228. .rx_fixup = asix_rx_fixup,
  1229. .tx_fixup = asix_tx_fixup,
  1230. };
  1231. static const struct usb_device_id products [] = {
  1232. {
  1233. // Linksys USB200M
  1234. USB_DEVICE (0x077b, 0x2226),
  1235. .driver_info = (unsigned long) &ax8817x_info,
  1236. }, {
  1237. // Netgear FA120
  1238. USB_DEVICE (0x0846, 0x1040),
  1239. .driver_info = (unsigned long) &netgear_fa120_info,
  1240. }, {
  1241. // DLink DUB-E100
  1242. USB_DEVICE (0x2001, 0x1a00),
  1243. .driver_info = (unsigned long) &dlink_dub_e100_info,
  1244. }, {
  1245. // Intellinet, ST Lab USB Ethernet
  1246. USB_DEVICE (0x0b95, 0x1720),
  1247. .driver_info = (unsigned long) &ax8817x_info,
  1248. }, {
  1249. // Hawking UF200, TrendNet TU2-ET100
  1250. USB_DEVICE (0x07b8, 0x420a),
  1251. .driver_info = (unsigned long) &hawking_uf200_info,
  1252. }, {
  1253. // Billionton Systems, USB2AR
  1254. USB_DEVICE (0x08dd, 0x90ff),
  1255. .driver_info = (unsigned long) &ax8817x_info,
  1256. }, {
  1257. // ATEN UC210T
  1258. USB_DEVICE (0x0557, 0x2009),
  1259. .driver_info = (unsigned long) &ax8817x_info,
  1260. }, {
  1261. // Buffalo LUA-U2-KTX
  1262. USB_DEVICE (0x0411, 0x003d),
  1263. .driver_info = (unsigned long) &ax8817x_info,
  1264. }, {
  1265. // Buffalo LUA-U2-GT 10/100/1000
  1266. USB_DEVICE (0x0411, 0x006e),
  1267. .driver_info = (unsigned long) &ax88178_info,
  1268. }, {
  1269. // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
  1270. USB_DEVICE (0x6189, 0x182d),
  1271. .driver_info = (unsigned long) &ax8817x_info,
  1272. }, {
  1273. // Sitecom LN-031 "USB 2.0 10/100/1000 Ethernet adapter"
  1274. USB_DEVICE (0x0df6, 0x0056),
  1275. .driver_info = (unsigned long) &ax88178_info,
  1276. }, {
  1277. // corega FEther USB2-TX
  1278. USB_DEVICE (0x07aa, 0x0017),
  1279. .driver_info = (unsigned long) &ax8817x_info,
  1280. }, {
  1281. // Surecom EP-1427X-2
  1282. USB_DEVICE (0x1189, 0x0893),
  1283. .driver_info = (unsigned long) &ax8817x_info,
  1284. }, {
  1285. // goodway corp usb gwusb2e
  1286. USB_DEVICE (0x1631, 0x6200),
  1287. .driver_info = (unsigned long) &ax8817x_info,
  1288. }, {
  1289. // JVC MP-PRX1 Port Replicator
  1290. USB_DEVICE (0x04f1, 0x3008),
  1291. .driver_info = (unsigned long) &ax8817x_info,
  1292. }, {
  1293. // ASIX AX88772B 10/100
  1294. USB_DEVICE (0x0b95, 0x772b),
  1295. .driver_info = (unsigned long) &ax88772_info,
  1296. }, {
  1297. // ASIX AX88772 10/100
  1298. USB_DEVICE (0x0b95, 0x7720),
  1299. .driver_info = (unsigned long) &ax88772_info,
  1300. }, {
  1301. // ASIX AX88178 10/100/1000
  1302. USB_DEVICE (0x0b95, 0x1780),
  1303. .driver_info = (unsigned long) &ax88178_info,
  1304. }, {
  1305. // Logitec LAN-GTJ/U2A
  1306. USB_DEVICE (0x0789, 0x0160),
  1307. .driver_info = (unsigned long) &ax88178_info,
  1308. }, {
  1309. // Linksys USB200M Rev 2
  1310. USB_DEVICE (0x13b1, 0x0018),
  1311. .driver_info = (unsigned long) &ax88772_info,
  1312. }, {
  1313. // 0Q0 cable ethernet
  1314. USB_DEVICE (0x1557, 0x7720),
  1315. .driver_info = (unsigned long) &ax88772_info,
  1316. }, {
  1317. // DLink DUB-E100 H/W Ver B1
  1318. USB_DEVICE (0x07d1, 0x3c05),
  1319. .driver_info = (unsigned long) &ax88772_info,
  1320. }, {
  1321. // DLink DUB-E100 H/W Ver B1 Alternate
  1322. USB_DEVICE (0x2001, 0x3c05),
  1323. .driver_info = (unsigned long) &ax88772_info,
  1324. }, {
  1325. // Linksys USB1000
  1326. USB_DEVICE (0x1737, 0x0039),
  1327. .driver_info = (unsigned long) &ax88178_info,
  1328. }, {
  1329. // IO-DATA ETG-US2
  1330. USB_DEVICE (0x04bb, 0x0930),
  1331. .driver_info = (unsigned long) &ax88178_info,
  1332. }, {
  1333. // Belkin F5D5055
  1334. USB_DEVICE(0x050d, 0x5055),
  1335. .driver_info = (unsigned long) &ax88178_info,
  1336. }, {
  1337. // Apple USB Ethernet Adapter
  1338. USB_DEVICE(0x05ac, 0x1402),
  1339. .driver_info = (unsigned long) &ax88772_info,
  1340. }, {
  1341. // Cables-to-Go USB Ethernet Adapter
  1342. USB_DEVICE(0x0b95, 0x772a),
  1343. .driver_info = (unsigned long) &ax88772_info,
  1344. }, {
  1345. // ABOCOM for pci
  1346. USB_DEVICE(0x14ea, 0xab11),
  1347. .driver_info = (unsigned long) &ax88178_info,
  1348. }, {
  1349. // ASIX 88772a
  1350. USB_DEVICE(0x0db0, 0xa877),
  1351. .driver_info = (unsigned long) &ax88772_info,
  1352. }, {
  1353. // Asus USB Ethernet Adapter
  1354. USB_DEVICE (0x0b95, 0x7e2b),
  1355. .driver_info = (unsigned long) &ax88772_info,
  1356. },
  1357. { }, // END
  1358. };
  1359. MODULE_DEVICE_TABLE(usb, products);
  1360. static struct usb_driver asix_driver = {
  1361. .name = DRIVER_NAME,
  1362. .id_table = products,
  1363. .probe = usbnet_probe,
  1364. .suspend = usbnet_suspend,
  1365. .resume = usbnet_resume,
  1366. .disconnect = usbnet_disconnect,
  1367. .supports_autosuspend = 1,
  1368. };
  1369. module_usb_driver(asix_driver);
  1370. MODULE_AUTHOR("David Hollis");
  1371. MODULE_VERSION(DRIVER_VERSION);
  1372. MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
  1373. MODULE_LICENSE("GPL");