asix.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * ASIX AX8817X based USB 2.0 Ethernet Devices
  3. * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
  4. * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
  5. * Copyright (c) 2002-2003 TiVo Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. // #define DEBUG // error path messages, extra info
  22. // #define VERBOSE // more; success messages
  23. #include <linux/config.h>
  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. /* ASIX AX8817X based USB 2.0 Ethernet Devices */
  37. #define AX_CMD_SET_SW_MII 0x06
  38. #define AX_CMD_READ_MII_REG 0x07
  39. #define AX_CMD_WRITE_MII_REG 0x08
  40. #define AX_CMD_SET_HW_MII 0x0a
  41. #define AX_CMD_READ_EEPROM 0x0b
  42. #define AX_CMD_WRITE_EEPROM 0x0c
  43. #define AX_CMD_WRITE_ENABLE 0x0d
  44. #define AX_CMD_WRITE_DISABLE 0x0e
  45. #define AX_CMD_WRITE_RX_CTL 0x10
  46. #define AX_CMD_READ_IPG012 0x11
  47. #define AX_CMD_WRITE_IPG0 0x12
  48. #define AX_CMD_WRITE_IPG1 0x13
  49. #define AX_CMD_WRITE_IPG2 0x14
  50. #define AX_CMD_WRITE_MULTI_FILTER 0x16
  51. #define AX_CMD_READ_NODE_ID 0x17
  52. #define AX_CMD_READ_PHY_ID 0x19
  53. #define AX_CMD_READ_MEDIUM_STATUS 0x1a
  54. #define AX_CMD_WRITE_MEDIUM_MODE 0x1b
  55. #define AX_CMD_READ_MONITOR_MODE 0x1c
  56. #define AX_CMD_WRITE_MONITOR_MODE 0x1d
  57. #define AX_CMD_WRITE_GPIOS 0x1f
  58. #define AX_CMD_SW_RESET 0x20
  59. #define AX_CMD_SW_PHY_STATUS 0x21
  60. #define AX_CMD_SW_PHY_SELECT 0x22
  61. #define AX88772_CMD_READ_NODE_ID 0x13
  62. #define AX_MONITOR_MODE 0x01
  63. #define AX_MONITOR_LINK 0x02
  64. #define AX_MONITOR_MAGIC 0x04
  65. #define AX_MONITOR_HSFS 0x10
  66. /* AX88172 Medium Status Register values */
  67. #define AX_MEDIUM_FULL_DUPLEX 0x02
  68. #define AX_MEDIUM_TX_ABORT_ALLOW 0x04
  69. #define AX_MEDIUM_FLOW_CONTROL_EN 0x10
  70. #define AX_MCAST_FILTER_SIZE 8
  71. #define AX_MAX_MCAST 64
  72. #define AX_EEPROM_LEN 0x40
  73. #define AX_SWRESET_CLEAR 0x00
  74. #define AX_SWRESET_RR 0x01
  75. #define AX_SWRESET_RT 0x02
  76. #define AX_SWRESET_PRTE 0x04
  77. #define AX_SWRESET_PRL 0x08
  78. #define AX_SWRESET_BZ 0x10
  79. #define AX_SWRESET_IPRL 0x20
  80. #define AX_SWRESET_IPPD 0x40
  81. #define AX88772_IPG0_DEFAULT 0x15
  82. #define AX88772_IPG1_DEFAULT 0x0c
  83. #define AX88772_IPG2_DEFAULT 0x12
  84. #define AX88772_MEDIUM_FULL_DUPLEX 0x0002
  85. #define AX88772_MEDIUM_RESERVED 0x0004
  86. #define AX88772_MEDIUM_RX_FC_ENABLE 0x0010
  87. #define AX88772_MEDIUM_TX_FC_ENABLE 0x0020
  88. #define AX88772_MEDIUM_PAUSE_FORMAT 0x0080
  89. #define AX88772_MEDIUM_RX_ENABLE 0x0100
  90. #define AX88772_MEDIUM_100MB 0x0200
  91. #define AX88772_MEDIUM_DEFAULT \
  92. (AX88772_MEDIUM_FULL_DUPLEX | AX88772_MEDIUM_RX_FC_ENABLE | \
  93. AX88772_MEDIUM_TX_FC_ENABLE | AX88772_MEDIUM_100MB | \
  94. AX88772_MEDIUM_RESERVED | AX88772_MEDIUM_RX_ENABLE )
  95. #define AX_EEPROM_MAGIC 0xdeadbeef
  96. /* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */
  97. struct ax8817x_data {
  98. u8 multi_filter[AX_MCAST_FILTER_SIZE];
  99. };
  100. struct ax88172_int_data {
  101. u16 res1;
  102. u8 link;
  103. u16 res2;
  104. u8 status;
  105. u16 res3;
  106. } __attribute__ ((packed));
  107. static int ax8817x_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  108. u16 size, void *data)
  109. {
  110. return usb_control_msg(
  111. dev->udev,
  112. usb_rcvctrlpipe(dev->udev, 0),
  113. cmd,
  114. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  115. value,
  116. index,
  117. data,
  118. size,
  119. USB_CTRL_GET_TIMEOUT);
  120. }
  121. static int ax8817x_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  122. u16 size, void *data)
  123. {
  124. return usb_control_msg(
  125. dev->udev,
  126. usb_sndctrlpipe(dev->udev, 0),
  127. cmd,
  128. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  129. value,
  130. index,
  131. data,
  132. size,
  133. USB_CTRL_SET_TIMEOUT);
  134. }
  135. static void ax8817x_async_cmd_callback(struct urb *urb, struct pt_regs *regs)
  136. {
  137. struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
  138. if (urb->status < 0)
  139. printk(KERN_DEBUG "ax8817x_async_cmd_callback() failed with %d",
  140. urb->status);
  141. kfree(req);
  142. usb_free_urb(urb);
  143. }
  144. static void ax8817x_status(struct usbnet *dev, struct urb *urb)
  145. {
  146. struct ax88172_int_data *event;
  147. int link;
  148. if (urb->actual_length < 8)
  149. return;
  150. event = urb->transfer_buffer;
  151. link = event->link & 0x01;
  152. if (netif_carrier_ok(dev->net) != link) {
  153. if (link) {
  154. netif_carrier_on(dev->net);
  155. usbnet_defer_kevent (dev, EVENT_LINK_RESET );
  156. } else
  157. netif_carrier_off(dev->net);
  158. devdbg(dev, "ax8817x - Link Status is: %d", link);
  159. }
  160. }
  161. static void
  162. ax8817x_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  163. u16 size, void *data)
  164. {
  165. struct usb_ctrlrequest *req;
  166. int status;
  167. struct urb *urb;
  168. if ((urb = usb_alloc_urb(0, GFP_ATOMIC)) == NULL) {
  169. devdbg(dev, "Error allocating URB in write_cmd_async!");
  170. return;
  171. }
  172. if ((req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC)) == NULL) {
  173. deverr(dev, "Failed to allocate memory for control request");
  174. usb_free_urb(urb);
  175. return;
  176. }
  177. req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
  178. req->bRequest = cmd;
  179. req->wValue = cpu_to_le16(value);
  180. req->wIndex = cpu_to_le16(index);
  181. req->wLength = cpu_to_le16(size);
  182. usb_fill_control_urb(urb, dev->udev,
  183. usb_sndctrlpipe(dev->udev, 0),
  184. (void *)req, data, size,
  185. ax8817x_async_cmd_callback, req);
  186. if((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  187. deverr(dev, "Error submitting the control message: status=%d",
  188. status);
  189. kfree(req);
  190. usb_free_urb(urb);
  191. }
  192. }
  193. static void ax8817x_set_multicast(struct net_device *net)
  194. {
  195. struct usbnet *dev = netdev_priv(net);
  196. struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;
  197. u8 rx_ctl = 0x8c;
  198. if (net->flags & IFF_PROMISC) {
  199. rx_ctl |= 0x01;
  200. } else if (net->flags & IFF_ALLMULTI
  201. || net->mc_count > AX_MAX_MCAST) {
  202. rx_ctl |= 0x02;
  203. } else if (net->mc_count == 0) {
  204. /* just broadcast and directed */
  205. } else {
  206. /* We use the 20 byte dev->data
  207. * for our 8 byte filter buffer
  208. * to avoid allocating memory that
  209. * is tricky to free later */
  210. struct dev_mc_list *mc_list = net->mc_list;
  211. u32 crc_bits;
  212. int i;
  213. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  214. /* Build the multicast hash filter. */
  215. for (i = 0; i < net->mc_count; i++) {
  216. crc_bits =
  217. ether_crc(ETH_ALEN,
  218. mc_list->dmi_addr) >> 26;
  219. data->multi_filter[crc_bits >> 3] |=
  220. 1 << (crc_bits & 7);
  221. mc_list = mc_list->next;
  222. }
  223. ax8817x_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  224. AX_MCAST_FILTER_SIZE, data->multi_filter);
  225. rx_ctl |= 0x10;
  226. }
  227. ax8817x_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  228. }
  229. static int ax8817x_mdio_read(struct net_device *netdev, int phy_id, int loc)
  230. {
  231. struct usbnet *dev = netdev_priv(netdev);
  232. u16 res;
  233. u8 buf[1];
  234. ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf);
  235. ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
  236. (__u16)loc, 2, (u16 *)&res);
  237. ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf);
  238. return res & 0xffff;
  239. }
  240. /* same as above, but converts resulting value to cpu byte order */
  241. static int ax8817x_mdio_read_le(struct net_device *netdev, int phy_id, int loc)
  242. {
  243. return le16_to_cpu(ax8817x_mdio_read(netdev,phy_id, loc));
  244. }
  245. static void
  246. ax8817x_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
  247. {
  248. struct usbnet *dev = netdev_priv(netdev);
  249. u16 res = val;
  250. u8 buf[1];
  251. ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf);
  252. ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
  253. (__u16)loc, 2, (u16 *)&res);
  254. ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf);
  255. }
  256. /* same as above, but converts new value to le16 byte order before writing */
  257. static void
  258. ax8817x_mdio_write_le(struct net_device *netdev, int phy_id, int loc, int val)
  259. {
  260. ax8817x_mdio_write( netdev, phy_id, loc, cpu_to_le16(val) );
  261. }
  262. static int ax88172_link_reset(struct usbnet *dev)
  263. {
  264. u16 lpa;
  265. u16 adv;
  266. u16 res;
  267. u8 mode;
  268. mode = AX_MEDIUM_TX_ABORT_ALLOW | AX_MEDIUM_FLOW_CONTROL_EN;
  269. lpa = ax8817x_mdio_read_le(dev->net, dev->mii.phy_id, MII_LPA);
  270. adv = ax8817x_mdio_read_le(dev->net, dev->mii.phy_id, MII_ADVERTISE);
  271. res = mii_nway_result(lpa|adv);
  272. if (res & LPA_DUPLEX)
  273. mode |= AX_MEDIUM_FULL_DUPLEX;
  274. ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
  275. return 0;
  276. }
  277. static void
  278. ax8817x_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  279. {
  280. struct usbnet *dev = netdev_priv(net);
  281. u8 opt;
  282. if (ax8817x_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
  283. wolinfo->supported = 0;
  284. wolinfo->wolopts = 0;
  285. return;
  286. }
  287. wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
  288. wolinfo->wolopts = 0;
  289. if (opt & AX_MONITOR_MODE) {
  290. if (opt & AX_MONITOR_LINK)
  291. wolinfo->wolopts |= WAKE_PHY;
  292. if (opt & AX_MONITOR_MAGIC)
  293. wolinfo->wolopts |= WAKE_MAGIC;
  294. }
  295. }
  296. static int
  297. ax8817x_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  298. {
  299. struct usbnet *dev = netdev_priv(net);
  300. u8 opt = 0;
  301. u8 buf[1];
  302. if (wolinfo->wolopts & WAKE_PHY)
  303. opt |= AX_MONITOR_LINK;
  304. if (wolinfo->wolopts & WAKE_MAGIC)
  305. opt |= AX_MONITOR_MAGIC;
  306. if (opt != 0)
  307. opt |= AX_MONITOR_MODE;
  308. if (ax8817x_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
  309. opt, 0, 0, &buf) < 0)
  310. return -EINVAL;
  311. return 0;
  312. }
  313. static int ax8817x_get_eeprom_len(struct net_device *net)
  314. {
  315. return AX_EEPROM_LEN;
  316. }
  317. static int ax8817x_get_eeprom(struct net_device *net,
  318. struct ethtool_eeprom *eeprom, u8 *data)
  319. {
  320. struct usbnet *dev = netdev_priv(net);
  321. u16 *ebuf = (u16 *)data;
  322. int i;
  323. /* Crude hack to ensure that we don't overwrite memory
  324. * if an odd length is supplied
  325. */
  326. if (eeprom->len % 2)
  327. return -EINVAL;
  328. eeprom->magic = AX_EEPROM_MAGIC;
  329. /* ax8817x returns 2 bytes from eeprom on read */
  330. for (i=0; i < eeprom->len / 2; i++) {
  331. if (ax8817x_read_cmd(dev, AX_CMD_READ_EEPROM,
  332. eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
  333. return -EINVAL;
  334. }
  335. return 0;
  336. }
  337. static void ax8817x_get_drvinfo (struct net_device *net,
  338. struct ethtool_drvinfo *info)
  339. {
  340. /* Inherit standard device info */
  341. usbnet_get_drvinfo(net, info);
  342. info->eedump_len = 0x3e;
  343. }
  344. static int ax8817x_get_settings(struct net_device *net, struct ethtool_cmd *cmd)
  345. {
  346. struct usbnet *dev = netdev_priv(net);
  347. return mii_ethtool_gset(&dev->mii,cmd);
  348. }
  349. static int ax8817x_set_settings(struct net_device *net, struct ethtool_cmd *cmd)
  350. {
  351. struct usbnet *dev = netdev_priv(net);
  352. return mii_ethtool_sset(&dev->mii,cmd);
  353. }
  354. /* We need to override some ethtool_ops so we require our
  355. own structure so we don't interfere with other usbnet
  356. devices that may be connected at the same time. */
  357. static struct ethtool_ops ax8817x_ethtool_ops = {
  358. .get_drvinfo = ax8817x_get_drvinfo,
  359. .get_link = ethtool_op_get_link,
  360. .get_msglevel = usbnet_get_msglevel,
  361. .set_msglevel = usbnet_set_msglevel,
  362. .get_wol = ax8817x_get_wol,
  363. .set_wol = ax8817x_set_wol,
  364. .get_eeprom_len = ax8817x_get_eeprom_len,
  365. .get_eeprom = ax8817x_get_eeprom,
  366. .get_settings = ax8817x_get_settings,
  367. .set_settings = ax8817x_set_settings,
  368. };
  369. static int ax8817x_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
  370. {
  371. struct usbnet *dev = netdev_priv(net);
  372. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  373. }
  374. static int ax8817x_bind(struct usbnet *dev, struct usb_interface *intf)
  375. {
  376. int ret = 0;
  377. void *buf;
  378. int i;
  379. unsigned long gpio_bits = dev->driver_info->data;
  380. usbnet_get_endpoints(dev,intf);
  381. buf = kmalloc(ETH_ALEN, GFP_KERNEL);
  382. if(!buf) {
  383. ret = -ENOMEM;
  384. goto out1;
  385. }
  386. /* Toggle the GPIOs in a manufacturer/model specific way */
  387. for (i = 2; i >= 0; i--) {
  388. if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
  389. (gpio_bits >> (i * 8)) & 0xff, 0, 0,
  390. buf)) < 0)
  391. goto out2;
  392. msleep(5);
  393. }
  394. if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
  395. 0x80, 0, 0, buf)) < 0) {
  396. dbg("send AX_CMD_WRITE_RX_CTL failed: %d", ret);
  397. goto out2;
  398. }
  399. /* Get the MAC address */
  400. memset(buf, 0, ETH_ALEN);
  401. if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_NODE_ID,
  402. 0, 0, 6, buf)) < 0) {
  403. dbg("read AX_CMD_READ_NODE_ID failed: %d", ret);
  404. goto out2;
  405. }
  406. memcpy(dev->net->dev_addr, buf, ETH_ALEN);
  407. /* Get the PHY id */
  408. if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID,
  409. 0, 0, 2, buf)) < 0) {
  410. dbg("error on read AX_CMD_READ_PHY_ID: %02x", ret);
  411. goto out2;
  412. } else if (ret < 2) {
  413. /* this should always return 2 bytes */
  414. dbg("AX_CMD_READ_PHY_ID returned less than 2 bytes: ret=%02x",
  415. ret);
  416. ret = -EIO;
  417. goto out2;
  418. }
  419. /* Initialize MII structure */
  420. dev->mii.dev = dev->net;
  421. dev->mii.mdio_read = ax8817x_mdio_read;
  422. dev->mii.mdio_write = ax8817x_mdio_write;
  423. dev->mii.phy_id_mask = 0x3f;
  424. dev->mii.reg_num_mask = 0x1f;
  425. dev->mii.phy_id = *((u8 *)buf + 1);
  426. dev->net->do_ioctl = ax8817x_ioctl;
  427. dev->net->set_multicast_list = ax8817x_set_multicast;
  428. dev->net->ethtool_ops = &ax8817x_ethtool_ops;
  429. ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  430. ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  431. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  432. mii_nway_restart(&dev->mii);
  433. return 0;
  434. out2:
  435. kfree(buf);
  436. out1:
  437. return ret;
  438. }
  439. static struct ethtool_ops ax88772_ethtool_ops = {
  440. .get_drvinfo = ax8817x_get_drvinfo,
  441. .get_link = ethtool_op_get_link,
  442. .get_msglevel = usbnet_get_msglevel,
  443. .set_msglevel = usbnet_set_msglevel,
  444. .get_wol = ax8817x_get_wol,
  445. .set_wol = ax8817x_set_wol,
  446. .get_eeprom_len = ax8817x_get_eeprom_len,
  447. .get_eeprom = ax8817x_get_eeprom,
  448. .get_settings = ax8817x_get_settings,
  449. .set_settings = ax8817x_set_settings,
  450. };
  451. static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
  452. {
  453. int ret;
  454. void *buf;
  455. usbnet_get_endpoints(dev,intf);
  456. buf = kmalloc(6, GFP_KERNEL);
  457. if(!buf) {
  458. dbg ("Cannot allocate memory for buffer");
  459. ret = -ENOMEM;
  460. goto out1;
  461. }
  462. if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
  463. 0x00B0, 0, 0, buf)) < 0)
  464. goto out2;
  465. msleep(5);
  466. if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
  467. 0x0001, 0, 0, buf)) < 0) {
  468. dbg("Select PHY #1 failed: %d", ret);
  469. goto out2;
  470. }
  471. if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPPD,
  472. 0, 0, buf)) < 0) {
  473. dbg("Failed to power down internal PHY: %d", ret);
  474. goto out2;
  475. }
  476. msleep(150);
  477. if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_CLEAR,
  478. 0, 0, buf)) < 0) {
  479. dbg("Failed to perform software reset: %d", ret);
  480. goto out2;
  481. }
  482. msleep(150);
  483. if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
  484. AX_SWRESET_IPRL | AX_SWRESET_PRL,
  485. 0, 0, buf)) < 0) {
  486. dbg("Failed to set Internal/External PHY reset control: %d",
  487. ret);
  488. goto out2;
  489. }
  490. msleep(150);
  491. if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
  492. 0x0000, 0, 0, buf)) < 0) {
  493. dbg("Failed to reset RX_CTL: %d", ret);
  494. goto out2;
  495. }
  496. /* Get the MAC address */
  497. memset(buf, 0, ETH_ALEN);
  498. if ((ret = ax8817x_read_cmd(dev, AX88772_CMD_READ_NODE_ID,
  499. 0, 0, ETH_ALEN, buf)) < 0) {
  500. dbg("Failed to read MAC address: %d", ret);
  501. goto out2;
  502. }
  503. memcpy(dev->net->dev_addr, buf, ETH_ALEN);
  504. if ((ret = ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII,
  505. 0, 0, 0, buf)) < 0) {
  506. dbg("Enabling software MII failed: %d", ret);
  507. goto out2;
  508. }
  509. if (((ret = ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG,
  510. 0x0010, 2, 2, buf)) < 0)
  511. || (*((u16 *)buf) != 0x003b)) {
  512. dbg("Read PHY register 2 must be 0x3b00: %d", ret);
  513. goto out2;
  514. }
  515. /* Initialize MII structure */
  516. dev->mii.dev = dev->net;
  517. dev->mii.mdio_read = ax8817x_mdio_read;
  518. dev->mii.mdio_write = ax8817x_mdio_write;
  519. dev->mii.phy_id_mask = 0xff;
  520. dev->mii.reg_num_mask = 0xff;
  521. dev->net->do_ioctl = ax8817x_ioctl;
  522. /* Get the PHY id */
  523. if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID,
  524. 0, 0, 2, buf)) < 0) {
  525. dbg("Error reading PHY ID: %02x", ret);
  526. goto out2;
  527. } else if (ret < 2) {
  528. /* this should always return 2 bytes */
  529. dbg("AX_CMD_READ_PHY_ID returned less than 2 bytes: ret=%02x",
  530. ret);
  531. ret = -EIO;
  532. goto out2;
  533. }
  534. dev->mii.phy_id = *((u8 *)buf + 1);
  535. if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_PRL,
  536. 0, 0, buf)) < 0) {
  537. dbg("Set external PHY reset pin level: %d", ret);
  538. goto out2;
  539. }
  540. msleep(150);
  541. if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
  542. AX_SWRESET_IPRL | AX_SWRESET_PRL,
  543. 0, 0, buf)) < 0) {
  544. dbg("Set Internal/External PHY reset control: %d", ret);
  545. goto out2;
  546. }
  547. msleep(150);
  548. dev->net->set_multicast_list = ax8817x_set_multicast;
  549. dev->net->ethtool_ops = &ax88772_ethtool_ops;
  550. ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  551. ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  552. ADVERTISE_ALL | ADVERTISE_CSMA);
  553. mii_nway_restart(&dev->mii);
  554. if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
  555. AX88772_MEDIUM_DEFAULT, 0, 0, buf)) < 0) {
  556. dbg("Write medium mode register: %d", ret);
  557. goto out2;
  558. }
  559. if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_IPG0,
  560. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  561. AX88772_IPG2_DEFAULT, 0, buf)) < 0) {
  562. dbg("Write IPG,IPG1,IPG2 failed: %d", ret);
  563. goto out2;
  564. }
  565. if ((ret =
  566. ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf)) < 0) {
  567. dbg("Failed to set hardware MII: %02x", ret);
  568. goto out2;
  569. }
  570. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  571. if ((ret =
  572. ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x0088, 0, 0,
  573. buf)) < 0) {
  574. dbg("Reset RX_CTL failed: %d", ret);
  575. goto out2;
  576. }
  577. kfree(buf);
  578. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  579. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  580. /* hard_mtu is still the default - the device does not support
  581. jumbo eth frames */
  582. dev->rx_urb_size = 2048;
  583. }
  584. return 0;
  585. out2:
  586. kfree(buf);
  587. out1:
  588. return ret;
  589. }
  590. static int ax88772_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  591. {
  592. u8 *head;
  593. u32 header;
  594. char *packet;
  595. struct sk_buff *ax_skb;
  596. u16 size;
  597. head = (u8 *) skb->data;
  598. memcpy(&header, head, sizeof(header));
  599. le32_to_cpus(&header);
  600. packet = head + sizeof(header);
  601. skb_pull(skb, 4);
  602. while (skb->len > 0) {
  603. if ((short)(header & 0x0000ffff) !=
  604. ~((short)((header & 0xffff0000) >> 16))) {
  605. devdbg(dev,"header length data is error");
  606. }
  607. /* get the packet length */
  608. size = (u16) (header & 0x0000ffff);
  609. if ((skb->len) - ((size + 1) & 0xfffe) == 0)
  610. return 2;
  611. if (size > ETH_FRAME_LEN) {
  612. devdbg(dev,"invalid rx length %d", size);
  613. return 0;
  614. }
  615. ax_skb = skb_clone(skb, GFP_ATOMIC);
  616. if (ax_skb) {
  617. ax_skb->len = size;
  618. ax_skb->data = packet;
  619. ax_skb->tail = packet + size;
  620. usbnet_skb_return(dev, ax_skb);
  621. } else {
  622. return 0;
  623. }
  624. skb_pull(skb, (size + 1) & 0xfffe);
  625. if (skb->len == 0)
  626. break;
  627. head = (u8 *) skb->data;
  628. memcpy(&header, head, sizeof(header));
  629. le32_to_cpus(&header);
  630. packet = head + sizeof(header);
  631. skb_pull(skb, 4);
  632. }
  633. if (skb->len < 0) {
  634. devdbg(dev,"invalid rx length %d", skb->len);
  635. return 0;
  636. }
  637. return 1;
  638. }
  639. static struct sk_buff *ax88772_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  640. gfp_t flags)
  641. {
  642. int padlen;
  643. int headroom = skb_headroom(skb);
  644. int tailroom = skb_tailroom(skb);
  645. u32 packet_len;
  646. u32 padbytes = 0xffff0000;
  647. padlen = ((skb->len + 4) % 512) ? 0 : 4;
  648. if ((!skb_cloned(skb))
  649. && ((headroom + tailroom) >= (4 + padlen))) {
  650. if ((headroom < 4) || (tailroom < padlen)) {
  651. skb->data = memmove(skb->head + 4, skb->data, skb->len);
  652. skb->tail = skb->data + skb->len;
  653. }
  654. } else {
  655. struct sk_buff *skb2;
  656. skb2 = skb_copy_expand(skb, 4, padlen, flags);
  657. dev_kfree_skb_any(skb);
  658. skb = skb2;
  659. if (!skb)
  660. return NULL;
  661. }
  662. skb_push(skb, 4);
  663. packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
  664. memcpy(skb->data, &packet_len, sizeof(packet_len));
  665. if ((skb->len % 512) == 0) {
  666. memcpy( skb->tail, &padbytes, sizeof(padbytes));
  667. skb_put(skb, sizeof(padbytes));
  668. }
  669. return skb;
  670. }
  671. static int ax88772_link_reset(struct usbnet *dev)
  672. {
  673. u16 lpa;
  674. u16 adv;
  675. u16 res;
  676. u16 mode;
  677. mode = AX88772_MEDIUM_DEFAULT;
  678. lpa = ax8817x_mdio_read_le(dev->net, dev->mii.phy_id, MII_LPA);
  679. adv = ax8817x_mdio_read_le(dev->net, dev->mii.phy_id, MII_ADVERTISE);
  680. res = mii_nway_result(lpa|adv);
  681. if ((res & LPA_DUPLEX) == 0)
  682. mode &= ~AX88772_MEDIUM_FULL_DUPLEX;
  683. if ((res & LPA_100) == 0)
  684. mode &= ~AX88772_MEDIUM_100MB;
  685. ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
  686. return 0;
  687. }
  688. static const struct driver_info ax8817x_info = {
  689. .description = "ASIX AX8817x USB 2.0 Ethernet",
  690. .bind = ax8817x_bind,
  691. .status = ax8817x_status,
  692. .link_reset = ax88172_link_reset,
  693. .reset = ax88172_link_reset,
  694. .flags = FLAG_ETHER,
  695. .data = 0x00130103,
  696. };
  697. static const struct driver_info dlink_dub_e100_info = {
  698. .description = "DLink DUB-E100 USB Ethernet",
  699. .bind = ax8817x_bind,
  700. .status = ax8817x_status,
  701. .link_reset = ax88172_link_reset,
  702. .reset = ax88172_link_reset,
  703. .flags = FLAG_ETHER,
  704. .data = 0x009f9d9f,
  705. };
  706. static const struct driver_info netgear_fa120_info = {
  707. .description = "Netgear FA-120 USB Ethernet",
  708. .bind = ax8817x_bind,
  709. .status = ax8817x_status,
  710. .link_reset = ax88172_link_reset,
  711. .reset = ax88172_link_reset,
  712. .flags = FLAG_ETHER,
  713. .data = 0x00130103,
  714. };
  715. static const struct driver_info hawking_uf200_info = {
  716. .description = "Hawking UF200 USB Ethernet",
  717. .bind = ax8817x_bind,
  718. .status = ax8817x_status,
  719. .link_reset = ax88172_link_reset,
  720. .reset = ax88172_link_reset,
  721. .flags = FLAG_ETHER,
  722. .data = 0x001f1d1f,
  723. };
  724. static const struct driver_info ax88772_info = {
  725. .description = "ASIX AX88772 USB 2.0 Ethernet",
  726. .bind = ax88772_bind,
  727. .status = ax8817x_status,
  728. .link_reset = ax88772_link_reset,
  729. .reset = ax88772_link_reset,
  730. .flags = FLAG_ETHER | FLAG_FRAMING_AX,
  731. .rx_fixup = ax88772_rx_fixup,
  732. .tx_fixup = ax88772_tx_fixup,
  733. .data = 0x00130103,
  734. };
  735. static const struct usb_device_id products [] = {
  736. {
  737. // Linksys USB200M
  738. USB_DEVICE (0x077b, 0x2226),
  739. .driver_info = (unsigned long) &ax8817x_info,
  740. }, {
  741. // Netgear FA120
  742. USB_DEVICE (0x0846, 0x1040),
  743. .driver_info = (unsigned long) &netgear_fa120_info,
  744. }, {
  745. // DLink DUB-E100
  746. USB_DEVICE (0x2001, 0x1a00),
  747. .driver_info = (unsigned long) &dlink_dub_e100_info,
  748. }, {
  749. // Intellinet, ST Lab USB Ethernet
  750. USB_DEVICE (0x0b95, 0x1720),
  751. .driver_info = (unsigned long) &ax8817x_info,
  752. }, {
  753. // Hawking UF200, TrendNet TU2-ET100
  754. USB_DEVICE (0x07b8, 0x420a),
  755. .driver_info = (unsigned long) &hawking_uf200_info,
  756. }, {
  757. // Billionton Systems, USB2AR
  758. USB_DEVICE (0x08dd, 0x90ff),
  759. .driver_info = (unsigned long) &ax8817x_info,
  760. }, {
  761. // ATEN UC210T
  762. USB_DEVICE (0x0557, 0x2009),
  763. .driver_info = (unsigned long) &ax8817x_info,
  764. }, {
  765. // Buffalo LUA-U2-KTX
  766. USB_DEVICE (0x0411, 0x003d),
  767. .driver_info = (unsigned long) &ax8817x_info,
  768. }, {
  769. // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
  770. USB_DEVICE (0x6189, 0x182d),
  771. .driver_info = (unsigned long) &ax8817x_info,
  772. }, {
  773. // corega FEther USB2-TX
  774. USB_DEVICE (0x07aa, 0x0017),
  775. .driver_info = (unsigned long) &ax8817x_info,
  776. }, {
  777. // Surecom EP-1427X-2
  778. USB_DEVICE (0x1189, 0x0893),
  779. .driver_info = (unsigned long) &ax8817x_info,
  780. }, {
  781. // goodway corp usb gwusb2e
  782. USB_DEVICE (0x1631, 0x6200),
  783. .driver_info = (unsigned long) &ax8817x_info,
  784. }, {
  785. // ASIX AX88772 10/100
  786. USB_DEVICE (0x0b95, 0x7720),
  787. .driver_info = (unsigned long) &ax88772_info,
  788. }, {
  789. // Linksys USB200M Rev 2
  790. USB_DEVICE (0x13b1, 0x0018),
  791. .driver_info = (unsigned long) &ax88772_info,
  792. }, {
  793. // 0Q0 cable ethernet
  794. USB_DEVICE (0x1557, 0x7720),
  795. .driver_info = (unsigned long) &ax88772_info,
  796. },
  797. { }, // END
  798. };
  799. MODULE_DEVICE_TABLE(usb, products);
  800. static struct usb_driver asix_driver = {
  801. .name = "asix",
  802. .id_table = products,
  803. .probe = usbnet_probe,
  804. .suspend = usbnet_suspend,
  805. .resume = usbnet_resume,
  806. .disconnect = usbnet_disconnect,
  807. };
  808. static int __init asix_init(void)
  809. {
  810. return usb_register(&asix_driver);
  811. }
  812. module_init(asix_init);
  813. static void __exit asix_exit(void)
  814. {
  815. usb_deregister(&asix_driver);
  816. }
  817. module_exit(asix_exit);
  818. MODULE_AUTHOR("David Hollis");
  819. MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
  820. MODULE_LICENSE("GPL");