0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/module.h>
0011 #include <linux/netdevice.h>
0012 #include <linux/etherdevice.h>
0013 #include <linux/ethtool.h>
0014 #include <linux/workqueue.h>
0015 #include <linux/mii.h>
0016 #include <linux/usb.h>
0017 #include <linux/usb/usbnet.h>
0018 #include <linux/slab.h>
0019
0020 #include <asm/unaligned.h>
0021
0022
0023
0024
0025
0026
0027
0028
0029 #define frame_errors data[1]
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 struct nc_header {
0045 __le16 hdr_len;
0046 __le16 packet_len;
0047 __le16 packet_id;
0048 #define MIN_HEADER 6
0049
0050
0051
0052
0053 } __packed;
0054
0055 #define PAD_BYTE ((unsigned char)0xAC)
0056
0057 struct nc_trailer {
0058 __le16 packet_id;
0059 } __packed;
0060
0061
0062 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
0063 + sizeof (struct ethhdr) \
0064 + (mtu) \
0065 + 1 \
0066 + sizeof (struct nc_trailer))
0067
0068 #define MIN_FRAMED FRAMED_SIZE(0)
0069
0070
0071 #define NC_MAX_PACKET 32767
0072
0073
0074
0075
0076
0077
0078
0079
0080 #define NC_READ_TTL_MS ((u8)255)
0081
0082
0083
0084
0085 #define REG_USBCTL ((u8)0x04)
0086 #define REG_TTL ((u8)0x10)
0087 #define REG_STATUS ((u8)0x11)
0088
0089
0090
0091
0092 #define REQUEST_REGISTER ((u8)0x10)
0093 #define REQUEST_EEPROM ((u8)0x11)
0094
0095 static int
0096 nc_vendor_read(struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
0097 {
0098 int status = usbnet_read_cmd(dev, req,
0099 USB_DIR_IN | USB_TYPE_VENDOR |
0100 USB_RECIP_DEVICE,
0101 0, regnum, retval_ptr,
0102 sizeof *retval_ptr);
0103 if (status > 0)
0104 status = 0;
0105 if (!status)
0106 le16_to_cpus(retval_ptr);
0107 return status;
0108 }
0109
0110 static inline int
0111 nc_register_read(struct usbnet *dev, u8 regnum, u16 *retval_ptr)
0112 {
0113 return nc_vendor_read(dev, REQUEST_REGISTER, regnum, retval_ptr);
0114 }
0115
0116 static void
0117 nc_vendor_write(struct usbnet *dev, u8 req, u8 regnum, u16 value)
0118 {
0119 usbnet_write_cmd(dev, req,
0120 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0121 value, regnum, NULL, 0);
0122 }
0123
0124 static inline void
0125 nc_register_write(struct usbnet *dev, u8 regnum, u16 value)
0126 {
0127 nc_vendor_write(dev, REQUEST_REGISTER, regnum, value);
0128 }
0129
0130
0131 #if 0
0132 static void nc_dump_registers(struct usbnet *dev)
0133 {
0134 u8 reg;
0135 u16 *vp = kmalloc(sizeof (u16));
0136
0137 if (!vp)
0138 return;
0139
0140 netdev_dbg(dev->net, "registers:\n");
0141 for (reg = 0; reg < 0x20; reg++) {
0142 int retval;
0143
0144
0145 if (reg >= 0x08 && reg <= 0xf)
0146 continue;
0147 if (reg >= 0x12 && reg <= 0x1e)
0148 continue;
0149
0150 retval = nc_register_read(dev, reg, vp);
0151 if (retval < 0)
0152 netdev_dbg(dev->net, "reg [0x%x] ==> error %d\n",
0153 reg, retval);
0154 else
0155 netdev_dbg(dev->net, "reg [0x%x] = 0x%x\n", reg, *vp);
0156 }
0157 kfree(vp);
0158 }
0159 #endif
0160
0161
0162
0163
0164
0165
0166
0167
0168 #define USBCTL_WRITABLE_MASK 0x1f0f
0169
0170 #define USBCTL_ENABLE_LANG (1 << 12)
0171 #define USBCTL_ENABLE_MFGR (1 << 11)
0172 #define USBCTL_ENABLE_PROD (1 << 10)
0173 #define USBCTL_ENABLE_SERIAL (1 << 9)
0174 #define USBCTL_ENABLE_DEFAULTS (1 << 8)
0175
0176 #define USBCTL_FLUSH_OTHER (1 << 3)
0177 #define USBCTL_FLUSH_THIS (1 << 2)
0178 #define USBCTL_DISCONN_OTHER (1 << 1)
0179 #define USBCTL_DISCONN_THIS (1 << 0)
0180
0181 static inline void nc_dump_usbctl(struct usbnet *dev, u16 usbctl)
0182 {
0183 netif_dbg(dev, link, dev->net,
0184 "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s; this%s%s; other%s%s; r/o 0x%x\n",
0185 dev->udev->bus->bus_name, dev->udev->devpath,
0186 usbctl,
0187 (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
0188 (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
0189 (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
0190 (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
0191 (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
0192
0193 (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
0194 (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
0195
0196 (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
0197 (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
0198
0199 usbctl & ~USBCTL_WRITABLE_MASK);
0200 }
0201
0202
0203
0204
0205
0206
0207
0208 #define STATUS_PORT_A (1 << 15)
0209
0210 #define STATUS_CONN_OTHER (1 << 14)
0211 #define STATUS_SUSPEND_OTHER (1 << 13)
0212 #define STATUS_MAILBOX_OTHER (1 << 12)
0213 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) & 0x03)
0214
0215 #define STATUS_CONN_THIS (1 << 6)
0216 #define STATUS_SUSPEND_THIS (1 << 5)
0217 #define STATUS_MAILBOX_THIS (1 << 4)
0218 #define STATUS_PACKETS_THIS(n) (((n) >> 0) & 0x03)
0219
0220 #define STATUS_UNSPEC_MASK 0x0c8c
0221 #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK))
0222
0223
0224 static inline void nc_dump_status(struct usbnet *dev, u16 status)
0225 {
0226 netif_dbg(dev, link, dev->net,
0227 "net1080 %s-%s status 0x%x: this (%c) PKT=%d%s%s%s; other PKT=%d%s%s%s; unspec 0x%x\n",
0228 dev->udev->bus->bus_name, dev->udev->devpath,
0229 status,
0230
0231
0232
0233
0234 (status & STATUS_PORT_A) ? 'A' : 'B',
0235 STATUS_PACKETS_THIS(status),
0236 (status & STATUS_CONN_THIS) ? " CON" : "",
0237 (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
0238 (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
0239
0240 STATUS_PACKETS_OTHER(status),
0241 (status & STATUS_CONN_OTHER) ? " CON" : "",
0242 (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
0243 (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
0244
0245 status & STATUS_UNSPEC_MASK);
0246 }
0247
0248
0249
0250
0251
0252
0253
0254 #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8))
0255 #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this))))
0256
0257
0258
0259 static int net1080_reset(struct usbnet *dev)
0260 {
0261 u16 usbctl, status, ttl;
0262 u16 vp;
0263 int retval;
0264
0265
0266
0267 if ((retval = nc_register_read(dev, REG_STATUS, &vp)) < 0) {
0268 netdev_dbg(dev->net, "can't read %s-%s status: %d\n",
0269 dev->udev->bus->bus_name, dev->udev->devpath, retval);
0270 goto done;
0271 }
0272 status = vp;
0273 nc_dump_status(dev, status);
0274
0275 if ((retval = nc_register_read(dev, REG_USBCTL, &vp)) < 0) {
0276 netdev_dbg(dev->net, "can't read USBCTL, %d\n", retval);
0277 goto done;
0278 }
0279 usbctl = vp;
0280 nc_dump_usbctl(dev, usbctl);
0281
0282 nc_register_write(dev, REG_USBCTL,
0283 USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
0284
0285 if ((retval = nc_register_read(dev, REG_TTL, &vp)) < 0) {
0286 netdev_dbg(dev->net, "can't read TTL, %d\n", retval);
0287 goto done;
0288 }
0289 ttl = vp;
0290
0291 nc_register_write(dev, REG_TTL,
0292 MK_TTL(NC_READ_TTL_MS, TTL_OTHER(ttl)) );
0293 netdev_dbg(dev->net, "assigned TTL, %d ms\n", NC_READ_TTL_MS);
0294
0295 netif_info(dev, link, dev->net, "port %c, peer %sconnected\n",
0296 (status & STATUS_PORT_A) ? 'A' : 'B',
0297 (status & STATUS_CONN_OTHER) ? "" : "dis");
0298 retval = 0;
0299
0300 done:
0301 return retval;
0302 }
0303
0304 static int net1080_check_connect(struct usbnet *dev)
0305 {
0306 int retval;
0307 u16 status;
0308 u16 vp;
0309
0310 retval = nc_register_read(dev, REG_STATUS, &vp);
0311 status = vp;
0312 if (retval != 0) {
0313 netdev_dbg(dev->net, "net1080_check_conn read - %d\n", retval);
0314 return retval;
0315 }
0316 if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
0317 return -ENOLINK;
0318 return 0;
0319 }
0320
0321 static void nc_ensure_sync(struct usbnet *dev)
0322 {
0323 if (++dev->frame_errors <= 5)
0324 return;
0325
0326 if (usbnet_write_cmd_async(dev, REQUEST_REGISTER,
0327 USB_DIR_OUT | USB_TYPE_VENDOR |
0328 USB_RECIP_DEVICE,
0329 USBCTL_FLUSH_THIS |
0330 USBCTL_FLUSH_OTHER,
0331 REG_USBCTL, NULL, 0))
0332 return;
0333
0334 netif_dbg(dev, rx_err, dev->net,
0335 "flush net1080; too many framing errors\n");
0336 dev->frame_errors = 0;
0337 }
0338
0339 static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
0340 {
0341 struct nc_header *header;
0342 struct nc_trailer *trailer;
0343 u16 hdr_len, packet_len;
0344
0345
0346 if (skb->len < dev->net->hard_header_len)
0347 return 0;
0348
0349 if (!(skb->len & 0x01)) {
0350 netdev_dbg(dev->net, "rx framesize %d range %d..%d mtu %d\n",
0351 skb->len, dev->net->hard_header_len, dev->hard_mtu,
0352 dev->net->mtu);
0353 dev->net->stats.rx_frame_errors++;
0354 nc_ensure_sync(dev);
0355 return 0;
0356 }
0357
0358 header = (struct nc_header *) skb->data;
0359 hdr_len = le16_to_cpup(&header->hdr_len);
0360 packet_len = le16_to_cpup(&header->packet_len);
0361 if (FRAMED_SIZE(packet_len) > NC_MAX_PACKET) {
0362 dev->net->stats.rx_frame_errors++;
0363 netdev_dbg(dev->net, "packet too big, %d\n", packet_len);
0364 nc_ensure_sync(dev);
0365 return 0;
0366 } else if (hdr_len < MIN_HEADER) {
0367 dev->net->stats.rx_frame_errors++;
0368 netdev_dbg(dev->net, "header too short, %d\n", hdr_len);
0369 nc_ensure_sync(dev);
0370 return 0;
0371 } else if (hdr_len > MIN_HEADER) {
0372
0373 netdev_dbg(dev->net, "header OOB, %d bytes\n", hdr_len - MIN_HEADER);
0374 nc_ensure_sync(dev);
0375
0376 }
0377 skb_pull(skb, hdr_len);
0378
0379 trailer = (struct nc_trailer *)
0380 (skb->data + skb->len - sizeof *trailer);
0381 skb_trim(skb, skb->len - sizeof *trailer);
0382
0383 if ((packet_len & 0x01) == 0) {
0384 if (skb->data [packet_len] != PAD_BYTE) {
0385 dev->net->stats.rx_frame_errors++;
0386 netdev_dbg(dev->net, "bad pad\n");
0387 return 0;
0388 }
0389 skb_trim(skb, skb->len - 1);
0390 }
0391 if (skb->len != packet_len) {
0392 dev->net->stats.rx_frame_errors++;
0393 netdev_dbg(dev->net, "bad packet len %d (expected %d)\n",
0394 skb->len, packet_len);
0395 nc_ensure_sync(dev);
0396 return 0;
0397 }
0398 if (header->packet_id != get_unaligned(&trailer->packet_id)) {
0399 dev->net->stats.rx_fifo_errors++;
0400 netdev_dbg(dev->net, "(2+ dropped) rx packet_id mismatch 0x%x 0x%x\n",
0401 le16_to_cpu(header->packet_id),
0402 le16_to_cpu(trailer->packet_id));
0403 return 0;
0404 }
0405 #if 0
0406 netdev_dbg(dev->net, "frame <rx h %d p %d id %d\n", header->hdr_len,
0407 header->packet_len, header->packet_id);
0408 #endif
0409 dev->frame_errors = 0;
0410 return 1;
0411 }
0412
0413 static struct sk_buff *
0414 net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
0415 {
0416 struct sk_buff *skb2;
0417 struct nc_header *header = NULL;
0418 struct nc_trailer *trailer = NULL;
0419 int padlen = sizeof (struct nc_trailer);
0420 int len = skb->len;
0421
0422 if (!((len + padlen + sizeof (struct nc_header)) & 0x01))
0423 padlen++;
0424 if (!skb_cloned(skb)) {
0425 int headroom = skb_headroom(skb);
0426 int tailroom = skb_tailroom(skb);
0427
0428 if (padlen <= tailroom &&
0429 sizeof(struct nc_header) <= headroom)
0430
0431 goto encapsulate;
0432
0433 if ((sizeof (struct nc_header) + padlen) <
0434 (headroom + tailroom)) {
0435
0436 skb->data = memmove(skb->head
0437 + sizeof (struct nc_header),
0438 skb->data, skb->len);
0439 skb_set_tail_pointer(skb, len);
0440 goto encapsulate;
0441 }
0442 }
0443
0444
0445 skb2 = skb_copy_expand(skb,
0446 sizeof (struct nc_header),
0447 padlen,
0448 flags);
0449 dev_kfree_skb_any(skb);
0450 if (!skb2)
0451 return skb2;
0452 skb = skb2;
0453
0454 encapsulate:
0455
0456 header = skb_push(skb, sizeof *header);
0457 header->hdr_len = cpu_to_le16(sizeof (*header));
0458 header->packet_len = cpu_to_le16(len);
0459 header->packet_id = cpu_to_le16((u16)dev->xid++);
0460
0461
0462 if (!((skb->len + sizeof *trailer) & 0x01))
0463 skb_put_u8(skb, PAD_BYTE);
0464 trailer = skb_put(skb, sizeof *trailer);
0465 put_unaligned(header->packet_id, &trailer->packet_id);
0466 #if 0
0467 netdev_dbg(dev->net, "frame >tx h %d p %d id %d\n",
0468 header->hdr_len, header->packet_len,
0469 header->packet_id);
0470 #endif
0471 return skb;
0472 }
0473
0474 static int net1080_bind(struct usbnet *dev, struct usb_interface *intf)
0475 {
0476 unsigned extra = sizeof (struct nc_header)
0477 + 1
0478 + sizeof (struct nc_trailer);
0479
0480 dev->net->hard_header_len += extra;
0481 dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu;
0482 dev->hard_mtu = NC_MAX_PACKET;
0483 return usbnet_get_endpoints (dev, intf);
0484 }
0485
0486 static const struct driver_info net1080_info = {
0487 .description = "NetChip TurboCONNECT",
0488 .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_NC,
0489 .bind = net1080_bind,
0490 .reset = net1080_reset,
0491 .check_connect = net1080_check_connect,
0492 .rx_fixup = net1080_rx_fixup,
0493 .tx_fixup = net1080_tx_fixup,
0494 };
0495
0496 static const struct usb_device_id products [] = {
0497 {
0498 USB_DEVICE(0x0525, 0x1080),
0499 .driver_info = (unsigned long) &net1080_info,
0500 }, {
0501 USB_DEVICE(0x06D0, 0x0622),
0502 .driver_info = (unsigned long) &net1080_info,
0503 },
0504 { },
0505 };
0506 MODULE_DEVICE_TABLE(usb, products);
0507
0508 static struct usb_driver net1080_driver = {
0509 .name = "net1080",
0510 .id_table = products,
0511 .probe = usbnet_probe,
0512 .disconnect = usbnet_disconnect,
0513 .suspend = usbnet_suspend,
0514 .resume = usbnet_resume,
0515 .disable_hub_initiated_lpm = 1,
0516 };
0517
0518 module_usb_driver(net1080_driver);
0519
0520 MODULE_AUTHOR("David Brownell");
0521 MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
0522 MODULE_LICENSE("GPL");