]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/usb/dm9601.c
dm9601: Consolidate common parts of dm_write_*_async
[linux-2.6-omap-h63xx.git] / drivers / net / usb / dm9601.c
1 /*
2  * Davicom DM9601 USB 1.1 10/100Mbps ethernet devices
3  *
4  * Peter Korsgaard <jacmet@sunsite.dk>
5  *
6  * This file is licensed under the terms of the GNU General Public License
7  * version 2.  This program is licensed "as is" without any warranty of any
8  * kind, whether express or implied.
9  */
10
11 //#define DEBUG
12
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/stddef.h>
16 #include <linux/init.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/ethtool.h>
20 #include <linux/mii.h>
21 #include <linux/usb.h>
22 #include <linux/crc32.h>
23
24 #include "usbnet.h"
25
26 /* datasheet:
27  http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-P01-930914.pdf
28 */
29
30 /* control requests */
31 #define DM_READ_REGS    0x00
32 #define DM_WRITE_REGS   0x01
33 #define DM_READ_MEMS    0x02
34 #define DM_WRITE_REG    0x03
35 #define DM_WRITE_MEMS   0x05
36 #define DM_WRITE_MEM    0x07
37
38 /* registers */
39 #define DM_NET_CTRL     0x00
40 #define DM_RX_CTRL      0x05
41 #define DM_SHARED_CTRL  0x0b
42 #define DM_SHARED_ADDR  0x0c
43 #define DM_SHARED_DATA  0x0d    /* low + high */
44 #define DM_PHY_ADDR     0x10    /* 6 bytes */
45 #define DM_MCAST_ADDR   0x16    /* 8 bytes */
46 #define DM_GPR_CTRL     0x1e
47 #define DM_GPR_DATA     0x1f
48
49 #define DM_MAX_MCAST    64
50 #define DM_MCAST_SIZE   8
51 #define DM_EEPROM_LEN   256
52 #define DM_TX_OVERHEAD  2       /* 2 byte header */
53 #define DM_RX_OVERHEAD  7       /* 3 byte header + 4 byte crc tail */
54 #define DM_TIMEOUT      1000
55
56
57 static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data)
58 {
59         devdbg(dev, "dm_read() reg=0x%02x length=%d", reg, length);
60         return usb_control_msg(dev->udev,
61                                usb_rcvctrlpipe(dev->udev, 0),
62                                DM_READ_REGS,
63                                USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
64                                0, reg, data, length, USB_CTRL_SET_TIMEOUT);
65 }
66
67 static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value)
68 {
69         return dm_read(dev, reg, 1, value);
70 }
71
72 static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data)
73 {
74         devdbg(dev, "dm_write() reg=0x%02x, length=%d", reg, length);
75         return usb_control_msg(dev->udev,
76                                usb_sndctrlpipe(dev->udev, 0),
77                                DM_WRITE_REGS,
78                                USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
79                                0, reg, data, length, USB_CTRL_SET_TIMEOUT);
80 }
81
82 static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value)
83 {
84         devdbg(dev, "dm_write_reg() reg=0x%02x, value=0x%02x", reg, value);
85         return usb_control_msg(dev->udev,
86                                usb_sndctrlpipe(dev->udev, 0),
87                                DM_WRITE_REG,
88                                USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
89                                value, reg, NULL, 0, USB_CTRL_SET_TIMEOUT);
90 }
91
92 static void dm_write_async_callback(struct urb *urb)
93 {
94         struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
95
96         if (urb->status < 0)
97                 printk(KERN_DEBUG "dm_write_async_callback() failed with %d\n",
98                        urb->status);
99
100         kfree(req);
101         usb_free_urb(urb);
102 }
103
104 static void dm_write_async_helper(struct usbnet *dev, u8 reg, u8 value,
105                                   u16 length, void *data)
106 {
107         struct usb_ctrlrequest *req;
108         struct urb *urb;
109         int status;
110
111         urb = usb_alloc_urb(0, GFP_ATOMIC);
112         if (!urb) {
113                 deverr(dev, "Error allocating URB in dm_write_async_helper!");
114                 return;
115         }
116
117         req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
118         if (!req) {
119                 deverr(dev, "Failed to allocate memory for control request");
120                 usb_free_urb(urb);
121                 return;
122         }
123
124         req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
125         req->bRequest = length ? DM_WRITE_REGS : DM_WRITE_REG;
126         req->wValue = cpu_to_le16(value);
127         req->wIndex = cpu_to_le16(reg);
128         req->wLength = cpu_to_le16(length);
129
130         usb_fill_control_urb(urb, dev->udev,
131                              usb_sndctrlpipe(dev->udev, 0),
132                              (void *)req, data, length,
133                              dm_write_async_callback, req);
134
135         status = usb_submit_urb(urb, GFP_ATOMIC);
136         if (status < 0) {
137                 deverr(dev, "Error submitting the control message: status=%d",
138                        status);
139                 kfree(req);
140                 usb_free_urb(urb);
141         }
142 }
143
144 static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
145 {
146         devdbg(dev, "dm_write_async() reg=0x%02x length=%d", reg, length);
147
148         dm_write_async_helper(dev, reg, 0, length, data);
149 }
150
151 static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
152 {
153         devdbg(dev, "dm_write_reg_async() reg=0x%02x value=0x%02x",
154                reg, value);
155
156         dm_write_async_helper(dev, reg, value, 0, NULL);
157 }
158
159 static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, u16 *value)
160 {
161         int ret, i;
162
163         mutex_lock(&dev->phy_mutex);
164
165         dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
166         dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4);
167
168         for (i = 0; i < DM_TIMEOUT; i++) {
169                 u8 tmp;
170
171                 udelay(1);
172                 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
173                 if (ret < 0)
174                         goto out;
175
176                 /* ready */
177                 if ((tmp & 1) == 0)
178                         break;
179         }
180
181         if (i == DM_TIMEOUT) {
182                 deverr(dev, "%s read timed out!", phy ? "phy" : "eeprom");
183                 ret = -EIO;
184                 goto out;
185         }
186
187         dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
188         ret = dm_read(dev, DM_SHARED_DATA, 2, value);
189
190         devdbg(dev, "read shared %d 0x%02x returned 0x%04x, %d",
191                phy, reg, *value, ret);
192
193  out:
194         mutex_unlock(&dev->phy_mutex);
195         return ret;
196 }
197
198 static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, u16 value)
199 {
200         int ret, i;
201
202         mutex_lock(&dev->phy_mutex);
203
204         ret = dm_write(dev, DM_SHARED_DATA, 2, &value);
205         if (ret < 0)
206                 goto out;
207
208         dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
209         dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1c : 0x14);
210
211         for (i = 0; i < DM_TIMEOUT; i++) {
212                 u8 tmp;
213
214                 udelay(1);
215                 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
216                 if (ret < 0)
217                         goto out;
218
219                 /* ready */
220                 if ((tmp & 1) == 0)
221                         break;
222         }
223
224         if (i == DM_TIMEOUT) {
225                 deverr(dev, "%s write timed out!", phy ? "phy" : "eeprom");
226                 ret = -EIO;
227                 goto out;
228         }
229
230         dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
231
232 out:
233         mutex_unlock(&dev->phy_mutex);
234         return ret;
235 }
236
237 static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
238 {
239         return dm_read_shared_word(dev, 0, offset, value);
240 }
241
242
243
244 static int dm9601_get_eeprom_len(struct net_device *dev)
245 {
246         return DM_EEPROM_LEN;
247 }
248
249 static int dm9601_get_eeprom(struct net_device *net,
250                              struct ethtool_eeprom *eeprom, u8 * data)
251 {
252         struct usbnet *dev = netdev_priv(net);
253         u16 *ebuf = (u16 *) data;
254         int i;
255
256         /* access is 16bit */
257         if ((eeprom->offset % 2) || (eeprom->len % 2))
258                 return -EINVAL;
259
260         for (i = 0; i < eeprom->len / 2; i++) {
261                 if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i,
262                                         &ebuf[i]) < 0)
263                         return -EINVAL;
264         }
265         return 0;
266 }
267
268 static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc)
269 {
270         struct usbnet *dev = netdev_priv(netdev);
271
272         u16 res;
273
274         if (phy_id) {
275                 devdbg(dev, "Only internal phy supported");
276                 return 0;
277         }
278
279         dm_read_shared_word(dev, 1, loc, &res);
280
281         devdbg(dev,
282                "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x",
283                phy_id, loc, le16_to_cpu(res));
284
285         return le16_to_cpu(res);
286 }
287
288 static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc,
289                               int val)
290 {
291         struct usbnet *dev = netdev_priv(netdev);
292         u16 res = cpu_to_le16(val);
293
294         if (phy_id) {
295                 devdbg(dev, "Only internal phy supported");
296                 return;
297         }
298
299         devdbg(dev,"dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x",
300                phy_id, loc, val);
301
302         dm_write_shared_word(dev, 1, loc, res);
303 }
304
305 static void dm9601_get_drvinfo(struct net_device *net,
306                                struct ethtool_drvinfo *info)
307 {
308         /* Inherit standard device info */
309         usbnet_get_drvinfo(net, info);
310         info->eedump_len = DM_EEPROM_LEN;
311 }
312
313 static u32 dm9601_get_link(struct net_device *net)
314 {
315         struct usbnet *dev = netdev_priv(net);
316
317         return mii_link_ok(&dev->mii);
318 }
319
320 static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
321 {
322         struct usbnet *dev = netdev_priv(net);
323
324         return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
325 }
326
327 static struct ethtool_ops dm9601_ethtool_ops = {
328         .get_drvinfo    = dm9601_get_drvinfo,
329         .get_link       = dm9601_get_link,
330         .get_msglevel   = usbnet_get_msglevel,
331         .set_msglevel   = usbnet_set_msglevel,
332         .get_eeprom_len = dm9601_get_eeprom_len,
333         .get_eeprom     = dm9601_get_eeprom,
334         .get_settings   = usbnet_get_settings,
335         .set_settings   = usbnet_set_settings,
336         .nway_reset     = usbnet_nway_reset,
337 };
338
339 static void dm9601_set_multicast(struct net_device *net)
340 {
341         struct usbnet *dev = netdev_priv(net);
342         /* We use the 20 byte dev->data for our 8 byte filter buffer
343          * to avoid allocating memory that is tricky to free later */
344         u8 *hashes = (u8 *) & dev->data;
345         u8 rx_ctl = 0x01;
346
347         memset(hashes, 0x00, DM_MCAST_SIZE);
348         hashes[DM_MCAST_SIZE - 1] |= 0x80;      /* broadcast address */
349
350         if (net->flags & IFF_PROMISC) {
351                 rx_ctl |= 0x02;
352         } else if (net->flags & IFF_ALLMULTI || net->mc_count > DM_MAX_MCAST) {
353                 rx_ctl |= 0x04;
354         } else if (net->mc_count) {
355                 struct dev_mc_list *mc_list = net->mc_list;
356                 int i;
357
358                 for (i = 0; i < net->mc_count; i++) {
359                         u32 crc = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
360                         hashes[crc >> 3] |= 1 << (crc & 0x7);
361                 }
362         }
363
364         dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes);
365         dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl);
366 }
367
368 static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf)
369 {
370         int ret;
371
372         ret = usbnet_get_endpoints(dev, intf);
373         if (ret)
374                 goto out;
375
376         dev->net->do_ioctl = dm9601_ioctl;
377         dev->net->set_multicast_list = dm9601_set_multicast;
378         dev->net->ethtool_ops = &dm9601_ethtool_ops;
379         dev->net->hard_header_len += DM_TX_OVERHEAD;
380         dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
381         dev->rx_urb_size = dev->net->mtu + ETH_HLEN + DM_RX_OVERHEAD;
382
383         dev->mii.dev = dev->net;
384         dev->mii.mdio_read = dm9601_mdio_read;
385         dev->mii.mdio_write = dm9601_mdio_write;
386         dev->mii.phy_id_mask = 0x1f;
387         dev->mii.reg_num_mask = 0x1f;
388
389         /* reset */
390         dm_write_reg(dev, DM_NET_CTRL, 1);
391         udelay(20);
392
393         /* read MAC */
394         if (dm_read(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr) < 0) {
395                 printk(KERN_ERR "Error reading MAC address\n");
396                 ret = -ENODEV;
397                 goto out;
398         }
399
400         /* power up phy */
401         dm_write_reg(dev, DM_GPR_CTRL, 1);
402         dm_write_reg(dev, DM_GPR_DATA, 0);
403
404         /* receive broadcast packets */
405         dm9601_set_multicast(dev->net);
406
407         dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
408         dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
409                           ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
410         mii_nway_restart(&dev->mii);
411
412 out:
413         return ret;
414 }
415
416 static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
417 {
418         u8 status;
419         int len;
420
421         /* format:
422            b0: rx status
423            b1: packet length (incl crc) low
424            b2: packet length (incl crc) high
425            b3..n-4: packet data
426            bn-3..bn: ethernet crc
427          */
428
429         if (unlikely(skb->len < DM_RX_OVERHEAD)) {
430                 dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
431                 return 0;
432         }
433
434         status = skb->data[0];
435         len = (skb->data[1] | (skb->data[2] << 8)) - 4;
436
437         if (unlikely(status & 0xbf)) {
438                 if (status & 0x01) dev->stats.rx_fifo_errors++;
439                 if (status & 0x02) dev->stats.rx_crc_errors++;
440                 if (status & 0x04) dev->stats.rx_frame_errors++;
441                 if (status & 0x20) dev->stats.rx_missed_errors++;
442                 if (status & 0x90) dev->stats.rx_length_errors++;
443                 return 0;
444         }
445
446         skb_pull(skb, 3);
447         skb_trim(skb, len);
448
449         return 1;
450 }
451
452 static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
453                                        gfp_t flags)
454 {
455         int len;
456
457         /* format:
458            b0: packet length low
459            b1: packet length high
460            b3..n: packet data
461         */
462
463         len = skb->len;
464
465         if (skb_headroom(skb) < DM_TX_OVERHEAD) {
466                 struct sk_buff *skb2;
467
468                 skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, 0, flags);
469                 dev_kfree_skb_any(skb);
470                 skb = skb2;
471                 if (!skb)
472                         return NULL;
473         }
474
475         __skb_push(skb, DM_TX_OVERHEAD);
476
477         /* usbnet adds padding if length is a multiple of packet size
478            if so, adjust length value in header */
479         if ((skb->len % dev->maxpacket) == 0)
480                 len++;
481
482         skb->data[0] = len;
483         skb->data[1] = len >> 8;
484
485         return skb;
486 }
487
488 static void dm9601_status(struct usbnet *dev, struct urb *urb)
489 {
490         int link;
491         u8 *buf;
492
493         /* format:
494            b0: net status
495            b1: tx status 1
496            b2: tx status 2
497            b3: rx status
498            b4: rx overflow
499            b5: rx count
500            b6: tx count
501            b7: gpr
502         */
503
504         if (urb->actual_length < 8)
505                 return;
506
507         buf = urb->transfer_buffer;
508
509         link = !!(buf[0] & 0x40);
510         if (netif_carrier_ok(dev->net) != link) {
511                 if (link) {
512                         netif_carrier_on(dev->net);
513                         usbnet_defer_kevent (dev, EVENT_LINK_RESET);
514                 }
515                 else
516                         netif_carrier_off(dev->net);
517                 devdbg(dev, "Link Status is: %d", link);
518         }
519 }
520
521 static int dm9601_link_reset(struct usbnet *dev)
522 {
523         struct ethtool_cmd ecmd;
524
525         mii_check_media(&dev->mii, 1, 1);
526         mii_ethtool_gset(&dev->mii, &ecmd);
527
528         devdbg(dev, "link_reset() speed: %d duplex: %d",
529                ecmd.speed, ecmd.duplex);
530
531         return 0;
532 }
533
534 static const struct driver_info dm9601_info = {
535         .description    = "Davicom DM9601 USB Ethernet",
536         .flags          = FLAG_ETHER,
537         .bind           = dm9601_bind,
538         .rx_fixup       = dm9601_rx_fixup,
539         .tx_fixup       = dm9601_tx_fixup,
540         .status         = dm9601_status,
541         .link_reset     = dm9601_link_reset,
542         .reset          = dm9601_link_reset,
543 };
544
545 static const struct usb_device_id products[] = {
546         {
547          USB_DEVICE(0x07aa, 0x9601),    /* Corega FEther USB-TXC */
548          .driver_info = (unsigned long)&dm9601_info,
549          },
550         {
551          USB_DEVICE(0x0a46, 0x9601),    /* Davicom USB-100 */
552          .driver_info = (unsigned long)&dm9601_info,
553          },
554         {
555          USB_DEVICE(0x0a46, 0x6688),    /* ZT6688 USB NIC */
556          .driver_info = (unsigned long)&dm9601_info,
557          },
558         {
559          USB_DEVICE(0x0a46, 0x0268),    /* ShanTou ST268 USB NIC */
560          .driver_info = (unsigned long)&dm9601_info,
561          },
562         {
563          USB_DEVICE(0x0a46, 0x8515),    /* ADMtek ADM8515 USB NIC */
564          .driver_info = (unsigned long)&dm9601_info,
565          },
566         {},                     // END
567 };
568
569 MODULE_DEVICE_TABLE(usb, products);
570
571 static struct usb_driver dm9601_driver = {
572         .name = "dm9601",
573         .id_table = products,
574         .probe = usbnet_probe,
575         .disconnect = usbnet_disconnect,
576         .suspend = usbnet_suspend,
577         .resume = usbnet_resume,
578 };
579
580 static int __init dm9601_init(void)
581 {
582         return usb_register(&dm9601_driver);
583 }
584
585 static void __exit dm9601_exit(void)
586 {
587         usb_deregister(&dm9601_driver);
588 }
589
590 module_init(dm9601_init);
591 module_exit(dm9601_exit);
592
593 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
594 MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices");
595 MODULE_LICENSE("GPL");