asix.c 37 KB

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