Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * f_phonet.c -- USB CDC Phonet function |
| 3 | * |
| 4 | * Copyright (C) 2007-2008 Nokia Corporation. All rights reserved. |
| 5 | * |
| 6 | * Author: Rémi Denis-Courmont |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * version 2 as published by the Free Software Foundation. |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 13 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 15 | #include <linux/kernel.h> |
Andrzej Pietrasiewicz | fcbdf12 | 2013-05-23 10:51:11 +0200 | [diff] [blame] | 16 | #include <linux/module.h> |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 17 | #include <linux/device.h> |
| 18 | |
| 19 | #include <linux/netdevice.h> |
| 20 | #include <linux/if_ether.h> |
| 21 | #include <linux/if_phonet.h> |
| 22 | #include <linux/if_arp.h> |
| 23 | |
| 24 | #include <linux/usb/ch9.h> |
| 25 | #include <linux/usb/cdc.h> |
| 26 | #include <linux/usb/composite.h> |
| 27 | |
| 28 | #include "u_phonet.h" |
Andrzej Pietrasiewicz | 8340874 | 2013-05-23 10:51:15 +0200 | [diff] [blame] | 29 | #include "u_ether.h" |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 30 | |
| 31 | #define PN_MEDIA_USB 0x1B |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 32 | #define MAXPACKET 512 |
| 33 | #if (PAGE_SIZE % MAXPACKET) |
| 34 | #error MAXPACKET must divide PAGE_SIZE! |
| 35 | #endif |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 36 | |
| 37 | /*-------------------------------------------------------------------------*/ |
| 38 | |
| 39 | struct phonet_port { |
| 40 | struct f_phonet *usb; |
| 41 | spinlock_t lock; |
| 42 | }; |
| 43 | |
| 44 | struct f_phonet { |
| 45 | struct usb_function function; |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 46 | struct { |
| 47 | struct sk_buff *skb; |
| 48 | spinlock_t lock; |
| 49 | } rx; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 50 | struct net_device *dev; |
| 51 | struct usb_ep *in_ep, *out_ep; |
| 52 | |
| 53 | struct usb_request *in_req; |
| 54 | struct usb_request *out_reqv[0]; |
| 55 | }; |
| 56 | |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 57 | static int phonet_rxq_size = 17; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 58 | |
| 59 | static inline struct f_phonet *func_to_pn(struct usb_function *f) |
| 60 | { |
| 61 | return container_of(f, struct f_phonet, function); |
| 62 | } |
| 63 | |
| 64 | /*-------------------------------------------------------------------------*/ |
| 65 | |
| 66 | #define USB_CDC_SUBCLASS_PHONET 0xfe |
| 67 | #define USB_CDC_PHONET_TYPE 0xab |
| 68 | |
| 69 | static struct usb_interface_descriptor |
| 70 | pn_control_intf_desc = { |
| 71 | .bLength = sizeof pn_control_intf_desc, |
| 72 | .bDescriptorType = USB_DT_INTERFACE, |
| 73 | |
| 74 | /* .bInterfaceNumber = DYNAMIC, */ |
| 75 | .bInterfaceClass = USB_CLASS_COMM, |
| 76 | .bInterfaceSubClass = USB_CDC_SUBCLASS_PHONET, |
| 77 | }; |
| 78 | |
| 79 | static const struct usb_cdc_header_desc |
| 80 | pn_header_desc = { |
| 81 | .bLength = sizeof pn_header_desc, |
| 82 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 83 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 84 | .bcdCDC = cpu_to_le16(0x0110), |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static const struct usb_cdc_header_desc |
| 88 | pn_phonet_desc = { |
| 89 | .bLength = sizeof pn_phonet_desc, |
| 90 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 91 | .bDescriptorSubType = USB_CDC_PHONET_TYPE, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 92 | .bcdCDC = cpu_to_le16(0x1505), /* ??? */ |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | static struct usb_cdc_union_desc |
| 96 | pn_union_desc = { |
| 97 | .bLength = sizeof pn_union_desc, |
| 98 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 99 | .bDescriptorSubType = USB_CDC_UNION_TYPE, |
| 100 | |
| 101 | /* .bMasterInterface0 = DYNAMIC, */ |
| 102 | /* .bSlaveInterface0 = DYNAMIC, */ |
| 103 | }; |
| 104 | |
| 105 | static struct usb_interface_descriptor |
| 106 | pn_data_nop_intf_desc = { |
| 107 | .bLength = sizeof pn_data_nop_intf_desc, |
| 108 | .bDescriptorType = USB_DT_INTERFACE, |
| 109 | |
| 110 | /* .bInterfaceNumber = DYNAMIC, */ |
| 111 | .bAlternateSetting = 0, |
| 112 | .bNumEndpoints = 0, |
| 113 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 114 | }; |
| 115 | |
| 116 | static struct usb_interface_descriptor |
| 117 | pn_data_intf_desc = { |
| 118 | .bLength = sizeof pn_data_intf_desc, |
| 119 | .bDescriptorType = USB_DT_INTERFACE, |
| 120 | |
| 121 | /* .bInterfaceNumber = DYNAMIC, */ |
| 122 | .bAlternateSetting = 1, |
| 123 | .bNumEndpoints = 2, |
| 124 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 125 | }; |
| 126 | |
| 127 | static struct usb_endpoint_descriptor |
| 128 | pn_fs_sink_desc = { |
| 129 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 130 | .bDescriptorType = USB_DT_ENDPOINT, |
| 131 | |
| 132 | .bEndpointAddress = USB_DIR_OUT, |
| 133 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 134 | }; |
| 135 | |
| 136 | static struct usb_endpoint_descriptor |
| 137 | pn_hs_sink_desc = { |
| 138 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 139 | .bDescriptorType = USB_DT_ENDPOINT, |
| 140 | |
| 141 | .bEndpointAddress = USB_DIR_OUT, |
| 142 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 143 | .wMaxPacketSize = cpu_to_le16(MAXPACKET), |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static struct usb_endpoint_descriptor |
| 147 | pn_fs_source_desc = { |
| 148 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 149 | .bDescriptorType = USB_DT_ENDPOINT, |
| 150 | |
| 151 | .bEndpointAddress = USB_DIR_IN, |
| 152 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 153 | }; |
| 154 | |
| 155 | static struct usb_endpoint_descriptor |
| 156 | pn_hs_source_desc = { |
| 157 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 158 | .bDescriptorType = USB_DT_ENDPOINT, |
| 159 | |
| 160 | .bEndpointAddress = USB_DIR_IN, |
| 161 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 162 | .wMaxPacketSize = cpu_to_le16(512), |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static struct usb_descriptor_header *fs_pn_function[] = { |
| 166 | (struct usb_descriptor_header *) &pn_control_intf_desc, |
| 167 | (struct usb_descriptor_header *) &pn_header_desc, |
| 168 | (struct usb_descriptor_header *) &pn_phonet_desc, |
| 169 | (struct usb_descriptor_header *) &pn_union_desc, |
| 170 | (struct usb_descriptor_header *) &pn_data_nop_intf_desc, |
| 171 | (struct usb_descriptor_header *) &pn_data_intf_desc, |
| 172 | (struct usb_descriptor_header *) &pn_fs_sink_desc, |
| 173 | (struct usb_descriptor_header *) &pn_fs_source_desc, |
| 174 | NULL, |
| 175 | }; |
| 176 | |
| 177 | static struct usb_descriptor_header *hs_pn_function[] = { |
| 178 | (struct usb_descriptor_header *) &pn_control_intf_desc, |
| 179 | (struct usb_descriptor_header *) &pn_header_desc, |
| 180 | (struct usb_descriptor_header *) &pn_phonet_desc, |
| 181 | (struct usb_descriptor_header *) &pn_union_desc, |
| 182 | (struct usb_descriptor_header *) &pn_data_nop_intf_desc, |
| 183 | (struct usb_descriptor_header *) &pn_data_intf_desc, |
| 184 | (struct usb_descriptor_header *) &pn_hs_sink_desc, |
| 185 | (struct usb_descriptor_header *) &pn_hs_source_desc, |
| 186 | NULL, |
| 187 | }; |
| 188 | |
| 189 | /*-------------------------------------------------------------------------*/ |
| 190 | |
| 191 | static int pn_net_open(struct net_device *dev) |
| 192 | { |
Rémi Denis-Courmont | c69367f | 2009-06-01 01:18:56 +0000 | [diff] [blame] | 193 | netif_wake_queue(dev); |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int pn_net_close(struct net_device *dev) |
| 198 | { |
| 199 | netif_stop_queue(dev); |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static void pn_tx_complete(struct usb_ep *ep, struct usb_request *req) |
| 204 | { |
| 205 | struct f_phonet *fp = ep->driver_data; |
| 206 | struct net_device *dev = fp->dev; |
| 207 | struct sk_buff *skb = req->context; |
| 208 | |
| 209 | switch (req->status) { |
| 210 | case 0: |
| 211 | dev->stats.tx_packets++; |
| 212 | dev->stats.tx_bytes += skb->len; |
| 213 | break; |
| 214 | |
| 215 | case -ESHUTDOWN: /* disconnected */ |
| 216 | case -ECONNRESET: /* disabled */ |
| 217 | dev->stats.tx_aborted_errors++; |
| 218 | default: |
| 219 | dev->stats.tx_errors++; |
| 220 | } |
| 221 | |
| 222 | dev_kfree_skb_any(skb); |
Rémi Denis-Courmont | c69367f | 2009-06-01 01:18:56 +0000 | [diff] [blame] | 223 | netif_wake_queue(dev); |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static int pn_net_xmit(struct sk_buff *skb, struct net_device *dev) |
| 227 | { |
| 228 | struct phonet_port *port = netdev_priv(dev); |
| 229 | struct f_phonet *fp; |
| 230 | struct usb_request *req; |
| 231 | unsigned long flags; |
| 232 | |
| 233 | if (skb->protocol != htons(ETH_P_PHONET)) |
| 234 | goto out; |
| 235 | |
| 236 | spin_lock_irqsave(&port->lock, flags); |
| 237 | fp = port->usb; |
| 238 | if (unlikely(!fp)) /* race with carrier loss */ |
| 239 | goto out_unlock; |
| 240 | |
| 241 | req = fp->in_req; |
| 242 | req->buf = skb->data; |
| 243 | req->length = skb->len; |
| 244 | req->complete = pn_tx_complete; |
| 245 | req->zero = 1; |
| 246 | req->context = skb; |
| 247 | |
| 248 | if (unlikely(usb_ep_queue(fp->in_ep, req, GFP_ATOMIC))) |
| 249 | goto out_unlock; |
| 250 | |
| 251 | netif_stop_queue(dev); |
| 252 | skb = NULL; |
| 253 | |
| 254 | out_unlock: |
| 255 | spin_unlock_irqrestore(&port->lock, flags); |
| 256 | out: |
| 257 | if (unlikely(skb)) { |
Rémi Denis-Courmont | fa20259 | 2009-06-01 01:18:55 +0000 | [diff] [blame] | 258 | dev_kfree_skb(skb); |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 259 | dev->stats.tx_dropped++; |
| 260 | } |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 261 | return NETDEV_TX_OK; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Stephen Hemminger | ab638e6 | 2009-01-07 17:24:34 -0800 | [diff] [blame] | 264 | static const struct net_device_ops pn_netdev_ops = { |
| 265 | .ndo_open = pn_net_open, |
| 266 | .ndo_stop = pn_net_close, |
| 267 | .ndo_start_xmit = pn_net_xmit, |
Stephen Hemminger | ab638e6 | 2009-01-07 17:24:34 -0800 | [diff] [blame] | 268 | }; |
| 269 | |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 270 | static void pn_net_setup(struct net_device *dev) |
| 271 | { |
| 272 | dev->features = 0; |
| 273 | dev->type = ARPHRD_PHONET; |
| 274 | dev->flags = IFF_POINTOPOINT | IFF_NOARP; |
| 275 | dev->mtu = PHONET_DEV_MTU; |
Jarod Wilson | b3e3893 | 2016-10-20 13:55:22 -0400 | [diff] [blame] | 276 | dev->min_mtu = PHONET_MIN_MTU; |
| 277 | dev->max_mtu = PHONET_MAX_MTU; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 278 | dev->hard_header_len = 1; |
| 279 | dev->dev_addr[0] = PN_MEDIA_USB; |
| 280 | dev->addr_len = 1; |
| 281 | dev->tx_queue_len = 1; |
| 282 | |
Stephen Hemminger | ab638e6 | 2009-01-07 17:24:34 -0800 | [diff] [blame] | 283 | dev->netdev_ops = &pn_netdev_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 284 | dev->needs_free_netdev = true; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 285 | dev->header_ops = &phonet_header_ops; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /*-------------------------------------------------------------------------*/ |
| 289 | |
| 290 | /* |
| 291 | * Queue buffer for data from the host |
| 292 | */ |
| 293 | static int |
| 294 | pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags) |
| 295 | { |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 296 | struct page *page; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 297 | int err; |
| 298 | |
Alexander Duyck | 5693d28 | 2014-11-11 09:26:50 -0800 | [diff] [blame] | 299 | page = __dev_alloc_page(gfp_flags | __GFP_NOMEMALLOC); |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 300 | if (!page) |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 301 | return -ENOMEM; |
| 302 | |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 303 | req->buf = page_address(page); |
| 304 | req->length = PAGE_SIZE; |
| 305 | req->context = page; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 306 | |
| 307 | err = usb_ep_queue(fp->out_ep, req, gfp_flags); |
| 308 | if (unlikely(err)) |
Eric Dumazet | 1f2149c | 2011-11-22 10:57:41 +0000 | [diff] [blame] | 309 | put_page(page); |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 310 | return err; |
| 311 | } |
| 312 | |
| 313 | static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req) |
| 314 | { |
| 315 | struct f_phonet *fp = ep->driver_data; |
| 316 | struct net_device *dev = fp->dev; |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 317 | struct page *page = req->context; |
| 318 | struct sk_buff *skb; |
| 319 | unsigned long flags; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 320 | int status = req->status; |
| 321 | |
| 322 | switch (status) { |
| 323 | case 0: |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 324 | spin_lock_irqsave(&fp->rx.lock, flags); |
| 325 | skb = fp->rx.skb; |
| 326 | if (!skb) |
| 327 | skb = fp->rx.skb = netdev_alloc_skb(dev, 12); |
| 328 | if (req->actual < req->length) /* Last fragment */ |
| 329 | fp->rx.skb = NULL; |
| 330 | spin_unlock_irqrestore(&fp->rx.lock, flags); |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 331 | |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 332 | if (unlikely(!skb)) |
| 333 | break; |
Rémi Denis-Courmont | f5a4532 | 2011-02-23 02:51:33 +0000 | [diff] [blame] | 334 | |
| 335 | if (skb->len == 0) { /* First fragment */ |
| 336 | skb->protocol = htons(ETH_P_PHONET); |
| 337 | skb_reset_mac_header(skb); |
| 338 | /* Can't use pskb_pull() on page in IRQ */ |
| 339 | memcpy(skb_put(skb, 1), page_address(page), 1); |
| 340 | } |
| 341 | |
| 342 | skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, |
Eric Dumazet | 21dcda6 | 2012-03-27 03:04:02 +0000 | [diff] [blame] | 343 | skb->len <= 1, req->actual, PAGE_SIZE); |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 344 | page = NULL; |
| 345 | |
| 346 | if (req->actual < req->length) { /* Last fragment */ |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 347 | skb->dev = dev; |
| 348 | dev->stats.rx_packets++; |
| 349 | dev->stats.rx_bytes += skb->len; |
| 350 | |
| 351 | netif_rx(skb); |
| 352 | } |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 353 | break; |
| 354 | |
| 355 | /* Do not resubmit in these cases: */ |
| 356 | case -ESHUTDOWN: /* disconnect */ |
| 357 | case -ECONNABORTED: /* hw reset */ |
| 358 | case -ECONNRESET: /* dequeued (unlink or netif down) */ |
| 359 | req = NULL; |
| 360 | break; |
| 361 | |
| 362 | /* Do resubmit in these cases: */ |
| 363 | case -EOVERFLOW: /* request buffer overflow */ |
| 364 | dev->stats.rx_over_errors++; |
| 365 | default: |
| 366 | dev->stats.rx_errors++; |
| 367 | break; |
| 368 | } |
| 369 | |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 370 | if (page) |
Eric Dumazet | 1f2149c | 2011-11-22 10:57:41 +0000 | [diff] [blame] | 371 | put_page(page); |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 372 | if (req) |
Alexander Duyck | 5693d28 | 2014-11-11 09:26:50 -0800 | [diff] [blame] | 373 | pn_rx_submit(fp, req, GFP_ATOMIC); |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /*-------------------------------------------------------------------------*/ |
| 377 | |
| 378 | static void __pn_reset(struct usb_function *f) |
| 379 | { |
| 380 | struct f_phonet *fp = func_to_pn(f); |
| 381 | struct net_device *dev = fp->dev; |
| 382 | struct phonet_port *port = netdev_priv(dev); |
| 383 | |
| 384 | netif_carrier_off(dev); |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 385 | port->usb = NULL; |
| 386 | |
| 387 | usb_ep_disable(fp->out_ep); |
| 388 | usb_ep_disable(fp->in_ep); |
Rémi Denis-Courmont | b91cd14 | 2009-08-06 21:56:44 +0000 | [diff] [blame] | 389 | if (fp->rx.skb) { |
| 390 | dev_kfree_skb_irq(fp->rx.skb); |
| 391 | fp->rx.skb = NULL; |
| 392 | } |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 396 | { |
| 397 | struct f_phonet *fp = func_to_pn(f); |
| 398 | struct usb_gadget *gadget = fp->function.config->cdev->gadget; |
| 399 | |
| 400 | if (intf == pn_control_intf_desc.bInterfaceNumber) |
| 401 | /* control interface, no altsetting */ |
| 402 | return (alt > 0) ? -EINVAL : 0; |
| 403 | |
| 404 | if (intf == pn_data_intf_desc.bInterfaceNumber) { |
| 405 | struct net_device *dev = fp->dev; |
| 406 | struct phonet_port *port = netdev_priv(dev); |
| 407 | |
| 408 | /* data intf (0: inactive, 1: active) */ |
| 409 | if (alt > 1) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | spin_lock(&port->lock); |
Felipe Balbi | 9ec36f7 | 2015-02-02 16:24:17 -0600 | [diff] [blame] | 413 | |
Robert Baldyga | 101382f | 2015-09-16 12:10:52 +0200 | [diff] [blame] | 414 | if (fp->in_ep->enabled) |
Felipe Balbi | 9ec36f7 | 2015-02-02 16:24:17 -0600 | [diff] [blame] | 415 | __pn_reset(f); |
| 416 | |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 417 | if (alt == 1) { |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 418 | int i; |
| 419 | |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 420 | if (config_ep_by_speed(gadget, f, fp->in_ep) || |
| 421 | config_ep_by_speed(gadget, f, fp->out_ep)) { |
| 422 | fp->in_ep->desc = NULL; |
| 423 | fp->out_ep->desc = NULL; |
Sebastian Andrzej Siewior | bb8070c | 2011-08-05 22:14:46 +0200 | [diff] [blame] | 424 | spin_unlock(&port->lock); |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 425 | return -EINVAL; |
| 426 | } |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 427 | usb_ep_enable(fp->out_ep); |
| 428 | usb_ep_enable(fp->in_ep); |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 429 | |
| 430 | port->usb = fp; |
| 431 | fp->out_ep->driver_data = fp; |
| 432 | fp->in_ep->driver_data = fp; |
| 433 | |
| 434 | netif_carrier_on(dev); |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 435 | for (i = 0; i < phonet_rxq_size; i++) |
Alexander Duyck | 5693d28 | 2014-11-11 09:26:50 -0800 | [diff] [blame] | 436 | pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC); |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 437 | } |
| 438 | spin_unlock(&port->lock); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | return -EINVAL; |
| 443 | } |
| 444 | |
| 445 | static int pn_get_alt(struct usb_function *f, unsigned intf) |
| 446 | { |
| 447 | struct f_phonet *fp = func_to_pn(f); |
| 448 | |
| 449 | if (intf == pn_control_intf_desc.bInterfaceNumber) |
| 450 | return 0; |
| 451 | |
| 452 | if (intf == pn_data_intf_desc.bInterfaceNumber) { |
| 453 | struct phonet_port *port = netdev_priv(fp->dev); |
| 454 | u8 alt; |
| 455 | |
| 456 | spin_lock(&port->lock); |
| 457 | alt = port->usb != NULL; |
| 458 | spin_unlock(&port->lock); |
| 459 | return alt; |
| 460 | } |
| 461 | |
| 462 | return -EINVAL; |
| 463 | } |
| 464 | |
| 465 | static void pn_disconnect(struct usb_function *f) |
| 466 | { |
| 467 | struct f_phonet *fp = func_to_pn(f); |
| 468 | struct phonet_port *port = netdev_priv(fp->dev); |
| 469 | unsigned long flags; |
| 470 | |
| 471 | /* remain disabled until set_alt */ |
| 472 | spin_lock_irqsave(&port->lock, flags); |
| 473 | __pn_reset(f); |
| 474 | spin_unlock_irqrestore(&port->lock, flags); |
| 475 | } |
| 476 | |
| 477 | /*-------------------------------------------------------------------------*/ |
| 478 | |
Andrzej Pietrasiewicz | fcbdf12 | 2013-05-23 10:51:11 +0200 | [diff] [blame] | 479 | static int pn_bind(struct usb_configuration *c, struct usb_function *f) |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 480 | { |
| 481 | struct usb_composite_dev *cdev = c->cdev; |
| 482 | struct usb_gadget *gadget = cdev->gadget; |
| 483 | struct f_phonet *fp = func_to_pn(f); |
| 484 | struct usb_ep *ep; |
| 485 | int status, i; |
| 486 | |
Andrzej Pietrasiewicz | fcbdf12 | 2013-05-23 10:51:11 +0200 | [diff] [blame] | 487 | struct f_phonet_opts *phonet_opts; |
| 488 | |
| 489 | phonet_opts = container_of(f->fi, struct f_phonet_opts, func_inst); |
| 490 | |
| 491 | /* |
| 492 | * in drivers/usb/gadget/configfs.c:configfs_composite_bind() |
| 493 | * configurations are bound in sequence with list_for_each_entry, |
| 494 | * in each configuration its functions are bound in sequence |
| 495 | * with list_for_each_entry, so we assume no race condition |
| 496 | * with regard to phonet_opts->bound access |
| 497 | */ |
| 498 | if (!phonet_opts->bound) { |
| 499 | gphonet_set_gadget(phonet_opts->net, gadget); |
| 500 | status = gphonet_register_netdev(phonet_opts->net); |
| 501 | if (status) |
| 502 | return status; |
| 503 | phonet_opts->bound = true; |
| 504 | } |
Andrzej Pietrasiewicz | fcbdf12 | 2013-05-23 10:51:11 +0200 | [diff] [blame] | 505 | |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 506 | /* Reserve interface IDs */ |
| 507 | status = usb_interface_id(c, f); |
| 508 | if (status < 0) |
| 509 | goto err; |
| 510 | pn_control_intf_desc.bInterfaceNumber = status; |
| 511 | pn_union_desc.bMasterInterface0 = status; |
| 512 | |
| 513 | status = usb_interface_id(c, f); |
| 514 | if (status < 0) |
| 515 | goto err; |
| 516 | pn_data_nop_intf_desc.bInterfaceNumber = status; |
| 517 | pn_data_intf_desc.bInterfaceNumber = status; |
| 518 | pn_union_desc.bSlaveInterface0 = status; |
| 519 | |
| 520 | /* Reserve endpoints */ |
| 521 | status = -ENODEV; |
| 522 | ep = usb_ep_autoconfig(gadget, &pn_fs_sink_desc); |
| 523 | if (!ep) |
| 524 | goto err; |
| 525 | fp->out_ep = ep; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 526 | |
| 527 | ep = usb_ep_autoconfig(gadget, &pn_fs_source_desc); |
| 528 | if (!ep) |
| 529 | goto err; |
| 530 | fp->in_ep = ep; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 531 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 532 | pn_hs_sink_desc.bEndpointAddress = pn_fs_sink_desc.bEndpointAddress; |
| 533 | pn_hs_source_desc.bEndpointAddress = pn_fs_source_desc.bEndpointAddress; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 534 | |
| 535 | /* Do not try to bind Phonet twice... */ |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 536 | status = usb_assign_descriptors(f, fs_pn_function, hs_pn_function, |
John Youn | eaef50c | 2016-02-05 17:06:07 -0800 | [diff] [blame] | 537 | NULL, NULL); |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 538 | if (status) |
| 539 | goto err; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 540 | |
| 541 | /* Incoming USB requests */ |
| 542 | status = -ENOMEM; |
| 543 | for (i = 0; i < phonet_rxq_size; i++) { |
| 544 | struct usb_request *req; |
| 545 | |
| 546 | req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL); |
| 547 | if (!req) |
Sebastian Andrzej Siewior | d0eca71 | 2012-10-22 22:15:04 +0200 | [diff] [blame] | 548 | goto err_req; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 549 | |
| 550 | req->complete = pn_rx_complete; |
| 551 | fp->out_reqv[i] = req; |
| 552 | } |
| 553 | |
| 554 | /* Outgoing USB requests */ |
| 555 | fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL); |
| 556 | if (!fp->in_req) |
Sebastian Andrzej Siewior | d0eca71 | 2012-10-22 22:15:04 +0200 | [diff] [blame] | 557 | goto err_req; |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 558 | |
| 559 | INFO(cdev, "USB CDC Phonet function\n"); |
| 560 | INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name, |
| 561 | fp->out_ep->name, fp->in_ep->name); |
| 562 | return 0; |
| 563 | |
Sebastian Andrzej Siewior | d0eca71 | 2012-10-22 22:15:04 +0200 | [diff] [blame] | 564 | err_req: |
| 565 | for (i = 0; i < phonet_rxq_size && fp->out_reqv[i]; i++) |
| 566 | usb_ep_free_request(fp->out_ep, fp->out_reqv[i]); |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 567 | usb_free_all_descriptors(f); |
Pavitrakumar Managutte | d12a872 | 2014-10-22 19:24:58 +0530 | [diff] [blame] | 568 | err: |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 569 | ERROR(cdev, "USB CDC Phonet: cannot autoconfigure\n"); |
| 570 | return status; |
| 571 | } |
| 572 | |
Andrzej Pietrasiewicz | 8340874 | 2013-05-23 10:51:15 +0200 | [diff] [blame] | 573 | static inline struct f_phonet_opts *to_f_phonet_opts(struct config_item *item) |
| 574 | { |
| 575 | return container_of(to_config_group(item), struct f_phonet_opts, |
| 576 | func_inst.group); |
| 577 | } |
| 578 | |
Andrzej Pietrasiewicz | 8340874 | 2013-05-23 10:51:15 +0200 | [diff] [blame] | 579 | static void phonet_attr_release(struct config_item *item) |
| 580 | { |
| 581 | struct f_phonet_opts *opts = to_f_phonet_opts(item); |
| 582 | |
| 583 | usb_put_function_instance(&opts->func_inst); |
| 584 | } |
| 585 | |
| 586 | static struct configfs_item_operations phonet_item_ops = { |
| 587 | .release = phonet_attr_release, |
Andrzej Pietrasiewicz | 8340874 | 2013-05-23 10:51:15 +0200 | [diff] [blame] | 588 | }; |
| 589 | |
Christoph Hellwig | 0736390 | 2015-10-03 15:32:51 +0200 | [diff] [blame] | 590 | static ssize_t f_phonet_ifname_show(struct config_item *item, char *page) |
Andrzej Pietrasiewicz | 8340874 | 2013-05-23 10:51:15 +0200 | [diff] [blame] | 591 | { |
Christoph Hellwig | 0736390 | 2015-10-03 15:32:51 +0200 | [diff] [blame] | 592 | return gether_get_ifname(to_f_phonet_opts(item)->net, page, PAGE_SIZE); |
Andrzej Pietrasiewicz | 8340874 | 2013-05-23 10:51:15 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Christoph Hellwig | 0736390 | 2015-10-03 15:32:51 +0200 | [diff] [blame] | 595 | CONFIGFS_ATTR_RO(f_phonet_, ifname); |
Andrzej Pietrasiewicz | 8340874 | 2013-05-23 10:51:15 +0200 | [diff] [blame] | 596 | |
| 597 | static struct configfs_attribute *phonet_attrs[] = { |
Christoph Hellwig | 0736390 | 2015-10-03 15:32:51 +0200 | [diff] [blame] | 598 | &f_phonet_attr_ifname, |
Andrzej Pietrasiewicz | 8340874 | 2013-05-23 10:51:15 +0200 | [diff] [blame] | 599 | NULL, |
| 600 | }; |
| 601 | |
| 602 | static struct config_item_type phonet_func_type = { |
| 603 | .ct_item_ops = &phonet_item_ops, |
| 604 | .ct_attrs = phonet_attrs, |
| 605 | .ct_owner = THIS_MODULE, |
| 606 | }; |
| 607 | |
Andrzej Pietrasiewicz | fcbdf12 | 2013-05-23 10:51:11 +0200 | [diff] [blame] | 608 | static void phonet_free_inst(struct usb_function_instance *f) |
| 609 | { |
| 610 | struct f_phonet_opts *opts; |
| 611 | |
| 612 | opts = container_of(f, struct f_phonet_opts, func_inst); |
| 613 | if (opts->bound) |
| 614 | gphonet_cleanup(opts->net); |
| 615 | else |
| 616 | free_netdev(opts->net); |
| 617 | kfree(opts); |
| 618 | } |
| 619 | |
| 620 | static struct usb_function_instance *phonet_alloc_inst(void) |
| 621 | { |
| 622 | struct f_phonet_opts *opts; |
| 623 | |
| 624 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 625 | if (!opts) |
| 626 | return ERR_PTR(-ENOMEM); |
| 627 | |
| 628 | opts->func_inst.free_func_inst = phonet_free_inst; |
| 629 | opts->net = gphonet_setup_default(); |
Andrzej Pietrasiewicz | 3b45b2a | 2013-07-25 09:13:18 +0200 | [diff] [blame] | 630 | if (IS_ERR(opts->net)) { |
| 631 | struct net_device *net = opts->net; |
| 632 | kfree(opts); |
| 633 | return ERR_CAST(net); |
| 634 | } |
Andrzej Pietrasiewicz | fcbdf12 | 2013-05-23 10:51:11 +0200 | [diff] [blame] | 635 | |
Andrzej Pietrasiewicz | 8340874 | 2013-05-23 10:51:15 +0200 | [diff] [blame] | 636 | config_group_init_type_name(&opts->func_inst.group, "", |
| 637 | &phonet_func_type); |
| 638 | |
Andrzej Pietrasiewicz | fcbdf12 | 2013-05-23 10:51:11 +0200 | [diff] [blame] | 639 | return &opts->func_inst; |
| 640 | } |
| 641 | |
| 642 | static void phonet_free(struct usb_function *f) |
| 643 | { |
| 644 | struct f_phonet *phonet; |
| 645 | |
| 646 | phonet = func_to_pn(f); |
| 647 | kfree(phonet); |
| 648 | } |
| 649 | |
| 650 | static void pn_unbind(struct usb_configuration *c, struct usb_function *f) |
| 651 | { |
| 652 | struct f_phonet *fp = func_to_pn(f); |
| 653 | int i; |
| 654 | |
| 655 | /* We are already disconnected */ |
| 656 | if (fp->in_req) |
| 657 | usb_ep_free_request(fp->in_ep, fp->in_req); |
| 658 | for (i = 0; i < phonet_rxq_size; i++) |
| 659 | if (fp->out_reqv[i]) |
| 660 | usb_ep_free_request(fp->out_ep, fp->out_reqv[i]); |
| 661 | |
| 662 | usb_free_all_descriptors(f); |
| 663 | } |
| 664 | |
Jingoo Han | c3af822 | 2013-12-16 18:44:07 +0900 | [diff] [blame] | 665 | static struct usb_function *phonet_alloc(struct usb_function_instance *fi) |
Andrzej Pietrasiewicz | fcbdf12 | 2013-05-23 10:51:11 +0200 | [diff] [blame] | 666 | { |
| 667 | struct f_phonet *fp; |
| 668 | struct f_phonet_opts *opts; |
| 669 | int size; |
| 670 | |
| 671 | size = sizeof(*fp) + (phonet_rxq_size * sizeof(struct usb_request *)); |
| 672 | fp = kzalloc(size, GFP_KERNEL); |
| 673 | if (!fp) |
| 674 | return ERR_PTR(-ENOMEM); |
| 675 | |
| 676 | opts = container_of(fi, struct f_phonet_opts, func_inst); |
| 677 | |
| 678 | fp->dev = opts->net; |
| 679 | fp->function.name = "phonet"; |
| 680 | fp->function.bind = pn_bind; |
| 681 | fp->function.unbind = pn_unbind; |
| 682 | fp->function.set_alt = pn_set_alt; |
| 683 | fp->function.get_alt = pn_get_alt; |
| 684 | fp->function.disable = pn_disconnect; |
| 685 | fp->function.free_func = phonet_free; |
| 686 | spin_lock_init(&fp->rx.lock); |
| 687 | |
| 688 | return &fp->function; |
| 689 | } |
| 690 | |
| 691 | struct net_device *gphonet_setup_default(void) |
| 692 | { |
| 693 | struct net_device *dev; |
| 694 | struct phonet_port *port; |
| 695 | |
| 696 | /* Create net device */ |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 697 | dev = alloc_netdev(sizeof(*port), "upnlink%d", NET_NAME_UNKNOWN, |
| 698 | pn_net_setup); |
Andrzej Pietrasiewicz | fcbdf12 | 2013-05-23 10:51:11 +0200 | [diff] [blame] | 699 | if (!dev) |
| 700 | return ERR_PTR(-ENOMEM); |
| 701 | |
| 702 | port = netdev_priv(dev); |
| 703 | spin_lock_init(&port->lock); |
| 704 | netif_carrier_off(dev); |
| 705 | |
| 706 | return dev; |
| 707 | } |
| 708 | |
| 709 | void gphonet_set_gadget(struct net_device *net, struct usb_gadget *g) |
| 710 | { |
| 711 | SET_NETDEV_DEV(net, &g->dev); |
| 712 | } |
| 713 | |
| 714 | int gphonet_register_netdev(struct net_device *net) |
| 715 | { |
| 716 | int status; |
| 717 | |
| 718 | status = register_netdev(net); |
| 719 | if (status) |
| 720 | free_netdev(net); |
| 721 | |
| 722 | return status; |
| 723 | } |
| 724 | |
Andrzej Pietrasiewicz | 0189e63 | 2013-05-23 10:51:10 +0200 | [diff] [blame] | 725 | void gphonet_cleanup(struct net_device *dev) |
Rémi Denis-Courmont | 9732d52 | 2008-12-17 15:49:09 -0800 | [diff] [blame] | 726 | { |
| 727 | unregister_netdev(dev); |
| 728 | } |
Andrzej Pietrasiewicz | 0383070 | 2013-05-23 10:51:13 +0200 | [diff] [blame] | 729 | |
| 730 | DECLARE_USB_FUNCTION_INIT(phonet, phonet_alloc_inst, phonet_alloc); |
| 731 | MODULE_AUTHOR("Rémi Denis-Courmont"); |
| 732 | MODULE_LICENSE("GPL"); |