p54: fix EEPROM structure endianness
[linux-2.6] / drivers / net / usb / rndis_host.c
1 /*
2  * Host Side support for RNDIS Networking Links
3  * Copyright (C) 2005 by David Brownell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 // #define      DEBUG                   // error path messages, extra info
21 // #define      VERBOSE                 // more; success messages
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/cdc.h>
32 #include <linux/usb/usbnet.h>
33 #include <linux/usb/rndis_host.h>
34
35
36 /*
37  * RNDIS is NDIS remoted over USB.  It's a MSFT variant of CDC ACM ... of
38  * course ACM was intended for modems, not Ethernet links!  USB's standard
39  * for Ethernet links is "CDC Ethernet", which is significantly simpler.
40  *
41  * NOTE that Microsoft's "RNDIS 1.0" specification is incomplete.  Issues
42  * include:
43  *    - Power management in particular relies on information that's scattered
44  *      through other documentation, and which is incomplete or incorrect even
45  *      there.
46  *    - There are various undocumented protocol requirements, such as the
47  *      need to send unused garbage in control-OUT messages.
48  *    - In some cases, MS-Windows will emit undocumented requests; this
49  *      matters more to peripheral implementations than host ones.
50  *
51  * Moreover there's a no-open-specs variant of RNDIS called "ActiveSync".
52  *
53  * For these reasons and others, ** USE OF RNDIS IS STRONGLY DISCOURAGED ** in
54  * favor of such non-proprietary alternatives as CDC Ethernet or the newer (and
55  * currently rare) "Ethernet Emulation Model" (EEM).
56  */
57
58 /*
59  * RNDIS notifications from device: command completion; "reverse"
60  * keepalives; etc
61  */
62 void rndis_status(struct usbnet *dev, struct urb *urb)
63 {
64         devdbg(dev, "rndis status urb, len %d stat %d",
65                 urb->actual_length, urb->status);
66         // FIXME for keepalives, respond immediately (asynchronously)
67         // if not an RNDIS status, do like cdc_status(dev,urb) does
68 }
69 EXPORT_SYMBOL_GPL(rndis_status);
70
71 /*
72  * RPC done RNDIS-style.  Caller guarantees:
73  * - message is properly byteswapped
74  * - there's no other request pending
75  * - buf can hold up to 1KB response (required by RNDIS spec)
76  * On return, the first few entries are already byteswapped.
77  *
78  * Call context is likely probe(), before interface name is known,
79  * which is why we won't try to use it in the diagnostics.
80  */
81 int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf)
82 {
83         struct cdc_state        *info = (void *) &dev->data;
84         int                     master_ifnum;
85         int                     retval;
86         unsigned                count;
87         __le32                  rsp;
88         u32                     xid = 0, msg_len, request_id;
89
90         /* REVISIT when this gets called from contexts other than probe() or
91          * disconnect(): either serialize, or dispatch responses on xid
92          */
93
94         /* Issue the request; xid is unique, don't bother byteswapping it */
95         if (likely(buf->msg_type != RNDIS_MSG_HALT
96                         && buf->msg_type != RNDIS_MSG_RESET)) {
97                 xid = dev->xid++;
98                 if (!xid)
99                         xid = dev->xid++;
100                 buf->request_id = (__force __le32) xid;
101         }
102         master_ifnum = info->control->cur_altsetting->desc.bInterfaceNumber;
103         retval = usb_control_msg(dev->udev,
104                 usb_sndctrlpipe(dev->udev, 0),
105                 USB_CDC_SEND_ENCAPSULATED_COMMAND,
106                 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
107                 0, master_ifnum,
108                 buf, le32_to_cpu(buf->msg_len),
109                 RNDIS_CONTROL_TIMEOUT_MS);
110         if (unlikely(retval < 0 || xid == 0))
111                 return retval;
112
113         // FIXME Seems like some devices discard responses when
114         // we time out and cancel our "get response" requests...
115         // so, this is fragile.  Probably need to poll for status.
116
117         /* ignore status endpoint, just poll the control channel;
118          * the request probably completed immediately
119          */
120         rsp = buf->msg_type | RNDIS_MSG_COMPLETION;
121         for (count = 0; count < 10; count++) {
122                 memset(buf, 0, CONTROL_BUFFER_SIZE);
123                 retval = usb_control_msg(dev->udev,
124                         usb_rcvctrlpipe(dev->udev, 0),
125                         USB_CDC_GET_ENCAPSULATED_RESPONSE,
126                         USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
127                         0, master_ifnum,
128                         buf, CONTROL_BUFFER_SIZE,
129                         RNDIS_CONTROL_TIMEOUT_MS);
130                 if (likely(retval >= 8)) {
131                         msg_len = le32_to_cpu(buf->msg_len);
132                         request_id = (__force u32) buf->request_id;
133                         if (likely(buf->msg_type == rsp)) {
134                                 if (likely(request_id == xid)) {
135                                         if (unlikely(rsp == RNDIS_MSG_RESET_C))
136                                                 return 0;
137                                         if (likely(RNDIS_STATUS_SUCCESS
138                                                         == buf->status))
139                                                 return 0;
140                                         dev_dbg(&info->control->dev,
141                                                 "rndis reply status %08x\n",
142                                                 le32_to_cpu(buf->status));
143                                         return -EL3RST;
144                                 }
145                                 dev_dbg(&info->control->dev,
146                                         "rndis reply id %d expected %d\n",
147                                         request_id, xid);
148                                 /* then likely retry */
149                         } else switch (buf->msg_type) {
150                         case RNDIS_MSG_INDICATE: {      /* fault/event */
151                                 struct rndis_indicate *msg = (void *)buf;
152                                 int state = 0;
153
154                                 switch (msg->status) {
155                                 case RNDIS_STATUS_MEDIA_CONNECT:
156                                         state = 1;
157                                 case RNDIS_STATUS_MEDIA_DISCONNECT:
158                                         dev_info(&info->control->dev,
159                                                 "rndis media %sconnect\n",
160                                                 !state?"dis":"");
161                                         if (dev->driver_info->link_change)
162                                                 dev->driver_info->link_change(
163                                                         dev, state);
164                                         break;
165                                 default:
166                                         dev_info(&info->control->dev,
167                                                 "rndis indication: 0x%08x\n",
168                                                 le32_to_cpu(msg->status));
169                                 }
170                                 }
171                                 break;
172                         case RNDIS_MSG_KEEPALIVE: {     /* ping */
173                                 struct rndis_keepalive_c *msg = (void *)buf;
174
175                                 msg->msg_type = RNDIS_MSG_KEEPALIVE_C;
176                                 msg->msg_len = ccpu2(sizeof *msg);
177                                 msg->status = RNDIS_STATUS_SUCCESS;
178                                 retval = usb_control_msg(dev->udev,
179                                         usb_sndctrlpipe(dev->udev, 0),
180                                         USB_CDC_SEND_ENCAPSULATED_COMMAND,
181                                         USB_TYPE_CLASS | USB_RECIP_INTERFACE,
182                                         0, master_ifnum,
183                                         msg, sizeof *msg,
184                                         RNDIS_CONTROL_TIMEOUT_MS);
185                                 if (unlikely(retval < 0))
186                                         dev_dbg(&info->control->dev,
187                                                 "rndis keepalive err %d\n",
188                                                 retval);
189                                 }
190                                 break;
191                         default:
192                                 dev_dbg(&info->control->dev,
193                                         "unexpected rndis msg %08x len %d\n",
194                                         le32_to_cpu(buf->msg_type), msg_len);
195                         }
196                 } else {
197                         /* device probably issued a protocol stall; ignore */
198                         dev_dbg(&info->control->dev,
199                                 "rndis response error, code %d\n", retval);
200                 }
201                 msleep(2);
202         }
203         dev_dbg(&info->control->dev, "rndis response timeout\n");
204         return -ETIMEDOUT;
205 }
206 EXPORT_SYMBOL_GPL(rndis_command);
207
208 /*
209  * rndis_query:
210  *
211  * Performs a query for @oid along with 0 or more bytes of payload as
212  * specified by @in_len. If @reply_len is not set to -1 then the reply
213  * length is checked against this value, resulting in an error if it
214  * doesn't match.
215  *
216  * NOTE: Adding a payload exactly or greater than the size of the expected
217  * response payload is an evident requirement MSFT added for ActiveSync.
218  *
219  * The only exception is for OIDs that return a variably sized response,
220  * in which case no payload should be added.  This undocumented (and
221  * nonsensical!) issue was found by sniffing protocol requests from the
222  * ActiveSync 4.1 Windows driver.
223  */
224 static int rndis_query(struct usbnet *dev, struct usb_interface *intf,
225                 void *buf, u32 oid, u32 in_len,
226                 void **reply, int *reply_len)
227 {
228         int retval;
229         union {
230                 void                    *buf;
231                 struct rndis_msg_hdr    *header;
232                 struct rndis_query      *get;
233                 struct rndis_query_c    *get_c;
234         } u;
235         u32 off, len;
236
237         u.buf = buf;
238
239         memset(u.get, 0, sizeof *u.get + in_len);
240         u.get->msg_type = RNDIS_MSG_QUERY;
241         u.get->msg_len = cpu_to_le32(sizeof *u.get + in_len);
242         u.get->oid = oid;
243         u.get->len = cpu_to_le32(in_len);
244         u.get->offset = ccpu2(20);
245
246         retval = rndis_command(dev, u.header);
247         if (unlikely(retval < 0)) {
248                 dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) failed, %d\n",
249                                 oid, retval);
250                 return retval;
251         }
252
253         off = le32_to_cpu(u.get_c->offset);
254         len = le32_to_cpu(u.get_c->len);
255         if (unlikely((8 + off + len) > CONTROL_BUFFER_SIZE))
256                 goto response_error;
257
258         if (*reply_len != -1 && len != *reply_len)
259                 goto response_error;
260
261         *reply = (unsigned char *) &u.get_c->request_id + off;
262         *reply_len = len;
263
264         return retval;
265
266 response_error:
267         dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) "
268                         "invalid response - off %d len %d\n",
269                 oid, off, len);
270         return -EDOM;
271 }
272
273 int
274 generic_rndis_bind(struct usbnet *dev, struct usb_interface *intf, int flags)
275 {
276         int                     retval;
277         struct net_device       *net = dev->net;
278         struct cdc_state        *info = (void *) &dev->data;
279         union {
280                 void                    *buf;
281                 struct rndis_msg_hdr    *header;
282                 struct rndis_init       *init;
283                 struct rndis_init_c     *init_c;
284                 struct rndis_query      *get;
285                 struct rndis_query_c    *get_c;
286                 struct rndis_set        *set;
287                 struct rndis_set_c      *set_c;
288                 struct rndis_halt       *halt;
289         } u;
290         u32                     tmp, *phym;
291         int                     reply_len;
292         unsigned char           *bp;
293
294         /* we can't rely on i/o from stack working, or stack allocation */
295         u.buf = kmalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
296         if (!u.buf)
297                 return -ENOMEM;
298         retval = usbnet_generic_cdc_bind(dev, intf);
299         if (retval < 0)
300                 goto fail;
301
302         u.init->msg_type = RNDIS_MSG_INIT;
303         u.init->msg_len = ccpu2(sizeof *u.init);
304         u.init->major_version = ccpu2(1);
305         u.init->minor_version = ccpu2(0);
306
307         /* max transfer (in spec) is 0x4000 at full speed, but for
308          * TX we'll stick to one Ethernet packet plus RNDIS framing.
309          * For RX we handle drivers that zero-pad to end-of-packet.
310          * Don't let userspace change these settings.
311          *
312          * NOTE: there still seems to be wierdness here, as if we need
313          * to do some more things to make sure WinCE targets accept this.
314          * They default to jumbograms of 8KB or 16KB, which is absurd
315          * for such low data rates and which is also more than Linux
316          * can usually expect to allocate for SKB data...
317          */
318         net->hard_header_len += sizeof (struct rndis_data_hdr);
319         dev->hard_mtu = net->mtu + net->hard_header_len;
320
321         dev->rx_urb_size = dev->hard_mtu + (dev->maxpacket + 1);
322         dev->rx_urb_size &= ~(dev->maxpacket - 1);
323         u.init->max_transfer_size = cpu_to_le32(dev->rx_urb_size);
324
325         net->change_mtu = NULL;
326         retval = rndis_command(dev, u.header);
327         if (unlikely(retval < 0)) {
328                 /* it might not even be an RNDIS device!! */
329                 dev_err(&intf->dev, "RNDIS init failed, %d\n", retval);
330                 goto fail_and_release;
331         }
332         tmp = le32_to_cpu(u.init_c->max_transfer_size);
333         if (tmp < dev->hard_mtu) {
334                 if (tmp <= net->hard_header_len) {
335                         dev_err(&intf->dev,
336                                 "dev can't take %u byte packets (max %u)\n",
337                                 dev->hard_mtu, tmp);
338                         retval = -EINVAL;
339                         goto halt_fail_and_release;
340                 }
341                 dev->hard_mtu = tmp;
342                 net->mtu = dev->hard_mtu - net->hard_header_len;
343                 dev_warn(&intf->dev,
344                          "dev can't take %u byte packets (max %u), "
345                          "adjusting MTU to %u\n",
346                          dev->hard_mtu, tmp, net->mtu);
347         }
348
349         /* REVISIT:  peripheral "alignment" request is ignored ... */
350         dev_dbg(&intf->dev,
351                 "hard mtu %u (%u from dev), rx buflen %Zu, align %d\n",
352                 dev->hard_mtu, tmp, dev->rx_urb_size,
353                 1 << le32_to_cpu(u.init_c->packet_alignment));
354
355         /* module has some device initialization code needs to be done right
356          * after RNDIS_INIT */
357         if (dev->driver_info->early_init &&
358                         dev->driver_info->early_init(dev) != 0)
359                 goto halt_fail_and_release;
360
361         /* Check physical medium */
362         reply_len = sizeof *phym;
363         retval = rndis_query(dev, intf, u.buf, OID_GEN_PHYSICAL_MEDIUM,
364                         0, (void **) &phym, &reply_len);
365         if (retval != 0)
366                 /* OID is optional so don't fail here. */
367                 *phym = RNDIS_PHYSICAL_MEDIUM_UNSPECIFIED;
368         if ((flags & FLAG_RNDIS_PHYM_WIRELESS) &&
369                         *phym != RNDIS_PHYSICAL_MEDIUM_WIRELESS_LAN) {
370                 if (netif_msg_probe(dev))
371                         dev_dbg(&intf->dev, "driver requires wireless "
372                                 "physical medium, but device is not.\n");
373                 retval = -ENODEV;
374                 goto halt_fail_and_release;
375         }
376         if ((flags & FLAG_RNDIS_PHYM_NOT_WIRELESS) &&
377                         *phym == RNDIS_PHYSICAL_MEDIUM_WIRELESS_LAN) {
378                 if (netif_msg_probe(dev))
379                         dev_dbg(&intf->dev, "driver requires non-wireless "
380                                 "physical medium, but device is wireless.\n");
381                 retval = -ENODEV;
382                 goto halt_fail_and_release;
383         }
384
385         /* Get designated host ethernet address */
386         reply_len = ETH_ALEN;
387         retval = rndis_query(dev, intf, u.buf, OID_802_3_PERMANENT_ADDRESS,
388                         48, (void **) &bp, &reply_len);
389         if (unlikely(retval< 0)) {
390                 dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval);
391                 goto halt_fail_and_release;
392         }
393         memcpy(net->dev_addr, bp, ETH_ALEN);
394
395         /* set a nonzero filter to enable data transfers */
396         memset(u.set, 0, sizeof *u.set);
397         u.set->msg_type = RNDIS_MSG_SET;
398         u.set->msg_len = ccpu2(4 + sizeof *u.set);
399         u.set->oid = OID_GEN_CURRENT_PACKET_FILTER;
400         u.set->len = ccpu2(4);
401         u.set->offset = ccpu2((sizeof *u.set) - 8);
402         *(__le32 *)(u.buf + sizeof *u.set) = RNDIS_DEFAULT_FILTER;
403
404         retval = rndis_command(dev, u.header);
405         if (unlikely(retval < 0)) {
406                 dev_err(&intf->dev, "rndis set packet filter, %d\n", retval);
407                 goto halt_fail_and_release;
408         }
409
410         retval = 0;
411
412         kfree(u.buf);
413         return retval;
414
415 halt_fail_and_release:
416         memset(u.halt, 0, sizeof *u.halt);
417         u.halt->msg_type = RNDIS_MSG_HALT;
418         u.halt->msg_len = ccpu2(sizeof *u.halt);
419         (void) rndis_command(dev, (void *)u.halt);
420 fail_and_release:
421         usb_set_intfdata(info->data, NULL);
422         usb_driver_release_interface(driver_of(intf), info->data);
423         info->data = NULL;
424 fail:
425         kfree(u.buf);
426         return retval;
427 }
428 EXPORT_SYMBOL_GPL(generic_rndis_bind);
429
430 static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
431 {
432         return generic_rndis_bind(dev, intf, FLAG_RNDIS_PHYM_NOT_WIRELESS);
433 }
434
435 void rndis_unbind(struct usbnet *dev, struct usb_interface *intf)
436 {
437         struct rndis_halt       *halt;
438
439         /* try to clear any rndis state/activity (no i/o from stack!) */
440         halt = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
441         if (halt) {
442                 halt->msg_type = RNDIS_MSG_HALT;
443                 halt->msg_len = ccpu2(sizeof *halt);
444                 (void) rndis_command(dev, (void *)halt);
445                 kfree(halt);
446         }
447
448         usbnet_cdc_unbind(dev, intf);
449 }
450 EXPORT_SYMBOL_GPL(rndis_unbind);
451
452 /*
453  * DATA -- host must not write zlps
454  */
455 int rndis_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
456 {
457         /* peripheral may have batched packets to us... */
458         while (likely(skb->len)) {
459                 struct rndis_data_hdr   *hdr = (void *)skb->data;
460                 struct sk_buff          *skb2;
461                 u32                     msg_len, data_offset, data_len;
462
463                 msg_len = le32_to_cpu(hdr->msg_len);
464                 data_offset = le32_to_cpu(hdr->data_offset);
465                 data_len = le32_to_cpu(hdr->data_len);
466
467                 /* don't choke if we see oob, per-packet data, etc */
468                 if (unlikely(hdr->msg_type != RNDIS_MSG_PACKET
469                                 || skb->len < msg_len
470                                 || (data_offset + data_len + 8) > msg_len)) {
471                         dev->stats.rx_frame_errors++;
472                         devdbg(dev, "bad rndis message %d/%d/%d/%d, len %d",
473                                 le32_to_cpu(hdr->msg_type),
474                                 msg_len, data_offset, data_len, skb->len);
475                         return 0;
476                 }
477                 skb_pull(skb, 8 + data_offset);
478
479                 /* at most one packet left? */
480                 if (likely((data_len - skb->len) <= sizeof *hdr)) {
481                         skb_trim(skb, data_len);
482                         break;
483                 }
484
485                 /* try to return all the packets in the batch */
486                 skb2 = skb_clone(skb, GFP_ATOMIC);
487                 if (unlikely(!skb2))
488                         break;
489                 skb_pull(skb, msg_len - sizeof *hdr);
490                 skb_trim(skb2, data_len);
491                 usbnet_skb_return(dev, skb2);
492         }
493
494         /* caller will usbnet_skb_return the remaining packet */
495         return 1;
496 }
497 EXPORT_SYMBOL_GPL(rndis_rx_fixup);
498
499 struct sk_buff *
500 rndis_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
501 {
502         struct rndis_data_hdr   *hdr;
503         struct sk_buff          *skb2;
504         unsigned                len = skb->len;
505
506         if (likely(!skb_cloned(skb))) {
507                 int     room = skb_headroom(skb);
508
509                 /* enough head room as-is? */
510                 if (unlikely((sizeof *hdr) <= room))
511                         goto fill;
512
513                 /* enough room, but needs to be readjusted? */
514                 room += skb_tailroom(skb);
515                 if (likely((sizeof *hdr) <= room)) {
516                         skb->data = memmove(skb->head + sizeof *hdr,
517                                             skb->data, len);
518                         skb_set_tail_pointer(skb, len);
519                         goto fill;
520                 }
521         }
522
523         /* create a new skb, with the correct size (and tailpad) */
524         skb2 = skb_copy_expand(skb, sizeof *hdr, 1, flags);
525         dev_kfree_skb_any(skb);
526         if (unlikely(!skb2))
527                 return skb2;
528         skb = skb2;
529
530         /* fill out the RNDIS header.  we won't bother trying to batch
531          * packets; Linux minimizes wasted bandwidth through tx queues.
532          */
533 fill:
534         hdr = (void *) __skb_push(skb, sizeof *hdr);
535         memset(hdr, 0, sizeof *hdr);
536         hdr->msg_type = RNDIS_MSG_PACKET;
537         hdr->msg_len = cpu_to_le32(skb->len);
538         hdr->data_offset = ccpu2(sizeof(*hdr) - 8);
539         hdr->data_len = cpu_to_le32(len);
540
541         /* FIXME make the last packet always be short ... */
542         return skb;
543 }
544 EXPORT_SYMBOL_GPL(rndis_tx_fixup);
545
546
547 static const struct driver_info rndis_info = {
548         .description =  "RNDIS device",
549         .flags =        FLAG_ETHER | FLAG_FRAMING_RN | FLAG_NO_SETINT,
550         .bind =         rndis_bind,
551         .unbind =       rndis_unbind,
552         .status =       rndis_status,
553         .rx_fixup =     rndis_rx_fixup,
554         .tx_fixup =     rndis_tx_fixup,
555 };
556
557 #undef ccpu2
558
559
560 /*-------------------------------------------------------------------------*/
561
562 static const struct usb_device_id       products [] = {
563 {
564         /* RNDIS is MSFT's un-official variant of CDC ACM */
565         USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
566         .driver_info = (unsigned long) &rndis_info,
567 }, {
568         /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
569         USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
570         .driver_info = (unsigned long) &rndis_info,
571 },
572         { },            // END
573 };
574 MODULE_DEVICE_TABLE(usb, products);
575
576 static struct usb_driver rndis_driver = {
577         .name =         "rndis_host",
578         .id_table =     products,
579         .probe =        usbnet_probe,
580         .disconnect =   usbnet_disconnect,
581         .suspend =      usbnet_suspend,
582         .resume =       usbnet_resume,
583 };
584
585 static int __init rndis_init(void)
586 {
587         return usb_register(&rndis_driver);
588 }
589 module_init(rndis_init);
590
591 static void __exit rndis_exit(void)
592 {
593         usb_deregister(&rndis_driver);
594 }
595 module_exit(rndis_exit);
596
597 MODULE_AUTHOR("David Brownell");
598 MODULE_DESCRIPTION("USB Host side RNDIS driver");
599 MODULE_LICENSE("GPL");