asix.c 39 KB

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