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