asix.c 36 KB

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