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