rtl8150.c 24 KB

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