asix.c 37 KB

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