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