rtl8150.c 22 KB

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