1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
27#include <linux/ethtool.h>
28#include <linux/workqueue.h>
29#include <linux/mii.h>
30#include <linux/usb.h>
31#include <linux/usb/usbnet.h>
32#include <linux/slab.h>
33
34#include <asm/unaligned.h>
35
36
37
38
39
40
41
42
43#define frame_errors data[1]
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58struct nc_header {
59 __le16 hdr_len;
60 __le16 packet_len;
61 __le16 packet_id;
62#define MIN_HEADER 6
63
64
65
66
67} __packed;
68
69#define PAD_BYTE ((unsigned char)0xAC)
70
71struct nc_trailer {
72 __le16 packet_id;
73} __packed;
74
75
76#define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
77 + sizeof (struct ethhdr) \
78 + (mtu) \
79 + 1 \
80 + sizeof (struct nc_trailer))
81
82#define MIN_FRAMED FRAMED_SIZE(0)
83
84
85#define NC_MAX_PACKET 32767
86
87
88
89
90
91
92
93
94#define NC_READ_TTL_MS ((u8)255)
95
96
97
98
99#define REG_USBCTL ((u8)0x04)
100#define REG_TTL ((u8)0x10)
101#define REG_STATUS ((u8)0x11)
102
103
104
105
106#define REQUEST_REGISTER ((u8)0x10)
107#define REQUEST_EEPROM ((u8)0x11)
108
109static int
110nc_vendor_read(struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
111{
112 int status = usbnet_read_cmd(dev, req,
113 USB_DIR_IN | USB_TYPE_VENDOR |
114 USB_RECIP_DEVICE,
115 0, regnum, retval_ptr,
116 sizeof *retval_ptr);
117 if (status > 0)
118 status = 0;
119 if (!status)
120 le16_to_cpus(retval_ptr);
121 return status;
122}
123
124static inline int
125nc_register_read(struct usbnet *dev, u8 regnum, u16 *retval_ptr)
126{
127 return nc_vendor_read(dev, REQUEST_REGISTER, regnum, retval_ptr);
128}
129
130
131static void
132nc_vendor_write(struct usbnet *dev, u8 req, u8 regnum, u16 value)
133{
134 usbnet_write_cmd(dev, req,
135 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
136 value, regnum, NULL, 0);
137}
138
139static inline void
140nc_register_write(struct usbnet *dev, u8 regnum, u16 value)
141{
142 nc_vendor_write(dev, REQUEST_REGISTER, regnum, value);
143}
144
145
146#if 0
147static void nc_dump_registers(struct usbnet *dev)
148{
149 u8 reg;
150 u16 *vp = kmalloc(sizeof (u16));
151
152 if (!vp)
153 return;
154
155 netdev_dbg(dev->net, "registers:\n");
156 for (reg = 0; reg < 0x20; reg++) {
157 int retval;
158
159
160 if (reg >= 0x08 && reg <= 0xf)
161 continue;
162 if (reg >= 0x12 && reg <= 0x1e)
163 continue;
164
165 retval = nc_register_read(dev, reg, vp);
166 if (retval < 0)
167 netdev_dbg(dev->net, "reg [0x%x] ==> error %d\n",
168 reg, retval);
169 else
170 netdev_dbg(dev->net, "reg [0x%x] = 0x%x\n", reg, *vp);
171 }
172 kfree(vp);
173}
174#endif
175
176
177
178
179
180
181
182
183#define USBCTL_WRITABLE_MASK 0x1f0f
184
185#define USBCTL_ENABLE_LANG (1 << 12)
186#define USBCTL_ENABLE_MFGR (1 << 11)
187#define USBCTL_ENABLE_PROD (1 << 10)
188#define USBCTL_ENABLE_SERIAL (1 << 9)
189#define USBCTL_ENABLE_DEFAULTS (1 << 8)
190
191#define USBCTL_FLUSH_OTHER (1 << 3)
192#define USBCTL_FLUSH_THIS (1 << 2)
193#define USBCTL_DISCONN_OTHER (1 << 1)
194#define USBCTL_DISCONN_THIS (1 << 0)
195
196static inline void nc_dump_usbctl(struct usbnet *dev, u16 usbctl)
197{
198 netif_dbg(dev, link, dev->net,
199 "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s; this%s%s; other%s%s; r/o 0x%x\n",
200 dev->udev->bus->bus_name, dev->udev->devpath,
201 usbctl,
202 (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
203 (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
204 (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
205 (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
206 (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
207
208 (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
209 (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
210
211 (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
212 (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
213
214 usbctl & ~USBCTL_WRITABLE_MASK);
215}
216
217
218
219
220
221
222
223#define STATUS_PORT_A (1 << 15)
224
225#define STATUS_CONN_OTHER (1 << 14)
226#define STATUS_SUSPEND_OTHER (1 << 13)
227#define STATUS_MAILBOX_OTHER (1 << 12)
228#define STATUS_PACKETS_OTHER(n) (((n) >> 8) & 0x03)
229
230#define STATUS_CONN_THIS (1 << 6)
231#define STATUS_SUSPEND_THIS (1 << 5)
232#define STATUS_MAILBOX_THIS (1 << 4)
233#define STATUS_PACKETS_THIS(n) (((n) >> 0) & 0x03)
234
235#define STATUS_UNSPEC_MASK 0x0c8c
236#define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK))
237
238
239static inline void nc_dump_status(struct usbnet *dev, u16 status)
240{
241 netif_dbg(dev, link, dev->net,
242 "net1080 %s-%s status 0x%x: this (%c) PKT=%d%s%s%s; other PKT=%d%s%s%s; unspec 0x%x\n",
243 dev->udev->bus->bus_name, dev->udev->devpath,
244 status,
245
246
247
248
249 (status & STATUS_PORT_A) ? 'A' : 'B',
250 STATUS_PACKETS_THIS(status),
251 (status & STATUS_CONN_THIS) ? " CON" : "",
252 (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
253 (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
254
255 STATUS_PACKETS_OTHER(status),
256 (status & STATUS_CONN_OTHER) ? " CON" : "",
257 (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
258 (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
259
260 status & STATUS_UNSPEC_MASK);
261}
262
263
264
265
266
267
268
269#define TTL_THIS(ttl) (0x00ff & ttl)
270#define TTL_OTHER(ttl) (0x00ff & (ttl >> 8))
271#define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this))))
272
273static inline void nc_dump_ttl(struct usbnet *dev, u16 ttl)
274{
275 netif_dbg(dev, link, dev->net, "net1080 %s-%s ttl 0x%x this = %d, other = %d\n",
276 dev->udev->bus->bus_name, dev->udev->devpath,
277 ttl, TTL_THIS(ttl), TTL_OTHER(ttl));
278}
279
280
281
282static int net1080_reset(struct usbnet *dev)
283{
284 u16 usbctl, status, ttl;
285 u16 vp;
286 int retval;
287
288
289
290 if ((retval = nc_register_read(dev, REG_STATUS, &vp)) < 0) {
291 netdev_dbg(dev->net, "can't read %s-%s status: %d\n",
292 dev->udev->bus->bus_name, dev->udev->devpath, retval);
293 goto done;
294 }
295 status = vp;
296 nc_dump_status(dev, status);
297
298 if ((retval = nc_register_read(dev, REG_USBCTL, &vp)) < 0) {
299 netdev_dbg(dev->net, "can't read USBCTL, %d\n", retval);
300 goto done;
301 }
302 usbctl = vp;
303 nc_dump_usbctl(dev, usbctl);
304
305 nc_register_write(dev, REG_USBCTL,
306 USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
307
308 if ((retval = nc_register_read(dev, REG_TTL, &vp)) < 0) {
309 netdev_dbg(dev->net, "can't read TTL, %d\n", retval);
310 goto done;
311 }
312 ttl = vp;
313
314
315 nc_register_write(dev, REG_TTL,
316 MK_TTL(NC_READ_TTL_MS, TTL_OTHER(ttl)) );
317 netdev_dbg(dev->net, "assigned TTL, %d ms\n", NC_READ_TTL_MS);
318
319 netif_info(dev, link, dev->net, "port %c, peer %sconnected\n",
320 (status & STATUS_PORT_A) ? 'A' : 'B',
321 (status & STATUS_CONN_OTHER) ? "" : "dis");
322 retval = 0;
323
324done:
325 return retval;
326}
327
328static int net1080_check_connect(struct usbnet *dev)
329{
330 int retval;
331 u16 status;
332 u16 vp;
333
334 retval = nc_register_read(dev, REG_STATUS, &vp);
335 status = vp;
336 if (retval != 0) {
337 netdev_dbg(dev->net, "net1080_check_conn read - %d\n", retval);
338 return retval;
339 }
340 if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
341 return -ENOLINK;
342 return 0;
343}
344
345static void nc_ensure_sync(struct usbnet *dev)
346{
347 if (++dev->frame_errors <= 5)
348 return;
349
350 if (usbnet_write_cmd_async(dev, REQUEST_REGISTER,
351 USB_DIR_OUT | USB_TYPE_VENDOR |
352 USB_RECIP_DEVICE,
353 USBCTL_FLUSH_THIS |
354 USBCTL_FLUSH_OTHER,
355 REG_USBCTL, NULL, 0))
356 return;
357
358 netif_dbg(dev, rx_err, dev->net,
359 "flush net1080; too many framing errors\n");
360 dev->frame_errors = 0;
361}
362
363static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
364{
365 struct nc_header *header;
366 struct nc_trailer *trailer;
367 u16 hdr_len, packet_len;
368
369 if (!(skb->len & 0x01)) {
370 netdev_dbg(dev->net, "rx framesize %d range %d..%d mtu %d\n",
371 skb->len, dev->net->hard_header_len, dev->hard_mtu,
372 dev->net->mtu);
373 dev->net->stats.rx_frame_errors++;
374 nc_ensure_sync(dev);
375 return 0;
376 }
377
378 header = (struct nc_header *) skb->data;
379 hdr_len = le16_to_cpup(&header->hdr_len);
380 packet_len = le16_to_cpup(&header->packet_len);
381 if (FRAMED_SIZE(packet_len) > NC_MAX_PACKET) {
382 dev->net->stats.rx_frame_errors++;
383 netdev_dbg(dev->net, "packet too big, %d\n", packet_len);
384 nc_ensure_sync(dev);
385 return 0;
386 } else if (hdr_len < MIN_HEADER) {
387 dev->net->stats.rx_frame_errors++;
388 netdev_dbg(dev->net, "header too short, %d\n", hdr_len);
389 nc_ensure_sync(dev);
390 return 0;
391 } else if (hdr_len > MIN_HEADER) {
392
393 netdev_dbg(dev->net, "header OOB, %d bytes\n", hdr_len - MIN_HEADER);
394 nc_ensure_sync(dev);
395
396 }
397 skb_pull(skb, hdr_len);
398
399 trailer = (struct nc_trailer *)
400 (skb->data + skb->len - sizeof *trailer);
401 skb_trim(skb, skb->len - sizeof *trailer);
402
403 if ((packet_len & 0x01) == 0) {
404 if (skb->data [packet_len] != PAD_BYTE) {
405 dev->net->stats.rx_frame_errors++;
406 netdev_dbg(dev->net, "bad pad\n");
407 return 0;
408 }
409 skb_trim(skb, skb->len - 1);
410 }
411 if (skb->len != packet_len) {
412 dev->net->stats.rx_frame_errors++;
413 netdev_dbg(dev->net, "bad packet len %d (expected %d)\n",
414 skb->len, packet_len);
415 nc_ensure_sync(dev);
416 return 0;
417 }
418 if (header->packet_id != get_unaligned(&trailer->packet_id)) {
419 dev->net->stats.rx_fifo_errors++;
420 netdev_dbg(dev->net, "(2+ dropped) rx packet_id mismatch 0x%x 0x%x\n",
421 le16_to_cpu(header->packet_id),
422 le16_to_cpu(trailer->packet_id));
423 return 0;
424 }
425#if 0
426 netdev_dbg(dev->net, "frame <rx h %d p %d id %d\n", header->hdr_len,
427 header->packet_len, header->packet_id);
428#endif
429 dev->frame_errors = 0;
430 return 1;
431}
432
433static struct sk_buff *
434net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
435{
436 struct sk_buff *skb2;
437 struct nc_header *header = NULL;
438 struct nc_trailer *trailer = NULL;
439 int padlen = sizeof (struct nc_trailer);
440 int len = skb->len;
441
442 if (!((len + padlen + sizeof (struct nc_header)) & 0x01))
443 padlen++;
444 if (!skb_cloned(skb)) {
445 int headroom = skb_headroom(skb);
446 int tailroom = skb_tailroom(skb);
447
448 if (padlen <= tailroom &&
449 sizeof(struct nc_header) <= headroom)
450
451 goto encapsulate;
452
453 if ((sizeof (struct nc_header) + padlen) <
454 (headroom + tailroom)) {
455
456 skb->data = memmove(skb->head
457 + sizeof (struct nc_header),
458 skb->data, skb->len);
459 skb_set_tail_pointer(skb, len);
460 goto encapsulate;
461 }
462 }
463
464
465 skb2 = skb_copy_expand(skb,
466 sizeof (struct nc_header),
467 padlen,
468 flags);
469 dev_kfree_skb_any(skb);
470 if (!skb2)
471 return skb2;
472 skb = skb2;
473
474encapsulate:
475
476 header = (struct nc_header *) skb_push(skb, sizeof *header);
477 header->hdr_len = cpu_to_le16(sizeof (*header));
478 header->packet_len = cpu_to_le16(len);
479 header->packet_id = cpu_to_le16((u16)dev->xid++);
480
481
482 if (!((skb->len + sizeof *trailer) & 0x01))
483 *skb_put(skb, 1) = PAD_BYTE;
484 trailer = (struct nc_trailer *) skb_put(skb, sizeof *trailer);
485 put_unaligned(header->packet_id, &trailer->packet_id);
486#if 0
487 netdev_dbg(dev->net, "frame >tx h %d p %d id %d\n",
488 header->hdr_len, header->packet_len,
489 header->packet_id);
490#endif
491 return skb;
492}
493
494static int net1080_bind(struct usbnet *dev, struct usb_interface *intf)
495{
496 unsigned extra = sizeof (struct nc_header)
497 + 1
498 + sizeof (struct nc_trailer);
499
500 dev->net->hard_header_len += extra;
501 dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu;
502 dev->hard_mtu = NC_MAX_PACKET;
503 return usbnet_get_endpoints (dev, intf);
504}
505
506static const struct driver_info net1080_info = {
507 .description = "NetChip TurboCONNECT",
508 .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_NC,
509 .bind = net1080_bind,
510 .reset = net1080_reset,
511 .check_connect = net1080_check_connect,
512 .rx_fixup = net1080_rx_fixup,
513 .tx_fixup = net1080_tx_fixup,
514};
515
516static const struct usb_device_id products [] = {
517{
518 USB_DEVICE(0x0525, 0x1080),
519 .driver_info = (unsigned long) &net1080_info,
520}, {
521 USB_DEVICE(0x06D0, 0x0622),
522 .driver_info = (unsigned long) &net1080_info,
523},
524 { },
525};
526MODULE_DEVICE_TABLE(usb, products);
527
528static struct usb_driver net1080_driver = {
529 .name = "net1080",
530 .id_table = products,
531 .probe = usbnet_probe,
532 .disconnect = usbnet_disconnect,
533 .suspend = usbnet_suspend,
534 .resume = usbnet_resume,
535 .disable_hub_initiated_lpm = 1,
536};
537
538module_usb_driver(net1080_driver);
539
540MODULE_AUTHOR("David Brownell");
541MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
542MODULE_LICENSE("GPL");
543