rtl8150.c 22 KB

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