asix.c 38 KB

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