asix.c 39 KB

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