rtl8150.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2002 Petko Manolov (petkan@users.sourceforge.net)
  4. */
  5. #include <linux/signal.h>
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/mii.h>
  11. #include <linux/ethtool.h>
  12. #include <linux/usb.h>
  13. #include <linux/uaccess.h>
  14. /* Version Information */
  15. #define DRIVER_VERSION "v0.6.2 (2004/08/27)"
  16. #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
  17. #define DRIVER_DESC "rtl8150 based usb-ethernet driver"
  18. #define IDR 0x0120
  19. #define MAR 0x0126
  20. #define CR 0x012e
  21. #define TCR 0x012f
  22. #define RCR 0x0130
  23. #define TSR 0x0132
  24. #define RSR 0x0133
  25. #define CON0 0x0135
  26. #define CON1 0x0136
  27. #define MSR 0x0137
  28. #define PHYADD 0x0138
  29. #define PHYDAT 0x0139
  30. #define PHYCNT 0x013b
  31. #define GPPC 0x013d
  32. #define BMCR 0x0140
  33. #define BMSR 0x0142
  34. #define ANAR 0x0144
  35. #define ANLP 0x0146
  36. #define AER 0x0148
  37. #define CSCR 0x014C /* This one has the link status */
  38. #define CSCR_LINK_STATUS (1 << 3)
  39. #define IDR_EEPROM 0x1202
  40. #define PHY_READ 0
  41. #define PHY_WRITE 0x20
  42. #define PHY_GO 0x40
  43. #define MII_TIMEOUT 10
  44. #define INTBUFSIZE 8
  45. #define RTL8150_REQT_READ 0xc0
  46. #define RTL8150_REQT_WRITE 0x40
  47. #define RTL8150_REQ_GET_REGS 0x05
  48. #define RTL8150_REQ_SET_REGS 0x05
  49. /* Transmit status register errors */
  50. #define TSR_ECOL (1<<5)
  51. #define TSR_LCOL (1<<4)
  52. #define TSR_LOSS_CRS (1<<3)
  53. #define TSR_JBR (1<<2)
  54. #define TSR_ERRORS (TSR_ECOL | TSR_LCOL | TSR_LOSS_CRS | TSR_JBR)
  55. /* Receive status register errors */
  56. #define RSR_CRC (1<<2)
  57. #define RSR_FAE (1<<1)
  58. #define RSR_ERRORS (RSR_CRC | RSR_FAE)
  59. /* Media status register definitions */
  60. #define MSR_DUPLEX (1<<4)
  61. #define MSR_SPEED (1<<3)
  62. #define MSR_LINK (1<<2)
  63. /* Interrupt pipe data */
  64. #define INT_TSR 0x00
  65. #define INT_RSR 0x01
  66. #define INT_MSR 0x02
  67. #define INT_WAKSR 0x03
  68. #define INT_TXOK_CNT 0x04
  69. #define INT_RXLOST_CNT 0x05
  70. #define INT_CRERR_CNT 0x06
  71. #define INT_COL_CNT 0x07
  72. #define RTL8150_MTU 1540
  73. #define RTL8150_TX_TIMEOUT (HZ)
  74. #define RX_SKB_POOL_SIZE 4
  75. /* rtl8150 flags */
  76. #define RTL8150_HW_CRC 0
  77. #define RX_REG_SET 1
  78. #define RTL8150_UNPLUG 2
  79. #define RX_URB_FAIL 3
  80. /* Define these values to match your device */
  81. #define VENDOR_ID_REALTEK 0x0bda
  82. #define VENDOR_ID_MELCO 0x0411
  83. #define VENDOR_ID_MICRONET 0x3980
  84. #define VENDOR_ID_LONGSHINE 0x07b8
  85. #define VENDOR_ID_OQO 0x1557
  86. #define VENDOR_ID_ZYXEL 0x0586
  87. #define PRODUCT_ID_RTL8150 0x8150
  88. #define PRODUCT_ID_LUAKTX 0x0012
  89. #define PRODUCT_ID_LCS8138TX 0x401a
  90. #define PRODUCT_ID_SP128AR 0x0003
  91. #define PRODUCT_ID_PRESTIGE 0x401a
  92. #undef EEPROM_WRITE
  93. /* table of devices that work with this driver */
  94. static const struct usb_device_id rtl8150_table[] = {
  95. {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150)},
  96. {USB_DEVICE(VENDOR_ID_MELCO, PRODUCT_ID_LUAKTX)},
  97. {USB_DEVICE(VENDOR_ID_MICRONET, PRODUCT_ID_SP128AR)},
  98. {USB_DEVICE(VENDOR_ID_LONGSHINE, PRODUCT_ID_LCS8138TX)},
  99. {USB_DEVICE(VENDOR_ID_OQO, PRODUCT_ID_RTL8150)},
  100. {USB_DEVICE(VENDOR_ID_ZYXEL, PRODUCT_ID_PRESTIGE)},
  101. {}
  102. };
  103. MODULE_DEVICE_TABLE(usb, rtl8150_table);
  104. struct rtl8150 {
  105. unsigned long flags;
  106. struct usb_device *udev;
  107. struct tasklet_struct tl;
  108. struct net_device *netdev;
  109. struct urb *rx_urb, *tx_urb, *intr_urb;
  110. struct sk_buff *tx_skb, *rx_skb;
  111. struct sk_buff *rx_skb_pool[RX_SKB_POOL_SIZE];
  112. spinlock_t rx_pool_lock;
  113. struct usb_ctrlrequest dr;
  114. int intr_interval;
  115. u8 *intr_buff;
  116. u8 phy;
  117. };
  118. typedef struct rtl8150 rtl8150_t;
  119. struct async_req {
  120. struct usb_ctrlrequest dr;
  121. u16 rx_creg;
  122. };
  123. static const char driver_name [] = "rtl8150";
  124. /*
  125. **
  126. ** device related part of the code
  127. **
  128. */
  129. static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
  130. {
  131. void *buf;
  132. int ret;
  133. buf = kmalloc(size, GFP_NOIO);
  134. if (!buf)
  135. return -ENOMEM;
  136. ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  137. RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
  138. indx, 0, buf, size, 500);
  139. if (ret > 0 && ret <= size)
  140. memcpy(data, buf, ret);
  141. kfree(buf);
  142. return ret;
  143. }
  144. static int set_registers(rtl8150_t * dev, u16 indx, u16 size, const void *data)
  145. {
  146. void *buf;
  147. int ret;
  148. buf = kmemdup(data, size, GFP_NOIO);
  149. if (!buf)
  150. return -ENOMEM;
  151. ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  152. RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
  153. indx, 0, buf, size, 500);
  154. kfree(buf);
  155. return ret;
  156. }
  157. static void async_set_reg_cb(struct urb *urb)
  158. {
  159. struct async_req *req = (struct async_req *)urb->context;
  160. int status = urb->status;
  161. if (status < 0)
  162. dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
  163. kfree(req);
  164. usb_free_urb(urb);
  165. }
  166. static int async_set_registers(rtl8150_t *dev, u16 indx, u16 size, u16 reg)
  167. {
  168. int res = -ENOMEM;
  169. struct urb *async_urb;
  170. struct async_req *req;
  171. req = kmalloc(sizeof(struct async_req), GFP_ATOMIC);
  172. if (req == NULL)
  173. return res;
  174. async_urb = usb_alloc_urb(0, GFP_ATOMIC);
  175. if (async_urb == NULL) {
  176. kfree(req);
  177. return res;
  178. }
  179. req->rx_creg = cpu_to_le16(reg);
  180. req->dr.bRequestType = RTL8150_REQT_WRITE;
  181. req->dr.bRequest = RTL8150_REQ_SET_REGS;
  182. req->dr.wIndex = 0;
  183. req->dr.wValue = cpu_to_le16(indx);
  184. req->dr.wLength = cpu_to_le16(size);
  185. usb_fill_control_urb(async_urb, dev->udev,
  186. usb_sndctrlpipe(dev->udev, 0), (void *)&req->dr,
  187. &req->rx_creg, size, async_set_reg_cb, req);
  188. res = usb_submit_urb(async_urb, GFP_ATOMIC);
  189. if (res) {
  190. if (res == -ENODEV)
  191. netif_device_detach(dev->netdev);
  192. dev_err(&dev->udev->dev, "%s failed with %d\n", __func__, res);
  193. }
  194. return res;
  195. }
  196. static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
  197. {
  198. int i;
  199. u8 data[3], tmp;
  200. data[0] = phy;
  201. data[1] = data[2] = 0;
  202. tmp = indx | PHY_READ | PHY_GO;
  203. i = 0;
  204. set_registers(dev, PHYADD, sizeof(data), data);
  205. set_registers(dev, PHYCNT, 1, &tmp);
  206. do {
  207. get_registers(dev, PHYCNT, 1, data);
  208. } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
  209. if (i <= MII_TIMEOUT) {
  210. get_registers(dev, PHYDAT, 2, data);
  211. *reg = data[0] | (data[1] << 8);
  212. return 0;
  213. } else
  214. return 1;
  215. }
  216. static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
  217. {
  218. int i;
  219. u8 data[3], tmp;
  220. data[0] = phy;
  221. data[1] = reg & 0xff;
  222. data[2] = (reg >> 8) & 0xff;
  223. tmp = indx | PHY_WRITE | PHY_GO;
  224. i = 0;
  225. set_registers(dev, PHYADD, sizeof(data), data);
  226. set_registers(dev, PHYCNT, 1, &tmp);
  227. do {
  228. get_registers(dev, PHYCNT, 1, data);
  229. } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
  230. if (i <= MII_TIMEOUT)
  231. return 0;
  232. else
  233. return 1;
  234. }
  235. static void set_ethernet_addr(rtl8150_t *dev)
  236. {
  237. u8 node_id[ETH_ALEN];
  238. int ret;
  239. ret = get_registers(dev, IDR, sizeof(node_id), node_id);
  240. if (ret == sizeof(node_id)) {
  241. ether_addr_copy(dev->netdev->dev_addr, node_id);
  242. } else {
  243. eth_hw_addr_random(dev->netdev);
  244. netdev_notice(dev->netdev, "Assigned a random MAC address: %pM\n",
  245. dev->netdev->dev_addr);
  246. }
  247. }
  248. static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
  249. {
  250. struct sockaddr *addr = p;
  251. rtl8150_t *dev = netdev_priv(netdev);
  252. if (netif_running(netdev))
  253. return -EBUSY;
  254. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  255. netdev_dbg(netdev, "Setting MAC address to %pM\n", netdev->dev_addr);
  256. /* Set the IDR registers. */
  257. set_registers(dev, IDR, netdev->addr_len, netdev->dev_addr);
  258. #ifdef EEPROM_WRITE
  259. {
  260. int i;
  261. u8 cr;
  262. /* Get the CR contents. */
  263. get_registers(dev, CR, 1, &cr);
  264. /* Set the WEPROM bit (eeprom write enable). */
  265. cr |= 0x20;
  266. set_registers(dev, CR, 1, &cr);
  267. /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
  268. so we need to split them up. */
  269. for (i = 0; i * 2 < netdev->addr_len; i++) {
  270. set_registers(dev, IDR_EEPROM + (i * 2), 2,
  271. netdev->dev_addr + (i * 2));
  272. }
  273. /* Clear the WEPROM bit (preventing accidental eeprom writes). */
  274. cr &= 0xdf;
  275. set_registers(dev, CR, 1, &cr);
  276. }
  277. #endif
  278. return 0;
  279. }
  280. static int rtl8150_reset(rtl8150_t * dev)
  281. {
  282. u8 data = 0x10;
  283. int i = HZ;
  284. set_registers(dev, CR, 1, &data);
  285. do {
  286. get_registers(dev, CR, 1, &data);
  287. } while ((data & 0x10) && --i);
  288. return (i > 0) ? 1 : 0;
  289. }
  290. static int alloc_all_urbs(rtl8150_t * dev)
  291. {
  292. dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  293. if (!dev->rx_urb)
  294. return 0;
  295. dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  296. if (!dev->tx_urb) {
  297. usb_free_urb(dev->rx_urb);
  298. return 0;
  299. }
  300. dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  301. if (!dev->intr_urb) {
  302. usb_free_urb(dev->rx_urb);
  303. usb_free_urb(dev->tx_urb);
  304. return 0;
  305. }
  306. return 1;
  307. }
  308. static void free_all_urbs(rtl8150_t * dev)
  309. {
  310. usb_free_urb(dev->rx_urb);
  311. usb_free_urb(dev->tx_urb);
  312. usb_free_urb(dev->intr_urb);
  313. }
  314. static void unlink_all_urbs(rtl8150_t * dev)
  315. {
  316. usb_kill_urb(dev->rx_urb);
  317. usb_kill_urb(dev->tx_urb);
  318. usb_kill_urb(dev->intr_urb);
  319. }
  320. static inline struct sk_buff *pull_skb(rtl8150_t *dev)
  321. {
  322. struct sk_buff *skb;
  323. int i;
  324. for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
  325. if (dev->rx_skb_pool[i]) {
  326. skb = dev->rx_skb_pool[i];
  327. dev->rx_skb_pool[i] = NULL;
  328. return skb;
  329. }
  330. }
  331. return NULL;
  332. }
  333. static void read_bulk_callback(struct urb *urb)
  334. {
  335. rtl8150_t *dev;
  336. unsigned pkt_len, res;
  337. struct sk_buff *skb;
  338. struct net_device *netdev;
  339. int status = urb->status;
  340. int result;
  341. unsigned long flags;
  342. dev = urb->context;
  343. if (!dev)
  344. return;
  345. if (test_bit(RTL8150_UNPLUG, &dev->flags))
  346. return;
  347. netdev = dev->netdev;
  348. if (!netif_device_present(netdev))
  349. return;
  350. switch (status) {
  351. case 0:
  352. break;
  353. case -ENOENT:
  354. return; /* the urb is in unlink state */
  355. case -ETIME:
  356. if (printk_ratelimit())
  357. dev_warn(&urb->dev->dev, "may be reset is needed?..\n");
  358. goto goon;
  359. default:
  360. if (printk_ratelimit())
  361. dev_warn(&urb->dev->dev, "Rx status %d\n", status);
  362. goto goon;
  363. }
  364. if (!dev->rx_skb)
  365. goto resched;
  366. /* protect against short packets (tell me why we got some?!?) */
  367. if (urb->actual_length < 4)
  368. goto goon;
  369. res = urb->actual_length;
  370. pkt_len = res - 4;
  371. skb_put(dev->rx_skb, pkt_len);
  372. dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
  373. netif_rx(dev->rx_skb);
  374. netdev->stats.rx_packets++;
  375. netdev->stats.rx_bytes += pkt_len;
  376. spin_lock_irqsave(&dev->rx_pool_lock, flags);
  377. skb = pull_skb(dev);
  378. spin_unlock_irqrestore(&dev->rx_pool_lock, flags);
  379. if (!skb)
  380. goto resched;
  381. dev->rx_skb = skb;
  382. goon:
  383. usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
  384. dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
  385. result = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
  386. if (result == -ENODEV)
  387. netif_device_detach(dev->netdev);
  388. else if (result) {
  389. set_bit(RX_URB_FAIL, &dev->flags);
  390. goto resched;
  391. } else {
  392. clear_bit(RX_URB_FAIL, &dev->flags);
  393. }
  394. return;
  395. resched:
  396. tasklet_schedule(&dev->tl);
  397. }
  398. static void write_bulk_callback(struct urb *urb)
  399. {
  400. rtl8150_t *dev;
  401. int status = urb->status;
  402. dev = urb->context;
  403. if (!dev)
  404. return;
  405. dev_kfree_skb_irq(dev->tx_skb);
  406. if (!netif_device_present(dev->netdev))
  407. return;
  408. if (status)
  409. dev_info(&urb->dev->dev, "%s: Tx status %d\n",
  410. dev->netdev->name, status);
  411. netif_trans_update(dev->netdev);
  412. netif_wake_queue(dev->netdev);
  413. }
  414. static void intr_callback(struct urb *urb)
  415. {
  416. rtl8150_t *dev;
  417. __u8 *d;
  418. int status = urb->status;
  419. int res;
  420. dev = urb->context;
  421. if (!dev)
  422. return;
  423. switch (status) {
  424. case 0: /* success */
  425. break;
  426. case -ECONNRESET: /* unlink */
  427. case -ENOENT:
  428. case -ESHUTDOWN:
  429. return;
  430. /* -EPIPE: should clear the halt */
  431. default:
  432. dev_info(&urb->dev->dev, "%s: intr status %d\n",
  433. dev->netdev->name, status);
  434. goto resubmit;
  435. }
  436. d = urb->transfer_buffer;
  437. if (d[0] & TSR_ERRORS) {
  438. dev->netdev->stats.tx_errors++;
  439. if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
  440. dev->netdev->stats.tx_aborted_errors++;
  441. if (d[INT_TSR] & TSR_LCOL)
  442. dev->netdev->stats.tx_window_errors++;
  443. if (d[INT_TSR] & TSR_LOSS_CRS)
  444. dev->netdev->stats.tx_carrier_errors++;
  445. }
  446. /* Report link status changes to the network stack */
  447. if ((d[INT_MSR] & MSR_LINK) == 0) {
  448. if (netif_carrier_ok(dev->netdev)) {
  449. netif_carrier_off(dev->netdev);
  450. netdev_dbg(dev->netdev, "%s: LINK LOST\n", __func__);
  451. }
  452. } else {
  453. if (!netif_carrier_ok(dev->netdev)) {
  454. netif_carrier_on(dev->netdev);
  455. netdev_dbg(dev->netdev, "%s: LINK CAME BACK\n", __func__);
  456. }
  457. }
  458. resubmit:
  459. res = usb_submit_urb (urb, GFP_ATOMIC);
  460. if (res == -ENODEV)
  461. netif_device_detach(dev->netdev);
  462. else if (res)
  463. dev_err(&dev->udev->dev,
  464. "can't resubmit intr, %s-%s/input0, status %d\n",
  465. dev->udev->bus->bus_name, dev->udev->devpath, res);
  466. }
  467. static int rtl8150_suspend(struct usb_interface *intf, pm_message_t message)
  468. {
  469. rtl8150_t *dev = usb_get_intfdata(intf);
  470. netif_device_detach(dev->netdev);
  471. if (netif_running(dev->netdev)) {
  472. usb_kill_urb(dev->rx_urb);
  473. usb_kill_urb(dev->intr_urb);
  474. }
  475. return 0;
  476. }
  477. static int rtl8150_resume(struct usb_interface *intf)
  478. {
  479. rtl8150_t *dev = usb_get_intfdata(intf);
  480. netif_device_attach(dev->netdev);
  481. if (netif_running(dev->netdev)) {
  482. dev->rx_urb->status = 0;
  483. dev->rx_urb->actual_length = 0;
  484. read_bulk_callback(dev->rx_urb);
  485. dev->intr_urb->status = 0;
  486. dev->intr_urb->actual_length = 0;
  487. intr_callback(dev->intr_urb);
  488. }
  489. return 0;
  490. }
  491. /*
  492. **
  493. ** network related part of the code
  494. **
  495. */
  496. static void fill_skb_pool(rtl8150_t *dev)
  497. {
  498. struct sk_buff *skb;
  499. int i;
  500. for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
  501. if (dev->rx_skb_pool[i])
  502. continue;
  503. skb = dev_alloc_skb(RTL8150_MTU + 2);
  504. if (!skb) {
  505. return;
  506. }
  507. skb_reserve(skb, 2);
  508. dev->rx_skb_pool[i] = skb;
  509. }
  510. }
  511. static void free_skb_pool(rtl8150_t *dev)
  512. {
  513. int i;
  514. for (i = 0; i < RX_SKB_POOL_SIZE; i++)
  515. dev_kfree_skb(dev->rx_skb_pool[i]);
  516. }
  517. static void rx_fixup(unsigned long data)
  518. {
  519. struct rtl8150 *dev = (struct rtl8150 *)data;
  520. struct sk_buff *skb;
  521. int status;
  522. spin_lock_irq(&dev->rx_pool_lock);
  523. fill_skb_pool(dev);
  524. spin_unlock_irq(&dev->rx_pool_lock);
  525. if (test_bit(RX_URB_FAIL, &dev->flags))
  526. if (dev->rx_skb)
  527. goto try_again;
  528. spin_lock_irq(&dev->rx_pool_lock);
  529. skb = pull_skb(dev);
  530. spin_unlock_irq(&dev->rx_pool_lock);
  531. if (skb == NULL)
  532. goto tlsched;
  533. dev->rx_skb = skb;
  534. usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
  535. dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
  536. try_again:
  537. status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
  538. if (status == -ENODEV) {
  539. netif_device_detach(dev->netdev);
  540. } else if (status) {
  541. set_bit(RX_URB_FAIL, &dev->flags);
  542. goto tlsched;
  543. } else {
  544. clear_bit(RX_URB_FAIL, &dev->flags);
  545. }
  546. return;
  547. tlsched:
  548. tasklet_schedule(&dev->tl);
  549. }
  550. static int enable_net_traffic(rtl8150_t * dev)
  551. {
  552. u8 cr, tcr, rcr, msr;
  553. if (!rtl8150_reset(dev)) {
  554. dev_warn(&dev->udev->dev, "device reset failed\n");
  555. }
  556. /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
  557. rcr = 0x9e;
  558. tcr = 0xd8;
  559. cr = 0x0c;
  560. if (!(rcr & 0x80))
  561. set_bit(RTL8150_HW_CRC, &dev->flags);
  562. set_registers(dev, RCR, 1, &rcr);
  563. set_registers(dev, TCR, 1, &tcr);
  564. set_registers(dev, CR, 1, &cr);
  565. get_registers(dev, MSR, 1, &msr);
  566. return 0;
  567. }
  568. static void disable_net_traffic(rtl8150_t * dev)
  569. {
  570. u8 cr;
  571. get_registers(dev, CR, 1, &cr);
  572. cr &= 0xf3;
  573. set_registers(dev, CR, 1, &cr);
  574. }
  575. static void rtl8150_tx_timeout(struct net_device *netdev)
  576. {
  577. rtl8150_t *dev = netdev_priv(netdev);
  578. dev_warn(&netdev->dev, "Tx timeout.\n");
  579. usb_unlink_urb(dev->tx_urb);
  580. netdev->stats.tx_errors++;
  581. }
  582. static void rtl8150_set_multicast(struct net_device *netdev)
  583. {
  584. rtl8150_t *dev = netdev_priv(netdev);
  585. u16 rx_creg = 0x9e;
  586. netif_stop_queue(netdev);
  587. if (netdev->flags & IFF_PROMISC) {
  588. rx_creg |= 0x0001;
  589. dev_info(&netdev->dev, "%s: promiscuous mode\n", netdev->name);
  590. } else if (!netdev_mc_empty(netdev) ||
  591. (netdev->flags & IFF_ALLMULTI)) {
  592. rx_creg &= 0xfffe;
  593. rx_creg |= 0x0002;
  594. dev_dbg(&netdev->dev, "%s: allmulti set\n", netdev->name);
  595. } else {
  596. /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
  597. rx_creg &= 0x00fc;
  598. }
  599. async_set_registers(dev, RCR, sizeof(rx_creg), rx_creg);
  600. netif_wake_queue(netdev);
  601. }
  602. static netdev_tx_t rtl8150_start_xmit(struct sk_buff *skb,
  603. struct net_device *netdev)
  604. {
  605. rtl8150_t *dev = netdev_priv(netdev);
  606. int count, res;
  607. netif_stop_queue(netdev);
  608. count = (skb->len < 60) ? 60 : skb->len;
  609. count = (count & 0x3f) ? count : count + 1;
  610. dev->tx_skb = skb;
  611. usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
  612. skb->data, count, write_bulk_callback, dev);
  613. if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
  614. /* Can we get/handle EPIPE here? */
  615. if (res == -ENODEV)
  616. netif_device_detach(dev->netdev);
  617. else {
  618. dev_warn(&netdev->dev, "failed tx_urb %d\n", res);
  619. netdev->stats.tx_errors++;
  620. netif_start_queue(netdev);
  621. }
  622. } else {
  623. netdev->stats.tx_packets++;
  624. netdev->stats.tx_bytes += skb->len;
  625. netif_trans_update(netdev);
  626. }
  627. return NETDEV_TX_OK;
  628. }
  629. static void set_carrier(struct net_device *netdev)
  630. {
  631. rtl8150_t *dev = netdev_priv(netdev);
  632. short tmp;
  633. get_registers(dev, CSCR, 2, &tmp);
  634. if (tmp & CSCR_LINK_STATUS)
  635. netif_carrier_on(netdev);
  636. else
  637. netif_carrier_off(netdev);
  638. }
  639. static int rtl8150_open(struct net_device *netdev)
  640. {
  641. rtl8150_t *dev = netdev_priv(netdev);
  642. int res;
  643. if (dev->rx_skb == NULL)
  644. dev->rx_skb = pull_skb(dev);
  645. if (!dev->rx_skb)
  646. return -ENOMEM;
  647. set_registers(dev, IDR, 6, netdev->dev_addr);
  648. usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
  649. dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
  650. if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {
  651. if (res == -ENODEV)
  652. netif_device_detach(dev->netdev);
  653. dev_warn(&netdev->dev, "rx_urb submit failed: %d\n", res);
  654. return res;
  655. }
  656. usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
  657. dev->intr_buff, INTBUFSIZE, intr_callback,
  658. dev, dev->intr_interval);
  659. if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL))) {
  660. if (res == -ENODEV)
  661. netif_device_detach(dev->netdev);
  662. dev_warn(&netdev->dev, "intr_urb submit failed: %d\n", res);
  663. usb_kill_urb(dev->rx_urb);
  664. return res;
  665. }
  666. enable_net_traffic(dev);
  667. set_carrier(netdev);
  668. netif_start_queue(netdev);
  669. return res;
  670. }
  671. static int rtl8150_close(struct net_device *netdev)
  672. {
  673. rtl8150_t *dev = netdev_priv(netdev);
  674. netif_stop_queue(netdev);
  675. if (!test_bit(RTL8150_UNPLUG, &dev->flags))
  676. disable_net_traffic(dev);
  677. unlink_all_urbs(dev);
  678. return 0;
  679. }
  680. static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
  681. {
  682. rtl8150_t *dev = netdev_priv(netdev);
  683. strlcpy(info->driver, driver_name, sizeof(info->driver));
  684. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  685. usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
  686. }
  687. static int rtl8150_get_link_ksettings(struct net_device *netdev,
  688. struct ethtool_link_ksettings *ecmd)
  689. {
  690. rtl8150_t *dev = netdev_priv(netdev);
  691. short lpa, bmcr;
  692. u32 supported;
  693. supported = (SUPPORTED_10baseT_Half |
  694. SUPPORTED_10baseT_Full |
  695. SUPPORTED_100baseT_Half |
  696. SUPPORTED_100baseT_Full |
  697. SUPPORTED_Autoneg |
  698. SUPPORTED_TP | SUPPORTED_MII);
  699. ecmd->base.port = PORT_TP;
  700. ecmd->base.phy_address = dev->phy;
  701. get_registers(dev, BMCR, 2, &bmcr);
  702. get_registers(dev, ANLP, 2, &lpa);
  703. if (bmcr & BMCR_ANENABLE) {
  704. u32 speed = ((lpa & (LPA_100HALF | LPA_100FULL)) ?
  705. SPEED_100 : SPEED_10);
  706. ecmd->base.speed = speed;
  707. ecmd->base.autoneg = AUTONEG_ENABLE;
  708. if (speed == SPEED_100)
  709. ecmd->base.duplex = (lpa & LPA_100FULL) ?
  710. DUPLEX_FULL : DUPLEX_HALF;
  711. else
  712. ecmd->base.duplex = (lpa & LPA_10FULL) ?
  713. DUPLEX_FULL : DUPLEX_HALF;
  714. } else {
  715. ecmd->base.autoneg = AUTONEG_DISABLE;
  716. ecmd->base.speed = ((bmcr & BMCR_SPEED100) ?
  717. SPEED_100 : SPEED_10);
  718. ecmd->base.duplex = (bmcr & BMCR_FULLDPLX) ?
  719. DUPLEX_FULL : DUPLEX_HALF;
  720. }
  721. ethtool_convert_legacy_u32_to_link_mode(ecmd->link_modes.supported,
  722. supported);
  723. return 0;
  724. }
  725. static const struct ethtool_ops ops = {
  726. .get_drvinfo = rtl8150_get_drvinfo,
  727. .get_link = ethtool_op_get_link,
  728. .get_link_ksettings = rtl8150_get_link_ksettings,
  729. };
  730. static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  731. {
  732. rtl8150_t *dev = netdev_priv(netdev);
  733. u16 *data = (u16 *) & rq->ifr_ifru;
  734. int res = 0;
  735. switch (cmd) {
  736. case SIOCDEVPRIVATE:
  737. data[0] = dev->phy;
  738. /* fall through */
  739. case SIOCDEVPRIVATE + 1:
  740. read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
  741. break;
  742. case SIOCDEVPRIVATE + 2:
  743. if (!capable(CAP_NET_ADMIN))
  744. return -EPERM;
  745. write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
  746. break;
  747. default:
  748. res = -EOPNOTSUPP;
  749. }
  750. return res;
  751. }
  752. static const struct net_device_ops rtl8150_netdev_ops = {
  753. .ndo_open = rtl8150_open,
  754. .ndo_stop = rtl8150_close,
  755. .ndo_do_ioctl = rtl8150_ioctl,
  756. .ndo_start_xmit = rtl8150_start_xmit,
  757. .ndo_tx_timeout = rtl8150_tx_timeout,
  758. .ndo_set_rx_mode = rtl8150_set_multicast,
  759. .ndo_set_mac_address = rtl8150_set_mac_address,
  760. .ndo_validate_addr = eth_validate_addr,
  761. };
  762. static int rtl8150_probe(struct usb_interface *intf,
  763. const struct usb_device_id *id)
  764. {
  765. struct usb_device *udev = interface_to_usbdev(intf);
  766. rtl8150_t *dev;
  767. struct net_device *netdev;
  768. netdev = alloc_etherdev(sizeof(rtl8150_t));
  769. if (!netdev)
  770. return -ENOMEM;
  771. dev = netdev_priv(netdev);
  772. dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
  773. if (!dev->intr_buff) {
  774. free_netdev(netdev);
  775. return -ENOMEM;
  776. }
  777. tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
  778. spin_lock_init(&dev->rx_pool_lock);
  779. dev->udev = udev;
  780. dev->netdev = netdev;
  781. netdev->netdev_ops = &rtl8150_netdev_ops;
  782. netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
  783. netdev->ethtool_ops = &ops;
  784. dev->intr_interval = 100; /* 100ms */
  785. if (!alloc_all_urbs(dev)) {
  786. dev_err(&intf->dev, "out of memory\n");
  787. goto out;
  788. }
  789. if (!rtl8150_reset(dev)) {
  790. dev_err(&intf->dev, "couldn't reset the device\n");
  791. goto out1;
  792. }
  793. fill_skb_pool(dev);
  794. set_ethernet_addr(dev);
  795. usb_set_intfdata(intf, dev);
  796. SET_NETDEV_DEV(netdev, &intf->dev);
  797. if (register_netdev(netdev) != 0) {
  798. dev_err(&intf->dev, "couldn't register the device\n");
  799. goto out2;
  800. }
  801. dev_info(&intf->dev, "%s: rtl8150 is detected\n", netdev->name);
  802. return 0;
  803. out2:
  804. usb_set_intfdata(intf, NULL);
  805. free_skb_pool(dev);
  806. out1:
  807. free_all_urbs(dev);
  808. out:
  809. kfree(dev->intr_buff);
  810. free_netdev(netdev);
  811. return -EIO;
  812. }
  813. static void rtl8150_disconnect(struct usb_interface *intf)
  814. {
  815. rtl8150_t *dev = usb_get_intfdata(intf);
  816. usb_set_intfdata(intf, NULL);
  817. if (dev) {
  818. set_bit(RTL8150_UNPLUG, &dev->flags);
  819. tasklet_kill(&dev->tl);
  820. unregister_netdev(dev->netdev);
  821. unlink_all_urbs(dev);
  822. free_all_urbs(dev);
  823. free_skb_pool(dev);
  824. dev_kfree_skb(dev->rx_skb);
  825. kfree(dev->intr_buff);
  826. free_netdev(dev->netdev);
  827. }
  828. }
  829. static struct usb_driver rtl8150_driver = {
  830. .name = driver_name,
  831. .probe = rtl8150_probe,
  832. .disconnect = rtl8150_disconnect,
  833. .id_table = rtl8150_table,
  834. .suspend = rtl8150_suspend,
  835. .resume = rtl8150_resume,
  836. .disable_hub_initiated_lpm = 1,
  837. };
  838. module_usb_driver(rtl8150_driver);
  839. MODULE_AUTHOR(DRIVER_AUTHOR);
  840. MODULE_DESCRIPTION(DRIVER_DESC);
  841. MODULE_LICENSE("GPL");