Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* drivers/usb/gadget/f_diag.c |
| 2 | * Diag Function Device - Route ARM9 and ARM11 DIAG messages |
| 3 | * between HOST and DEVICE. |
| 4 | * Copyright (C) 2007 Google, Inc. |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 5 | * Copyright (c) 2008-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 6 | * Author: Brian Swetland <swetland@google.com> |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/platform_device.h> |
Pavankumar Kondeti | ac3aa91 | 2012-11-26 14:52:37 +0530 | [diff] [blame] | 21 | #include <linux/ratelimit.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | |
| 23 | #include <mach/usbdiag.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/usb/composite.h> |
| 26 | #include <linux/usb/gadget.h> |
| 27 | #include <linux/workqueue.h> |
| 28 | #include <linux/debugfs.h> |
| 29 | |
| 30 | static DEFINE_SPINLOCK(ch_lock); |
| 31 | static LIST_HEAD(usb_diag_ch_list); |
| 32 | |
| 33 | static struct usb_interface_descriptor intf_desc = { |
| 34 | .bLength = sizeof intf_desc, |
| 35 | .bDescriptorType = USB_DT_INTERFACE, |
| 36 | .bNumEndpoints = 2, |
| 37 | .bInterfaceClass = 0xFF, |
| 38 | .bInterfaceSubClass = 0xFF, |
| 39 | .bInterfaceProtocol = 0xFF, |
| 40 | }; |
| 41 | |
| 42 | static struct usb_endpoint_descriptor hs_bulk_in_desc = { |
| 43 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 44 | .bDescriptorType = USB_DT_ENDPOINT, |
| 45 | .bEndpointAddress = USB_DIR_IN, |
| 46 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 47 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 48 | .bInterval = 0, |
| 49 | }; |
| 50 | static struct usb_endpoint_descriptor fs_bulk_in_desc = { |
| 51 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 52 | .bDescriptorType = USB_DT_ENDPOINT, |
| 53 | .bEndpointAddress = USB_DIR_IN, |
| 54 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 55 | .wMaxPacketSize = __constant_cpu_to_le16(64), |
| 56 | .bInterval = 0, |
| 57 | }; |
| 58 | |
| 59 | static struct usb_endpoint_descriptor hs_bulk_out_desc = { |
| 60 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 61 | .bDescriptorType = USB_DT_ENDPOINT, |
| 62 | .bEndpointAddress = USB_DIR_OUT, |
| 63 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 64 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 65 | .bInterval = 0, |
| 66 | }; |
| 67 | |
| 68 | static struct usb_endpoint_descriptor fs_bulk_out_desc = { |
| 69 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 70 | .bDescriptorType = USB_DT_ENDPOINT, |
| 71 | .bEndpointAddress = USB_DIR_OUT, |
| 72 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 73 | .wMaxPacketSize = __constant_cpu_to_le16(64), |
| 74 | .bInterval = 0, |
| 75 | }; |
| 76 | |
Pavankumar Kondeti | 6f94bc9 | 2012-08-03 09:34:32 +0530 | [diff] [blame] | 77 | static struct usb_endpoint_descriptor ss_bulk_in_desc = { |
| 78 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 79 | .bDescriptorType = USB_DT_ENDPOINT, |
| 80 | .bEndpointAddress = USB_DIR_IN, |
| 81 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 82 | .wMaxPacketSize = __constant_cpu_to_le16(1024), |
| 83 | }; |
| 84 | |
| 85 | static struct usb_ss_ep_comp_descriptor ss_bulk_in_comp_desc = { |
| 86 | .bLength = sizeof ss_bulk_in_comp_desc, |
| 87 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 88 | |
| 89 | /* the following 2 values can be tweaked if necessary */ |
| 90 | /* .bMaxBurst = 0, */ |
| 91 | /* .bmAttributes = 0, */ |
| 92 | }; |
| 93 | |
| 94 | static struct usb_endpoint_descriptor ss_bulk_out_desc = { |
| 95 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 96 | .bDescriptorType = USB_DT_ENDPOINT, |
| 97 | .bEndpointAddress = USB_DIR_OUT, |
| 98 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 99 | .wMaxPacketSize = __constant_cpu_to_le16(1024), |
| 100 | }; |
| 101 | |
| 102 | static struct usb_ss_ep_comp_descriptor ss_bulk_out_comp_desc = { |
| 103 | .bLength = sizeof ss_bulk_out_comp_desc, |
| 104 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 105 | |
| 106 | /* the following 2 values can be tweaked if necessary */ |
| 107 | /* .bMaxBurst = 0, */ |
| 108 | /* .bmAttributes = 0, */ |
| 109 | }; |
| 110 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 111 | static struct usb_descriptor_header *fs_diag_desc[] = { |
| 112 | (struct usb_descriptor_header *) &intf_desc, |
| 113 | (struct usb_descriptor_header *) &fs_bulk_in_desc, |
| 114 | (struct usb_descriptor_header *) &fs_bulk_out_desc, |
| 115 | NULL, |
| 116 | }; |
| 117 | static struct usb_descriptor_header *hs_diag_desc[] = { |
| 118 | (struct usb_descriptor_header *) &intf_desc, |
| 119 | (struct usb_descriptor_header *) &hs_bulk_in_desc, |
| 120 | (struct usb_descriptor_header *) &hs_bulk_out_desc, |
| 121 | NULL, |
| 122 | }; |
| 123 | |
Pavankumar Kondeti | 6f94bc9 | 2012-08-03 09:34:32 +0530 | [diff] [blame] | 124 | static struct usb_descriptor_header *ss_diag_desc[] = { |
| 125 | (struct usb_descriptor_header *) &intf_desc, |
| 126 | (struct usb_descriptor_header *) &ss_bulk_in_desc, |
| 127 | (struct usb_descriptor_header *) &ss_bulk_in_comp_desc, |
| 128 | (struct usb_descriptor_header *) &ss_bulk_out_desc, |
| 129 | (struct usb_descriptor_header *) &ss_bulk_out_comp_desc, |
| 130 | NULL, |
| 131 | }; |
| 132 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | /** |
| 134 | * struct diag_context - USB diag function driver private structure |
| 135 | * @function: function structure for USB interface |
| 136 | * @out: USB OUT endpoint struct |
| 137 | * @in: USB IN endpoint struct |
| 138 | * @in_desc: USB IN endpoint descriptor struct |
| 139 | * @out_desc: USB OUT endpoint descriptor struct |
| 140 | * @read_pool: List of requests used for Rx (OUT ep) |
| 141 | * @write_pool: List of requests used for Tx (IN ep) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 142 | * @lock: Spinlock to proctect read_pool, write_pool lists |
| 143 | * @cdev: USB composite device struct |
| 144 | * @ch: USB diag channel |
| 145 | * |
| 146 | */ |
| 147 | struct diag_context { |
| 148 | struct usb_function function; |
| 149 | struct usb_ep *out; |
| 150 | struct usb_ep *in; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 151 | struct list_head read_pool; |
| 152 | struct list_head write_pool; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 153 | spinlock_t lock; |
| 154 | unsigned configured; |
| 155 | struct usb_composite_dev *cdev; |
| 156 | int (*update_pid_and_serial_num)(uint32_t, const char *); |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 157 | struct usb_diag_ch *ch; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 158 | |
| 159 | /* pkt counters */ |
| 160 | unsigned long dpkts_tolaptop; |
| 161 | unsigned long dpkts_tomodem; |
| 162 | unsigned dpkts_tolaptop_pending; |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 163 | |
| 164 | /* A list node inside the diag_dev_list */ |
| 165 | struct list_head list_item; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 168 | static struct list_head diag_dev_list; |
| 169 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 170 | static inline struct diag_context *func_to_diag(struct usb_function *f) |
| 171 | { |
| 172 | return container_of(f, struct diag_context, function); |
| 173 | } |
| 174 | |
Pavankumar Kondeti | c358658 | 2013-04-18 11:02:28 +0530 | [diff] [blame^] | 175 | static void diag_update_pid_and_serial_num(struct diag_context *ctxt) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 176 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 177 | struct usb_composite_dev *cdev = ctxt->cdev; |
| 178 | struct usb_gadget_strings *table; |
| 179 | struct usb_string *s; |
| 180 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 181 | if (!ctxt->update_pid_and_serial_num) |
| 182 | return; |
| 183 | |
| 184 | /* pass on product id and serial number to dload */ |
| 185 | if (!cdev->desc.iSerialNumber) { |
| 186 | ctxt->update_pid_and_serial_num( |
| 187 | cdev->desc.idProduct, 0); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Serial number is filled by the composite driver. So |
| 193 | * it is fair enough to assume that it will always be |
| 194 | * found at first table of strings. |
| 195 | */ |
| 196 | table = *(cdev->driver->strings); |
| 197 | for (s = table->strings; s && s->s; s++) |
| 198 | if (s->id == cdev->desc.iSerialNumber) { |
| 199 | ctxt->update_pid_and_serial_num( |
| 200 | cdev->desc.idProduct, s->s); |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static void diag_write_complete(struct usb_ep *ep, |
| 206 | struct usb_request *req) |
| 207 | { |
| 208 | struct diag_context *ctxt = ep->driver_data; |
| 209 | struct diag_request *d_req = req->context; |
| 210 | unsigned long flags; |
| 211 | |
| 212 | ctxt->dpkts_tolaptop_pending--; |
| 213 | |
| 214 | if (!req->status) { |
| 215 | if ((req->length >= ep->maxpacket) && |
| 216 | ((req->length % ep->maxpacket) == 0)) { |
| 217 | ctxt->dpkts_tolaptop_pending++; |
| 218 | req->length = 0; |
| 219 | d_req->actual = req->actual; |
| 220 | d_req->status = req->status; |
| 221 | /* Queue zero length packet */ |
| 222 | usb_ep_queue(ctxt->in, req, GFP_ATOMIC); |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | spin_lock_irqsave(&ctxt->lock, flags); |
| 228 | list_add_tail(&req->list, &ctxt->write_pool); |
| 229 | if (req->length != 0) { |
| 230 | d_req->actual = req->actual; |
| 231 | d_req->status = req->status; |
| 232 | } |
| 233 | spin_unlock_irqrestore(&ctxt->lock, flags); |
| 234 | |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 235 | if (ctxt->ch && ctxt->ch->notify) |
| 236 | ctxt->ch->notify(ctxt->ch->priv, USB_DIAG_WRITE_DONE, d_req); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void diag_read_complete(struct usb_ep *ep, |
| 240 | struct usb_request *req) |
| 241 | { |
| 242 | struct diag_context *ctxt = ep->driver_data; |
| 243 | struct diag_request *d_req = req->context; |
| 244 | unsigned long flags; |
| 245 | |
| 246 | d_req->actual = req->actual; |
| 247 | d_req->status = req->status; |
| 248 | |
| 249 | spin_lock_irqsave(&ctxt->lock, flags); |
| 250 | list_add_tail(&req->list, &ctxt->read_pool); |
| 251 | spin_unlock_irqrestore(&ctxt->lock, flags); |
| 252 | |
| 253 | ctxt->dpkts_tomodem++; |
| 254 | |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 255 | if (ctxt->ch && ctxt->ch->notify) |
| 256 | ctxt->ch->notify(ctxt->ch->priv, USB_DIAG_READ_DONE, d_req); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
| 260 | * usb_diag_open() - Open a diag channel over USB |
| 261 | * @name: Name of the channel |
| 262 | * @priv: Private structure pointer which will be passed in notify() |
| 263 | * @notify: Callback function to receive notifications |
| 264 | * |
| 265 | * This function iterates overs the available channels and returns |
| 266 | * the channel handler if the name matches. The notify callback is called |
| 267 | * for CONNECT, DISCONNECT, READ_DONE and WRITE_DONE events. |
| 268 | * |
| 269 | */ |
| 270 | struct usb_diag_ch *usb_diag_open(const char *name, void *priv, |
| 271 | void (*notify)(void *, unsigned, struct diag_request *)) |
| 272 | { |
| 273 | struct usb_diag_ch *ch; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 274 | unsigned long flags; |
| 275 | int found = 0; |
| 276 | |
| 277 | spin_lock_irqsave(&ch_lock, flags); |
| 278 | /* Check if we already have a channel with this name */ |
| 279 | list_for_each_entry(ch, &usb_diag_ch_list, list) { |
| 280 | if (!strcmp(name, ch->name)) { |
| 281 | found = 1; |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | spin_unlock_irqrestore(&ch_lock, flags); |
| 286 | |
| 287 | if (!found) { |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 288 | ch = kzalloc(sizeof(*ch), GFP_KERNEL); |
| 289 | if (!ch) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 290 | return ERR_PTR(-ENOMEM); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | ch->name = name; |
| 294 | ch->priv = priv; |
| 295 | ch->notify = notify; |
| 296 | |
| 297 | spin_lock_irqsave(&ch_lock, flags); |
| 298 | list_add_tail(&ch->list, &usb_diag_ch_list); |
| 299 | spin_unlock_irqrestore(&ch_lock, flags); |
| 300 | |
| 301 | return ch; |
| 302 | } |
| 303 | EXPORT_SYMBOL(usb_diag_open); |
| 304 | |
| 305 | /** |
| 306 | * usb_diag_close() - Close a diag channel over USB |
| 307 | * @ch: Channel handler |
| 308 | * |
| 309 | * This function closes the diag channel. |
| 310 | * |
| 311 | */ |
| 312 | void usb_diag_close(struct usb_diag_ch *ch) |
| 313 | { |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 314 | struct diag_context *dev = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 315 | unsigned long flags; |
| 316 | |
| 317 | spin_lock_irqsave(&ch_lock, flags); |
| 318 | ch->priv = NULL; |
| 319 | ch->notify = NULL; |
| 320 | /* Free-up the resources if channel is no more active */ |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 321 | list_del(&ch->list); |
| 322 | list_for_each_entry(dev, &diag_dev_list, list_item) |
| 323 | if (dev->ch == ch) |
| 324 | dev->ch = NULL; |
| 325 | kfree(ch); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 326 | |
| 327 | spin_unlock_irqrestore(&ch_lock, flags); |
| 328 | } |
| 329 | EXPORT_SYMBOL(usb_diag_close); |
| 330 | |
Pavankumar Kondeti | eb1ed0e | 2013-01-02 14:46:58 +0530 | [diff] [blame] | 331 | static void free_reqs(struct diag_context *ctxt) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 332 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 333 | struct list_head *act, *tmp; |
Pavankumar Kondeti | eb1ed0e | 2013-01-02 14:46:58 +0530 | [diff] [blame] | 334 | struct usb_request *req; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 335 | |
| 336 | list_for_each_safe(act, tmp, &ctxt->write_pool) { |
| 337 | req = list_entry(act, struct usb_request, list); |
| 338 | list_del(&req->list); |
| 339 | usb_ep_free_request(ctxt->in, req); |
| 340 | } |
| 341 | |
| 342 | list_for_each_safe(act, tmp, &ctxt->read_pool) { |
| 343 | req = list_entry(act, struct usb_request, list); |
| 344 | list_del(&req->list); |
| 345 | usb_ep_free_request(ctxt->out, req); |
| 346 | } |
| 347 | } |
Pavankumar Kondeti | eb1ed0e | 2013-01-02 14:46:58 +0530 | [diff] [blame] | 348 | |
| 349 | /** |
| 350 | * usb_diag_free_req() - Free USB requests |
| 351 | * @ch: Channel handler |
| 352 | * |
| 353 | * This function free read and write USB requests for the interface |
| 354 | * associated with this channel. |
| 355 | * |
| 356 | */ |
| 357 | void usb_diag_free_req(struct usb_diag_ch *ch) |
| 358 | { |
| 359 | struct diag_context *ctxt = ch->priv_usb; |
| 360 | unsigned long flags; |
| 361 | |
| 362 | if (ctxt) { |
| 363 | spin_lock_irqsave(&ctxt->lock, flags); |
| 364 | free_reqs(ctxt); |
| 365 | spin_unlock_irqrestore(&ctxt->lock, flags); |
| 366 | } |
| 367 | |
| 368 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 369 | EXPORT_SYMBOL(usb_diag_free_req); |
| 370 | |
| 371 | /** |
| 372 | * usb_diag_alloc_req() - Allocate USB requests |
| 373 | * @ch: Channel handler |
| 374 | * @n_write: Number of requests for Tx |
| 375 | * @n_read: Number of requests for Rx |
| 376 | * |
| 377 | * This function allocate read and write USB requests for the interface |
| 378 | * associated with this channel. The actual buffer is not allocated. |
| 379 | * The buffer is passed by diag char driver. |
| 380 | * |
| 381 | */ |
| 382 | int usb_diag_alloc_req(struct usb_diag_ch *ch, int n_write, int n_read) |
| 383 | { |
| 384 | struct diag_context *ctxt = ch->priv_usb; |
| 385 | struct usb_request *req; |
| 386 | int i; |
Pavankumar Kondeti | eb1ed0e | 2013-01-02 14:46:58 +0530 | [diff] [blame] | 387 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 388 | |
| 389 | if (!ctxt) |
| 390 | return -ENODEV; |
| 391 | |
Pavankumar Kondeti | eb1ed0e | 2013-01-02 14:46:58 +0530 | [diff] [blame] | 392 | spin_lock_irqsave(&ctxt->lock, flags); |
| 393 | /* Free previous session's stale requests */ |
| 394 | free_reqs(ctxt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 395 | for (i = 0; i < n_write; i++) { |
| 396 | req = usb_ep_alloc_request(ctxt->in, GFP_ATOMIC); |
| 397 | if (!req) |
| 398 | goto fail; |
| 399 | req->complete = diag_write_complete; |
| 400 | list_add_tail(&req->list, &ctxt->write_pool); |
| 401 | } |
| 402 | |
| 403 | for (i = 0; i < n_read; i++) { |
| 404 | req = usb_ep_alloc_request(ctxt->out, GFP_ATOMIC); |
| 405 | if (!req) |
| 406 | goto fail; |
| 407 | req->complete = diag_read_complete; |
| 408 | list_add_tail(&req->list, &ctxt->read_pool); |
| 409 | } |
Pavankumar Kondeti | eb1ed0e | 2013-01-02 14:46:58 +0530 | [diff] [blame] | 410 | spin_unlock_irqrestore(&ctxt->lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 411 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 412 | fail: |
Pavankumar Kondeti | eb1ed0e | 2013-01-02 14:46:58 +0530 | [diff] [blame] | 413 | free_reqs(ctxt); |
| 414 | spin_unlock_irqrestore(&ctxt->lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 415 | return -ENOMEM; |
| 416 | |
| 417 | } |
| 418 | EXPORT_SYMBOL(usb_diag_alloc_req); |
| 419 | |
| 420 | /** |
| 421 | * usb_diag_read() - Read data from USB diag channel |
| 422 | * @ch: Channel handler |
| 423 | * @d_req: Diag request struct |
| 424 | * |
| 425 | * Enqueue a request on OUT endpoint of the interface corresponding to this |
| 426 | * channel. This function returns proper error code when interface is not |
| 427 | * in configured state, no Rx requests available and ep queue is failed. |
| 428 | * |
| 429 | * This function operates asynchronously. READ_DONE event is notified after |
| 430 | * completion of OUT request. |
| 431 | * |
| 432 | */ |
| 433 | int usb_diag_read(struct usb_diag_ch *ch, struct diag_request *d_req) |
| 434 | { |
| 435 | struct diag_context *ctxt = ch->priv_usb; |
| 436 | unsigned long flags; |
| 437 | struct usb_request *req; |
Pavankumar Kondeti | ac3aa91 | 2012-11-26 14:52:37 +0530 | [diff] [blame] | 438 | static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 439 | |
Manu Gautam | b33f33a | 2011-09-09 14:35:57 +0530 | [diff] [blame] | 440 | if (!ctxt) |
| 441 | return -ENODEV; |
| 442 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 443 | spin_lock_irqsave(&ctxt->lock, flags); |
| 444 | |
| 445 | if (!ctxt->configured) { |
| 446 | spin_unlock_irqrestore(&ctxt->lock, flags); |
| 447 | return -EIO; |
| 448 | } |
| 449 | |
| 450 | if (list_empty(&ctxt->read_pool)) { |
| 451 | spin_unlock_irqrestore(&ctxt->lock, flags); |
| 452 | ERROR(ctxt->cdev, "%s: no requests available\n", __func__); |
| 453 | return -EAGAIN; |
| 454 | } |
| 455 | |
| 456 | req = list_first_entry(&ctxt->read_pool, struct usb_request, list); |
| 457 | list_del(&req->list); |
| 458 | spin_unlock_irqrestore(&ctxt->lock, flags); |
| 459 | |
| 460 | req->buf = d_req->buf; |
| 461 | req->length = d_req->length; |
| 462 | req->context = d_req; |
| 463 | if (usb_ep_queue(ctxt->out, req, GFP_ATOMIC)) { |
| 464 | /* If error add the link to linked list again*/ |
| 465 | spin_lock_irqsave(&ctxt->lock, flags); |
| 466 | list_add_tail(&req->list, &ctxt->read_pool); |
| 467 | spin_unlock_irqrestore(&ctxt->lock, flags); |
Pavankumar Kondeti | ac3aa91 | 2012-11-26 14:52:37 +0530 | [diff] [blame] | 468 | /* 1 error message for every 10 sec */ |
| 469 | if (__ratelimit(&rl)) |
| 470 | ERROR(ctxt->cdev, "%s: cannot queue" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 471 | " read request\n", __func__); |
| 472 | return -EIO; |
| 473 | } |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | EXPORT_SYMBOL(usb_diag_read); |
| 478 | |
| 479 | /** |
| 480 | * usb_diag_write() - Write data from USB diag channel |
| 481 | * @ch: Channel handler |
| 482 | * @d_req: Diag request struct |
| 483 | * |
| 484 | * Enqueue a request on IN endpoint of the interface corresponding to this |
| 485 | * channel. This function returns proper error code when interface is not |
| 486 | * in configured state, no Tx requests available and ep queue is failed. |
| 487 | * |
| 488 | * This function operates asynchronously. WRITE_DONE event is notified after |
| 489 | * completion of IN request. |
| 490 | * |
| 491 | */ |
| 492 | int usb_diag_write(struct usb_diag_ch *ch, struct diag_request *d_req) |
| 493 | { |
| 494 | struct diag_context *ctxt = ch->priv_usb; |
| 495 | unsigned long flags; |
| 496 | struct usb_request *req = NULL; |
Pavankumar Kondeti | ac3aa91 | 2012-11-26 14:52:37 +0530 | [diff] [blame] | 497 | static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 498 | |
Manu Gautam | b33f33a | 2011-09-09 14:35:57 +0530 | [diff] [blame] | 499 | if (!ctxt) |
| 500 | return -ENODEV; |
| 501 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 502 | spin_lock_irqsave(&ctxt->lock, flags); |
| 503 | |
| 504 | if (!ctxt->configured) { |
| 505 | spin_unlock_irqrestore(&ctxt->lock, flags); |
| 506 | return -EIO; |
| 507 | } |
| 508 | |
| 509 | if (list_empty(&ctxt->write_pool)) { |
| 510 | spin_unlock_irqrestore(&ctxt->lock, flags); |
| 511 | ERROR(ctxt->cdev, "%s: no requests available\n", __func__); |
| 512 | return -EAGAIN; |
| 513 | } |
| 514 | |
| 515 | req = list_first_entry(&ctxt->write_pool, struct usb_request, list); |
| 516 | list_del(&req->list); |
| 517 | spin_unlock_irqrestore(&ctxt->lock, flags); |
| 518 | |
| 519 | req->buf = d_req->buf; |
| 520 | req->length = d_req->length; |
| 521 | req->context = d_req; |
| 522 | if (usb_ep_queue(ctxt->in, req, GFP_ATOMIC)) { |
| 523 | /* If error add the link to linked list again*/ |
| 524 | spin_lock_irqsave(&ctxt->lock, flags); |
| 525 | list_add_tail(&req->list, &ctxt->write_pool); |
| 526 | spin_unlock_irqrestore(&ctxt->lock, flags); |
Pavankumar Kondeti | ac3aa91 | 2012-11-26 14:52:37 +0530 | [diff] [blame] | 527 | /* 1 error message for every 10 sec */ |
| 528 | if (__ratelimit(&rl)) |
| 529 | ERROR(ctxt->cdev, "%s: cannot queue" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 530 | " read request\n", __func__); |
| 531 | return -EIO; |
| 532 | } |
| 533 | |
| 534 | ctxt->dpkts_tolaptop++; |
| 535 | ctxt->dpkts_tolaptop_pending++; |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | EXPORT_SYMBOL(usb_diag_write); |
| 540 | |
| 541 | static void diag_function_disable(struct usb_function *f) |
| 542 | { |
| 543 | struct diag_context *dev = func_to_diag(f); |
| 544 | unsigned long flags; |
| 545 | |
| 546 | DBG(dev->cdev, "diag_function_disable\n"); |
| 547 | |
| 548 | spin_lock_irqsave(&dev->lock, flags); |
| 549 | dev->configured = 0; |
| 550 | spin_unlock_irqrestore(&dev->lock, flags); |
| 551 | |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 552 | if (dev->ch && dev->ch->notify) |
| 553 | dev->ch->notify(dev->ch->priv, USB_DIAG_DISCONNECT, NULL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 554 | |
| 555 | usb_ep_disable(dev->in); |
| 556 | dev->in->driver_data = NULL; |
| 557 | |
| 558 | usb_ep_disable(dev->out); |
| 559 | dev->out->driver_data = NULL; |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 560 | if (dev->ch) |
| 561 | dev->ch->priv_usb = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static int diag_function_set_alt(struct usb_function *f, |
| 565 | unsigned intf, unsigned alt) |
| 566 | { |
| 567 | struct diag_context *dev = func_to_diag(f); |
| 568 | struct usb_composite_dev *cdev = f->config->cdev; |
| 569 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 570 | int rc = 0; |
| 571 | |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 572 | if (config_ep_by_speed(cdev->gadget, f, dev->in) || |
| 573 | config_ep_by_speed(cdev->gadget, f, dev->out)) { |
| 574 | dev->in->desc = NULL; |
| 575 | dev->out->desc = NULL; |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 579 | if (!dev->ch) |
| 580 | return -ENODEV; |
| 581 | |
| 582 | /* |
| 583 | * Indicate to the diag channel that the active diag device is dev. |
| 584 | * Since a few diag devices can point to the same channel. |
| 585 | */ |
| 586 | dev->ch->priv_usb = dev; |
| 587 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 588 | dev->in->driver_data = dev; |
Tatyana Brokhman | cf709c1 | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 589 | rc = usb_ep_enable(dev->in); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 590 | if (rc) { |
| 591 | ERROR(dev->cdev, "can't enable %s, result %d\n", |
| 592 | dev->in->name, rc); |
| 593 | return rc; |
| 594 | } |
| 595 | dev->out->driver_data = dev; |
Tatyana Brokhman | cf709c1 | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 596 | rc = usb_ep_enable(dev->out); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 597 | if (rc) { |
| 598 | ERROR(dev->cdev, "can't enable %s, result %d\n", |
| 599 | dev->out->name, rc); |
| 600 | usb_ep_disable(dev->in); |
| 601 | return rc; |
| 602 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 603 | |
Manu Gautam | 0c61107 | 2011-11-11 17:40:05 +0530 | [diff] [blame] | 604 | dev->dpkts_tolaptop = 0; |
| 605 | dev->dpkts_tomodem = 0; |
| 606 | dev->dpkts_tolaptop_pending = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 607 | |
| 608 | spin_lock_irqsave(&dev->lock, flags); |
| 609 | dev->configured = 1; |
| 610 | spin_unlock_irqrestore(&dev->lock, flags); |
| 611 | |
Pavankumar Kondeti | c358658 | 2013-04-18 11:02:28 +0530 | [diff] [blame^] | 612 | if (dev->ch->notify) |
| 613 | dev->ch->notify(dev->ch->priv, USB_DIAG_CONNECT, NULL); |
| 614 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 615 | return rc; |
| 616 | } |
| 617 | |
| 618 | static void diag_function_unbind(struct usb_configuration *c, |
| 619 | struct usb_function *f) |
| 620 | { |
| 621 | struct diag_context *ctxt = func_to_diag(f); |
| 622 | |
Pavankumar Kondeti | 6f94bc9 | 2012-08-03 09:34:32 +0530 | [diff] [blame] | 623 | if (gadget_is_superspeed(c->cdev->gadget)) |
| 624 | usb_free_descriptors(f->ss_descriptors); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 625 | if (gadget_is_dualspeed(c->cdev->gadget)) |
| 626 | usb_free_descriptors(f->hs_descriptors); |
| 627 | |
| 628 | usb_free_descriptors(f->descriptors); |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 629 | |
| 630 | /* |
| 631 | * Channel priv_usb may point to other diag function. |
| 632 | * Clear the priv_usb only if the channel is used by the |
| 633 | * diag dev we unbind here. |
| 634 | */ |
| 635 | if (ctxt->ch && ctxt->ch->priv_usb == ctxt) |
| 636 | ctxt->ch->priv_usb = NULL; |
| 637 | list_del(&ctxt->list_item); |
| 638 | kfree(ctxt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | static int diag_function_bind(struct usb_configuration *c, |
| 642 | struct usb_function *f) |
| 643 | { |
| 644 | struct usb_composite_dev *cdev = c->cdev; |
| 645 | struct diag_context *ctxt = func_to_diag(f); |
| 646 | struct usb_ep *ep; |
| 647 | int status = -ENODEV; |
| 648 | |
| 649 | intf_desc.bInterfaceNumber = usb_interface_id(c, f); |
| 650 | |
| 651 | ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_in_desc); |
Vamsi Krishna | e606a7b | 2011-08-18 12:21:44 -0700 | [diff] [blame] | 652 | if (!ep) |
| 653 | goto fail; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 654 | ctxt->in = ep; |
| 655 | ep->driver_data = ctxt; |
| 656 | |
| 657 | ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_out_desc); |
Vamsi Krishna | e606a7b | 2011-08-18 12:21:44 -0700 | [diff] [blame] | 658 | if (!ep) |
| 659 | goto fail; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 660 | ctxt->out = ep; |
| 661 | ep->driver_data = ctxt; |
| 662 | |
Pavankumar Kondeti | 6f94bc9 | 2012-08-03 09:34:32 +0530 | [diff] [blame] | 663 | status = -ENOMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 664 | /* copy descriptors, and track endpoint copies */ |
| 665 | f->descriptors = usb_copy_descriptors(fs_diag_desc); |
| 666 | if (!f->descriptors) |
| 667 | goto fail; |
| 668 | |
| 669 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 670 | hs_bulk_in_desc.bEndpointAddress = |
| 671 | fs_bulk_in_desc.bEndpointAddress; |
| 672 | hs_bulk_out_desc.bEndpointAddress = |
| 673 | fs_bulk_out_desc.bEndpointAddress; |
| 674 | |
| 675 | /* copy descriptors, and track endpoint copies */ |
| 676 | f->hs_descriptors = usb_copy_descriptors(hs_diag_desc); |
Pavankumar Kondeti | 6f94bc9 | 2012-08-03 09:34:32 +0530 | [diff] [blame] | 677 | if (!f->hs_descriptors) |
| 678 | goto fail; |
| 679 | } |
| 680 | |
| 681 | if (gadget_is_superspeed(c->cdev->gadget)) { |
| 682 | ss_bulk_in_desc.bEndpointAddress = |
| 683 | fs_bulk_in_desc.bEndpointAddress; |
| 684 | ss_bulk_out_desc.bEndpointAddress = |
| 685 | fs_bulk_out_desc.bEndpointAddress; |
| 686 | |
| 687 | /* copy descriptors, and track endpoint copies */ |
| 688 | f->ss_descriptors = usb_copy_descriptors(ss_diag_desc); |
| 689 | if (!f->ss_descriptors) |
| 690 | goto fail; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 691 | } |
Pavankumar Kondeti | c358658 | 2013-04-18 11:02:28 +0530 | [diff] [blame^] | 692 | diag_update_pid_and_serial_num(ctxt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 693 | return 0; |
| 694 | fail: |
Pavankumar Kondeti | 6f94bc9 | 2012-08-03 09:34:32 +0530 | [diff] [blame] | 695 | if (f->ss_descriptors) |
| 696 | usb_free_descriptors(f->ss_descriptors); |
| 697 | if (f->hs_descriptors) |
| 698 | usb_free_descriptors(f->hs_descriptors); |
| 699 | if (f->descriptors) |
| 700 | usb_free_descriptors(f->descriptors); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 701 | if (ctxt->out) |
| 702 | ctxt->out->driver_data = NULL; |
| 703 | if (ctxt->in) |
| 704 | ctxt->in->driver_data = NULL; |
| 705 | return status; |
| 706 | |
| 707 | } |
| 708 | |
| 709 | int diag_function_add(struct usb_configuration *c, const char *name, |
| 710 | int (*update_pid)(uint32_t, const char *)) |
| 711 | { |
| 712 | struct diag_context *dev; |
| 713 | struct usb_diag_ch *_ch; |
| 714 | int found = 0, ret; |
| 715 | |
| 716 | DBG(c->cdev, "diag_function_add\n"); |
| 717 | |
| 718 | list_for_each_entry(_ch, &usb_diag_ch_list, list) { |
| 719 | if (!strcmp(name, _ch->name)) { |
| 720 | found = 1; |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | if (!found) { |
| 725 | ERROR(c->cdev, "unable to get diag usb channel\n"); |
| 726 | return -ENODEV; |
| 727 | } |
| 728 | |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 729 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 730 | if (!dev) |
| 731 | return -ENOMEM; |
| 732 | |
| 733 | list_add_tail(&dev->list_item, &diag_dev_list); |
| 734 | |
| 735 | /* |
| 736 | * A few diag devices can point to the same channel, in case that |
| 737 | * the diag devices belong to different configurations, however |
| 738 | * only the active diag device will claim the channel by setting |
| 739 | * the ch->priv_usb (see diag_function_set_alt). |
| 740 | */ |
| 741 | dev->ch = _ch; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 742 | |
Tatyana Brokhman | cf709c1 | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 743 | dev->update_pid_and_serial_num = update_pid; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 744 | dev->cdev = c->cdev; |
| 745 | dev->function.name = _ch->name; |
| 746 | dev->function.descriptors = fs_diag_desc; |
| 747 | dev->function.hs_descriptors = hs_diag_desc; |
| 748 | dev->function.bind = diag_function_bind; |
| 749 | dev->function.unbind = diag_function_unbind; |
| 750 | dev->function.set_alt = diag_function_set_alt; |
| 751 | dev->function.disable = diag_function_disable; |
| 752 | spin_lock_init(&dev->lock); |
| 753 | INIT_LIST_HEAD(&dev->read_pool); |
| 754 | INIT_LIST_HEAD(&dev->write_pool); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 755 | |
| 756 | ret = usb_add_function(c, &dev->function); |
| 757 | if (ret) { |
| 758 | INFO(c->cdev, "usb_add_function failed\n"); |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 759 | list_del(&dev->list_item); |
| 760 | kfree(dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 766 | #if defined(CONFIG_DEBUG_FS) |
| 767 | static char debug_buffer[PAGE_SIZE]; |
| 768 | |
| 769 | static ssize_t debug_read_stats(struct file *file, char __user *ubuf, |
| 770 | size_t count, loff_t *ppos) |
| 771 | { |
| 772 | char *buf = debug_buffer; |
| 773 | int temp = 0; |
| 774 | struct usb_diag_ch *ch; |
| 775 | |
| 776 | list_for_each_entry(ch, &usb_diag_ch_list, list) { |
Jack Pham | 1c20e3f | 2012-04-04 19:29:14 -0700 | [diff] [blame] | 777 | struct diag_context *ctxt = ch->priv_usb; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 778 | |
Jack Pham | 1c20e3f | 2012-04-04 19:29:14 -0700 | [diff] [blame] | 779 | if (ctxt) |
| 780 | temp += scnprintf(buf + temp, PAGE_SIZE - temp, |
| 781 | "---Name: %s---\n" |
| 782 | "endpoints: %s, %s\n" |
| 783 | "dpkts_tolaptop: %lu\n" |
| 784 | "dpkts_tomodem: %lu\n" |
| 785 | "pkts_tolaptop_pending: %u\n", |
| 786 | ch->name, |
| 787 | ctxt->in->name, ctxt->out->name, |
| 788 | ctxt->dpkts_tolaptop, |
| 789 | ctxt->dpkts_tomodem, |
| 790 | ctxt->dpkts_tolaptop_pending); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | return simple_read_from_buffer(ubuf, count, ppos, buf, temp); |
| 794 | } |
| 795 | |
| 796 | static ssize_t debug_reset_stats(struct file *file, const char __user *buf, |
| 797 | size_t count, loff_t *ppos) |
| 798 | { |
| 799 | struct usb_diag_ch *ch; |
| 800 | |
| 801 | list_for_each_entry(ch, &usb_diag_ch_list, list) { |
Jack Pham | 1c20e3f | 2012-04-04 19:29:14 -0700 | [diff] [blame] | 802 | struct diag_context *ctxt = ch->priv_usb; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 803 | |
Jack Pham | 1c20e3f | 2012-04-04 19:29:14 -0700 | [diff] [blame] | 804 | if (ctxt) { |
| 805 | ctxt->dpkts_tolaptop = 0; |
| 806 | ctxt->dpkts_tomodem = 0; |
| 807 | ctxt->dpkts_tolaptop_pending = 0; |
| 808 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | return count; |
| 812 | } |
| 813 | |
| 814 | static int debug_open(struct inode *inode, struct file *file) |
| 815 | { |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | static const struct file_operations debug_fdiag_ops = { |
| 820 | .open = debug_open, |
| 821 | .read = debug_read_stats, |
| 822 | .write = debug_reset_stats, |
| 823 | }; |
| 824 | |
Manu Gautam | d9f56a1 | 2011-09-26 14:04:49 +0530 | [diff] [blame] | 825 | struct dentry *dent_diag; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 826 | static void fdiag_debugfs_init(void) |
| 827 | { |
Manu Gautam | d9f56a1 | 2011-09-26 14:04:49 +0530 | [diff] [blame] | 828 | dent_diag = debugfs_create_dir("usb_diag", 0); |
| 829 | if (IS_ERR(dent_diag)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 830 | return; |
| 831 | |
Manu Gautam | d9f56a1 | 2011-09-26 14:04:49 +0530 | [diff] [blame] | 832 | debugfs_create_file("status", 0444, dent_diag, 0, &debug_fdiag_ops); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 833 | } |
| 834 | #else |
| 835 | static void fdiag_debugfs_init(void) |
| 836 | { |
| 837 | return; |
| 838 | } |
| 839 | #endif |
| 840 | |
| 841 | static void diag_cleanup(void) |
| 842 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 843 | struct list_head *act, *tmp; |
| 844 | struct usb_diag_ch *_ch; |
| 845 | unsigned long flags; |
| 846 | |
Manu Gautam | d9f56a1 | 2011-09-26 14:04:49 +0530 | [diff] [blame] | 847 | debugfs_remove_recursive(dent_diag); |
| 848 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 849 | list_for_each_safe(act, tmp, &usb_diag_ch_list) { |
| 850 | _ch = list_entry(act, struct usb_diag_ch, list); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 851 | |
| 852 | spin_lock_irqsave(&ch_lock, flags); |
| 853 | /* Free if diagchar is not using the channel anymore */ |
| 854 | if (!_ch->priv) { |
| 855 | list_del(&_ch->list); |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 856 | kfree(_ch); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 857 | } |
| 858 | spin_unlock_irqrestore(&ch_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
| 862 | static int diag_setup(void) |
| 863 | { |
Ido Shayevitz | 6c00b3d | 2013-01-18 18:05:40 +0200 | [diff] [blame] | 864 | INIT_LIST_HEAD(&diag_dev_list); |
| 865 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 866 | fdiag_debugfs_init(); |
| 867 | |
| 868 | return 0; |
| 869 | } |