GNU Linux-libre 5.15.54-gnu
[releases.git] / drivers / net / usb / asix_common.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ASIX AX8817X based USB 2.0 Ethernet Devices
4  * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
5  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
6  * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
7  * Copyright (c) 2002-2003 TiVo Inc.
8  */
9
10 #include "asix.h"
11
12 #define AX_HOST_EN_RETRIES      30
13
14 int __must_check asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
15                                u16 size, void *data, int in_pm)
16 {
17         int ret;
18         int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
19
20         BUG_ON(!dev);
21
22         if (!in_pm)
23                 fn = usbnet_read_cmd;
24         else
25                 fn = usbnet_read_cmd_nopm;
26
27         ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
28                  value, index, data, size);
29
30         if (unlikely(ret < size)) {
31                 ret = ret < 0 ? ret : -ENODATA;
32
33                 netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n",
34                             index, ret);
35         }
36
37         return ret;
38 }
39
40 int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
41                    u16 size, void *data, int in_pm)
42 {
43         int ret;
44         int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
45
46         BUG_ON(!dev);
47
48         if (!in_pm)
49                 fn = usbnet_write_cmd;
50         else
51                 fn = usbnet_write_cmd_nopm;
52
53         ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
54                  value, index, data, size);
55
56         if (unlikely(ret < 0))
57                 netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n",
58                             index, ret);
59
60         return ret;
61 }
62
63 void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
64                           u16 size, void *data)
65 {
66         usbnet_write_cmd_async(dev, cmd,
67                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
68                                value, index, data, size);
69 }
70
71 static int asix_check_host_enable(struct usbnet *dev, int in_pm)
72 {
73         int i, ret;
74         u8 smsr;
75
76         for (i = 0; i < AX_HOST_EN_RETRIES; ++i) {
77                 ret = asix_set_sw_mii(dev, in_pm);
78                 if (ret == -ENODEV || ret == -ETIMEDOUT)
79                         break;
80                 usleep_range(1000, 1100);
81                 ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
82                                     0, 0, 1, &smsr, in_pm);
83                 if (ret == -ENODEV)
84                         break;
85                 else if (ret < 0)
86                         continue;
87                 else if (smsr & AX_HOST_EN)
88                         break;
89         }
90
91         return i >= AX_HOST_EN_RETRIES ? -ETIMEDOUT : ret;
92 }
93
94 static void reset_asix_rx_fixup_info(struct asix_rx_fixup_info *rx)
95 {
96         /* Reset the variables that have a lifetime outside of
97          * asix_rx_fixup_internal() so that future processing starts from a
98          * known set of initial conditions.
99          */
100
101         if (rx->ax_skb) {
102                 /* Discard any incomplete Ethernet frame in the netdev buffer */
103                 kfree_skb(rx->ax_skb);
104                 rx->ax_skb = NULL;
105         }
106
107         /* Assume the Data header 32-bit word is at the start of the current
108          * or next URB socket buffer so reset all the state variables.
109          */
110         rx->remaining = 0;
111         rx->split_head = false;
112         rx->header = 0;
113 }
114
115 int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
116                            struct asix_rx_fixup_info *rx)
117 {
118         int offset = 0;
119         u16 size;
120
121         /* When an Ethernet frame spans multiple URB socket buffers,
122          * do a sanity test for the Data header synchronisation.
123          * Attempt to detect the situation of the previous socket buffer having
124          * been truncated or a socket buffer was missing. These situations
125          * cause a discontinuity in the data stream and therefore need to avoid
126          * appending bad data to the end of the current netdev socket buffer.
127          * Also avoid unnecessarily discarding a good current netdev socket
128          * buffer.
129          */
130         if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) {
131                 offset = ((rx->remaining + 1) & 0xfffe);
132                 rx->header = get_unaligned_le32(skb->data + offset);
133                 offset = 0;
134
135                 size = (u16)(rx->header & 0x7ff);
136                 if (size != ((~rx->header >> 16) & 0x7ff)) {
137                         netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n",
138                                    rx->remaining);
139                         reset_asix_rx_fixup_info(rx);
140                 }
141         }
142
143         while (offset + sizeof(u16) <= skb->len) {
144                 u16 copy_length;
145
146                 if (!rx->remaining) {
147                         if (skb->len - offset == sizeof(u16)) {
148                                 rx->header = get_unaligned_le16(
149                                                 skb->data + offset);
150                                 rx->split_head = true;
151                                 offset += sizeof(u16);
152                                 break;
153                         }
154
155                         if (rx->split_head == true) {
156                                 rx->header |= (get_unaligned_le16(
157                                                 skb->data + offset) << 16);
158                                 rx->split_head = false;
159                                 offset += sizeof(u16);
160                         } else {
161                                 rx->header = get_unaligned_le32(skb->data +
162                                                                 offset);
163                                 offset += sizeof(u32);
164                         }
165
166                         /* take frame length from Data header 32-bit word */
167                         size = (u16)(rx->header & 0x7ff);
168                         if (size != ((~rx->header >> 16) & 0x7ff)) {
169                                 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
170                                            rx->header, offset);
171                                 reset_asix_rx_fixup_info(rx);
172                                 return 0;
173                         }
174                         if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
175                                 netdev_dbg(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
176                                            size);
177                                 reset_asix_rx_fixup_info(rx);
178                                 return 0;
179                         }
180
181                         /* Sometimes may fail to get a netdev socket buffer but
182                          * continue to process the URB socket buffer so that
183                          * synchronisation of the Ethernet frame Data header
184                          * word is maintained.
185                          */
186                         rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
187
188                         rx->remaining = size;
189                 }
190
191                 if (rx->remaining > skb->len - offset) {
192                         copy_length = skb->len - offset;
193                         rx->remaining -= copy_length;
194                 } else {
195                         copy_length = rx->remaining;
196                         rx->remaining = 0;
197                 }
198
199                 if (rx->ax_skb) {
200                         skb_put_data(rx->ax_skb, skb->data + offset,
201                                      copy_length);
202                         if (!rx->remaining) {
203                                 usbnet_skb_return(dev, rx->ax_skb);
204                                 rx->ax_skb = NULL;
205                         }
206                 }
207
208                 offset += (copy_length + 1) & 0xfffe;
209         }
210
211         if (skb->len != offset) {
212                 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
213                            skb->len, offset);
214                 reset_asix_rx_fixup_info(rx);
215                 return 0;
216         }
217
218         return 1;
219 }
220
221 int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
222 {
223         struct asix_common_private *dp = dev->driver_priv;
224         struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
225
226         return asix_rx_fixup_internal(dev, skb, rx);
227 }
228
229 void asix_rx_fixup_common_free(struct asix_common_private *dp)
230 {
231         struct asix_rx_fixup_info *rx;
232
233         if (!dp)
234                 return;
235
236         rx = &dp->rx_fixup_info;
237
238         if (rx->ax_skb) {
239                 kfree_skb(rx->ax_skb);
240                 rx->ax_skb = NULL;
241         }
242 }
243
244 struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
245                               gfp_t flags)
246 {
247         int padlen;
248         int headroom = skb_headroom(skb);
249         int tailroom = skb_tailroom(skb);
250         u32 packet_len;
251         u32 padbytes = 0xffff0000;
252         void *ptr;
253
254         padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
255
256         /* We need to push 4 bytes in front of frame (packet_len)
257          * and maybe add 4 bytes after the end (if padlen is 4)
258          *
259          * Avoid skb_copy_expand() expensive call, using following rules :
260          * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
261          *   is false (and if we have 4 bytes of headroom)
262          * - We are allowed to put 4 bytes at tail if skb_cloned()
263          *   is false (and if we have 4 bytes of tailroom)
264          *
265          * TCP packets for example are cloned, but __skb_header_release()
266          * was called in tcp stack, allowing us to use headroom for our needs.
267          */
268         if (!skb_header_cloned(skb) &&
269             !(padlen && skb_cloned(skb)) &&
270             headroom + tailroom >= 4 + padlen) {
271                 /* following should not happen, but better be safe */
272                 if (headroom < 4 ||
273                     tailroom < padlen) {
274                         skb->data = memmove(skb->head + 4, skb->data, skb->len);
275                         skb_set_tail_pointer(skb, skb->len);
276                 }
277         } else {
278                 struct sk_buff *skb2;
279
280                 skb2 = skb_copy_expand(skb, 4, padlen, flags);
281                 dev_kfree_skb_any(skb);
282                 skb = skb2;
283                 if (!skb)
284                         return NULL;
285         }
286
287         packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
288         ptr = skb_push(skb, 4);
289         put_unaligned_le32(packet_len, ptr);
290
291         if (padlen) {
292                 put_unaligned_le32(padbytes, skb_tail_pointer(skb));
293                 skb_put(skb, sizeof(padbytes));
294         }
295
296         usbnet_set_skb_tx_stats(skb, 1, 0);
297         return skb;
298 }
299
300 int asix_set_sw_mii(struct usbnet *dev, int in_pm)
301 {
302         int ret;
303         ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL, in_pm);
304
305         if (ret < 0)
306                 netdev_err(dev->net, "Failed to enable software MII access\n");
307         return ret;
308 }
309
310 int asix_set_hw_mii(struct usbnet *dev, int in_pm)
311 {
312         int ret;
313         ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL, in_pm);
314         if (ret < 0)
315                 netdev_err(dev->net, "Failed to enable hardware MII access\n");
316         return ret;
317 }
318
319 int asix_read_phy_addr(struct usbnet *dev, bool internal)
320 {
321         int ret, offset;
322         u8 buf[2];
323
324         ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf, 0);
325         if (ret < 0)
326                 goto error;
327
328         if (ret < 2) {
329                 ret = -EIO;
330                 goto error;
331         }
332
333         offset = (internal ? 1 : 0);
334         ret = buf[offset];
335
336         netdev_dbg(dev->net, "%s PHY address 0x%x\n",
337                    internal ? "internal" : "external", ret);
338
339         return ret;
340
341 error:
342         netdev_err(dev->net, "Error reading PHY_ID register: %02x\n", ret);
343
344         return ret;
345 }
346
347 int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm)
348 {
349         int ret;
350
351         ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm);
352         if (ret < 0)
353                 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
354
355         return ret;
356 }
357
358 u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm)
359 {
360         __le16 v;
361         int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm);
362
363         if (ret < 0) {
364                 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
365                 goto out;
366         }
367         ret = le16_to_cpu(v);
368 out:
369         return ret;
370 }
371
372 int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm)
373 {
374         int ret;
375
376         netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
377         ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm);
378         if (ret < 0)
379                 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
380                            mode, ret);
381
382         return ret;
383 }
384
385 u16 asix_read_medium_status(struct usbnet *dev, int in_pm)
386 {
387         __le16 v;
388         int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS,
389                                 0, 0, 2, &v, in_pm);
390
391         if (ret < 0) {
392                 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
393                            ret);
394                 return ret;     /* TODO: callers not checking for error ret */
395         }
396
397         return le16_to_cpu(v);
398
399 }
400
401 int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm)
402 {
403         int ret;
404
405         netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
406         ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
407                              mode, 0, 0, NULL, in_pm);
408         if (ret < 0)
409                 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
410                            mode, ret);
411
412         return ret;
413 }
414
415 /* set MAC link settings according to information from phylib */
416 void asix_adjust_link(struct net_device *netdev)
417 {
418         struct phy_device *phydev = netdev->phydev;
419         struct usbnet *dev = netdev_priv(netdev);
420         u16 mode = 0;
421
422         if (phydev->link) {
423                 mode = AX88772_MEDIUM_DEFAULT;
424
425                 if (phydev->duplex == DUPLEX_HALF)
426                         mode &= ~AX_MEDIUM_FD;
427
428                 if (phydev->speed != SPEED_100)
429                         mode &= ~AX_MEDIUM_PS;
430         }
431
432         asix_write_medium_mode(dev, mode, 0);
433         phy_print_status(phydev);
434         usbnet_link_change(dev, phydev->link, 0);
435 }
436
437 int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
438 {
439         int ret;
440
441         netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
442         ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
443         if (ret < 0)
444                 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
445                            value, ret);
446
447         if (sleep)
448                 msleep(sleep);
449
450         return ret;
451 }
452
453 /*
454  * AX88772 & AX88178 have a 16-bit RX_CTL value
455  */
456 void asix_set_multicast(struct net_device *net)
457 {
458         struct usbnet *dev = netdev_priv(net);
459         struct asix_data *data = (struct asix_data *)&dev->data;
460         u16 rx_ctl = AX_DEFAULT_RX_CTL;
461
462         if (net->flags & IFF_PROMISC) {
463                 rx_ctl |= AX_RX_CTL_PRO;
464         } else if (net->flags & IFF_ALLMULTI ||
465                    netdev_mc_count(net) > AX_MAX_MCAST) {
466                 rx_ctl |= AX_RX_CTL_AMALL;
467         } else if (netdev_mc_empty(net)) {
468                 /* just broadcast and directed */
469         } else {
470                 /* We use the 20 byte dev->data
471                  * for our 8 byte filter buffer
472                  * to avoid allocating memory that
473                  * is tricky to free later */
474                 struct netdev_hw_addr *ha;
475                 u32 crc_bits;
476
477                 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
478
479                 /* Build the multicast hash filter. */
480                 netdev_for_each_mc_addr(ha, net) {
481                         crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
482                         data->multi_filter[crc_bits >> 3] |=
483                             1 << (crc_bits & 7);
484                 }
485
486                 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
487                                    AX_MCAST_FILTER_SIZE, data->multi_filter);
488
489                 rx_ctl |= AX_RX_CTL_AM;
490         }
491
492         asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
493 }
494
495 int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
496 {
497         struct usbnet *dev = netdev_priv(netdev);
498         __le16 res;
499         int ret;
500
501         mutex_lock(&dev->phy_mutex);
502
503         ret = asix_check_host_enable(dev, 0);
504         if (ret == -ENODEV || ret == -ETIMEDOUT) {
505                 mutex_unlock(&dev->phy_mutex);
506                 return ret;
507         }
508
509         ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2,
510                             &res, 0);
511         if (ret < 0)
512                 goto out;
513
514         ret = asix_set_hw_mii(dev, 0);
515 out:
516         mutex_unlock(&dev->phy_mutex);
517
518         netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
519                         phy_id, loc, le16_to_cpu(res));
520
521         return ret < 0 ? ret : le16_to_cpu(res);
522 }
523
524 static int __asix_mdio_write(struct net_device *netdev, int phy_id, int loc,
525                              int val)
526 {
527         struct usbnet *dev = netdev_priv(netdev);
528         __le16 res = cpu_to_le16(val);
529         int ret;
530
531         netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
532                         phy_id, loc, val);
533
534         mutex_lock(&dev->phy_mutex);
535
536         ret = asix_check_host_enable(dev, 0);
537         if (ret == -ENODEV)
538                 goto out;
539
540         ret = asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2,
541                              &res, 0);
542         if (ret < 0)
543                 goto out;
544
545         ret = asix_set_hw_mii(dev, 0);
546 out:
547         mutex_unlock(&dev->phy_mutex);
548
549         return ret < 0 ? ret : 0;
550 }
551
552 void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
553 {
554         __asix_mdio_write(netdev, phy_id, loc, val);
555 }
556
557 /* MDIO read and write wrappers for phylib */
558 int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum)
559 {
560         struct usbnet *priv = bus->priv;
561
562         return asix_mdio_read(priv->net, phy_id, regnum);
563 }
564
565 int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum, u16 val)
566 {
567         struct usbnet *priv = bus->priv;
568
569         return __asix_mdio_write(priv->net, phy_id, regnum, val);
570 }
571
572 int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
573 {
574         struct usbnet *dev = netdev_priv(netdev);
575         __le16 res;
576         int ret;
577
578         mutex_lock(&dev->phy_mutex);
579
580         ret = asix_check_host_enable(dev, 1);
581         if (ret == -ENODEV || ret == -ETIMEDOUT) {
582                 mutex_unlock(&dev->phy_mutex);
583                 return ret;
584         }
585
586         ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
587                             (__u16)loc, 2, &res, 1);
588         if (ret < 0) {
589                 mutex_unlock(&dev->phy_mutex);
590                 return ret;
591         }
592         asix_set_hw_mii(dev, 1);
593         mutex_unlock(&dev->phy_mutex);
594
595         netdev_dbg(dev->net, "asix_mdio_read_nopm() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
596                         phy_id, loc, le16_to_cpu(res));
597
598         return le16_to_cpu(res);
599 }
600
601 void
602 asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
603 {
604         struct usbnet *dev = netdev_priv(netdev);
605         __le16 res = cpu_to_le16(val);
606         int ret;
607
608         netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
609                         phy_id, loc, val);
610
611         mutex_lock(&dev->phy_mutex);
612
613         ret = asix_check_host_enable(dev, 1);
614         if (ret == -ENODEV) {
615                 mutex_unlock(&dev->phy_mutex);
616                 return;
617         }
618
619         asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
620                        (__u16)loc, 2, &res, 1);
621         asix_set_hw_mii(dev, 1);
622         mutex_unlock(&dev->phy_mutex);
623 }
624
625 void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
626 {
627         struct usbnet *dev = netdev_priv(net);
628         u8 opt;
629
630         if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE,
631                           0, 0, 1, &opt, 0) < 0) {
632                 wolinfo->supported = 0;
633                 wolinfo->wolopts = 0;
634                 return;
635         }
636         wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
637         wolinfo->wolopts = 0;
638         if (opt & AX_MONITOR_LINK)
639                 wolinfo->wolopts |= WAKE_PHY;
640         if (opt & AX_MONITOR_MAGIC)
641                 wolinfo->wolopts |= WAKE_MAGIC;
642 }
643
644 int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
645 {
646         struct usbnet *dev = netdev_priv(net);
647         u8 opt = 0;
648
649         if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
650                 return -EINVAL;
651
652         if (wolinfo->wolopts & WAKE_PHY)
653                 opt |= AX_MONITOR_LINK;
654         if (wolinfo->wolopts & WAKE_MAGIC)
655                 opt |= AX_MONITOR_MAGIC;
656
657         if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
658                               opt, 0, 0, NULL, 0) < 0)
659                 return -EINVAL;
660
661         return 0;
662 }
663
664 int asix_get_eeprom_len(struct net_device *net)
665 {
666         return AX_EEPROM_LEN;
667 }
668
669 int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
670                     u8 *data)
671 {
672         struct usbnet *dev = netdev_priv(net);
673         u16 *eeprom_buff;
674         int first_word, last_word;
675         int i;
676
677         if (eeprom->len == 0)
678                 return -EINVAL;
679
680         eeprom->magic = AX_EEPROM_MAGIC;
681
682         first_word = eeprom->offset >> 1;
683         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
684
685         eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
686                                     GFP_KERNEL);
687         if (!eeprom_buff)
688                 return -ENOMEM;
689
690         /* ax8817x returns 2 bytes from eeprom on read */
691         for (i = first_word; i <= last_word; i++) {
692                 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
693                                   &eeprom_buff[i - first_word], 0) < 0) {
694                         kfree(eeprom_buff);
695                         return -EIO;
696                 }
697         }
698
699         memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
700         kfree(eeprom_buff);
701         return 0;
702 }
703
704 int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
705                     u8 *data)
706 {
707         struct usbnet *dev = netdev_priv(net);
708         u16 *eeprom_buff;
709         int first_word, last_word;
710         int i;
711         int ret;
712
713         netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
714                    eeprom->len, eeprom->offset, eeprom->magic);
715
716         if (eeprom->len == 0)
717                 return -EINVAL;
718
719         if (eeprom->magic != AX_EEPROM_MAGIC)
720                 return -EINVAL;
721
722         first_word = eeprom->offset >> 1;
723         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
724
725         eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
726                                     GFP_KERNEL);
727         if (!eeprom_buff)
728                 return -ENOMEM;
729
730         /* align data to 16 bit boundaries, read the missing data from
731            the EEPROM */
732         if (eeprom->offset & 1) {
733                 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
734                                     &eeprom_buff[0], 0);
735                 if (ret < 0) {
736                         netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
737                         goto free;
738                 }
739         }
740
741         if ((eeprom->offset + eeprom->len) & 1) {
742                 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
743                                     &eeprom_buff[last_word - first_word], 0);
744                 if (ret < 0) {
745                         netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
746                         goto free;
747                 }
748         }
749
750         memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
751
752         /* write data to EEPROM */
753         ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0);
754         if (ret < 0) {
755                 netdev_err(net, "Failed to enable EEPROM write\n");
756                 goto free;
757         }
758         msleep(20);
759
760         for (i = first_word; i <= last_word; i++) {
761                 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
762                            i, eeprom_buff[i - first_word]);
763                 ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
764                                      eeprom_buff[i - first_word], 0, NULL, 0);
765                 if (ret < 0) {
766                         netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
767                                    i);
768                         goto free;
769                 }
770                 msleep(20);
771         }
772
773         ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0);
774         if (ret < 0) {
775                 netdev_err(net, "Failed to disable EEPROM write\n");
776                 goto free;
777         }
778
779         ret = 0;
780 free:
781         kfree(eeprom_buff);
782         return ret;
783 }
784
785 void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
786 {
787         /* Inherit standard device info */
788         usbnet_get_drvinfo(net, info);
789         strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
790         strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
791 }
792
793 int asix_set_mac_address(struct net_device *net, void *p)
794 {
795         struct usbnet *dev = netdev_priv(net);
796         struct asix_data *data = (struct asix_data *)&dev->data;
797         struct sockaddr *addr = p;
798
799         if (netif_running(net))
800                 return -EBUSY;
801         if (!is_valid_ether_addr(addr->sa_data))
802                 return -EADDRNOTAVAIL;
803
804         memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
805
806         /* We use the 20 byte dev->data
807          * for our 6 byte mac buffer
808          * to avoid allocating memory that
809          * is tricky to free later */
810         memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
811         asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
812                                                         data->mac_addr);
813
814         return 0;
815 }