rtl8150.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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 unsigned long multicast_filter_limit = 32;
  146. static void fill_skb_pool(rtl8150_t *);
  147. static void free_skb_pool(rtl8150_t *);
  148. static inline struct sk_buff *pull_skb(rtl8150_t *);
  149. static void rtl8150_disconnect(struct usb_interface *intf);
  150. static int rtl8150_probe(struct usb_interface *intf,
  151. const struct usb_device_id *id);
  152. static const char driver_name [] = "rtl8150";
  153. static struct usb_driver rtl8150_driver = {
  154. .owner = THIS_MODULE,
  155. .name = driver_name,
  156. .probe = rtl8150_probe,
  157. .disconnect = rtl8150_disconnect,
  158. .id_table = rtl8150_table,
  159. };
  160. /*
  161. **
  162. ** device related part of the code
  163. **
  164. */
  165. static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
  166. {
  167. return usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  168. RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
  169. indx, 0, data, size, 500);
  170. }
  171. static int set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
  172. {
  173. return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  174. RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
  175. indx, 0, data, size, 500);
  176. }
  177. static void ctrl_callback(struct urb *urb, struct pt_regs *regs)
  178. {
  179. rtl8150_t *dev;
  180. switch (urb->status) {
  181. case 0:
  182. break;
  183. case -EINPROGRESS:
  184. break;
  185. case -ENOENT:
  186. break;
  187. default:
  188. warn("ctrl urb status %d", urb->status);
  189. }
  190. dev = urb->context;
  191. clear_bit(RX_REG_SET, &dev->flags);
  192. }
  193. static int async_set_registers(rtl8150_t * dev, u16 indx, u16 size)
  194. {
  195. int ret;
  196. if (test_bit(RX_REG_SET, &dev->flags))
  197. return -EAGAIN;
  198. dev->dr.bRequestType = RTL8150_REQT_WRITE;
  199. dev->dr.bRequest = RTL8150_REQ_SET_REGS;
  200. dev->dr.wValue = cpu_to_le16(indx);
  201. dev->dr.wIndex = 0;
  202. dev->dr.wLength = cpu_to_le16(size);
  203. dev->ctrl_urb->transfer_buffer_length = size;
  204. usb_fill_control_urb(dev->ctrl_urb, dev->udev,
  205. usb_sndctrlpipe(dev->udev, 0), (char *) &dev->dr,
  206. &dev->rx_creg, size, ctrl_callback, dev);
  207. if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC)))
  208. err("control request submission failed: %d", ret);
  209. else
  210. set_bit(RX_REG_SET, &dev->flags);
  211. return ret;
  212. }
  213. static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
  214. {
  215. int i;
  216. u8 data[3], tmp;
  217. data[0] = phy;
  218. data[1] = data[2] = 0;
  219. tmp = indx | PHY_READ | PHY_GO;
  220. i = 0;
  221. set_registers(dev, PHYADD, sizeof(data), data);
  222. set_registers(dev, PHYCNT, 1, &tmp);
  223. do {
  224. get_registers(dev, PHYCNT, 1, data);
  225. } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
  226. if (i < MII_TIMEOUT) {
  227. get_registers(dev, PHYDAT, 2, data);
  228. *reg = data[0] | (data[1] << 8);
  229. return 0;
  230. } else
  231. return 1;
  232. }
  233. static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
  234. {
  235. int i;
  236. u8 data[3], tmp;
  237. data[0] = phy;
  238. *(data + 1) = cpu_to_le16p(&reg);
  239. tmp = indx | PHY_WRITE | PHY_GO;
  240. i = 0;
  241. set_registers(dev, PHYADD, sizeof(data), data);
  242. set_registers(dev, PHYCNT, 1, &tmp);
  243. do {
  244. get_registers(dev, PHYCNT, 1, data);
  245. } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
  246. if (i < MII_TIMEOUT)
  247. return 0;
  248. else
  249. return 1;
  250. }
  251. static inline void set_ethernet_addr(rtl8150_t * dev)
  252. {
  253. u8 node_id[6];
  254. get_registers(dev, IDR, sizeof(node_id), node_id);
  255. memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
  256. }
  257. static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
  258. {
  259. struct sockaddr *addr = p;
  260. rtl8150_t *dev = netdev_priv(netdev);
  261. int i;
  262. if (netif_running(netdev))
  263. return -EBUSY;
  264. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  265. dbg("%s: Setting MAC address to ", netdev->name);
  266. for (i = 0; i < 5; i++)
  267. dbg("%02X:", netdev->dev_addr[i]);
  268. dbg("%02X\n", netdev->dev_addr[i]);
  269. /* Set the IDR registers. */
  270. set_registers(dev, IDR, sizeof(netdev->dev_addr), netdev->dev_addr);
  271. #ifdef EEPROM_WRITE
  272. {
  273. u8 cr;
  274. /* Get the CR contents. */
  275. get_registers(dev, CR, 1, &cr);
  276. /* Set the WEPROM bit (eeprom write enable). */
  277. cr |= 0x20;
  278. set_registers(dev, CR, 1, &cr);
  279. /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
  280. so we need to split them up. */
  281. for (i = 0; i * 2 < netdev->addr_len; i++) {
  282. set_registers(dev, IDR_EEPROM + (i * 2), 2,
  283. netdev->dev_addr + (i * 2));
  284. }
  285. /* Clear the WEPROM bit (preventing accidental eeprom writes). */
  286. cr &= 0xdf;
  287. set_registers(dev, CR, 1, &cr);
  288. }
  289. #endif
  290. return 0;
  291. }
  292. static int rtl8150_reset(rtl8150_t * dev)
  293. {
  294. u8 data = 0x10;
  295. int i = HZ;
  296. set_registers(dev, CR, 1, &data);
  297. do {
  298. get_registers(dev, CR, 1, &data);
  299. } while ((data & 0x10) && --i);
  300. return (i > 0) ? 1 : 0;
  301. }
  302. static int alloc_all_urbs(rtl8150_t * dev)
  303. {
  304. dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  305. if (!dev->rx_urb)
  306. return 0;
  307. dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  308. if (!dev->tx_urb) {
  309. usb_free_urb(dev->rx_urb);
  310. return 0;
  311. }
  312. dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  313. if (!dev->intr_urb) {
  314. usb_free_urb(dev->rx_urb);
  315. usb_free_urb(dev->tx_urb);
  316. return 0;
  317. }
  318. dev->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
  319. if (!dev->intr_urb) {
  320. usb_free_urb(dev->rx_urb);
  321. usb_free_urb(dev->tx_urb);
  322. usb_free_urb(dev->intr_urb);
  323. return 0;
  324. }
  325. return 1;
  326. }
  327. static void free_all_urbs(rtl8150_t * dev)
  328. {
  329. usb_free_urb(dev->rx_urb);
  330. usb_free_urb(dev->tx_urb);
  331. usb_free_urb(dev->intr_urb);
  332. usb_free_urb(dev->ctrl_urb);
  333. }
  334. static void unlink_all_urbs(rtl8150_t * dev)
  335. {
  336. usb_kill_urb(dev->rx_urb);
  337. usb_kill_urb(dev->tx_urb);
  338. usb_kill_urb(dev->intr_urb);
  339. usb_kill_urb(dev->ctrl_urb);
  340. }
  341. static inline struct sk_buff *pull_skb(rtl8150_t *dev)
  342. {
  343. struct sk_buff *skb;
  344. int i;
  345. for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
  346. if (dev->rx_skb_pool[i]) {
  347. skb = dev->rx_skb_pool[i];
  348. dev->rx_skb_pool[i] = NULL;
  349. return skb;
  350. }
  351. }
  352. return NULL;
  353. }
  354. static void read_bulk_callback(struct urb *urb, struct pt_regs *regs)
  355. {
  356. rtl8150_t *dev;
  357. unsigned pkt_len, res;
  358. struct sk_buff *skb;
  359. struct net_device *netdev;
  360. u16 rx_stat;
  361. dev = urb->context;
  362. if (!dev)
  363. return;
  364. if (test_bit(RTL8150_UNPLUG, &dev->flags))
  365. return;
  366. netdev = dev->netdev;
  367. if (!netif_device_present(netdev))
  368. return;
  369. switch (urb->status) {
  370. case 0:
  371. break;
  372. case -ENOENT:
  373. return; /* the urb is in unlink state */
  374. case -ETIMEDOUT:
  375. warn("may be reset is needed?..");
  376. goto goon;
  377. default:
  378. warn("Rx status %d", urb->status);
  379. goto goon;
  380. }
  381. if (!dev->rx_skb)
  382. goto resched;
  383. /* protect against short packets (tell me why we got some?!?) */
  384. if (urb->actual_length < 4)
  385. goto goon;
  386. res = urb->actual_length;
  387. rx_stat = le16_to_cpu(*(__le16 *)(urb->transfer_buffer + res - 4));
  388. pkt_len = res - 4;
  389. skb_put(dev->rx_skb, pkt_len);
  390. dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
  391. netif_rx(dev->rx_skb);
  392. dev->stats.rx_packets++;
  393. dev->stats.rx_bytes += pkt_len;
  394. spin_lock(&dev->rx_pool_lock);
  395. skb = pull_skb(dev);
  396. spin_unlock(&dev->rx_pool_lock);
  397. if (!skb)
  398. goto resched;
  399. dev->rx_skb = skb;
  400. goon:
  401. usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
  402. dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
  403. if (usb_submit_urb(dev->rx_urb, GFP_ATOMIC)) {
  404. set_bit(RX_URB_FAIL, &dev->flags);
  405. goto resched;
  406. } else {
  407. clear_bit(RX_URB_FAIL, &dev->flags);
  408. }
  409. return;
  410. resched:
  411. tasklet_schedule(&dev->tl);
  412. }
  413. static void rx_fixup(unsigned long data)
  414. {
  415. rtl8150_t *dev;
  416. struct sk_buff *skb;
  417. dev = (rtl8150_t *)data;
  418. spin_lock_irq(&dev->rx_pool_lock);
  419. fill_skb_pool(dev);
  420. spin_unlock_irq(&dev->rx_pool_lock);
  421. if (test_bit(RX_URB_FAIL, &dev->flags))
  422. if (dev->rx_skb)
  423. goto try_again;
  424. spin_lock_irq(&dev->rx_pool_lock);
  425. skb = pull_skb(dev);
  426. spin_unlock_irq(&dev->rx_pool_lock);
  427. if (skb == NULL)
  428. goto tlsched;
  429. dev->rx_skb = skb;
  430. usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
  431. dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
  432. try_again:
  433. if (usb_submit_urb(dev->rx_urb, GFP_ATOMIC)) {
  434. set_bit(RX_URB_FAIL, &dev->flags);
  435. goto tlsched;
  436. } else {
  437. clear_bit(RX_URB_FAIL, &dev->flags);
  438. }
  439. return;
  440. tlsched:
  441. tasklet_schedule(&dev->tl);
  442. }
  443. static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
  444. {
  445. rtl8150_t *dev;
  446. dev = urb->context;
  447. if (!dev)
  448. return;
  449. dev_kfree_skb_irq(dev->tx_skb);
  450. if (!netif_device_present(dev->netdev))
  451. return;
  452. if (urb->status)
  453. info("%s: Tx status %d", dev->netdev->name, urb->status);
  454. dev->netdev->trans_start = jiffies;
  455. netif_wake_queue(dev->netdev);
  456. }
  457. static void intr_callback(struct urb *urb, struct pt_regs *regs)
  458. {
  459. rtl8150_t *dev;
  460. __u8 *d;
  461. int status;
  462. dev = urb->context;
  463. if (!dev)
  464. return;
  465. switch (urb->status) {
  466. case 0: /* success */
  467. break;
  468. case -ECONNRESET: /* unlink */
  469. case -ENOENT:
  470. case -ESHUTDOWN:
  471. return;
  472. /* -EPIPE: should clear the halt */
  473. default:
  474. info("%s: intr status %d", dev->netdev->name, urb->status);
  475. goto resubmit;
  476. }
  477. d = urb->transfer_buffer;
  478. if (d[0] & TSR_ERRORS) {
  479. dev->stats.tx_errors++;
  480. if (d[INT_TSR] & (TSR_ECOL | TSR_JBR))
  481. dev->stats.tx_aborted_errors++;
  482. if (d[INT_TSR] & TSR_LCOL)
  483. dev->stats.tx_window_errors++;
  484. if (d[INT_TSR] & TSR_LOSS_CRS)
  485. dev->stats.tx_carrier_errors++;
  486. }
  487. /* Report link status changes to the network stack */
  488. if ((d[INT_MSR] & MSR_LINK) == 0) {
  489. if (netif_carrier_ok(dev->netdev)) {
  490. netif_carrier_off(dev->netdev);
  491. dbg("%s: LINK LOST\n", __func__);
  492. }
  493. } else {
  494. if (!netif_carrier_ok(dev->netdev)) {
  495. netif_carrier_on(dev->netdev);
  496. dbg("%s: LINK CAME BACK\n", __func__);
  497. }
  498. }
  499. resubmit:
  500. status = usb_submit_urb (urb, SLAB_ATOMIC);
  501. if (status)
  502. err ("can't resubmit intr, %s-%s/input0, status %d",
  503. dev->udev->bus->bus_name,
  504. dev->udev->devpath, status);
  505. }
  506. /*
  507. **
  508. ** network related part of the code
  509. **
  510. */
  511. static void fill_skb_pool(rtl8150_t *dev)
  512. {
  513. struct sk_buff *skb;
  514. int i;
  515. for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
  516. if (dev->rx_skb_pool[i])
  517. continue;
  518. skb = dev_alloc_skb(RTL8150_MTU + 2);
  519. if (!skb) {
  520. return;
  521. }
  522. skb->dev = dev->netdev;
  523. skb_reserve(skb, 2);
  524. dev->rx_skb_pool[i] = skb;
  525. }
  526. }
  527. static void free_skb_pool(rtl8150_t *dev)
  528. {
  529. int i;
  530. for (i = 0; i < RX_SKB_POOL_SIZE; i++)
  531. if (dev->rx_skb_pool[i])
  532. dev_kfree_skb(dev->rx_skb_pool[i]);
  533. }
  534. static int enable_net_traffic(rtl8150_t * dev)
  535. {
  536. u8 cr, tcr, rcr, msr;
  537. if (!rtl8150_reset(dev)) {
  538. warn("%s - device reset failed", __FUNCTION__);
  539. }
  540. /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
  541. rcr = 0x9e;
  542. dev->rx_creg = cpu_to_le16(rcr);
  543. tcr = 0xd8;
  544. cr = 0x0c;
  545. if (!(rcr & 0x80))
  546. set_bit(RTL8150_HW_CRC, &dev->flags);
  547. set_registers(dev, RCR, 1, &rcr);
  548. set_registers(dev, TCR, 1, &tcr);
  549. set_registers(dev, CR, 1, &cr);
  550. get_registers(dev, MSR, 1, &msr);
  551. return 0;
  552. }
  553. static void disable_net_traffic(rtl8150_t * dev)
  554. {
  555. u8 cr;
  556. get_registers(dev, CR, 1, &cr);
  557. cr &= 0xf3;
  558. set_registers(dev, CR, 1, &cr);
  559. }
  560. static struct net_device_stats *rtl8150_netdev_stats(struct net_device *dev)
  561. {
  562. return &((rtl8150_t *)netdev_priv(dev))->stats;
  563. }
  564. static void rtl8150_tx_timeout(struct net_device *netdev)
  565. {
  566. rtl8150_t *dev = netdev_priv(netdev);
  567. warn("%s: Tx timeout.", netdev->name);
  568. dev->tx_urb->transfer_flags |= URB_ASYNC_UNLINK;
  569. usb_unlink_urb(dev->tx_urb);
  570. dev->stats.tx_errors++;
  571. }
  572. static void rtl8150_set_multicast(struct net_device *netdev)
  573. {
  574. rtl8150_t *dev = netdev_priv(netdev);
  575. netif_stop_queue(netdev);
  576. if (netdev->flags & IFF_PROMISC) {
  577. dev->rx_creg |= cpu_to_le16(0x0001);
  578. info("%s: promiscuous mode", netdev->name);
  579. } else if (netdev->mc_count ||
  580. (netdev->flags & IFF_ALLMULTI)) {
  581. dev->rx_creg &= cpu_to_le16(0xfffe);
  582. dev->rx_creg |= cpu_to_le16(0x0002);
  583. info("%s: allmulti set", netdev->name);
  584. } else {
  585. /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
  586. dev->rx_creg &= cpu_to_le16(0x00fc);
  587. }
  588. async_set_registers(dev, RCR, 2);
  589. netif_wake_queue(netdev);
  590. }
  591. static int rtl8150_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  592. {
  593. rtl8150_t *dev = netdev_priv(netdev);
  594. int count, res;
  595. netif_stop_queue(netdev);
  596. count = (skb->len < 60) ? 60 : skb->len;
  597. count = (count & 0x3f) ? count : count + 1;
  598. dev->tx_skb = skb;
  599. usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
  600. skb->data, count, write_bulk_callback, dev);
  601. if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
  602. warn("failed tx_urb %d\n", res);
  603. dev->stats.tx_errors++;
  604. netif_start_queue(netdev);
  605. } else {
  606. dev->stats.tx_packets++;
  607. dev->stats.tx_bytes += skb->len;
  608. netdev->trans_start = jiffies;
  609. }
  610. return 0;
  611. }
  612. static void set_carrier(struct net_device *netdev)
  613. {
  614. rtl8150_t *dev = netdev_priv(netdev);
  615. short tmp;
  616. get_registers(dev, CSCR, 2, &tmp);
  617. if (tmp & CSCR_LINK_STATUS)
  618. netif_carrier_on(netdev);
  619. else
  620. netif_carrier_off(netdev);
  621. }
  622. static int rtl8150_open(struct net_device *netdev)
  623. {
  624. rtl8150_t *dev = netdev_priv(netdev);
  625. int res;
  626. if (dev->rx_skb == NULL)
  627. dev->rx_skb = pull_skb(dev);
  628. if (!dev->rx_skb)
  629. return -ENOMEM;
  630. set_registers(dev, IDR, 6, netdev->dev_addr);
  631. usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
  632. dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
  633. if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL)))
  634. warn("%s: rx_urb submit failed: %d", __FUNCTION__, res);
  635. usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
  636. dev->intr_buff, INTBUFSIZE, intr_callback,
  637. dev, dev->intr_interval);
  638. if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL)))
  639. warn("%s: intr_urb submit failed: %d", __FUNCTION__, res);
  640. netif_start_queue(netdev);
  641. enable_net_traffic(dev);
  642. set_carrier(netdev);
  643. return res;
  644. }
  645. static int rtl8150_close(struct net_device *netdev)
  646. {
  647. rtl8150_t *dev = netdev_priv(netdev);
  648. int res = 0;
  649. netif_stop_queue(netdev);
  650. if (!test_bit(RTL8150_UNPLUG, &dev->flags))
  651. disable_net_traffic(dev);
  652. unlink_all_urbs(dev);
  653. return res;
  654. }
  655. static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
  656. {
  657. rtl8150_t *dev = netdev_priv(netdev);
  658. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  659. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  660. usb_make_path(dev->udev, info->bus_info, sizeof info->bus_info);
  661. }
  662. static int rtl8150_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
  663. {
  664. rtl8150_t *dev = netdev_priv(netdev);
  665. short lpa, bmcr;
  666. ecmd->supported = (SUPPORTED_10baseT_Half |
  667. SUPPORTED_10baseT_Full |
  668. SUPPORTED_100baseT_Half |
  669. SUPPORTED_100baseT_Full |
  670. SUPPORTED_Autoneg |
  671. SUPPORTED_TP | SUPPORTED_MII);
  672. ecmd->port = PORT_TP;
  673. ecmd->transceiver = XCVR_INTERNAL;
  674. ecmd->phy_address = dev->phy;
  675. get_registers(dev, BMCR, 2, &bmcr);
  676. get_registers(dev, ANLP, 2, &lpa);
  677. if (bmcr & BMCR_ANENABLE) {
  678. ecmd->autoneg = AUTONEG_ENABLE;
  679. ecmd->speed = (lpa & (LPA_100HALF | LPA_100FULL)) ?
  680. SPEED_100 : SPEED_10;
  681. if (ecmd->speed == SPEED_100)
  682. ecmd->duplex = (lpa & LPA_100FULL) ?
  683. DUPLEX_FULL : DUPLEX_HALF;
  684. else
  685. ecmd->duplex = (lpa & LPA_10FULL) ?
  686. DUPLEX_FULL : DUPLEX_HALF;
  687. } else {
  688. ecmd->autoneg = AUTONEG_DISABLE;
  689. ecmd->speed = (bmcr & BMCR_SPEED100) ?
  690. SPEED_100 : SPEED_10;
  691. ecmd->duplex = (bmcr & BMCR_FULLDPLX) ?
  692. DUPLEX_FULL : DUPLEX_HALF;
  693. }
  694. return 0;
  695. }
  696. static struct ethtool_ops ops = {
  697. .get_drvinfo = rtl8150_get_drvinfo,
  698. .get_settings = rtl8150_get_settings,
  699. .get_link = ethtool_op_get_link
  700. };
  701. static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  702. {
  703. rtl8150_t *dev = netdev_priv(netdev);
  704. u16 *data = (u16 *) & rq->ifr_ifru;
  705. int res = 0;
  706. switch (cmd) {
  707. case SIOCDEVPRIVATE:
  708. data[0] = dev->phy;
  709. case SIOCDEVPRIVATE + 1:
  710. read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
  711. break;
  712. case SIOCDEVPRIVATE + 2:
  713. if (!capable(CAP_NET_ADMIN))
  714. return -EPERM;
  715. write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
  716. break;
  717. default:
  718. res = -EOPNOTSUPP;
  719. }
  720. return res;
  721. }
  722. static int rtl8150_probe(struct usb_interface *intf,
  723. const struct usb_device_id *id)
  724. {
  725. struct usb_device *udev = interface_to_usbdev(intf);
  726. rtl8150_t *dev;
  727. struct net_device *netdev;
  728. netdev = alloc_etherdev(sizeof(rtl8150_t));
  729. if (!netdev) {
  730. err("Out of memory");
  731. return -ENOMEM;
  732. }
  733. dev = netdev_priv(netdev);
  734. memset(dev, 0, sizeof(rtl8150_t));
  735. dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
  736. if (!dev->intr_buff) {
  737. free_netdev(netdev);
  738. return -ENOMEM;
  739. }
  740. tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
  741. spin_lock_init(&dev->rx_pool_lock);
  742. dev->udev = udev;
  743. dev->netdev = netdev;
  744. SET_MODULE_OWNER(netdev);
  745. netdev->open = rtl8150_open;
  746. netdev->stop = rtl8150_close;
  747. netdev->do_ioctl = rtl8150_ioctl;
  748. netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
  749. netdev->tx_timeout = rtl8150_tx_timeout;
  750. netdev->hard_start_xmit = rtl8150_start_xmit;
  751. netdev->set_multicast_list = rtl8150_set_multicast;
  752. netdev->set_mac_address = rtl8150_set_mac_address;
  753. netdev->get_stats = rtl8150_netdev_stats;
  754. netdev->mtu = RTL8150_MTU;
  755. SET_ETHTOOL_OPS(netdev, &ops);
  756. dev->intr_interval = 100; /* 100ms */
  757. if (!alloc_all_urbs(dev)) {
  758. err("out of memory");
  759. goto out;
  760. }
  761. if (!rtl8150_reset(dev)) {
  762. err("couldn't reset the device");
  763. goto out1;
  764. }
  765. fill_skb_pool(dev);
  766. set_ethernet_addr(dev);
  767. info("%s: rtl8150 is detected", netdev->name);
  768. usb_set_intfdata(intf, dev);
  769. SET_NETDEV_DEV(netdev, &intf->dev);
  770. if (register_netdev(netdev) != 0) {
  771. err("couldn't register the device");
  772. goto out2;
  773. }
  774. return 0;
  775. out2:
  776. usb_set_intfdata(intf, NULL);
  777. free_skb_pool(dev);
  778. out1:
  779. free_all_urbs(dev);
  780. out:
  781. kfree(dev->intr_buff);
  782. free_netdev(netdev);
  783. return -EIO;
  784. }
  785. static void rtl8150_disconnect(struct usb_interface *intf)
  786. {
  787. rtl8150_t *dev = usb_get_intfdata(intf);
  788. usb_set_intfdata(intf, NULL);
  789. if (dev) {
  790. set_bit(RTL8150_UNPLUG, &dev->flags);
  791. unregister_netdev(dev->netdev);
  792. unlink_all_urbs(dev);
  793. free_all_urbs(dev);
  794. free_skb_pool(dev);
  795. if (dev->rx_skb)
  796. dev_kfree_skb(dev->rx_skb);
  797. kfree(dev->intr_buff);
  798. free_netdev(dev->netdev);
  799. }
  800. }
  801. static int __init usb_rtl8150_init(void)
  802. {
  803. info(DRIVER_DESC " " DRIVER_VERSION);
  804. return usb_register(&rtl8150_driver);
  805. }
  806. static void __exit usb_rtl8150_exit(void)
  807. {
  808. usb_deregister(&rtl8150_driver);
  809. }
  810. module_init(usb_rtl8150_init);
  811. module_exit(usb_rtl8150_exit);
  812. MODULE_AUTHOR(DRIVER_AUTHOR);
  813. MODULE_DESCRIPTION(DRIVER_DESC);
  814. MODULE_LICENSE("GPL");