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