rtl8150.c 22 KB

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