cregit-Linux how code gets into the kernel

Release 4.7 drivers/usb/gadget/legacy/dbgp.c

/*
 * dbgp.c -- EHCI Debug Port device gadget
 *
 * Copyright (C) 2010 Stephane Duverger
 *
 * Released under the GPLv2.
 */

/* verbose messages */
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>

#include "u_serial.h"


#define DRIVER_VENDOR_ID	0x0525 
/* NetChip */

#define DRIVER_PRODUCT_ID	0xc0de 
/* undefined */


#define USB_DEBUG_MAX_PACKET_SIZE     8

#define DBGP_REQ_EP0_LEN              128

#define DBGP_REQ_LEN                  512


static struct dbgp {
	
struct usb_gadget  *gadget;
	
struct usb_request *req;
	
struct usb_ep      *i_ep;
	
struct usb_ep      *o_ep;
#ifdef CONFIG_USB_G_DBGP_SERIAL
	
struct gserial     *serial;
#endif
} 
dbgp;


static struct usb_device_descriptor device_desc = {
	.bLength = sizeof device_desc,
	.bDescriptorType = USB_DT_DEVICE,
	.bcdUSB = cpu_to_le16(0x0200),
	.bDeviceClass =	USB_CLASS_VENDOR_SPEC,
	.idVendor = cpu_to_le16(DRIVER_VENDOR_ID),
	.idProduct = cpu_to_le16(DRIVER_PRODUCT_ID),
	.bNumConfigurations = 1,
};


static struct usb_debug_descriptor dbg_desc = {
	.bLength = sizeof dbg_desc,
	.bDescriptorType = USB_DT_DEBUG,
};


static struct usb_endpoint_descriptor i_desc = {
	.bLength = USB_DT_ENDPOINT_SIZE,
	.bDescriptorType = USB_DT_ENDPOINT,
	.bmAttributes = USB_ENDPOINT_XFER_BULK,
	.bEndpointAddress = USB_DIR_IN,
};


static struct usb_endpoint_descriptor o_desc = {
	.bLength = USB_DT_ENDPOINT_SIZE,
	.bDescriptorType = USB_DT_ENDPOINT,
	.bmAttributes = USB_ENDPOINT_XFER_BULK,
	.bEndpointAddress = USB_DIR_OUT,
};

#ifdef CONFIG_USB_G_DBGP_PRINTK

static int dbgp_consume(char *buf, unsigned len) { char c; if (!len) return 0; c = buf[len-1]; if (c != 0) buf[len-1] = 0; printk(KERN_NOTICE "%s%c", buf, c); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger61100.00%1100.00%
Total61100.00%1100.00%


static void __disable_ep(struct usb_ep *ep) { usb_ep_disable(ep); }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger16100.00%1100.00%
Total16100.00%1100.00%


static void dbgp_disable_ep(void) { __disable_ep(dbgp.i_ep); __disable_ep(dbgp.o_ep); }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger22100.00%1100.00%
Total22100.00%1100.00%


static void dbgp_complete(struct usb_ep *ep, struct usb_request *req) { int stp; int err = 0; int status = req->status; if (ep == dbgp.i_ep) { stp = 1; goto fail; } if (status != 0) { stp = 2; goto release_req; } dbgp_consume(req->buf, req->actual); req->length = DBGP_REQ_LEN; err = usb_ep_queue(ep, req, GFP_ATOMIC); if (err < 0) { stp = 3; goto release_req; } return; release_req: kfree(req->buf); usb_ep_free_request(dbgp.o_ep, req); dbgp_disable_ep(); fail: dev_dbg(&dbgp.gadget->dev, "complete: failure (%d:%d) ==> %d\n", stp, err, status); }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger148100.00%1100.00%
Total148100.00%1100.00%


static int dbgp_enable_ep_req(struct usb_ep *ep) { int err, stp; struct usb_request *req; req = usb_ep_alloc_request(ep, GFP_KERNEL); if (!req) { err = -ENOMEM; stp = 1; goto fail_1; } req->buf = kmalloc(DBGP_REQ_LEN, GFP_KERNEL); if (!req->buf) { err = -ENOMEM; stp = 2; goto fail_2; } req->complete = dbgp_complete; req->length = DBGP_REQ_LEN; err = usb_ep_queue(ep, req, GFP_ATOMIC); if (err < 0) { stp = 3; goto fail_3; } return 0; fail_3: kfree(req->buf); fail_2: usb_ep_free_request(dbgp.o_ep, req); fail_1: dev_dbg(&dbgp.gadget->dev, "enable ep req: failure (%d:%d)\n", stp, err); return err; }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger163100.00%1100.00%
Total163100.00%1100.00%


static int __enable_ep(struct usb_ep *ep, struct usb_endpoint_descriptor *desc) { int err; ep->desc = desc; err = usb_ep_enable(ep); return err; }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger2571.43%150.00%
tatyana brokhmantatyana brokhman1028.57%150.00%
Total35100.00%2100.00%


static int dbgp_enable_ep(void) { int err, stp; err = __enable_ep(dbgp.i_ep, &i_desc); if (err < 0) { stp = 1; goto fail_1; } err = __enable_ep(dbgp.o_ep, &o_desc); if (err < 0) { stp = 2; goto fail_2; } err = dbgp_enable_ep_req(dbgp.o_ep); if (err < 0) { stp = 3; goto fail_3; } return 0; fail_3: __disable_ep(dbgp.o_ep); fail_2: __disable_ep(dbgp.i_ep); fail_1: dev_dbg(&dbgp.gadget->dev, "enable ep: failure (%d:%d)\n", stp, err); return err; }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger133100.00%1100.00%
Total133100.00%1100.00%

#endif
static void dbgp_disconnect(struct usb_gadget *gadget) { #ifdef CONFIG_USB_G_DBGP_PRINTK dbgp_disable_ep(); #else gserial_disconnect(dbgp.serial); #endif }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger28100.00%1100.00%
Total28100.00%1100.00%


static void dbgp_unbind(struct usb_gadget *gadget) { #ifdef CONFIG_USB_G_DBGP_SERIAL kfree(dbgp.serial); dbgp.serial = NULL; #endif if (dbgp.req) { kfree(dbgp.req->buf); usb_ep_free_request(gadget->ep0, dbgp.req); dbgp.req = NULL; } }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger5180.95%150.00%
alexey khoroshilovalexey khoroshilov1219.05%150.00%
Total63100.00%2100.00%

#ifdef CONFIG_USB_G_DBGP_SERIAL static unsigned char tty_line; #endif
static int dbgp_configure_endpoints(struct usb_gadget *gadget) { int stp; usb_ep_autoconfig_reset(gadget); dbgp.i_ep = usb_ep_autoconfig(gadget, &i_desc); if (!dbgp.i_ep) { stp = 1; goto fail_1; } i_desc.wMaxPacketSize = cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE); dbgp.o_ep = usb_ep_autoconfig(gadget, &o_desc); if (!dbgp.o_ep) { stp = 2; goto fail_1; } o_desc.wMaxPacketSize = cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE); dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress; dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress; #ifdef CONFIG_USB_G_DBGP_SERIAL dbgp.serial->in = dbgp.i_ep; dbgp.serial->out = dbgp.o_ep; dbgp.serial->in->desc = &i_desc; dbgp.serial->out->desc = &o_desc; #endif return 0; fail_1: dev_dbg(&dbgp.gadget->dev, "ep config: failure (%d)\n", stp); return -ENODEV; }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger16893.85%120.00%
tatyana brokhmantatyana brokhman63.35%120.00%
kyosti malkkikyosti malkki21.12%120.00%
vaishali thakkarvaishali thakkar21.12%120.00%
robert baldygarobert baldyga10.56%120.00%
Total179100.00%5100.00%


static int dbgp_bind(struct usb_gadget *gadget, struct usb_gadget_driver *driver) { int err, stp; dbgp.gadget = gadget; dbgp.req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); if (!dbgp.req) { err = -ENOMEM; stp = 1; goto fail; } dbgp.req->buf = kmalloc(DBGP_REQ_EP0_LEN, GFP_KERNEL); if (!dbgp.req->buf) { err = -ENOMEM; stp = 2; goto fail; } dbgp.req->length = DBGP_REQ_EP0_LEN; #ifdef CONFIG_USB_G_DBGP_SERIAL dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL); if (!dbgp.serial) { stp = 3; err = -ENOMEM; goto fail; } if (gserial_alloc_line(&tty_line)) { stp = 4; err = -ENODEV; goto fail; } #endif err = dbgp_configure_endpoints(gadget); if (err < 0) { stp = 5; goto fail; } dev_dbg(&dbgp.gadget->dev, "bind: success\n"); return 0; fail: dev_dbg(&gadget->dev, "bind: failure (%d:%d)\n", stp, err); dbgp_unbind(gadget); return err; }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger20187.77%133.33%
kyosti malkkikyosti malkki2310.04%133.33%
sebastian andrzej siewiorsebastian andrzej siewior52.18%133.33%
Total229100.00%3100.00%


static void dbgp_setup_complete(struct usb_ep *ep, struct usb_request *req) { dev_dbg(&dbgp.gadget->dev, "setup complete: %d, %d/%d\n", req->status, req->actual, req->length); }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger40100.00%1100.00%
Total40100.00%1100.00%


static int dbgp_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) { struct usb_request *req = dbgp.req; u8 request = ctrl->bRequest; u16 value = le16_to_cpu(ctrl->wValue); u16 length = le16_to_cpu(ctrl->wLength); int err = -EOPNOTSUPP; void *data = NULL; u16 len = 0; if (request == USB_REQ_GET_DESCRIPTOR) { switch (value>>8) { case USB_DT_DEVICE: dev_dbg(&dbgp.gadget->dev, "setup: desc device\n"); len = sizeof device_desc; data = &device_desc; device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; break; case USB_DT_DEBUG: dev_dbg(&dbgp.gadget->dev, "setup: desc debug\n"); len = sizeof dbg_desc; data = &dbg_desc; break; default: goto fail; } err = 0; } else if (request == USB_REQ_SET_FEATURE && value == USB_DEVICE_DEBUG_MODE) { dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n"); #ifdef CONFIG_USB_G_DBGP_PRINTK err = dbgp_enable_ep(); #else err = dbgp_configure_endpoints(gadget); if (err < 0) { goto fail; } err = gserial_connect(dbgp.serial, tty_line); #endif if (err < 0) goto fail; } else goto fail; req->length = min(length, len); req->zero = len < req->length; if (data && req->length) memcpy(req->buf, data, req->length); req->complete = dbgp_setup_complete; return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); fail: dev_dbg(&dbgp.gadget->dev, "setup: failure req %x v %x\n", request, value); return err; }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger27787.66%120.00%
kyosti malkkikyosti malkki185.70%120.00%
sebastian andrzej siewiorsebastian andrzej siewior113.48%240.00%
sven schnellesven schnelle103.16%120.00%
Total316100.00%5100.00%

static struct usb_gadget_driver dbgp_driver = { .function = "dbgp", .max_speed = USB_SPEED_HIGH, .bind = dbgp_bind, .unbind = dbgp_unbind, .setup = dbgp_setup, .reset = dbgp_disconnect, .disconnect = dbgp_disconnect, .driver = { .owner = THIS_MODULE, .name = "dbgp" }, };
static int __init dbgp_init(void) { return usb_gadget_probe_driver(&dbgp_driver); }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger1593.75%150.00%
uwe kleine-koeniguwe kleine-koenig16.25%150.00%
Total16100.00%2100.00%


static void __exit dbgp_exit(void) { usb_gadget_unregister_driver(&dbgp_driver); #ifdef CONFIG_USB_G_DBGP_SERIAL gserial_free_line(tty_line); #endif }

Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger2184.00%150.00%
sebastian andrzej siewiorsebastian andrzej siewior416.00%150.00%
Total25100.00%2100.00%

MODULE_AUTHOR("Stephane Duverger"); MODULE_LICENSE("GPL"); module_init(dbgp_init); module_exit(dbgp_exit);

Overall Contributors

PersonTokensPropCommitsCommitProp
stephane duvergerstephane duverger163292.26%15.56%
kyosti malkkikyosti malkki432.43%15.56%
sebastian andrzej siewiorsebastian andrzej siewior362.04%527.78%
tatyana brokhmantatyana brokhman160.90%15.56%
alexey khoroshilovalexey khoroshilov120.68%15.56%
sven schnellesven schnelle100.57%15.56%
vaishali thakkarvaishali thakkar50.28%15.56%
peter chenpeter chen50.28%15.56%
arnd bergmannarnd bergmann30.17%15.56%
paul gortmakerpaul gortmaker30.17%15.56%
klaus schwarzkopfklaus schwarzkopf10.06%15.56%
michal nazarewiczmichal nazarewicz10.06%15.56%
uwe kleine-koeniguwe kleine-koenig10.06%15.56%
robert baldygarobert baldyga10.06%15.56%
Total1769100.00%18100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
{% endraw %}