rtl8150.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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 VENDOR_ID_ZYXEL 0x0586
  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. #define PRODUCT_ID_PRESTIGE 0x401a
  119. #undef EEPROM_WRITE
  120. /* table of devices that work with this driver */
  121. static struct usb_device_id rtl8150_table[] = {
  122. {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150)},
  123. {USB_DEVICE(VENDOR_ID_MELCO, PRODUCT_ID_LUAKTX)},
  124. {USB_DEVICE(VENDOR_ID_MICRONET, PRODUCT_ID_SP128AR)},
  125. {USB_DEVICE(VENDOR_ID_LONGSHINE, PRODUCT_ID_LCS8138TX)},
  126. {USB_DEVICE(VENDOR_ID_ZYXEL, PRODUCT_ID_PRESTIGE)},
  127. {}
  128. };
  129. MODULE_DEVICE_TABLE(usb, rtl8150_table);
  130. struct rtl8150 {
  131. unsigned long flags;
  132. struct usb_device *udev;
  133. struct tasklet_struct tl;
  134. struct net_device_stats stats;
  135. struct net_device *netdev;
  136. struct urb *rx_urb, *tx_urb, *intr_urb, *ctrl_urb;
  137. struct sk_buff *tx_skb, *rx_skb;
  138. struct sk_buff *rx_skb_pool[RX_SKB_POOL_SIZE];
  139. spinlock_t rx_pool_lock;
  140. struct usb_ctrlrequest dr;
  141. int intr_interval;
  142. __le16 rx_creg;
  143. u8 *intr_buff;
  144. u8 phy;
  145. };
  146. typedef struct rtl8150 rtl8150_t;
  147. static void fill_skb_pool(rtl8150_t *);
  148. static void free_skb_pool(rtl8150_t *);
  149. static inline struct sk_buff *pull_skb(rtl8150_t *);
  150. static void rtl8150_disconnect(struct usb_interface *intf);
  151. static int rtl8150_probe(struct usb_interface *intf,
  152. const struct usb_device_id *id);
  153. static const char driver_name [] = "rtl8150";
  154. static struct usb_driver rtl8150_driver = {
  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. usb_unlink_urb(dev->tx_urb);
  569. dev->stats.tx_errors++;
  570. }
  571. static void rtl8150_set_multicast(struct net_device *netdev)
  572. {
  573. rtl8150_t *dev = netdev_priv(netdev);
  574. netif_stop_queue(netdev);
  575. if (netdev->flags & IFF_PROMISC) {
  576. dev->rx_creg |= cpu_to_le16(0x0001);
  577. info("%s: promiscuous mode", netdev->name);
  578. } else if (netdev->mc_count ||
  579. (netdev->flags & IFF_ALLMULTI)) {
  580. dev->rx_creg &= cpu_to_le16(0xfffe);
  581. dev->rx_creg |= cpu_to_le16(0x0002);
  582. info("%s: allmulti set", netdev->name);
  583. } else {
  584. /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
  585. dev->rx_creg &= cpu_to_le16(0x00fc);
  586. }
  587. async_set_registers(dev, RCR, 2);
  588. netif_wake_queue(netdev);
  589. }
  590. static int rtl8150_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  591. {
  592. rtl8150_t *dev = netdev_priv(netdev);
  593. int count, res;
  594. netif_stop_queue(netdev);
  595. count = (skb->len < 60) ? 60 : skb->len;
  596. count = (count & 0x3f) ? count : count + 1;
  597. dev->tx_skb = skb;
  598. usb_fill_bulk_urb(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
  599. skb->data, count, write_bulk_callback, dev);
  600. if ((res = usb_submit_urb(dev->tx_urb, GFP_ATOMIC))) {
  601. warn("failed tx_urb %d\n", res);
  602. dev->stats.tx_errors++;
  603. netif_start_queue(netdev);
  604. } else {
  605. dev->stats.tx_packets++;
  606. dev->stats.tx_bytes += skb->len;
  607. netdev->trans_start = jiffies;
  608. }
  609. return 0;
  610. }
  611. static void set_carrier(struct net_device *netdev)
  612. {
  613. rtl8150_t *dev = netdev_priv(netdev);
  614. short tmp;
  615. get_registers(dev, CSCR, 2, &tmp);
  616. if (tmp & CSCR_LINK_STATUS)
  617. netif_carrier_on(netdev);
  618. else
  619. netif_carrier_off(netdev);
  620. }
  621. static int rtl8150_open(struct net_device *netdev)
  622. {
  623. rtl8150_t *dev = netdev_priv(netdev);
  624. int res;
  625. if (dev->rx_skb == NULL)
  626. dev->rx_skb = pull_skb(dev);
  627. if (!dev->rx_skb)
  628. return -ENOMEM;
  629. set_registers(dev, IDR, 6, netdev->dev_addr);
  630. usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
  631. dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
  632. if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL)))
  633. warn("%s: rx_urb submit failed: %d", __FUNCTION__, res);
  634. usb_fill_int_urb(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
  635. dev->intr_buff, INTBUFSIZE, intr_callback,
  636. dev, dev->intr_interval);
  637. if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL)))
  638. warn("%s: intr_urb submit failed: %d", __FUNCTION__, res);
  639. netif_start_queue(netdev);
  640. enable_net_traffic(dev);
  641. set_carrier(netdev);
  642. return res;
  643. }
  644. static int rtl8150_close(struct net_device *netdev)
  645. {
  646. rtl8150_t *dev = netdev_priv(netdev);
  647. int res = 0;
  648. netif_stop_queue(netdev);
  649. if (!test_bit(RTL8150_UNPLUG, &dev->flags))
  650. disable_net_traffic(dev);
  651. unlink_all_urbs(dev);
  652. return res;
  653. }
  654. static void rtl8150_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
  655. {
  656. rtl8150_t *dev = netdev_priv(netdev);
  657. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  658. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  659. usb_make_path(dev->udev, info->bus_info, sizeof info->bus_info);
  660. }
  661. static int rtl8150_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
  662. {
  663. rtl8150_t *dev = netdev_priv(netdev);
  664. short lpa, bmcr;
  665. ecmd->supported = (SUPPORTED_10baseT_Half |
  666. SUPPORTED_10baseT_Full |
  667. SUPPORTED_100baseT_Half |
  668. SUPPORTED_100baseT_Full |
  669. SUPPORTED_Autoneg |
  670. SUPPORTED_TP | SUPPORTED_MII);
  671. ecmd->port = PORT_TP;
  672. ecmd->transceiver = XCVR_INTERNAL;
  673. ecmd->phy_address = dev->phy;
  674. get_registers(dev, BMCR, 2, &bmcr);
  675. get_registers(dev, ANLP, 2, &lpa);
  676. if (bmcr & BMCR_ANENABLE) {
  677. ecmd->autoneg = AUTONEG_ENABLE;
  678. ecmd->speed = (lpa & (LPA_100HALF | LPA_100FULL)) ?
  679. SPEED_100 : SPEED_10;
  680. if (ecmd->speed == SPEED_100)
  681. ecmd->duplex = (lpa & LPA_100FULL) ?
  682. DUPLEX_FULL : DUPLEX_HALF;
  683. else
  684. ecmd->duplex = (lpa & LPA_10FULL) ?
  685. DUPLEX_FULL : DUPLEX_HALF;
  686. } else {
  687. ecmd->autoneg = AUTONEG_DISABLE;
  688. ecmd->speed = (bmcr & BMCR_SPEED100) ?
  689. SPEED_100 : SPEED_10;
  690. ecmd->duplex = (bmcr & BMCR_FULLDPLX) ?
  691. DUPLEX_FULL : DUPLEX_HALF;
  692. }
  693. return 0;
  694. }
  695. static struct ethtool_ops ops = {
  696. .get_drvinfo = rtl8150_get_drvinfo,
  697. .get_settings = rtl8150_get_settings,
  698. .get_link = ethtool_op_get_link
  699. };
  700. static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  701. {
  702. rtl8150_t *dev = netdev_priv(netdev);
  703. u16 *data = (u16 *) & rq->ifr_ifru;
  704. int res = 0;
  705. switch (cmd) {
  706. case SIOCDEVPRIVATE:
  707. data[0] = dev->phy;
  708. case SIOCDEVPRIVATE + 1:
  709. read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
  710. break;
  711. case SIOCDEVPRIVATE + 2:
  712. if (!capable(CAP_NET_ADMIN))
  713. return -EPERM;
  714. write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
  715. break;
  716. default:
  717. res = -EOPNOTSUPP;
  718. }
  719. return res;
  720. }
  721. static int rtl8150_probe(struct usb_interface *intf,
  722. const struct usb_device_id *id)
  723. {
  724. struct usb_device *udev = interface_to_usbdev(intf);
  725. rtl8150_t *dev;
  726. struct net_device *netdev;
  727. netdev = alloc_etherdev(sizeof(rtl8150_t));
  728. if (!netdev) {
  729. err("Out of memory");
  730. return -ENOMEM;
  731. }
  732. dev = netdev_priv(netdev);
  733. memset(dev, 0, sizeof(rtl8150_t));
  734. dev->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
  735. if (!dev->intr_buff) {
  736. free_netdev(netdev);
  737. return -ENOMEM;
  738. }
  739. tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
  740. spin_lock_init(&dev->rx_pool_lock);
  741. dev->udev = udev;
  742. dev->netdev = netdev;
  743. SET_MODULE_OWNER(netdev);
  744. netdev->open = rtl8150_open;
  745. netdev->stop = rtl8150_close;
  746. netdev->do_ioctl = rtl8150_ioctl;
  747. netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
  748. netdev->tx_timeout = rtl8150_tx_timeout;
  749. netdev->hard_start_xmit = rtl8150_start_xmit;
  750. netdev->set_multicast_list = rtl8150_set_multicast;
  751. netdev->set_mac_address = rtl8150_set_mac_address;
  752. netdev->get_stats = rtl8150_netdev_stats;
  753. netdev->mtu = RTL8150_MTU;
  754. SET_ETHTOOL_OPS(netdev, &ops);
  755. dev->intr_interval = 100; /* 100ms */
  756. if (!alloc_all_urbs(dev)) {
  757. err("out of memory");
  758. goto out;
  759. }
  760. if (!rtl8150_reset(dev)) {
  761. err("couldn't reset the device");
  762. goto out1;
  763. }
  764. fill_skb_pool(dev);
  765. set_ethernet_addr(dev);
  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. info("%s: rtl8150 is detected", netdev->name);
  773. return 0;
  774. out2:
  775. usb_set_intfdata(intf, NULL);
  776. free_skb_pool(dev);
  777. out1:
  778. free_all_urbs(dev);
  779. out:
  780. kfree(dev->intr_buff);
  781. free_netdev(netdev);
  782. return -EIO;
  783. }
  784. static void rtl8150_disconnect(struct usb_interface *intf)
  785. {
  786. rtl8150_t *dev = usb_get_intfdata(intf);
  787. usb_set_intfdata(intf, NULL);
  788. if (dev) {
  789. set_bit(RTL8150_UNPLUG, &dev->flags);
  790. tasklet_disable(&dev->tl);
  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");