asix.c 37 KB

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