~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/usb/net1080.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * NetChip 1080 Driver (USB Host-to-Host Link)
  3  * Copyright (C) 2000 by David Brownell <dbrownell@users.sourceforge.net>
  4  */
  5 
  6 /*
  7  * This talks to the NetChip 1080, which can appear in "network cables"
  8  * and other designs.  This driver interoperates with the Win32 network
  9  * drivers from NetChip, using the NetChip reference design.
 10  *
 11  * The IP-over-USB protocol here may be of interest.  Embedded devices
 12  * could implement it at the cost of two bulk endpoints, and whatever
 13  * other system resources the desired IP-based applications need.
 14  * Some Linux palmtops could support that today.  (Devices that don't
 15  * support the TTL-driven data mangling of the net1080 chip won't need
 16  * the header/trailer support though.)
 17  * 
 18  * STATUS:
 19  *
 20  * 13-sept-2000         experimental, new
 21  *
 22  * This doesn't yet do any network hotplugging, and there's no matching
 23  * ifup policy script ... it should arrange bridging with "brctl", and
 24  * should handle static and dynamic ("pump") setups.
 25  *
 26  * RX/TX queue sizes currently fixed at one due to URB unlink problems.
 27  *
 28  * 10-oct-2000
 29  * usb_device_id table created. 
 30  *
 31  * 28-oct-2000
 32  * misc fixes; mostly, discard more TTL-mangled rx packets.
 33  *
 34  * 01-nov-2000
 35  * usb_device_id table support added by Adam J. Richter <adam@yggdrasil.com>.
 36  * 
 37  *-------------------------------------------------------------------------*/
 38 
 39 #include <linux/config.h>
 40 #include <linux/module.h>
 41 #include <linux/kmod.h>
 42 #include <linux/sched.h>
 43 #include <linux/init.h>
 44 #include <linux/netdevice.h>
 45 #include <linux/etherdevice.h>
 46 #include <linux/random.h>
 47 #include <asm/unaligned.h>
 48 
 49 #define DEBUG                           // error path messages
 50 // #define VERBOSE                      // more; success messages
 51 #define USE_TTL                         // timeout our reads
 52 
 53 #if !defined (DEBUG) && defined (CONFIG_USB_DEBUG)
 54 #   define DEBUG
 55 #endif
 56 #include <linux/usb.h>
 57 
 58 
 59 static const struct usb_device_id       products [] = {
 60         // reference design
 61         { USB_DEVICE(0x1080, 0x525), 
 62           driver_info: (unsigned long) "NetChip TurboCONNECT" },
 63         // Belkin, ...
 64         { },            // END
 65 };
 66 
 67 MODULE_DEVICE_TABLE (usb, products);
 68 
 69 static u8       node_id [ETH_ALEN];
 70 
 71 
 72 /*-------------------------------------------------------------------------
 73  *
 74  * NetChip protocol:  ethernet framing, and only use bulk endpoints (01/81;
 75  * not mailboxes 02/82 or status interrupt 83).  Expects Ethernet bridging.
 76  * Odd USB length == always short read.
 77  *      - nc_header
 78  *      - payload, in Ethernet framing (14 byte header etc)
 79  *      - (optional padding byte, if needed so length is odd)
 80  *      - nc_trailer
 81  */
 82 
 83 struct nc_header {
 84         u16     hdr_len;                // sizeof nc_header (LE, all)
 85         u16     packet_len;             // packet size
 86         u16     packet_id;              // detects dropped packets
 87 #define NC_MIN_HEADER   6
 88 
 89         // all else is optional, and must start with:
 90         // u16  vendorId;               // from usb-if
 91         // u16  productId;
 92 };
 93 
 94 #define NC_PAD_BYTE     ((unsigned char)0xAC)
 95 
 96 struct nc_trailer {
 97         u16     packet_id;
 98 };
 99 
100 // packetsize == f(mtu setting), with upper limit
101 #define NC_MAX_PACKET(mtu) (sizeof (struct nc_header) \
102                                 + (mtu) \
103                                 + 1 \
104                                 + sizeof (struct nc_trailer))
105 #define MAX_PACKET      8191
106 
107 // zero means no timeout; else, how long a 64 byte bulk
108 // read may be queued before HW flushes it.
109 #define NC_READ_TTL     ((u8)255)       // ms
110 
111 
112 /*-------------------------------------------------------------------------*/
113 
114 // list of all devices we manage
115 static DECLARE_MUTEX (net1080_mutex);
116 static LIST_HEAD (net1080_list);
117 
118 
119 // Nineteen USB 1.1 max size bulk transactions per frame, max.
120 #if 0
121 #define RX_QLEN         4
122 #define TX_QLEN         4
123 
124 #else
125 // unlink_urbs() has probs on OHCI without test8-pre patches.
126 #define RX_QLEN         1
127 #define TX_QLEN         1
128 #endif
129 
130 enum skb_state {
131         illegal = 0,
132         tx_start, tx_done,
133         rx_start, rx_done, rx_cleanup
134 };
135 
136 struct skb_data {       // skb->cb is one of these
137         struct urb              *urb;
138         struct net1080          *dev;
139         enum skb_state          state;
140         size_t                  length;
141 };
142 
143 
144 struct net1080 {
145         // housekeeping
146         struct usb_device       *udev;
147         const struct usb_device_id      *prod_info;
148         struct semaphore        mutex;
149         struct list_head        dev_list;
150         wait_queue_head_t       *wait;
151 
152         // protocol/interface state
153         struct net_device       net;
154         struct net_device_stats stats;
155         u16                     packet_id;
156 
157         // various kinds of pending driver work
158         struct sk_buff_head     rxq;
159         struct sk_buff_head     txq;
160         struct sk_buff_head     done;
161         struct tasklet_struct   bh;
162 };
163 
164 #define mutex_lock(x)   down(x)
165 #define mutex_unlock(x) up(x)
166 
167 static void defer_bh (struct net1080 *dev, struct sk_buff *skb)
168 {
169         unsigned long   flags;
170 
171         skb_unlink (skb);
172         spin_lock_irqsave (&dev->done.lock, flags);
173         __skb_queue_tail (&dev->done, skb);
174         if (dev->done.qlen == 1)
175                 tasklet_schedule (&dev->bh);
176         spin_unlock_irqrestore (&dev->done.lock, flags);
177 }
178 
179 /*-------------------------------------------------------------------------
180  *
181  * We ignore most registers and EEPROM contents.
182  */
183 
184 #define REG_USBCTL      ((u8)0x04)
185 #define REG_TTL         ((u8)0x10)
186 #define REG_STATUS      ((u8)0x11)
187 
188 /*
189  * Vendor specific requests to read/write data
190  */
191 
192 #define REQUEST_REGISTER        ((u8)0x10)
193 #define REQUEST_EEPROM          ((u8)0x11)
194 
195 #define CONTROL_TIMEOUT         (500)                   /* msec */
196 
197 static int
198 vendor_read (struct net1080 *dev, u8 req, u8 regnum, u16 *retval_ptr)
199 {
200         int status = usb_control_msg (dev->udev,
201                 usb_rcvctrlpipe (dev->udev, 0),
202                 req,
203                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
204                 0, regnum,
205                 retval_ptr, sizeof *retval_ptr,
206                 CONTROL_TIMEOUT);
207         if (status > 0)
208                 status = 0;
209         if (!status)
210                 le16_to_cpus (retval_ptr);
211         return status;
212 }
213 
214 static inline int
215 register_read (struct net1080 *dev, u8 regnum, u16 *retval_ptr)
216 {
217         return vendor_read (dev, REQUEST_REGISTER, regnum, retval_ptr);
218 }
219 
220 // without retval, this can become fully async (usable in_interrupt)
221 static void
222 vendor_write (struct net1080 *dev, u8 req, u8 regnum, u16 value)
223 {
224         usb_control_msg (dev->udev,
225                 usb_sndctrlpipe (dev->udev, 0),
226                 req,
227                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
228                 value, regnum,
229                 0, 0,                   // data is in setup packet
230                 CONTROL_TIMEOUT);
231 }
232 
233 static inline void
234 register_write (struct net1080 *dev, u8 regnum, u16 value)
235 {
236         vendor_write (dev, REQUEST_REGISTER, regnum, value);
237 }
238 
239 
240 #if 0
241 static void dump_registers (struct net1080 *dev)
242 {
243         u8      reg;
244         u16     value;
245 
246         dbg ("%s registers:", dev->net.name);
247         for (reg = 0; reg < 0x20; reg++) {
248                 int retval;
249 
250                 // reading some registers is trouble
251                 if (reg >= 0x08 && reg <= 0xf)
252                         continue;
253                 if (reg >= 0x12 && reg <= 0x1e)
254                         continue;
255 
256                 retval = register_read (dev, reg, &value);
257                 if (retval < 0)
258                         dbg ("%s reg [0x%x] ==> error %d",
259                                 dev->net.name, reg, retval);
260                 else
261                         dbg ("%s reg [0x%x] = 0x%x",
262                                 dev->net.name, reg, value);
263         }
264 }
265 #endif
266 
267 
268 /*-------------------------------------------------------------------------
269  *
270  * Control register
271  */
272 
273 #define USBCTL_WRITABLE_MASK    0x1f0f
274 // bits 15-13 reserved, r/o
275 #define USBCTL_ENABLE_LANG      (1 << 12)
276 #define USBCTL_ENABLE_MFGR      (1 << 11)
277 #define USBCTL_ENABLE_PROD      (1 << 10)
278 #define USBCTL_ENABLE_SERIAL    (1 << 9)
279 #define USBCTL_ENABLE_DEFAULTS  (1 << 8)
280 // bits 7-4 reserved, r/o
281 #define USBCTL_FLUSH_OTHER      (1 << 3)
282 #define USBCTL_FLUSH_THIS       (1 << 2)
283 #define USBCTL_DISCONN_OTHER    (1 << 1)
284 #define USBCTL_DISCONN_THIS     (1 << 0)
285 
286 #ifdef DEBUG
287 static void dump_usbctl (struct net1080 *dev, u16 usbctl)
288 {
289         dbg ("%s: USB %d dev %d usbctl 0x%x:%s%s%s%s%s;"
290                         " this%s%s;"
291                         " other%s%s; r/o 0x%x",
292                 dev->net.name,
293                 dev->udev->bus->busnum, dev->udev->devnum,
294                 usbctl,
295                 (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
296                 (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
297                 (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
298                 (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
299                 (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
300 
301                 (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
302                 (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
303                 (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
304                 (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
305                 usbctl & ~USBCTL_WRITABLE_MASK
306                 );
307 }
308 #else
309 static inline void dump_usbctl (struct net1080 *dev, u16 usbctl) {}
310 #endif
311 
312 /*-------------------------------------------------------------------------
313  *
314  * Status register
315  */
316 
317 #define STATUS_PORT_A           (1 << 15)
318 
319 #define STATUS_CONN_OTHER       (1 << 14)
320 #define STATUS_SUSPEND_OTHER    (1 << 13)
321 #define STATUS_MAILBOX_OTHER    (1 << 12)
322 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03)
323 
324 #define STATUS_CONN_THIS        (1 << 6)
325 #define STATUS_SUSPEND_THIS     (1 << 5)
326 #define STATUS_MAILBOX_THIS     (1 << 4)
327 #define STATUS_PACKETS_THIS(n)  (((n) >> 0) && 0x03)
328 
329 #define STATUS_UNSPEC_MASK      0x0c8c
330 #define STATUS_NOISE_MASK       ((u16)~(0x0303|STATUS_UNSPEC_MASK))
331 
332 
333 #ifdef DEBUG
334 static void dump_status (struct net1080 *dev, u16 status)
335 {
336         dbg ("%s: USB %d dev %d status 0x%x:"
337                         " this (%c) PKT=%d%s%s%s;"
338                         " other PKT=%d%s%s%s; unspec 0x%x",
339                 dev->net.name,
340                 dev->udev->bus->busnum, dev->udev->devnum,
341                 status,
342 
343                 // XXX the packet counts don't seem right
344                 // (1 at reset, not 0); maybe UNSPEC too
345 
346                 (status & STATUS_PORT_A) ? 'A' : 'B',
347                 STATUS_PACKETS_THIS (status),
348                 (status & STATUS_CONN_THIS) ? " CON" : "",
349                 (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
350                 (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
351 
352                 STATUS_PACKETS_OTHER (status),
353                 (status & STATUS_CONN_OTHER) ? " CON" : "",
354                 (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
355                 (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
356 
357                 status & STATUS_UNSPEC_MASK
358                 );
359 }
360 #else
361 static inline void dump_status (struct net1080 *dev, u16 status) {}
362 #endif
363 
364 /*-------------------------------------------------------------------------
365  *
366  * TTL register
367  */
368 
369 #define TTL_THIS(ttl)   (0x00ff & ttl)
370 #define TTL_OTHER(ttl)  (0x00ff & (ttl >> 8))
371 #define MK_TTL(this,other)      ((u16)(((other)<<8)|(0x00ff&(this))))
372 
373 #ifdef DEBUG
374 static void dump_ttl (struct net1080 *dev, u16 ttl)
375 {
376         dbg ("%s: USB %d dev %d ttl 0x%x this = %d, other = %d",
377                 dev->net.name,
378                 dev->udev->bus->busnum, dev->udev->devnum,
379                 ttl,
380 
381                 TTL_THIS (ttl),
382                 TTL_OTHER (ttl)
383                 );
384 }
385 #else
386 static inline void dump_ttl (struct net1080 *dev, u16 ttl) {}
387 #endif
388 
389 #define RUN_CONTEXT (in_irq () ? "in_irq" \
390                         : (in_interrupt () ? "in_interrupt" : "can sleep"))
391 
392 /*-------------------------------------------------------------------------*/
393 
394 // ensure that the device is in a known state before using it.
395 
396 // preconditions:
397 //      caller owns the device mutex
398 //      caller has a process context
399 
400 static int net1080_reset (struct net1080 *dev)
401 {
402         u16             usbctl, status, ttl;
403         int             retval;
404 
405         if ((retval = register_read (dev, REG_STATUS, &status)) < 0) {
406                 dbg ("can't read dev %d status: %d", dev->udev->devnum, retval);
407                 goto done;
408         }
409         dump_status (dev, status);
410 
411         if ((retval = register_read (dev, REG_USBCTL, &usbctl)) < 0) {
412                 dbg ("can't read USBCTL, %d", retval);
413                 goto done;
414         }
415         dump_usbctl (dev, usbctl);
416 
417         register_write (dev, REG_USBCTL,
418                         USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
419 
420         if ((retval = register_read (dev, REG_TTL, &ttl)) < 0) {
421                 dbg ("can't read TTL, %d", retval);
422                 goto done;
423         }
424         dump_ttl (dev, ttl);
425 
426 #ifdef  USE_TTL
427         // Have the chip flush reads that seem to be starving for read
428         // bandwidth ... or we're otherwise reading.  Note, Win32 drivers
429         // may change our read TTL for us.
430 
431         register_write (dev, REG_TTL,
432                         MK_TTL (NC_READ_TTL, TTL_OTHER (ttl)) );
433         dbg ("%s: assigned TTL, %d ms", dev->net.name, NC_READ_TTL);
434 #endif
435 
436         info ("%s: %s, port %c on USB %d dev %d, peer %sconnected",
437                 dev->net.name, (char *) dev->prod_info->driver_info,
438                 (status & STATUS_PORT_A) ? 'A' : 'B',
439                 dev->udev->bus->busnum,
440                 dev->udev->devnum,
441                 (status & STATUS_CONN_OTHER) ? "" : "dis"
442                 );
443         retval = 0;
444 
445 done:
446         return retval;
447 }
448 
449 
450 /*-------------------------------------------------------------------------
451  *
452  * Network Device Driver support (peer link to USB Host)
453  *
454  --------------------------------------------------------------------------*/
455 
456 static int net1080_change_mtu (struct net_device *net, int new_mtu)
457 {
458         if ((new_mtu < 0) || NC_MAX_PACKET (new_mtu) > MAX_PACKET)
459                 return -EINVAL;
460         net->mtu = new_mtu;
461         return 0;
462 }
463 
464 /*-------------------------------------------------------------------------*/
465 
466 static struct net_device_stats *net1080_get_stats (struct net_device *net)
467 {
468         return &((struct net1080 *) net->priv)->stats;
469 }
470 
471 /*-------------------------------------------------------------------------*/
472 
473 static void rx_complete (struct urb *urb);
474 
475 static void rx_submit (struct net1080 *dev, struct urb *urb, int flags)
476 {
477         struct sk_buff          *skb;
478         struct skb_data         *entry;
479         int                     retval = 0;
480         unsigned long           lockflags;
481 
482         if ((skb = alloc_skb (NC_MAX_PACKET (dev->net.mtu), flags)) == 0) {
483                 err ("no rx skb");
484                 tasklet_schedule (&dev->bh);
485                 usb_free_urb (urb);
486                 return;
487         }
488 
489         entry = (struct skb_data *) skb->cb;
490         entry->urb = urb;
491         entry->dev = dev;
492         entry->state = rx_start;
493         entry->length = 0;
494 
495         FILL_BULK_URB (urb, dev->udev, usb_rcvbulkpipe (dev->udev, 1),
496                         skb->data, skb->truesize, rx_complete, skb);
497         urb->transfer_flags |= USB_QUEUE_BULK;
498 
499         spin_lock_irqsave (&dev->rxq.lock, lockflags);
500         if (!netif_queue_stopped (&dev->net)) {
501                 if ((retval = usb_submit_urb (urb)) != 0) {
502                         err ("%s rx submit, %d", dev->net.name, retval);
503                         tasklet_schedule (&dev->bh);
504                 } else {
505                         __skb_queue_tail (&dev->rxq, skb);
506                 }
507         } else {
508                 dbg ("rx: stopped");
509                 retval = -ENOLINK;
510         }
511         spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
512         if (retval) {
513                 dev_kfree_skb_any (skb);
514                 usb_free_urb (urb);
515         }
516 }
517 
518 
519 /*-------------------------------------------------------------------------*/
520 
521 static void rx_complete (struct urb *urb)
522 {
523         struct sk_buff          *skb = (struct sk_buff *) urb->context;
524         struct skb_data         *entry = (struct skb_data *) skb->cb;
525         struct net1080          *dev = entry->dev;
526         int                     urb_status = urb->status;
527 
528         urb->dev = 0;
529         skb->len = urb->actual_length;
530         entry->state = rx_done;
531         entry->urb = 0;
532 
533         if ((urb->transfer_flags & USB_ASYNC_UNLINK) != 0) {
534                 dbg ("rx ... shutting down");
535                 usb_free_urb (urb);
536                 urb = 0;
537         }
538 
539         switch (urb_status) {
540             // success
541             case 0:
542                 if (!(skb->len & 0x01)) {
543                         entry->state = rx_cleanup;
544                         dev->stats.rx_errors++;
545                         dev->stats.rx_length_errors++;
546                         dbg ("even rx len %d", skb->len);
547                 } else if (skb->len > MAX_PACKET) {
548                         entry->state = rx_cleanup;
549                         dev->stats.rx_errors++;
550                         dev->stats.rx_frame_errors++;
551                         dbg ("rx too big, %d", skb->len);
552                 }
553                 break;
554 
555             // hardware-reported interface shutdown ... which we
556             // typically see before khubd calls disconnect()
557             case -ETIMEDOUT:            // usb-ohci
558             case -EILSEQ:               // *uhci ... "crc"/timeout error
559                 // netif_device_detach (&dev->net);
560                 // FALLTHROUGH
561                         
562             // software-driven interface shutdown
563             case -ECONNRESET:
564                 entry->state = rx_cleanup;
565                 usb_free_urb (urb);
566                 urb = 0;
567                 dbg ("%s ... shutdown rx (%d)", dev->net.name, urb_status);
568                 break;
569 
570             // data overrun ... flush fifo?
571             case -EOVERFLOW:
572                 dev->stats.rx_over_errors++;
573                 // FALLTHROUGH
574             
575             default:
576                 entry->state = rx_cleanup;
577                 dev->stats.rx_errors++;
578                 err ("%s rx: status %d", dev->net.name, urb_status);
579                 break;
580         }
581         defer_bh (dev, skb);
582 
583         if (urb) {
584                 if (!netif_queue_stopped (&dev->net)) {
585                         rx_submit (dev, urb, GFP_ATOMIC);
586                         return;
587                 } else
588                         usb_free_urb (urb);
589         }
590 #ifdef  VERBOSE
591         dbg ("no read resubmitted");
592 #endif  VERBOSE
593 }
594 
595 /*-------------------------------------------------------------------------*/
596 
597 // unlink pending rx/tx; completion handlers do all other cleanup
598 
599 static int unlink_urbs (struct sk_buff_head *q)
600 {
601         unsigned long           flags;
602         struct sk_buff          *skb;
603         struct skb_data         *entry;
604         int                     retval;
605         int                     count = 0;
606 
607         spin_lock_irqsave (&q->lock, flags);
608         for (skb = q->next; skb != (struct sk_buff *) q; skb = skb->next) {
609                 entry = (struct skb_data *) skb->cb;
610                 entry->urb->transfer_flags |= USB_ASYNC_UNLINK;
611                 retval = usb_unlink_urb (entry->urb);
612                 if (retval < 0)
613                         dbg ("unlink urb err, %d", retval);
614                 else
615                         count++;
616         }
617         spin_unlock_irqrestore (&q->lock, flags);
618         return count;
619 }
620 
621 
622 /*-------------------------------------------------------------------------*/
623 
624 // precondition: never called in_interrupt
625 
626 static int net1080_stop (struct net_device *net)
627 {
628         struct net1080          *dev = (struct net1080 *) net->priv;
629         int                     temp;
630         DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 
631         DECLARE_WAITQUEUE (wait, current);
632 
633         mutex_lock (&dev->mutex);
634         
635         dbg ("%s stop stats: rx/tx %ld/%ld, errs %ld/%ld", net->name,
636                 dev->stats.rx_packets, dev->stats.tx_packets, 
637                 dev->stats.rx_errors, dev->stats.tx_errors
638                 );
639 
640         netif_stop_queue(net);
641 
642         // ensure there are no more active urbs
643         add_wait_queue (&unlink_wakeup, &wait);
644         dev->wait = &unlink_wakeup;
645         temp = unlink_urbs (&dev->txq) + unlink_urbs (&dev->rxq);
646 
647         // maybe wait for deletions to finish.
648         if (temp) {
649                 current->state = TASK_UNINTERRUPTIBLE;
650                 schedule ();
651                 dbg ("waited for %d urb completions", temp);
652         }
653         dev->wait = 0;
654         current->state = TASK_RUNNING;
655         remove_wait_queue (&unlink_wakeup, &wait); 
656 
657         mutex_unlock (&dev->mutex);
658         MOD_DEC_USE_COUNT;
659         return 0;
660 }
661 
662 /*-------------------------------------------------------------------------*/
663 
664 // posts a read, and enables write queing
665 
666 // precondition: never called in_interrupt
667 
668 static int net1080_open (struct net_device *net)
669 {
670         struct net1080          *dev = (struct net1080 *) net->priv;
671         int                     retval;
672         u16                     status;
673         int                     i;
674 
675         MOD_INC_USE_COUNT;
676         mutex_lock (&dev->mutex);
677 
678         // insist peer be connected -- is this the best place?
679         if ((retval = register_read (dev, REG_STATUS, &status)) != 0) {
680                 dbg ("%s open: status read failed - %d", net->name, retval);
681                 goto done;
682         }
683         if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)  {
684                 retval = -ENOLINK;
685                 dbg ("%s open: peer not connected", net->name);
686                 goto done;
687         }
688 
689         MOD_INC_USE_COUNT;
690         netif_start_queue (net);
691         for (i = 0; i < RX_QLEN; i++)
692                 rx_submit (dev, usb_alloc_urb (0), GFP_KERNEL);
693 
694         dbg ("%s open: started queueing (rx %d, tx %d)",
695                 net->name, RX_QLEN, TX_QLEN);
696 done:
697         mutex_unlock (&dev->mutex);
698         MOD_DEC_USE_COUNT;
699         return retval;
700 }
701 
702 /*-------------------------------------------------------------------------*/
703 
704 static void tx_complete (struct urb *urb)
705 {
706         struct sk_buff          *skb = (struct sk_buff *) urb->context;
707         struct skb_data         *entry = (struct skb_data *) skb->cb;
708         struct net1080          *dev = entry->dev;
709 
710         urb->dev = 0;
711         entry->state = tx_done;
712         defer_bh (dev, skb);
713         netif_wake_queue (&dev->net);
714 }
715 
716 /*-------------------------------------------------------------------------*/
717 
718 static struct sk_buff *fixup_skb (struct sk_buff *skb)
719 {
720         int                     padlen;
721         struct sk_buff          *skb2;
722 
723         padlen = ((skb->len + sizeof (struct nc_header)
724                         + sizeof (struct nc_trailer)) & 0x01) ? 0 : 1;
725         if (!skb_cloned (skb)) {
726                 int     headroom = skb_headroom (skb);
727                 int     tailroom = skb_tailroom (skb);
728 
729                 if ((padlen + sizeof (struct nc_trailer)) <= tailroom
730                             && sizeof (struct nc_header) <= headroom)
731                         return skb;
732 
733                 if ((sizeof (struct nc_header) + padlen
734                                         + sizeof (struct nc_trailer)) <
735                                 (headroom + tailroom)) {
736                         skb->data = memmove (skb->head
737                                                 + sizeof (struct nc_header),
738                                             skb->data, skb->len);
739                         skb->tail = skb->data + skb->len;
740                         return skb;
741                 }
742         }
743         skb2 = skb_copy_expand (skb,
744                                 sizeof (struct nc_header),
745                                 sizeof (struct nc_trailer) + padlen,
746                                 in_interrupt () ? GFP_ATOMIC : GFP_KERNEL);
747         dev_kfree_skb_any (skb);
748         return skb2;
749 }
750 
751 /*-------------------------------------------------------------------------*/
752 
753 static int net1080_start_xmit (struct sk_buff *skb, struct net_device *net)
754 {
755         struct net1080          *dev = (struct net1080 *) net->priv;
756         int                     length = skb->len;
757         int                     retval = 0;
758         struct urb              *urb = 0;
759         struct skb_data         *entry;
760         struct nc_header        *header;
761         struct nc_trailer       *trailer;
762         unsigned long           flags;
763 
764         if ((skb = fixup_skb (skb)) == 0) {
765                 dbg ("can't fixup skb");
766                 goto drop;
767         }
768         if ((urb = usb_alloc_urb (0)) == 0) {
769                 dbg ("no urb");
770                 goto drop;
771         }
772 
773         entry = (struct skb_data *) skb->cb;
774         entry->urb = urb;
775         entry->dev = dev;
776         entry->state = tx_start;
777         entry->length = length;
778 
779         header = (struct nc_header *) skb_push (skb, sizeof *header);
780         header->hdr_len = cpu_to_le16 (sizeof (*header));
781         header->packet_len = cpu_to_le16 (length);
782         if (!((skb->len + sizeof *trailer) & 0x01))
783                 *skb_put (skb, 1) = NC_PAD_BYTE;
784         trailer = (struct nc_trailer *) skb_put (skb, sizeof *trailer);
785 
786         FILL_BULK_URB (urb, dev->udev,
787                         usb_sndbulkpipe (dev->udev, 1),
788                         skb->data, skb->len, tx_complete, skb);
789         urb->transfer_flags |= USB_QUEUE_BULK;
790         // FIXME urb->timeout = ...;
791 
792         spin_lock_irqsave (&dev->txq.lock, flags);
793         if (!netif_queue_stopped (&dev->net)) {
794                 header->packet_id = cpu_to_le16 (dev->packet_id++);
795                 put_unaligned (header->packet_id, &trailer->packet_id);
796 
797                 netif_stop_queue (net);
798                 if ((retval = usb_submit_urb (urb)) != 0) {
799                         netif_start_queue (net);
800                         dbg ("%s tx: submit urb err %d", net->name, retval);
801                 } else {
802                         net->trans_start = jiffies;
803                         __skb_queue_tail (&dev->txq, skb);
804                         if (dev->txq.qlen < TX_QLEN)
805                                 netif_start_queue (net);
806                 }
807         } else
808                 retval = -ENOLINK;
809         spin_unlock_irqrestore (&dev->txq.lock, flags);
810 
811         if (retval) {
812                 dbg ("drop");
813 drop:
814                 dev->stats.tx_dropped++;
815                 dev_kfree_skb_any (skb);
816                 usb_free_urb (urb);
817 #ifdef  VERBOSE
818         } else {
819                 dbg ("%s: tx %p len %d", net->name, skb, length);
820 #endif
821         }
822         return retval;
823 }
824 
825 
826 /*-------------------------------------------------------------------------*/
827 
828 static void rx_process (struct net1080 *dev, struct sk_buff *skb)
829 {
830         struct nc_header        *header;
831         struct nc_trailer       *trailer;
832 
833         header = (struct nc_header *) skb->data;
834         le16_to_cpus (&header->hdr_len);
835         le16_to_cpus (&header->packet_len);
836         if (header->packet_len > MAX_PACKET) {
837                 dev->stats.rx_frame_errors++;
838                 dbg ("packet too big, %d", header->packet_len);
839                 goto error;
840         } else if (header->hdr_len < NC_MIN_HEADER) {
841                 dev->stats.rx_frame_errors++;
842                 dbg ("header too short, %d", header->hdr_len);
843                 goto error;
844         } else if (header->hdr_len > header->packet_len) {
845                 dev->stats.rx_frame_errors++;
846                 dbg ("header too big, %d packet %d", header->hdr_len, header->packet_len);
847                 goto error;
848         } else if (header->hdr_len != sizeof *header) {
849                 // out of band data for us?
850                 dbg ("header OOB, %d bytes", header->hdr_len - NC_MIN_HEADER);
851                 // switch (vendor/product ids) { ... }
852         }
853         skb_pull (skb, header->hdr_len);
854 
855         trailer = (struct nc_trailer *)
856                 (skb->data + skb->len - sizeof *trailer);
857         skb_trim (skb, skb->len - sizeof *trailer);
858 
859         if ((header->packet_len & 0x01) == 0) {
860                 if (skb->data [header->packet_len] != NC_PAD_BYTE) {
861                         dev->stats.rx_frame_errors++;
862                         dbg ("bad pad");
863                         goto error;
864                 }
865                 skb_trim (skb, skb->len - 1);
866         }
867         if (skb->len != header->packet_len) {
868                 dev->stats.rx_length_errors++;
869                 dbg ("bad packet len %d (expected %d)",
870                         skb->len, header->packet_len);
871                 goto error;
872         }
873         if (header->packet_id != get_unaligned (&trailer->packet_id)) {
874                 dev->stats.rx_fifo_errors++;
875                 dbg ("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
876                         header->packet_id, trailer->packet_id);
877                 goto error;
878         }
879 
880         if (skb->len) {
881                 skb->dev = &dev->net;
882                 skb->protocol = eth_type_trans (skb, &dev->net);
883                 dev->stats.rx_packets++;
884                 dev->stats.rx_bytes += skb->len;
885 
886 #ifdef  VERBOSE
887                 dbg ("%s: rx %p len %d, type 0x%x, id 0x%x",
888                         dev->net.name, skb, skb->len, skb->protocol,
889                         le16_to_cpu (header->packet_id));
890 #endif
891                 netif_rx (skb);
892         } else {
893                 dbg ("drop");
894 error:
895                 dev->stats.rx_errors++;
896                 dev_kfree_skb (skb);
897         }
898 }
899 
900 /*-------------------------------------------------------------------------*/
901 
902 // tasklet
903 
904 // We can have a state machine in this tasklet monitor the link state,
905 // using async control messaging and calling attach/detach routines.
906 
907 // But then some listener ought to respond to the changes; do those
908 // network attach/detach notifications get to userland somehow, such
909 // as by calling "ifup usb0" and "ifdown usb0"?
910 
911 static void net1080_bh (unsigned long param)
912 {
913         struct net1080          *dev = (struct net1080 *) param;
914         struct sk_buff          *skb;
915         struct skb_data         *entry;
916 
917         while ((skb = skb_dequeue (&dev->done))) {
918                 entry = (struct skb_data *) skb->cb;
919                 switch (entry->state) {
920                     case rx_done:
921                         rx_process (dev, skb);
922                         continue;
923                     case tx_done:
924                         if (entry->urb->status) {
925                                 // can this statistic become more specific?
926                                 dev->stats.tx_errors++;
927                                 dbg ("%s tx: err %d", dev->net.name,
928                                         entry->urb->status);
929                         } else {
930                                 dev->stats.tx_packets++;
931                                 dev->stats.tx_bytes += entry->length;
932                         }
933                         // FALLTHROUGH:
934                     case rx_cleanup:
935                         usb_free_urb (entry->urb);
936                         dev_kfree_skb (skb);
937                         continue;
938                     default:
939                         dbg ("%s: bogus skb state %d",
940                                 dev->net.name, entry->state);
941                 }
942         }
943 
944         // waiting for all pending urbs to complete?
945         if (dev->wait) {
946                 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
947                         wake_up (dev->wait);
948                 }
949 
950         // or are we maybe short a few urbs?
951         } else if (!netif_queue_stopped (&dev->net)) {
952                 if (dev->rxq.qlen < TX_QLEN) {
953                         struct urb      *urb;
954                         int             i;
955                         for (i = 0; i < 3 && dev->rxq.qlen < TX_QLEN; i++) {
956                                 if ((urb = usb_alloc_urb (0)) != 0)
957                                         rx_submit (dev, urb, GFP_ATOMIC);
958                         }
959                         dbg ("%s: rxqlen now %d",
960                                 dev->net.name, dev->rxq.qlen);
961                 }
962         }
963 }
964 
965 /*-------------------------------------------------------------------------
966  *
967  * USB Device Driver support
968  *
969  --------------------------------------------------------------------------*/
970  
971 // precondition: never called in_interrupt
972 
973 static void net1080_disconnect (struct usb_device *udev, void *ptr)
974 {
975         struct net1080  *dev = (struct net1080 *) ptr;
976 
977         info ("%s: USB %d dev %d, %s, disconnected",
978                 dev->net.name,
979                 udev->bus->busnum, udev->devnum,
980                 (char *) dev->prod_info->driver_info);
981         
982         unregister_netdev (&dev->net);
983 
984         mutex_lock (&net1080_mutex);
985         mutex_lock (&dev->mutex);
986         list_del (&dev->dev_list);
987         mutex_unlock (&net1080_mutex);
988 
989 #ifdef DEBUG
990         memset (dev, 0x55, sizeof *dev);
991 #endif
992         kfree (dev);
993         usb_dec_dev_use (udev);
994 }
995 
996 
997 /*-------------------------------------------------------------------------*/
998 
999 // precondition: never called in_interrupt
1000 
1001 static void *
1002 net1080_probe (struct usb_device *udev, unsigned ifnum, const struct usb_device_id *prod)
1003 {
1004         struct net1080          *dev;
1005         struct net_device       *net;
1006         struct usb_interface_descriptor *interface;
1007         int                     retval;
1008 
1009         // sanity check; expect dedicated interface/devices for now.
1010         interface = &udev->actconfig->interface [ifnum].altsetting[0];
1011         if (udev->descriptor.bNumConfigurations != 1
1012                         || udev->config[0].bNumInterfaces != 1
1013                         || udev->config[0].bNumInterfaces != 1
1014                         || interface->bInterfaceClass != USB_CLASS_VENDOR_SPEC
1015                         || interface->bNumEndpoints != 5
1016                         ) {
1017                 dbg ("Bogus config info");
1018                 return 0;
1019         }
1020 
1021         // set up our own records
1022         if (!(dev = kmalloc (sizeof *dev, GFP_KERNEL))) {
1023                 dbg ("can't kmalloc dev");
1024                 return 0;
1025         }
1026         memset (dev, 0, sizeof *dev);
1027 
1028         init_MUTEX_LOCKED (&dev->mutex);
1029         usb_inc_dev_use (udev);
1030         dev->udev = udev;
1031         dev->prod_info = prod;
1032         INIT_LIST_HEAD (&dev->dev_list);
1033         skb_queue_head_init (&dev->rxq);
1034         skb_queue_head_init (&dev->txq);
1035         skb_queue_head_init (&dev->done);
1036         dev->bh.func = net1080_bh;
1037         dev->bh.data = (unsigned long) dev;
1038 
1039         // set up network interface records
1040         net = &dev->net;
1041         net->priv = dev;
1042         strcpy (net->name, "usb%d");
1043         memcpy (net->dev_addr, node_id, sizeof node_id);
1044 
1045         ether_setup (net);
1046         // net->flags |= IFF_POINTOPOINT;
1047 
1048         net->change_mtu = net1080_change_mtu;
1049         net->get_stats = net1080_get_stats;
1050         net->hard_start_xmit = net1080_start_xmit;
1051         net->open = net1080_open;
1052         net->stop = net1080_stop;
1053 
1054         register_netdev (&dev->net);
1055 
1056         // ... talk to the device
1057         // dump_registers (dev);
1058 
1059         if ((retval = net1080_reset (dev)) < 0) {
1060                 err ("%s: init reset fail on USB %d dev %d - %d",
1061                         dev->net.name, udev->bus->busnum, udev->devnum, retval);
1062                 mutex_unlock (&dev->mutex);
1063                 net1080_disconnect (udev, dev);
1064                 return 0;
1065         }
1066 
1067         // ok, it's ready to go.
1068         mutex_lock (&net1080_mutex);
1069         list_add (&dev->dev_list, &net1080_list);
1070         mutex_unlock (&dev->mutex);
1071 
1072         // start as if the link is up
1073         netif_device_attach (&dev->net);
1074 
1075         mutex_unlock (&net1080_mutex);
1076 
1077         return dev;
1078 }
1079 
1080 
1081 /*-------------------------------------------------------------------------*/
1082 
1083 static struct usb_driver net1080_driver = {
1084         name:           "net1080",
1085         id_table:       products,
1086         probe:          net1080_probe,
1087         disconnect:     net1080_disconnect,
1088 };
1089 
1090 /*-------------------------------------------------------------------------*/
1091 
1092 static int __init net1080_init (void)
1093 {
1094         // compiler should optimize this out
1095         if (sizeof (((struct sk_buff *)0)->cb) < sizeof (struct skb_data))
1096                 BUG ();
1097 
1098         if (usb_register (&net1080_driver) < 0)
1099                 return -1;
1100 
1101         get_random_bytes (node_id, sizeof node_id);
1102         node_id [0] &= 0x7f;
1103 
1104         return 0;
1105 }
1106 module_init (net1080_init);
1107 
1108 static void __exit net1080_exit (void)
1109 {
1110         usb_deregister (&net1080_driver);
1111 }
1112 module_exit (net1080_exit);
1113 
1114 MODULE_AUTHOR ("David Brownell <dbrownell@users.sourceforge.net>");
1115 MODULE_DESCRIPTION ("NetChip 1080 Driver (USB Host-to-Host Link)");
1116 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.