asix: Simplify asix_rx_fixup_internal() netdev alloc
[sfrench/cifs-2.6.git] / 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, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "asix.h"
23
24 int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
25                   u16 size, void *data)
26 {
27         int ret;
28         ret = usbnet_read_cmd(dev, cmd,
29                                USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
30                                value, index, data, size);
31
32         if (ret != size && ret >= 0)
33                 return -EINVAL;
34         return ret;
35 }
36
37 int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
38                    u16 size, void *data)
39 {
40         return usbnet_write_cmd(dev, cmd,
41                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
42                                 value, index, data, size);
43 }
44
45 void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
46                           u16 size, void *data)
47 {
48         usbnet_write_cmd_async(dev, cmd,
49                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
50                                value, index, data, size);
51 }
52
53 int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
54                            struct asix_rx_fixup_info *rx)
55 {
56         int offset = 0;
57         u16 size;
58
59         while (offset + sizeof(u16) <= skb->len) {
60                 u16 copy_length;
61                 unsigned char *data;
62
63                 if (!rx->remaining) {
64                         if (skb->len - offset == sizeof(u16)) {
65                                 rx->header = get_unaligned_le16(
66                                                 skb->data + offset);
67                                 rx->split_head = true;
68                                 offset += sizeof(u16);
69                                 break;
70                         }
71
72                         if (rx->split_head == true) {
73                                 rx->header |= (get_unaligned_le16(
74                                                 skb->data + offset) << 16);
75                                 rx->split_head = false;
76                                 offset += sizeof(u16);
77                         } else {
78                                 rx->header = get_unaligned_le32(skb->data +
79                                                                 offset);
80                                 offset += sizeof(u32);
81                         }
82
83                         /* take frame length from Data header 32-bit word */
84                         size = (u16)(rx->header & 0x7ff);
85                         if (size != ((~rx->header >> 16) & 0x7ff)) {
86                                 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
87                                            rx->header, offset);
88                                 return 0;
89                         }
90                         if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
91                                 netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
92                                            size);
93                                 return 0;
94                         }
95
96                         rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
97                         if (!rx->ax_skb)
98                                 return 0;
99
100                         rx->remaining = size;
101                 }
102
103                 if (rx->remaining > skb->len - offset) {
104                         copy_length = skb->len - offset;
105                         rx->remaining -= copy_length;
106                 } else {
107                         copy_length = rx->remaining;
108                         rx->remaining = 0;
109                 }
110
111                 data = skb_put(rx->ax_skb, copy_length);
112                 memcpy(data, skb->data + offset, copy_length);
113                 if (!rx->remaining)
114                         usbnet_skb_return(dev, rx->ax_skb);
115
116                 offset += (copy_length + 1) & 0xfffe;
117         }
118
119         if (skb->len != offset) {
120                 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
121                            skb->len, offset);
122                 return 0;
123         }
124
125         return 1;
126 }
127
128 int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
129 {
130         struct asix_common_private *dp = dev->driver_priv;
131         struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
132
133         return asix_rx_fixup_internal(dev, skb, rx);
134 }
135
136 struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
137                               gfp_t flags)
138 {
139         int padlen;
140         int headroom = skb_headroom(skb);
141         int tailroom = skb_tailroom(skb);
142         u32 packet_len;
143         u32 padbytes = 0xffff0000;
144
145         padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
146
147         /* We need to push 4 bytes in front of frame (packet_len)
148          * and maybe add 4 bytes after the end (if padlen is 4)
149          *
150          * Avoid skb_copy_expand() expensive call, using following rules :
151          * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
152          *   is false (and if we have 4 bytes of headroom)
153          * - We are allowed to put 4 bytes at tail if skb_cloned()
154          *   is false (and if we have 4 bytes of tailroom)
155          *
156          * TCP packets for example are cloned, but skb_header_release()
157          * was called in tcp stack, allowing us to use headroom for our needs.
158          */
159         if (!skb_header_cloned(skb) &&
160             !(padlen && skb_cloned(skb)) &&
161             headroom + tailroom >= 4 + padlen) {
162                 /* following should not happen, but better be safe */
163                 if (headroom < 4 ||
164                     tailroom < padlen) {
165                         skb->data = memmove(skb->head + 4, skb->data, skb->len);
166                         skb_set_tail_pointer(skb, skb->len);
167                 }
168         } else {
169                 struct sk_buff *skb2;
170
171                 skb2 = skb_copy_expand(skb, 4, padlen, flags);
172                 dev_kfree_skb_any(skb);
173                 skb = skb2;
174                 if (!skb)
175                         return NULL;
176         }
177
178         packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
179         skb_push(skb, 4);
180         cpu_to_le32s(&packet_len);
181         skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
182
183         if (padlen) {
184                 cpu_to_le32s(&padbytes);
185                 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
186                 skb_put(skb, sizeof(padbytes));
187         }
188
189         usbnet_set_skb_tx_stats(skb, 1, 0);
190         return skb;
191 }
192
193 int asix_set_sw_mii(struct usbnet *dev)
194 {
195         int ret;
196         ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
197         if (ret < 0)
198                 netdev_err(dev->net, "Failed to enable software MII access\n");
199         return ret;
200 }
201
202 int asix_set_hw_mii(struct usbnet *dev)
203 {
204         int ret;
205         ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
206         if (ret < 0)
207                 netdev_err(dev->net, "Failed to enable hardware MII access\n");
208         return ret;
209 }
210
211 int asix_read_phy_addr(struct usbnet *dev, int internal)
212 {
213         int offset = (internal ? 1 : 0);
214         u8 buf[2];
215         int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
216
217         netdev_dbg(dev->net, "asix_get_phy_addr()\n");
218
219         if (ret < 0) {
220                 netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
221                 goto out;
222         }
223         netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
224                    *((__le16 *)buf));
225         ret = buf[offset];
226
227 out:
228         return ret;
229 }
230
231 int asix_get_phy_addr(struct usbnet *dev)
232 {
233         /* return the address of the internal phy */
234         return asix_read_phy_addr(dev, 1);
235 }
236
237
238 int asix_sw_reset(struct usbnet *dev, u8 flags)
239 {
240         int ret;
241
242         ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
243         if (ret < 0)
244                 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
245
246         return ret;
247 }
248
249 u16 asix_read_rx_ctl(struct usbnet *dev)
250 {
251         __le16 v;
252         int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
253
254         if (ret < 0) {
255                 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
256                 goto out;
257         }
258         ret = le16_to_cpu(v);
259 out:
260         return ret;
261 }
262
263 int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
264 {
265         int ret;
266
267         netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
268         ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
269         if (ret < 0)
270                 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
271                            mode, ret);
272
273         return ret;
274 }
275
276 u16 asix_read_medium_status(struct usbnet *dev)
277 {
278         __le16 v;
279         int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
280
281         if (ret < 0) {
282                 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
283                            ret);
284                 return ret;     /* TODO: callers not checking for error ret */
285         }
286
287         return le16_to_cpu(v);
288
289 }
290
291 int asix_write_medium_mode(struct usbnet *dev, u16 mode)
292 {
293         int ret;
294
295         netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
296         ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
297         if (ret < 0)
298                 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
299                            mode, ret);
300
301         return ret;
302 }
303
304 int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
305 {
306         int ret;
307
308         netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
309         ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
310         if (ret < 0)
311                 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
312                            value, ret);
313
314         if (sleep)
315                 msleep(sleep);
316
317         return ret;
318 }
319
320 /*
321  * AX88772 & AX88178 have a 16-bit RX_CTL value
322  */
323 void asix_set_multicast(struct net_device *net)
324 {
325         struct usbnet *dev = netdev_priv(net);
326         struct asix_data *data = (struct asix_data *)&dev->data;
327         u16 rx_ctl = AX_DEFAULT_RX_CTL;
328
329         if (net->flags & IFF_PROMISC) {
330                 rx_ctl |= AX_RX_CTL_PRO;
331         } else if (net->flags & IFF_ALLMULTI ||
332                    netdev_mc_count(net) > AX_MAX_MCAST) {
333                 rx_ctl |= AX_RX_CTL_AMALL;
334         } else if (netdev_mc_empty(net)) {
335                 /* just broadcast and directed */
336         } else {
337                 /* We use the 20 byte dev->data
338                  * for our 8 byte filter buffer
339                  * to avoid allocating memory that
340                  * is tricky to free later */
341                 struct netdev_hw_addr *ha;
342                 u32 crc_bits;
343
344                 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
345
346                 /* Build the multicast hash filter. */
347                 netdev_for_each_mc_addr(ha, net) {
348                         crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
349                         data->multi_filter[crc_bits >> 3] |=
350                             1 << (crc_bits & 7);
351                 }
352
353                 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
354                                    AX_MCAST_FILTER_SIZE, data->multi_filter);
355
356                 rx_ctl |= AX_RX_CTL_AM;
357         }
358
359         asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
360 }
361
362 int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
363 {
364         struct usbnet *dev = netdev_priv(netdev);
365         __le16 res;
366
367         mutex_lock(&dev->phy_mutex);
368         asix_set_sw_mii(dev);
369         asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
370                                 (__u16)loc, 2, &res);
371         asix_set_hw_mii(dev);
372         mutex_unlock(&dev->phy_mutex);
373
374         netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
375                    phy_id, loc, le16_to_cpu(res));
376
377         return le16_to_cpu(res);
378 }
379
380 void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
381 {
382         struct usbnet *dev = netdev_priv(netdev);
383         __le16 res = cpu_to_le16(val);
384
385         netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
386                    phy_id, loc, val);
387         mutex_lock(&dev->phy_mutex);
388         asix_set_sw_mii(dev);
389         asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
390         asix_set_hw_mii(dev);
391         mutex_unlock(&dev->phy_mutex);
392 }
393
394 void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
395 {
396         struct usbnet *dev = netdev_priv(net);
397         u8 opt;
398
399         if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
400                 wolinfo->supported = 0;
401                 wolinfo->wolopts = 0;
402                 return;
403         }
404         wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
405         wolinfo->wolopts = 0;
406         if (opt & AX_MONITOR_LINK)
407                 wolinfo->wolopts |= WAKE_PHY;
408         if (opt & AX_MONITOR_MAGIC)
409                 wolinfo->wolopts |= WAKE_MAGIC;
410 }
411
412 int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
413 {
414         struct usbnet *dev = netdev_priv(net);
415         u8 opt = 0;
416
417         if (wolinfo->wolopts & WAKE_PHY)
418                 opt |= AX_MONITOR_LINK;
419         if (wolinfo->wolopts & WAKE_MAGIC)
420                 opt |= AX_MONITOR_MAGIC;
421
422         if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
423                               opt, 0, 0, NULL) < 0)
424                 return -EINVAL;
425
426         return 0;
427 }
428
429 int asix_get_eeprom_len(struct net_device *net)
430 {
431         return AX_EEPROM_LEN;
432 }
433
434 int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
435                     u8 *data)
436 {
437         struct usbnet *dev = netdev_priv(net);
438         u16 *eeprom_buff;
439         int first_word, last_word;
440         int i;
441
442         if (eeprom->len == 0)
443                 return -EINVAL;
444
445         eeprom->magic = AX_EEPROM_MAGIC;
446
447         first_word = eeprom->offset >> 1;
448         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
449
450         eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
451                               GFP_KERNEL);
452         if (!eeprom_buff)
453                 return -ENOMEM;
454
455         /* ax8817x returns 2 bytes from eeprom on read */
456         for (i = first_word; i <= last_word; i++) {
457                 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
458                                   &(eeprom_buff[i - first_word])) < 0) {
459                         kfree(eeprom_buff);
460                         return -EIO;
461                 }
462         }
463
464         memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
465         kfree(eeprom_buff);
466         return 0;
467 }
468
469 int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
470                     u8 *data)
471 {
472         struct usbnet *dev = netdev_priv(net);
473         u16 *eeprom_buff;
474         int first_word, last_word;
475         int i;
476         int ret;
477
478         netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
479                    eeprom->len, eeprom->offset, eeprom->magic);
480
481         if (eeprom->len == 0)
482                 return -EINVAL;
483
484         if (eeprom->magic != AX_EEPROM_MAGIC)
485                 return -EINVAL;
486
487         first_word = eeprom->offset >> 1;
488         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
489
490         eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
491                               GFP_KERNEL);
492         if (!eeprom_buff)
493                 return -ENOMEM;
494
495         /* align data to 16 bit boundaries, read the missing data from
496            the EEPROM */
497         if (eeprom->offset & 1) {
498                 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
499                                     &(eeprom_buff[0]));
500                 if (ret < 0) {
501                         netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
502                         goto free;
503                 }
504         }
505
506         if ((eeprom->offset + eeprom->len) & 1) {
507                 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
508                                     &(eeprom_buff[last_word - first_word]));
509                 if (ret < 0) {
510                         netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
511                         goto free;
512                 }
513         }
514
515         memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
516
517         /* write data to EEPROM */
518         ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL);
519         if (ret < 0) {
520                 netdev_err(net, "Failed to enable EEPROM write\n");
521                 goto free;
522         }
523         msleep(20);
524
525         for (i = first_word; i <= last_word; i++) {
526                 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
527                            i, eeprom_buff[i - first_word]);
528                 ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
529                                      eeprom_buff[i - first_word], 0, NULL);
530                 if (ret < 0) {
531                         netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
532                                    i);
533                         goto free;
534                 }
535                 msleep(20);
536         }
537
538         ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL);
539         if (ret < 0) {
540                 netdev_err(net, "Failed to disable EEPROM write\n");
541                 goto free;
542         }
543
544         ret = 0;
545 free:
546         kfree(eeprom_buff);
547         return ret;
548 }
549
550 void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
551 {
552         /* Inherit standard device info */
553         usbnet_get_drvinfo(net, info);
554         strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
555         strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
556         info->eedump_len = AX_EEPROM_LEN;
557 }
558
559 int asix_set_mac_address(struct net_device *net, void *p)
560 {
561         struct usbnet *dev = netdev_priv(net);
562         struct asix_data *data = (struct asix_data *)&dev->data;
563         struct sockaddr *addr = p;
564
565         if (netif_running(net))
566                 return -EBUSY;
567         if (!is_valid_ether_addr(addr->sa_data))
568                 return -EADDRNOTAVAIL;
569
570         memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
571
572         /* We use the 20 byte dev->data
573          * for our 6 byte mac buffer
574          * to avoid allocating memory that
575          * is tricky to free later */
576         memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
577         asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
578                                                         data->mac_addr);
579
580         return 0;
581 }