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