]> Pileus Git - ~andy/linux/blob - drivers/net/usb/asix_common.c
asix: Factor out common code
[~andy/linux] / drivers / net / usb / asix_common.c
1 /*
2  * ASIX AX8817X based USB 2.0 Ethernet Devices
3  * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
4  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
5  * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
6  * Copyright (c) 2002-2003 TiVo Inc.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "asix.h"
24
25 int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
26                   u16 size, void *data)
27 {
28         void *buf;
29         int err = -ENOMEM;
30
31         netdev_dbg(dev->net, "asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
32                    cmd, value, index, size);
33
34         buf = kmalloc(size, GFP_KERNEL);
35         if (!buf)
36                 goto out;
37
38         err = usb_control_msg(
39                 dev->udev,
40                 usb_rcvctrlpipe(dev->udev, 0),
41                 cmd,
42                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
43                 value,
44                 index,
45                 buf,
46                 size,
47                 USB_CTRL_GET_TIMEOUT);
48         if (err == size)
49                 memcpy(data, buf, size);
50         else if (err >= 0)
51                 err = -EINVAL;
52         kfree(buf);
53
54 out:
55         return err;
56 }
57
58 int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
59                    u16 size, void *data)
60 {
61         void *buf = NULL;
62         int err = -ENOMEM;
63
64         netdev_dbg(dev->net, "asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
65                    cmd, value, index, size);
66
67         if (data) {
68                 buf = kmemdup(data, size, GFP_KERNEL);
69                 if (!buf)
70                         goto out;
71         }
72
73         err = usb_control_msg(
74                 dev->udev,
75                 usb_sndctrlpipe(dev->udev, 0),
76                 cmd,
77                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
78                 value,
79                 index,
80                 buf,
81                 size,
82                 USB_CTRL_SET_TIMEOUT);
83         kfree(buf);
84
85 out:
86         return err;
87 }
88
89 static void asix_async_cmd_callback(struct urb *urb)
90 {
91         struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
92         int status = urb->status;
93
94         if (status < 0)
95                 printk(KERN_DEBUG "asix_async_cmd_callback() failed with %d",
96                         status);
97
98         kfree(req);
99         usb_free_urb(urb);
100 }
101
102 void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
103                           u16 size, void *data)
104 {
105         struct usb_ctrlrequest *req;
106         int status;
107         struct urb *urb;
108
109         netdev_dbg(dev->net, "asix_write_cmd_async() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
110                    cmd, value, index, size);
111
112         urb = usb_alloc_urb(0, GFP_ATOMIC);
113         if (!urb) {
114                 netdev_err(dev->net, "Error allocating URB in write_cmd_async!\n");
115                 return;
116         }
117
118         req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
119         if (!req) {
120                 netdev_err(dev->net, "Failed to allocate memory for control request\n");
121                 usb_free_urb(urb);
122                 return;
123         }
124
125         req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
126         req->bRequest = cmd;
127         req->wValue = cpu_to_le16(value);
128         req->wIndex = cpu_to_le16(index);
129         req->wLength = cpu_to_le16(size);
130
131         usb_fill_control_urb(urb, dev->udev,
132                              usb_sndctrlpipe(dev->udev, 0),
133                              (void *)req, data, size,
134                              asix_async_cmd_callback, req);
135
136         status = usb_submit_urb(urb, GFP_ATOMIC);
137         if (status < 0) {
138                 netdev_err(dev->net, "Error submitting the control message: status=%d\n",
139                            status);
140                 kfree(req);
141                 usb_free_urb(urb);
142         }
143 }
144
145 int asix_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
146 {
147         int offset = 0;
148
149         while (offset + sizeof(u32) < skb->len) {
150                 struct sk_buff *ax_skb;
151                 u16 size;
152                 u32 header = get_unaligned_le32(skb->data + offset);
153
154                 offset += sizeof(u32);
155
156                 /* get the packet length */
157                 size = (u16) (header & 0x7ff);
158                 if (size != ((~header >> 16) & 0x07ff)) {
159                         netdev_err(dev->net, "asix_rx_fixup() Bad Header Length\n");
160                         return 0;
161                 }
162
163                 if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) ||
164                     (size + offset > skb->len)) {
165                         netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
166                                    size);
167                         return 0;
168                 }
169                 ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
170                 if (!ax_skb)
171                         return 0;
172
173                 skb_put(ax_skb, size);
174                 memcpy(ax_skb->data, skb->data + offset, size);
175                 usbnet_skb_return(dev, ax_skb);
176
177                 offset += (size + 1) & 0xfffe;
178         }
179
180         if (skb->len != offset) {
181                 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d\n",
182                            skb->len);
183                 return 0;
184         }
185         return 1;
186 }
187
188 struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
189                               gfp_t flags)
190 {
191         int padlen;
192         int headroom = skb_headroom(skb);
193         int tailroom = skb_tailroom(skb);
194         u32 packet_len;
195         u32 padbytes = 0xffff0000;
196
197         padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
198
199         /* We need to push 4 bytes in front of frame (packet_len)
200          * and maybe add 4 bytes after the end (if padlen is 4)
201          *
202          * Avoid skb_copy_expand() expensive call, using following rules :
203          * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
204          *   is false (and if we have 4 bytes of headroom)
205          * - We are allowed to put 4 bytes at tail if skb_cloned()
206          *   is false (and if we have 4 bytes of tailroom)
207          *
208          * TCP packets for example are cloned, but skb_header_release()
209          * was called in tcp stack, allowing us to use headroom for our needs.
210          */
211         if (!skb_header_cloned(skb) &&
212             !(padlen && skb_cloned(skb)) &&
213             headroom + tailroom >= 4 + padlen) {
214                 /* following should not happen, but better be safe */
215                 if (headroom < 4 ||
216                     tailroom < padlen) {
217                         skb->data = memmove(skb->head + 4, skb->data, skb->len);
218                         skb_set_tail_pointer(skb, skb->len);
219                 }
220         } else {
221                 struct sk_buff *skb2;
222
223                 skb2 = skb_copy_expand(skb, 4, padlen, flags);
224                 dev_kfree_skb_any(skb);
225                 skb = skb2;
226                 if (!skb)
227                         return NULL;
228         }
229
230         packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
231         skb_push(skb, 4);
232         cpu_to_le32s(&packet_len);
233         skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
234
235         if (padlen) {
236                 cpu_to_le32s(&padbytes);
237                 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
238                 skb_put(skb, sizeof(padbytes));
239         }
240         return skb;
241 }
242
243 int asix_set_sw_mii(struct usbnet *dev)
244 {
245         int ret;
246         ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
247         if (ret < 0)
248                 netdev_err(dev->net, "Failed to enable software MII access\n");
249         return ret;
250 }
251
252 int asix_set_hw_mii(struct usbnet *dev)
253 {
254         int ret;
255         ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
256         if (ret < 0)
257                 netdev_err(dev->net, "Failed to enable hardware MII access\n");
258         return ret;
259 }
260
261 int asix_get_phy_addr(struct usbnet *dev)
262 {
263         u8 buf[2];
264         int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
265
266         netdev_dbg(dev->net, "asix_get_phy_addr()\n");
267
268         if (ret < 0) {
269                 netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
270                 goto out;
271         }
272         netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
273                    *((__le16 *)buf));
274         ret = buf[1];
275
276 out:
277         return ret;
278 }
279
280 int asix_sw_reset(struct usbnet *dev, u8 flags)
281 {
282         int ret;
283
284         ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
285         if (ret < 0)
286                 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
287
288         return ret;
289 }
290
291 u16 asix_read_rx_ctl(struct usbnet *dev)
292 {
293         __le16 v;
294         int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
295
296         if (ret < 0) {
297                 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
298                 goto out;
299         }
300         ret = le16_to_cpu(v);
301 out:
302         return ret;
303 }
304
305 int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
306 {
307         int ret;
308
309         netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
310         ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
311         if (ret < 0)
312                 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
313                            mode, ret);
314
315         return ret;
316 }
317
318 u16 asix_read_medium_status(struct usbnet *dev)
319 {
320         __le16 v;
321         int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
322
323         if (ret < 0) {
324                 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
325                            ret);
326                 return ret;     /* TODO: callers not checking for error ret */
327         }
328
329         return le16_to_cpu(v);
330
331 }
332
333 int asix_write_medium_mode(struct usbnet *dev, u16 mode)
334 {
335         int ret;
336
337         netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
338         ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
339         if (ret < 0)
340                 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
341                            mode, ret);
342
343         return ret;
344 }
345
346 int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
347 {
348         int ret;
349
350         netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
351         ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
352         if (ret < 0)
353                 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
354                            value, ret);
355
356         if (sleep)
357                 msleep(sleep);
358
359         return ret;
360 }
361
362 /*
363  * AX88772 & AX88178 have a 16-bit RX_CTL value
364  */
365 void asix_set_multicast(struct net_device *net)
366 {
367         struct usbnet *dev = netdev_priv(net);
368         struct asix_data *data = (struct asix_data *)&dev->data;
369         u16 rx_ctl = AX_DEFAULT_RX_CTL;
370
371         if (net->flags & IFF_PROMISC) {
372                 rx_ctl |= AX_RX_CTL_PRO;
373         } else if (net->flags & IFF_ALLMULTI ||
374                    netdev_mc_count(net) > AX_MAX_MCAST) {
375                 rx_ctl |= AX_RX_CTL_AMALL;
376         } else if (netdev_mc_empty(net)) {
377                 /* just broadcast and directed */
378         } else {
379                 /* We use the 20 byte dev->data
380                  * for our 8 byte filter buffer
381                  * to avoid allocating memory that
382                  * is tricky to free later */
383                 struct netdev_hw_addr *ha;
384                 u32 crc_bits;
385
386                 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
387
388                 /* Build the multicast hash filter. */
389                 netdev_for_each_mc_addr(ha, net) {
390                         crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
391                         data->multi_filter[crc_bits >> 3] |=
392                             1 << (crc_bits & 7);
393                 }
394
395                 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
396                                    AX_MCAST_FILTER_SIZE, data->multi_filter);
397
398                 rx_ctl |= AX_RX_CTL_AM;
399         }
400
401         asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
402 }
403
404 int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
405 {
406         struct usbnet *dev = netdev_priv(netdev);
407         __le16 res;
408
409         mutex_lock(&dev->phy_mutex);
410         asix_set_sw_mii(dev);
411         asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
412                                 (__u16)loc, 2, &res);
413         asix_set_hw_mii(dev);
414         mutex_unlock(&dev->phy_mutex);
415
416         netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
417                    phy_id, loc, le16_to_cpu(res));
418
419         return le16_to_cpu(res);
420 }
421
422 void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
423 {
424         struct usbnet *dev = netdev_priv(netdev);
425         __le16 res = cpu_to_le16(val);
426
427         netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
428                    phy_id, loc, val);
429         mutex_lock(&dev->phy_mutex);
430         asix_set_sw_mii(dev);
431         asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
432         asix_set_hw_mii(dev);
433         mutex_unlock(&dev->phy_mutex);
434 }
435
436 void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
437 {
438         struct usbnet *dev = netdev_priv(net);
439         u8 opt;
440
441         if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
442                 wolinfo->supported = 0;
443                 wolinfo->wolopts = 0;
444                 return;
445         }
446         wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
447         wolinfo->wolopts = 0;
448         if (opt & AX_MONITOR_LINK)
449                 wolinfo->wolopts |= WAKE_PHY;
450         if (opt & AX_MONITOR_MAGIC)
451                 wolinfo->wolopts |= WAKE_MAGIC;
452 }
453
454 int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
455 {
456         struct usbnet *dev = netdev_priv(net);
457         u8 opt = 0;
458
459         if (wolinfo->wolopts & WAKE_PHY)
460                 opt |= AX_MONITOR_LINK;
461         if (wolinfo->wolopts & WAKE_MAGIC)
462                 opt |= AX_MONITOR_MAGIC;
463
464         if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
465                               opt, 0, 0, NULL) < 0)
466                 return -EINVAL;
467
468         return 0;
469 }
470
471 int asix_get_eeprom_len(struct net_device *net)
472 {
473         struct usbnet *dev = netdev_priv(net);
474         struct asix_data *data = (struct asix_data *)&dev->data;
475
476         return data->eeprom_len;
477 }
478
479 int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
480                     u8 *data)
481 {
482         struct usbnet *dev = netdev_priv(net);
483         __le16 *ebuf = (__le16 *)data;
484         int i;
485
486         /* Crude hack to ensure that we don't overwrite memory
487          * if an odd length is supplied
488          */
489         if (eeprom->len % 2)
490                 return -EINVAL;
491
492         eeprom->magic = AX_EEPROM_MAGIC;
493
494         /* ax8817x returns 2 bytes from eeprom on read */
495         for (i=0; i < eeprom->len / 2; i++) {
496                 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
497                         eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
498                         return -EINVAL;
499         }
500         return 0;
501 }
502
503 void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
504 {
505         struct usbnet *dev = netdev_priv(net);
506         struct asix_data *data = (struct asix_data *)&dev->data;
507
508         /* Inherit standard device info */
509         usbnet_get_drvinfo(net, info);
510         strncpy (info->driver, DRIVER_NAME, sizeof info->driver);
511         strncpy (info->version, DRIVER_VERSION, sizeof info->version);
512         info->eedump_len = data->eeprom_len;
513 }
514
515 int asix_set_mac_address(struct net_device *net, void *p)
516 {
517         struct usbnet *dev = netdev_priv(net);
518         struct asix_data *data = (struct asix_data *)&dev->data;
519         struct sockaddr *addr = p;
520
521         if (netif_running(net))
522                 return -EBUSY;
523         if (!is_valid_ether_addr(addr->sa_data))
524                 return -EADDRNOTAVAIL;
525
526         memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
527
528         /* We use the 20 byte dev->data
529          * for our 6 byte mac buffer
530          * to avoid allocating memory that
531          * is tricky to free later */
532         memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
533         asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
534                                                         data->mac_addr);
535
536         return 0;
537 }