asix.c 39 KB

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