Commit 38ff1edb52f737d490126728e3d5ba2b8d2f3ba0

Authored by Rémi Denis-Courmont
Committed by David S. Miller
1 parent 102463b18d

f_phonet: fix page offset of first received fragment

We pull one byte (the MAC header) from the first fragment before the
fragment is actually appended. So the socket buffer length is 1, not 0.

Signed-off-by: Rémi Denis-Courmont <remi.denis-courmont@nokia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

drivers/usb/gadget/f_phonet.c
1 /* 1 /*
2 * f_phonet.c -- USB CDC Phonet function 2 * f_phonet.c -- USB CDC Phonet function
3 * 3 *
4 * Copyright (C) 2007-2008 Nokia Corporation. All rights reserved. 4 * Copyright (C) 2007-2008 Nokia Corporation. All rights reserved.
5 * 5 *
6 * Author: Rémi Denis-Courmont 6 * Author: Rémi Denis-Courmont
7 * 7 *
8 * This program is free software; you can redistribute it and/or 8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License 9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation. 10 * version 2 as published by the Free Software Foundation.
11 */ 11 */
12 12
13 #include <linux/mm.h> 13 #include <linux/mm.h>
14 #include <linux/slab.h> 14 #include <linux/slab.h>
15 #include <linux/kernel.h> 15 #include <linux/kernel.h>
16 #include <linux/device.h> 16 #include <linux/device.h>
17 17
18 #include <linux/netdevice.h> 18 #include <linux/netdevice.h>
19 #include <linux/if_ether.h> 19 #include <linux/if_ether.h>
20 #include <linux/if_phonet.h> 20 #include <linux/if_phonet.h>
21 #include <linux/if_arp.h> 21 #include <linux/if_arp.h>
22 22
23 #include <linux/usb/ch9.h> 23 #include <linux/usb/ch9.h>
24 #include <linux/usb/cdc.h> 24 #include <linux/usb/cdc.h>
25 #include <linux/usb/composite.h> 25 #include <linux/usb/composite.h>
26 26
27 #include "u_phonet.h" 27 #include "u_phonet.h"
28 28
29 #define PN_MEDIA_USB 0x1B 29 #define PN_MEDIA_USB 0x1B
30 #define MAXPACKET 512 30 #define MAXPACKET 512
31 #if (PAGE_SIZE % MAXPACKET) 31 #if (PAGE_SIZE % MAXPACKET)
32 #error MAXPACKET must divide PAGE_SIZE! 32 #error MAXPACKET must divide PAGE_SIZE!
33 #endif 33 #endif
34 34
35 /*-------------------------------------------------------------------------*/ 35 /*-------------------------------------------------------------------------*/
36 36
37 struct phonet_port { 37 struct phonet_port {
38 struct f_phonet *usb; 38 struct f_phonet *usb;
39 spinlock_t lock; 39 spinlock_t lock;
40 }; 40 };
41 41
42 struct f_phonet { 42 struct f_phonet {
43 struct usb_function function; 43 struct usb_function function;
44 struct { 44 struct {
45 struct sk_buff *skb; 45 struct sk_buff *skb;
46 spinlock_t lock; 46 spinlock_t lock;
47 } rx; 47 } rx;
48 struct net_device *dev; 48 struct net_device *dev;
49 struct usb_ep *in_ep, *out_ep; 49 struct usb_ep *in_ep, *out_ep;
50 50
51 struct usb_request *in_req; 51 struct usb_request *in_req;
52 struct usb_request *out_reqv[0]; 52 struct usb_request *out_reqv[0];
53 }; 53 };
54 54
55 static int phonet_rxq_size = 17; 55 static int phonet_rxq_size = 17;
56 56
57 static inline struct f_phonet *func_to_pn(struct usb_function *f) 57 static inline struct f_phonet *func_to_pn(struct usb_function *f)
58 { 58 {
59 return container_of(f, struct f_phonet, function); 59 return container_of(f, struct f_phonet, function);
60 } 60 }
61 61
62 /*-------------------------------------------------------------------------*/ 62 /*-------------------------------------------------------------------------*/
63 63
64 #define USB_CDC_SUBCLASS_PHONET 0xfe 64 #define USB_CDC_SUBCLASS_PHONET 0xfe
65 #define USB_CDC_PHONET_TYPE 0xab 65 #define USB_CDC_PHONET_TYPE 0xab
66 66
67 static struct usb_interface_descriptor 67 static struct usb_interface_descriptor
68 pn_control_intf_desc = { 68 pn_control_intf_desc = {
69 .bLength = sizeof pn_control_intf_desc, 69 .bLength = sizeof pn_control_intf_desc,
70 .bDescriptorType = USB_DT_INTERFACE, 70 .bDescriptorType = USB_DT_INTERFACE,
71 71
72 /* .bInterfaceNumber = DYNAMIC, */ 72 /* .bInterfaceNumber = DYNAMIC, */
73 .bInterfaceClass = USB_CLASS_COMM, 73 .bInterfaceClass = USB_CLASS_COMM,
74 .bInterfaceSubClass = USB_CDC_SUBCLASS_PHONET, 74 .bInterfaceSubClass = USB_CDC_SUBCLASS_PHONET,
75 }; 75 };
76 76
77 static const struct usb_cdc_header_desc 77 static const struct usb_cdc_header_desc
78 pn_header_desc = { 78 pn_header_desc = {
79 .bLength = sizeof pn_header_desc, 79 .bLength = sizeof pn_header_desc,
80 .bDescriptorType = USB_DT_CS_INTERFACE, 80 .bDescriptorType = USB_DT_CS_INTERFACE,
81 .bDescriptorSubType = USB_CDC_HEADER_TYPE, 81 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
82 .bcdCDC = cpu_to_le16(0x0110), 82 .bcdCDC = cpu_to_le16(0x0110),
83 }; 83 };
84 84
85 static const struct usb_cdc_header_desc 85 static const struct usb_cdc_header_desc
86 pn_phonet_desc = { 86 pn_phonet_desc = {
87 .bLength = sizeof pn_phonet_desc, 87 .bLength = sizeof pn_phonet_desc,
88 .bDescriptorType = USB_DT_CS_INTERFACE, 88 .bDescriptorType = USB_DT_CS_INTERFACE,
89 .bDescriptorSubType = USB_CDC_PHONET_TYPE, 89 .bDescriptorSubType = USB_CDC_PHONET_TYPE,
90 .bcdCDC = cpu_to_le16(0x1505), /* ??? */ 90 .bcdCDC = cpu_to_le16(0x1505), /* ??? */
91 }; 91 };
92 92
93 static struct usb_cdc_union_desc 93 static struct usb_cdc_union_desc
94 pn_union_desc = { 94 pn_union_desc = {
95 .bLength = sizeof pn_union_desc, 95 .bLength = sizeof pn_union_desc,
96 .bDescriptorType = USB_DT_CS_INTERFACE, 96 .bDescriptorType = USB_DT_CS_INTERFACE,
97 .bDescriptorSubType = USB_CDC_UNION_TYPE, 97 .bDescriptorSubType = USB_CDC_UNION_TYPE,
98 98
99 /* .bMasterInterface0 = DYNAMIC, */ 99 /* .bMasterInterface0 = DYNAMIC, */
100 /* .bSlaveInterface0 = DYNAMIC, */ 100 /* .bSlaveInterface0 = DYNAMIC, */
101 }; 101 };
102 102
103 static struct usb_interface_descriptor 103 static struct usb_interface_descriptor
104 pn_data_nop_intf_desc = { 104 pn_data_nop_intf_desc = {
105 .bLength = sizeof pn_data_nop_intf_desc, 105 .bLength = sizeof pn_data_nop_intf_desc,
106 .bDescriptorType = USB_DT_INTERFACE, 106 .bDescriptorType = USB_DT_INTERFACE,
107 107
108 /* .bInterfaceNumber = DYNAMIC, */ 108 /* .bInterfaceNumber = DYNAMIC, */
109 .bAlternateSetting = 0, 109 .bAlternateSetting = 0,
110 .bNumEndpoints = 0, 110 .bNumEndpoints = 0,
111 .bInterfaceClass = USB_CLASS_CDC_DATA, 111 .bInterfaceClass = USB_CLASS_CDC_DATA,
112 }; 112 };
113 113
114 static struct usb_interface_descriptor 114 static struct usb_interface_descriptor
115 pn_data_intf_desc = { 115 pn_data_intf_desc = {
116 .bLength = sizeof pn_data_intf_desc, 116 .bLength = sizeof pn_data_intf_desc,
117 .bDescriptorType = USB_DT_INTERFACE, 117 .bDescriptorType = USB_DT_INTERFACE,
118 118
119 /* .bInterfaceNumber = DYNAMIC, */ 119 /* .bInterfaceNumber = DYNAMIC, */
120 .bAlternateSetting = 1, 120 .bAlternateSetting = 1,
121 .bNumEndpoints = 2, 121 .bNumEndpoints = 2,
122 .bInterfaceClass = USB_CLASS_CDC_DATA, 122 .bInterfaceClass = USB_CLASS_CDC_DATA,
123 }; 123 };
124 124
125 static struct usb_endpoint_descriptor 125 static struct usb_endpoint_descriptor
126 pn_fs_sink_desc = { 126 pn_fs_sink_desc = {
127 .bLength = USB_DT_ENDPOINT_SIZE, 127 .bLength = USB_DT_ENDPOINT_SIZE,
128 .bDescriptorType = USB_DT_ENDPOINT, 128 .bDescriptorType = USB_DT_ENDPOINT,
129 129
130 .bEndpointAddress = USB_DIR_OUT, 130 .bEndpointAddress = USB_DIR_OUT,
131 .bmAttributes = USB_ENDPOINT_XFER_BULK, 131 .bmAttributes = USB_ENDPOINT_XFER_BULK,
132 }; 132 };
133 133
134 static struct usb_endpoint_descriptor 134 static struct usb_endpoint_descriptor
135 pn_hs_sink_desc = { 135 pn_hs_sink_desc = {
136 .bLength = USB_DT_ENDPOINT_SIZE, 136 .bLength = USB_DT_ENDPOINT_SIZE,
137 .bDescriptorType = USB_DT_ENDPOINT, 137 .bDescriptorType = USB_DT_ENDPOINT,
138 138
139 .bEndpointAddress = USB_DIR_OUT, 139 .bEndpointAddress = USB_DIR_OUT,
140 .bmAttributes = USB_ENDPOINT_XFER_BULK, 140 .bmAttributes = USB_ENDPOINT_XFER_BULK,
141 .wMaxPacketSize = cpu_to_le16(MAXPACKET), 141 .wMaxPacketSize = cpu_to_le16(MAXPACKET),
142 }; 142 };
143 143
144 static struct usb_endpoint_descriptor 144 static struct usb_endpoint_descriptor
145 pn_fs_source_desc = { 145 pn_fs_source_desc = {
146 .bLength = USB_DT_ENDPOINT_SIZE, 146 .bLength = USB_DT_ENDPOINT_SIZE,
147 .bDescriptorType = USB_DT_ENDPOINT, 147 .bDescriptorType = USB_DT_ENDPOINT,
148 148
149 .bEndpointAddress = USB_DIR_IN, 149 .bEndpointAddress = USB_DIR_IN,
150 .bmAttributes = USB_ENDPOINT_XFER_BULK, 150 .bmAttributes = USB_ENDPOINT_XFER_BULK,
151 }; 151 };
152 152
153 static struct usb_endpoint_descriptor 153 static struct usb_endpoint_descriptor
154 pn_hs_source_desc = { 154 pn_hs_source_desc = {
155 .bLength = USB_DT_ENDPOINT_SIZE, 155 .bLength = USB_DT_ENDPOINT_SIZE,
156 .bDescriptorType = USB_DT_ENDPOINT, 156 .bDescriptorType = USB_DT_ENDPOINT,
157 157
158 .bEndpointAddress = USB_DIR_IN, 158 .bEndpointAddress = USB_DIR_IN,
159 .bmAttributes = USB_ENDPOINT_XFER_BULK, 159 .bmAttributes = USB_ENDPOINT_XFER_BULK,
160 .wMaxPacketSize = cpu_to_le16(512), 160 .wMaxPacketSize = cpu_to_le16(512),
161 }; 161 };
162 162
163 static struct usb_descriptor_header *fs_pn_function[] = { 163 static struct usb_descriptor_header *fs_pn_function[] = {
164 (struct usb_descriptor_header *) &pn_control_intf_desc, 164 (struct usb_descriptor_header *) &pn_control_intf_desc,
165 (struct usb_descriptor_header *) &pn_header_desc, 165 (struct usb_descriptor_header *) &pn_header_desc,
166 (struct usb_descriptor_header *) &pn_phonet_desc, 166 (struct usb_descriptor_header *) &pn_phonet_desc,
167 (struct usb_descriptor_header *) &pn_union_desc, 167 (struct usb_descriptor_header *) &pn_union_desc,
168 (struct usb_descriptor_header *) &pn_data_nop_intf_desc, 168 (struct usb_descriptor_header *) &pn_data_nop_intf_desc,
169 (struct usb_descriptor_header *) &pn_data_intf_desc, 169 (struct usb_descriptor_header *) &pn_data_intf_desc,
170 (struct usb_descriptor_header *) &pn_fs_sink_desc, 170 (struct usb_descriptor_header *) &pn_fs_sink_desc,
171 (struct usb_descriptor_header *) &pn_fs_source_desc, 171 (struct usb_descriptor_header *) &pn_fs_source_desc,
172 NULL, 172 NULL,
173 }; 173 };
174 174
175 static struct usb_descriptor_header *hs_pn_function[] = { 175 static struct usb_descriptor_header *hs_pn_function[] = {
176 (struct usb_descriptor_header *) &pn_control_intf_desc, 176 (struct usb_descriptor_header *) &pn_control_intf_desc,
177 (struct usb_descriptor_header *) &pn_header_desc, 177 (struct usb_descriptor_header *) &pn_header_desc,
178 (struct usb_descriptor_header *) &pn_phonet_desc, 178 (struct usb_descriptor_header *) &pn_phonet_desc,
179 (struct usb_descriptor_header *) &pn_union_desc, 179 (struct usb_descriptor_header *) &pn_union_desc,
180 (struct usb_descriptor_header *) &pn_data_nop_intf_desc, 180 (struct usb_descriptor_header *) &pn_data_nop_intf_desc,
181 (struct usb_descriptor_header *) &pn_data_intf_desc, 181 (struct usb_descriptor_header *) &pn_data_intf_desc,
182 (struct usb_descriptor_header *) &pn_hs_sink_desc, 182 (struct usb_descriptor_header *) &pn_hs_sink_desc,
183 (struct usb_descriptor_header *) &pn_hs_source_desc, 183 (struct usb_descriptor_header *) &pn_hs_source_desc,
184 NULL, 184 NULL,
185 }; 185 };
186 186
187 /*-------------------------------------------------------------------------*/ 187 /*-------------------------------------------------------------------------*/
188 188
189 static int pn_net_open(struct net_device *dev) 189 static int pn_net_open(struct net_device *dev)
190 { 190 {
191 netif_wake_queue(dev); 191 netif_wake_queue(dev);
192 return 0; 192 return 0;
193 } 193 }
194 194
195 static int pn_net_close(struct net_device *dev) 195 static int pn_net_close(struct net_device *dev)
196 { 196 {
197 netif_stop_queue(dev); 197 netif_stop_queue(dev);
198 return 0; 198 return 0;
199 } 199 }
200 200
201 static void pn_tx_complete(struct usb_ep *ep, struct usb_request *req) 201 static void pn_tx_complete(struct usb_ep *ep, struct usb_request *req)
202 { 202 {
203 struct f_phonet *fp = ep->driver_data; 203 struct f_phonet *fp = ep->driver_data;
204 struct net_device *dev = fp->dev; 204 struct net_device *dev = fp->dev;
205 struct sk_buff *skb = req->context; 205 struct sk_buff *skb = req->context;
206 206
207 switch (req->status) { 207 switch (req->status) {
208 case 0: 208 case 0:
209 dev->stats.tx_packets++; 209 dev->stats.tx_packets++;
210 dev->stats.tx_bytes += skb->len; 210 dev->stats.tx_bytes += skb->len;
211 break; 211 break;
212 212
213 case -ESHUTDOWN: /* disconnected */ 213 case -ESHUTDOWN: /* disconnected */
214 case -ECONNRESET: /* disabled */ 214 case -ECONNRESET: /* disabled */
215 dev->stats.tx_aborted_errors++; 215 dev->stats.tx_aborted_errors++;
216 default: 216 default:
217 dev->stats.tx_errors++; 217 dev->stats.tx_errors++;
218 } 218 }
219 219
220 dev_kfree_skb_any(skb); 220 dev_kfree_skb_any(skb);
221 netif_wake_queue(dev); 221 netif_wake_queue(dev);
222 } 222 }
223 223
224 static int pn_net_xmit(struct sk_buff *skb, struct net_device *dev) 224 static int pn_net_xmit(struct sk_buff *skb, struct net_device *dev)
225 { 225 {
226 struct phonet_port *port = netdev_priv(dev); 226 struct phonet_port *port = netdev_priv(dev);
227 struct f_phonet *fp; 227 struct f_phonet *fp;
228 struct usb_request *req; 228 struct usb_request *req;
229 unsigned long flags; 229 unsigned long flags;
230 230
231 if (skb->protocol != htons(ETH_P_PHONET)) 231 if (skb->protocol != htons(ETH_P_PHONET))
232 goto out; 232 goto out;
233 233
234 spin_lock_irqsave(&port->lock, flags); 234 spin_lock_irqsave(&port->lock, flags);
235 fp = port->usb; 235 fp = port->usb;
236 if (unlikely(!fp)) /* race with carrier loss */ 236 if (unlikely(!fp)) /* race with carrier loss */
237 goto out_unlock; 237 goto out_unlock;
238 238
239 req = fp->in_req; 239 req = fp->in_req;
240 req->buf = skb->data; 240 req->buf = skb->data;
241 req->length = skb->len; 241 req->length = skb->len;
242 req->complete = pn_tx_complete; 242 req->complete = pn_tx_complete;
243 req->zero = 1; 243 req->zero = 1;
244 req->context = skb; 244 req->context = skb;
245 245
246 if (unlikely(usb_ep_queue(fp->in_ep, req, GFP_ATOMIC))) 246 if (unlikely(usb_ep_queue(fp->in_ep, req, GFP_ATOMIC)))
247 goto out_unlock; 247 goto out_unlock;
248 248
249 netif_stop_queue(dev); 249 netif_stop_queue(dev);
250 skb = NULL; 250 skb = NULL;
251 251
252 out_unlock: 252 out_unlock:
253 spin_unlock_irqrestore(&port->lock, flags); 253 spin_unlock_irqrestore(&port->lock, flags);
254 out: 254 out:
255 if (unlikely(skb)) { 255 if (unlikely(skb)) {
256 dev_kfree_skb(skb); 256 dev_kfree_skb(skb);
257 dev->stats.tx_dropped++; 257 dev->stats.tx_dropped++;
258 } 258 }
259 return NETDEV_TX_OK; 259 return NETDEV_TX_OK;
260 } 260 }
261 261
262 static int pn_net_mtu(struct net_device *dev, int new_mtu) 262 static int pn_net_mtu(struct net_device *dev, int new_mtu)
263 { 263 {
264 if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU)) 264 if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
265 return -EINVAL; 265 return -EINVAL;
266 dev->mtu = new_mtu; 266 dev->mtu = new_mtu;
267 return 0; 267 return 0;
268 } 268 }
269 269
270 static const struct net_device_ops pn_netdev_ops = { 270 static const struct net_device_ops pn_netdev_ops = {
271 .ndo_open = pn_net_open, 271 .ndo_open = pn_net_open,
272 .ndo_stop = pn_net_close, 272 .ndo_stop = pn_net_close,
273 .ndo_start_xmit = pn_net_xmit, 273 .ndo_start_xmit = pn_net_xmit,
274 .ndo_change_mtu = pn_net_mtu, 274 .ndo_change_mtu = pn_net_mtu,
275 }; 275 };
276 276
277 static void pn_net_setup(struct net_device *dev) 277 static void pn_net_setup(struct net_device *dev)
278 { 278 {
279 dev->features = 0; 279 dev->features = 0;
280 dev->type = ARPHRD_PHONET; 280 dev->type = ARPHRD_PHONET;
281 dev->flags = IFF_POINTOPOINT | IFF_NOARP; 281 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
282 dev->mtu = PHONET_DEV_MTU; 282 dev->mtu = PHONET_DEV_MTU;
283 dev->hard_header_len = 1; 283 dev->hard_header_len = 1;
284 dev->dev_addr[0] = PN_MEDIA_USB; 284 dev->dev_addr[0] = PN_MEDIA_USB;
285 dev->addr_len = 1; 285 dev->addr_len = 1;
286 dev->tx_queue_len = 1; 286 dev->tx_queue_len = 1;
287 287
288 dev->netdev_ops = &pn_netdev_ops; 288 dev->netdev_ops = &pn_netdev_ops;
289 dev->destructor = free_netdev; 289 dev->destructor = free_netdev;
290 dev->header_ops = &phonet_header_ops; 290 dev->header_ops = &phonet_header_ops;
291 } 291 }
292 292
293 /*-------------------------------------------------------------------------*/ 293 /*-------------------------------------------------------------------------*/
294 294
295 /* 295 /*
296 * Queue buffer for data from the host 296 * Queue buffer for data from the host
297 */ 297 */
298 static int 298 static int
299 pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags) 299 pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags)
300 { 300 {
301 struct net_device *dev = fp->dev; 301 struct net_device *dev = fp->dev;
302 struct page *page; 302 struct page *page;
303 int err; 303 int err;
304 304
305 page = __netdev_alloc_page(dev, gfp_flags); 305 page = __netdev_alloc_page(dev, gfp_flags);
306 if (!page) 306 if (!page)
307 return -ENOMEM; 307 return -ENOMEM;
308 308
309 req->buf = page_address(page); 309 req->buf = page_address(page);
310 req->length = PAGE_SIZE; 310 req->length = PAGE_SIZE;
311 req->context = page; 311 req->context = page;
312 312
313 err = usb_ep_queue(fp->out_ep, req, gfp_flags); 313 err = usb_ep_queue(fp->out_ep, req, gfp_flags);
314 if (unlikely(err)) 314 if (unlikely(err))
315 netdev_free_page(dev, page); 315 netdev_free_page(dev, page);
316 return err; 316 return err;
317 } 317 }
318 318
319 static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req) 319 static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req)
320 { 320 {
321 struct f_phonet *fp = ep->driver_data; 321 struct f_phonet *fp = ep->driver_data;
322 struct net_device *dev = fp->dev; 322 struct net_device *dev = fp->dev;
323 struct page *page = req->context; 323 struct page *page = req->context;
324 struct sk_buff *skb; 324 struct sk_buff *skb;
325 unsigned long flags; 325 unsigned long flags;
326 int status = req->status; 326 int status = req->status;
327 327
328 switch (status) { 328 switch (status) {
329 case 0: 329 case 0:
330 spin_lock_irqsave(&fp->rx.lock, flags); 330 spin_lock_irqsave(&fp->rx.lock, flags);
331 skb = fp->rx.skb; 331 skb = fp->rx.skb;
332 if (!skb) 332 if (!skb)
333 skb = fp->rx.skb = netdev_alloc_skb(dev, 12); 333 skb = fp->rx.skb = netdev_alloc_skb(dev, 12);
334 if (req->actual < req->length) /* Last fragment */ 334 if (req->actual < req->length) /* Last fragment */
335 fp->rx.skb = NULL; 335 fp->rx.skb = NULL;
336 spin_unlock_irqrestore(&fp->rx.lock, flags); 336 spin_unlock_irqrestore(&fp->rx.lock, flags);
337 337
338 if (unlikely(!skb)) 338 if (unlikely(!skb))
339 break; 339 break;
340 340
341 if (skb->len == 0) { /* First fragment */ 341 if (skb->len == 0) { /* First fragment */
342 skb->protocol = htons(ETH_P_PHONET); 342 skb->protocol = htons(ETH_P_PHONET);
343 skb_reset_mac_header(skb); 343 skb_reset_mac_header(skb);
344 /* Can't use pskb_pull() on page in IRQ */ 344 /* Can't use pskb_pull() on page in IRQ */
345 memcpy(skb_put(skb, 1), page_address(page), 1); 345 memcpy(skb_put(skb, 1), page_address(page), 1);
346 } 346 }
347 347
348 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, 348 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
349 skb->len == 0, req->actual); 349 skb->len <= 1, req->actual);
350 page = NULL; 350 page = NULL;
351 351
352 if (req->actual < req->length) { /* Last fragment */ 352 if (req->actual < req->length) { /* Last fragment */
353 skb->dev = dev; 353 skb->dev = dev;
354 dev->stats.rx_packets++; 354 dev->stats.rx_packets++;
355 dev->stats.rx_bytes += skb->len; 355 dev->stats.rx_bytes += skb->len;
356 356
357 netif_rx(skb); 357 netif_rx(skb);
358 } 358 }
359 break; 359 break;
360 360
361 /* Do not resubmit in these cases: */ 361 /* Do not resubmit in these cases: */
362 case -ESHUTDOWN: /* disconnect */ 362 case -ESHUTDOWN: /* disconnect */
363 case -ECONNABORTED: /* hw reset */ 363 case -ECONNABORTED: /* hw reset */
364 case -ECONNRESET: /* dequeued (unlink or netif down) */ 364 case -ECONNRESET: /* dequeued (unlink or netif down) */
365 req = NULL; 365 req = NULL;
366 break; 366 break;
367 367
368 /* Do resubmit in these cases: */ 368 /* Do resubmit in these cases: */
369 case -EOVERFLOW: /* request buffer overflow */ 369 case -EOVERFLOW: /* request buffer overflow */
370 dev->stats.rx_over_errors++; 370 dev->stats.rx_over_errors++;
371 default: 371 default:
372 dev->stats.rx_errors++; 372 dev->stats.rx_errors++;
373 break; 373 break;
374 } 374 }
375 375
376 if (page) 376 if (page)
377 netdev_free_page(dev, page); 377 netdev_free_page(dev, page);
378 if (req) 378 if (req)
379 pn_rx_submit(fp, req, GFP_ATOMIC); 379 pn_rx_submit(fp, req, GFP_ATOMIC);
380 } 380 }
381 381
382 /*-------------------------------------------------------------------------*/ 382 /*-------------------------------------------------------------------------*/
383 383
384 static void __pn_reset(struct usb_function *f) 384 static void __pn_reset(struct usb_function *f)
385 { 385 {
386 struct f_phonet *fp = func_to_pn(f); 386 struct f_phonet *fp = func_to_pn(f);
387 struct net_device *dev = fp->dev; 387 struct net_device *dev = fp->dev;
388 struct phonet_port *port = netdev_priv(dev); 388 struct phonet_port *port = netdev_priv(dev);
389 389
390 netif_carrier_off(dev); 390 netif_carrier_off(dev);
391 port->usb = NULL; 391 port->usb = NULL;
392 392
393 usb_ep_disable(fp->out_ep); 393 usb_ep_disable(fp->out_ep);
394 usb_ep_disable(fp->in_ep); 394 usb_ep_disable(fp->in_ep);
395 if (fp->rx.skb) { 395 if (fp->rx.skb) {
396 dev_kfree_skb_irq(fp->rx.skb); 396 dev_kfree_skb_irq(fp->rx.skb);
397 fp->rx.skb = NULL; 397 fp->rx.skb = NULL;
398 } 398 }
399 } 399 }
400 400
401 static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 401 static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
402 { 402 {
403 struct f_phonet *fp = func_to_pn(f); 403 struct f_phonet *fp = func_to_pn(f);
404 struct usb_gadget *gadget = fp->function.config->cdev->gadget; 404 struct usb_gadget *gadget = fp->function.config->cdev->gadget;
405 405
406 if (intf == pn_control_intf_desc.bInterfaceNumber) 406 if (intf == pn_control_intf_desc.bInterfaceNumber)
407 /* control interface, no altsetting */ 407 /* control interface, no altsetting */
408 return (alt > 0) ? -EINVAL : 0; 408 return (alt > 0) ? -EINVAL : 0;
409 409
410 if (intf == pn_data_intf_desc.bInterfaceNumber) { 410 if (intf == pn_data_intf_desc.bInterfaceNumber) {
411 struct net_device *dev = fp->dev; 411 struct net_device *dev = fp->dev;
412 struct phonet_port *port = netdev_priv(dev); 412 struct phonet_port *port = netdev_priv(dev);
413 413
414 /* data intf (0: inactive, 1: active) */ 414 /* data intf (0: inactive, 1: active) */
415 if (alt > 1) 415 if (alt > 1)
416 return -EINVAL; 416 return -EINVAL;
417 417
418 spin_lock(&port->lock); 418 spin_lock(&port->lock);
419 __pn_reset(f); 419 __pn_reset(f);
420 if (alt == 1) { 420 if (alt == 1) {
421 int i; 421 int i;
422 422
423 if (config_ep_by_speed(gadget, f, fp->in_ep) || 423 if (config_ep_by_speed(gadget, f, fp->in_ep) ||
424 config_ep_by_speed(gadget, f, fp->out_ep)) { 424 config_ep_by_speed(gadget, f, fp->out_ep)) {
425 fp->in_ep->desc = NULL; 425 fp->in_ep->desc = NULL;
426 fp->out_ep->desc = NULL; 426 fp->out_ep->desc = NULL;
427 spin_unlock(&port->lock); 427 spin_unlock(&port->lock);
428 return -EINVAL; 428 return -EINVAL;
429 } 429 }
430 usb_ep_enable(fp->out_ep); 430 usb_ep_enable(fp->out_ep);
431 usb_ep_enable(fp->in_ep); 431 usb_ep_enable(fp->in_ep);
432 432
433 port->usb = fp; 433 port->usb = fp;
434 fp->out_ep->driver_data = fp; 434 fp->out_ep->driver_data = fp;
435 fp->in_ep->driver_data = fp; 435 fp->in_ep->driver_data = fp;
436 436
437 netif_carrier_on(dev); 437 netif_carrier_on(dev);
438 for (i = 0; i < phonet_rxq_size; i++) 438 for (i = 0; i < phonet_rxq_size; i++)
439 pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC); 439 pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC);
440 } 440 }
441 spin_unlock(&port->lock); 441 spin_unlock(&port->lock);
442 return 0; 442 return 0;
443 } 443 }
444 444
445 return -EINVAL; 445 return -EINVAL;
446 } 446 }
447 447
448 static int pn_get_alt(struct usb_function *f, unsigned intf) 448 static int pn_get_alt(struct usb_function *f, unsigned intf)
449 { 449 {
450 struct f_phonet *fp = func_to_pn(f); 450 struct f_phonet *fp = func_to_pn(f);
451 451
452 if (intf == pn_control_intf_desc.bInterfaceNumber) 452 if (intf == pn_control_intf_desc.bInterfaceNumber)
453 return 0; 453 return 0;
454 454
455 if (intf == pn_data_intf_desc.bInterfaceNumber) { 455 if (intf == pn_data_intf_desc.bInterfaceNumber) {
456 struct phonet_port *port = netdev_priv(fp->dev); 456 struct phonet_port *port = netdev_priv(fp->dev);
457 u8 alt; 457 u8 alt;
458 458
459 spin_lock(&port->lock); 459 spin_lock(&port->lock);
460 alt = port->usb != NULL; 460 alt = port->usb != NULL;
461 spin_unlock(&port->lock); 461 spin_unlock(&port->lock);
462 return alt; 462 return alt;
463 } 463 }
464 464
465 return -EINVAL; 465 return -EINVAL;
466 } 466 }
467 467
468 static void pn_disconnect(struct usb_function *f) 468 static void pn_disconnect(struct usb_function *f)
469 { 469 {
470 struct f_phonet *fp = func_to_pn(f); 470 struct f_phonet *fp = func_to_pn(f);
471 struct phonet_port *port = netdev_priv(fp->dev); 471 struct phonet_port *port = netdev_priv(fp->dev);
472 unsigned long flags; 472 unsigned long flags;
473 473
474 /* remain disabled until set_alt */ 474 /* remain disabled until set_alt */
475 spin_lock_irqsave(&port->lock, flags); 475 spin_lock_irqsave(&port->lock, flags);
476 __pn_reset(f); 476 __pn_reset(f);
477 spin_unlock_irqrestore(&port->lock, flags); 477 spin_unlock_irqrestore(&port->lock, flags);
478 } 478 }
479 479
480 /*-------------------------------------------------------------------------*/ 480 /*-------------------------------------------------------------------------*/
481 481
482 static __init 482 static __init
483 int pn_bind(struct usb_configuration *c, struct usb_function *f) 483 int pn_bind(struct usb_configuration *c, struct usb_function *f)
484 { 484 {
485 struct usb_composite_dev *cdev = c->cdev; 485 struct usb_composite_dev *cdev = c->cdev;
486 struct usb_gadget *gadget = cdev->gadget; 486 struct usb_gadget *gadget = cdev->gadget;
487 struct f_phonet *fp = func_to_pn(f); 487 struct f_phonet *fp = func_to_pn(f);
488 struct usb_ep *ep; 488 struct usb_ep *ep;
489 int status, i; 489 int status, i;
490 490
491 /* Reserve interface IDs */ 491 /* Reserve interface IDs */
492 status = usb_interface_id(c, f); 492 status = usb_interface_id(c, f);
493 if (status < 0) 493 if (status < 0)
494 goto err; 494 goto err;
495 pn_control_intf_desc.bInterfaceNumber = status; 495 pn_control_intf_desc.bInterfaceNumber = status;
496 pn_union_desc.bMasterInterface0 = status; 496 pn_union_desc.bMasterInterface0 = status;
497 497
498 status = usb_interface_id(c, f); 498 status = usb_interface_id(c, f);
499 if (status < 0) 499 if (status < 0)
500 goto err; 500 goto err;
501 pn_data_nop_intf_desc.bInterfaceNumber = status; 501 pn_data_nop_intf_desc.bInterfaceNumber = status;
502 pn_data_intf_desc.bInterfaceNumber = status; 502 pn_data_intf_desc.bInterfaceNumber = status;
503 pn_union_desc.bSlaveInterface0 = status; 503 pn_union_desc.bSlaveInterface0 = status;
504 504
505 /* Reserve endpoints */ 505 /* Reserve endpoints */
506 status = -ENODEV; 506 status = -ENODEV;
507 ep = usb_ep_autoconfig(gadget, &pn_fs_sink_desc); 507 ep = usb_ep_autoconfig(gadget, &pn_fs_sink_desc);
508 if (!ep) 508 if (!ep)
509 goto err; 509 goto err;
510 fp->out_ep = ep; 510 fp->out_ep = ep;
511 ep->driver_data = fp; /* Claim */ 511 ep->driver_data = fp; /* Claim */
512 512
513 ep = usb_ep_autoconfig(gadget, &pn_fs_source_desc); 513 ep = usb_ep_autoconfig(gadget, &pn_fs_source_desc);
514 if (!ep) 514 if (!ep)
515 goto err; 515 goto err;
516 fp->in_ep = ep; 516 fp->in_ep = ep;
517 ep->driver_data = fp; /* Claim */ 517 ep->driver_data = fp; /* Claim */
518 518
519 pn_hs_sink_desc.bEndpointAddress = 519 pn_hs_sink_desc.bEndpointAddress =
520 pn_fs_sink_desc.bEndpointAddress; 520 pn_fs_sink_desc.bEndpointAddress;
521 pn_hs_source_desc.bEndpointAddress = 521 pn_hs_source_desc.bEndpointAddress =
522 pn_fs_source_desc.bEndpointAddress; 522 pn_fs_source_desc.bEndpointAddress;
523 523
524 /* Do not try to bind Phonet twice... */ 524 /* Do not try to bind Phonet twice... */
525 fp->function.descriptors = fs_pn_function; 525 fp->function.descriptors = fs_pn_function;
526 fp->function.hs_descriptors = hs_pn_function; 526 fp->function.hs_descriptors = hs_pn_function;
527 527
528 /* Incoming USB requests */ 528 /* Incoming USB requests */
529 status = -ENOMEM; 529 status = -ENOMEM;
530 for (i = 0; i < phonet_rxq_size; i++) { 530 for (i = 0; i < phonet_rxq_size; i++) {
531 struct usb_request *req; 531 struct usb_request *req;
532 532
533 req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL); 533 req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL);
534 if (!req) 534 if (!req)
535 goto err; 535 goto err;
536 536
537 req->complete = pn_rx_complete; 537 req->complete = pn_rx_complete;
538 fp->out_reqv[i] = req; 538 fp->out_reqv[i] = req;
539 } 539 }
540 540
541 /* Outgoing USB requests */ 541 /* Outgoing USB requests */
542 fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL); 542 fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL);
543 if (!fp->in_req) 543 if (!fp->in_req)
544 goto err; 544 goto err;
545 545
546 INFO(cdev, "USB CDC Phonet function\n"); 546 INFO(cdev, "USB CDC Phonet function\n");
547 INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name, 547 INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name,
548 fp->out_ep->name, fp->in_ep->name); 548 fp->out_ep->name, fp->in_ep->name);
549 return 0; 549 return 0;
550 550
551 err: 551 err:
552 if (fp->out_ep) 552 if (fp->out_ep)
553 fp->out_ep->driver_data = NULL; 553 fp->out_ep->driver_data = NULL;
554 if (fp->in_ep) 554 if (fp->in_ep)
555 fp->in_ep->driver_data = NULL; 555 fp->in_ep->driver_data = NULL;
556 ERROR(cdev, "USB CDC Phonet: cannot autoconfigure\n"); 556 ERROR(cdev, "USB CDC Phonet: cannot autoconfigure\n");
557 return status; 557 return status;
558 } 558 }
559 559
560 static void 560 static void
561 pn_unbind(struct usb_configuration *c, struct usb_function *f) 561 pn_unbind(struct usb_configuration *c, struct usb_function *f)
562 { 562 {
563 struct f_phonet *fp = func_to_pn(f); 563 struct f_phonet *fp = func_to_pn(f);
564 int i; 564 int i;
565 565
566 /* We are already disconnected */ 566 /* We are already disconnected */
567 if (fp->in_req) 567 if (fp->in_req)
568 usb_ep_free_request(fp->in_ep, fp->in_req); 568 usb_ep_free_request(fp->in_ep, fp->in_req);
569 for (i = 0; i < phonet_rxq_size; i++) 569 for (i = 0; i < phonet_rxq_size; i++)
570 if (fp->out_reqv[i]) 570 if (fp->out_reqv[i])
571 usb_ep_free_request(fp->out_ep, fp->out_reqv[i]); 571 usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
572 572
573 kfree(fp); 573 kfree(fp);
574 } 574 }
575 575
576 /*-------------------------------------------------------------------------*/ 576 /*-------------------------------------------------------------------------*/
577 577
578 static struct net_device *dev; 578 static struct net_device *dev;
579 579
580 int __init phonet_bind_config(struct usb_configuration *c) 580 int __init phonet_bind_config(struct usb_configuration *c)
581 { 581 {
582 struct f_phonet *fp; 582 struct f_phonet *fp;
583 int err, size; 583 int err, size;
584 584
585 size = sizeof(*fp) + (phonet_rxq_size * sizeof(struct usb_request *)); 585 size = sizeof(*fp) + (phonet_rxq_size * sizeof(struct usb_request *));
586 fp = kzalloc(size, GFP_KERNEL); 586 fp = kzalloc(size, GFP_KERNEL);
587 if (!fp) 587 if (!fp)
588 return -ENOMEM; 588 return -ENOMEM;
589 589
590 fp->dev = dev; 590 fp->dev = dev;
591 fp->function.name = "phonet"; 591 fp->function.name = "phonet";
592 fp->function.bind = pn_bind; 592 fp->function.bind = pn_bind;
593 fp->function.unbind = pn_unbind; 593 fp->function.unbind = pn_unbind;
594 fp->function.set_alt = pn_set_alt; 594 fp->function.set_alt = pn_set_alt;
595 fp->function.get_alt = pn_get_alt; 595 fp->function.get_alt = pn_get_alt;
596 fp->function.disable = pn_disconnect; 596 fp->function.disable = pn_disconnect;
597 spin_lock_init(&fp->rx.lock); 597 spin_lock_init(&fp->rx.lock);
598 598
599 err = usb_add_function(c, &fp->function); 599 err = usb_add_function(c, &fp->function);
600 if (err) 600 if (err)
601 kfree(fp); 601 kfree(fp);
602 return err; 602 return err;
603 } 603 }
604 604
605 int __init gphonet_setup(struct usb_gadget *gadget) 605 int __init gphonet_setup(struct usb_gadget *gadget)
606 { 606 {
607 struct phonet_port *port; 607 struct phonet_port *port;
608 int err; 608 int err;
609 609
610 /* Create net device */ 610 /* Create net device */
611 BUG_ON(dev); 611 BUG_ON(dev);
612 dev = alloc_netdev(sizeof(*port), "upnlink%d", pn_net_setup); 612 dev = alloc_netdev(sizeof(*port), "upnlink%d", pn_net_setup);
613 if (!dev) 613 if (!dev)
614 return -ENOMEM; 614 return -ENOMEM;
615 615
616 port = netdev_priv(dev); 616 port = netdev_priv(dev);
617 spin_lock_init(&port->lock); 617 spin_lock_init(&port->lock);
618 netif_carrier_off(dev); 618 netif_carrier_off(dev);
619 SET_NETDEV_DEV(dev, &gadget->dev); 619 SET_NETDEV_DEV(dev, &gadget->dev);
620 620
621 err = register_netdev(dev); 621 err = register_netdev(dev);
622 if (err) 622 if (err)
623 free_netdev(dev); 623 free_netdev(dev);
624 return err; 624 return err;
625 } 625 }
626 626
627 void gphonet_cleanup(void) 627 void gphonet_cleanup(void)
628 { 628 {
629 unregister_netdev(dev); 629 unregister_netdev(dev);
630 } 630 }
631 631