asix.c 37 KB

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