Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* driver/usb/function/null.c |
| 2 | * |
| 3 | * Null Function Device - A Data Sink |
| 4 | * |
| 5 | * Copyright (C) 2007 Google, Inc. |
| 6 | * Author: Brian Swetland <swetland@google.com> |
| 7 | * |
| 8 | * This software is licensed under the terms of the GNU General Public |
| 9 | * License version 2, as published by the Free Software Foundation, and |
| 10 | * may be copied, distributed, and modified under those terms. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/kernel.h> |
| 22 | |
| 23 | #include "usb_function.h" |
| 24 | |
| 25 | struct null_context |
| 26 | { |
| 27 | struct usb_endpoint *out; |
| 28 | struct usb_request *req0; |
| 29 | struct usb_request *req1; |
| 30 | }; |
| 31 | |
| 32 | static struct null_context _context; |
| 33 | |
| 34 | static void null_bind(struct usb_endpoint **ept, void *_ctxt) |
| 35 | { |
| 36 | struct null_context *ctxt = _ctxt; |
| 37 | ctxt->out = ept[0]; |
| 38 | printk(KERN_INFO "null_bind() %p\n", ctxt->out); |
| 39 | |
| 40 | ctxt->req0 = usb_ept_alloc_req(ctxt->out, 4096); |
| 41 | ctxt->req1 = usb_ept_alloc_req(ctxt->out, 4096); |
| 42 | } |
| 43 | |
| 44 | static void null_unbind(void *_ctxt) |
| 45 | { |
| 46 | struct null_context *ctxt = _ctxt; |
| 47 | printk(KERN_INFO "null_unbind()\n"); |
| 48 | if (ctxt->req0) { |
| 49 | usb_ept_free_req(ctxt->out, ctxt->req0); |
| 50 | ctxt->req0 = 0; |
| 51 | } |
| 52 | if (ctxt->req1) { |
| 53 | usb_ept_free_req(ctxt->out, ctxt->req1); |
| 54 | ctxt->req1 = 0; |
| 55 | } |
| 56 | ctxt->out = 0; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static void null_queue_out(struct null_context *ctxt, struct usb_request *req); |
| 61 | |
| 62 | static void null_out_complete(struct usb_endpoint *ept, struct usb_request *req) |
| 63 | { |
| 64 | struct null_context *ctxt = req->context; |
| 65 | unsigned char *data = req->buf; |
| 66 | |
| 67 | if (req->status != -ENODEV) |
| 68 | null_queue_out(ctxt, req); |
| 69 | } |
| 70 | |
| 71 | static void null_queue_out(struct null_context *ctxt, struct usb_request *req) |
| 72 | { |
| 73 | req->complete = null_out_complete; |
| 74 | req->context = ctxt; |
| 75 | req->length = 4096; |
| 76 | |
| 77 | usb_ept_queue_xfer(ctxt->out, req); |
| 78 | } |
| 79 | |
| 80 | static void null_configure(int configured, void *_ctxt) |
| 81 | { |
| 82 | struct null_context *ctxt = _ctxt; |
| 83 | printk(KERN_INFO "null_configure() %d\n", configured); |
| 84 | |
| 85 | if (configured) { |
| 86 | null_queue_out(ctxt, ctxt->req0); |
| 87 | null_queue_out(ctxt, ctxt->req1); |
| 88 | } else { |
| 89 | /* all pending requests will be canceled */ |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static struct usb_function usb_func_null = { |
| 94 | .bind = null_bind, |
| 95 | .unbind = null_unbind, |
| 96 | .configure = null_configure, |
| 97 | |
| 98 | .name = "null", |
| 99 | .context = &_context, |
| 100 | |
| 101 | .ifc_class = 0xff, |
| 102 | .ifc_subclass = 0xfe, |
| 103 | .ifc_protocol = 0x01, |
| 104 | |
| 105 | .ifc_name = "null", |
| 106 | |
| 107 | .ifc_ept_count = 1, |
| 108 | .ifc_ept_type = { EPT_BULK_OUT }, |
| 109 | }; |
| 110 | |
| 111 | static int __init null_init(void) |
| 112 | { |
| 113 | printk(KERN_INFO "null_init()\n"); |
| 114 | usb_function_register(&usb_func_null); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | module_init(null_init); |