Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 1 | /* Target based USB-Gadget Function |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * UAS protocol handling, target callbacks, configfs handling, |
| 4 | * BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling. |
| 5 | * |
| 6 | * Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de> |
| 7 | * License: GPLv2 as published by FSF. |
| 8 | */ |
Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 9 | |
| 10 | #include <linux/init.h> |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 12 | |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 13 | #include <linux/usb/composite.h> |
| 14 | #include <linux/usb/gadget.h> |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include "usbstring.c" |
| 17 | #include "epautoconf.c" |
| 18 | #include "config.c" |
| 19 | #include "composite.c" |
| 20 | |
Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 21 | #include "f_tcm.c" |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 22 | |
| 23 | #define UAS_VENDOR_ID 0x0525 /* NetChip */ |
| 24 | #define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */ |
| 25 | |
Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 26 | #define USB_G_STR_MANUFACTOR 1 |
| 27 | #define USB_G_STR_PRODUCT 2 |
| 28 | #define USB_G_STR_SERIAL 3 |
| 29 | #define USB_G_STR_CONFIG 4 |
| 30 | |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 31 | static struct usb_device_descriptor usbg_device_desc = { |
| 32 | .bLength = sizeof(usbg_device_desc), |
| 33 | .bDescriptorType = USB_DT_DEVICE, |
| 34 | .bcdUSB = cpu_to_le16(0x0200), |
| 35 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 36 | .idVendor = cpu_to_le16(UAS_VENDOR_ID), |
| 37 | .idProduct = cpu_to_le16(UAS_PRODUCT_ID), |
| 38 | .iManufacturer = USB_G_STR_MANUFACTOR, |
| 39 | .iProduct = USB_G_STR_PRODUCT, |
| 40 | .iSerialNumber = USB_G_STR_SERIAL, |
| 41 | |
| 42 | .bNumConfigurations = 1, |
| 43 | }; |
| 44 | |
| 45 | static struct usb_string usbg_us_strings[] = { |
| 46 | { USB_G_STR_MANUFACTOR, "Target Manufactor"}, |
| 47 | { USB_G_STR_PRODUCT, "Target Product"}, |
| 48 | { USB_G_STR_SERIAL, "000000000001"}, |
| 49 | { USB_G_STR_CONFIG, "default config"}, |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 50 | { }, |
| 51 | }; |
| 52 | |
| 53 | static struct usb_gadget_strings usbg_stringtab = { |
| 54 | .language = 0x0409, |
| 55 | .strings = usbg_us_strings, |
| 56 | }; |
| 57 | |
| 58 | static struct usb_gadget_strings *usbg_strings[] = { |
| 59 | &usbg_stringtab, |
| 60 | NULL, |
| 61 | }; |
| 62 | |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 63 | static struct usb_configuration usbg_config_driver = { |
| 64 | .label = "Linux Target", |
| 65 | .bConfigurationValue = 1, |
| 66 | .iConfiguration = USB_G_STR_CONFIG, |
| 67 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 68 | }; |
| 69 | |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 70 | static int usbg_cfg_bind(struct usb_configuration *c) |
| 71 | { |
Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 72 | return tcm_bind_config(c); |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int usb_target_bind(struct usb_composite_dev *cdev) |
| 76 | { |
| 77 | int ret; |
| 78 | |
| 79 | ret = usb_add_config(cdev, &usbg_config_driver, |
| 80 | usbg_cfg_bind); |
Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | static int guas_unbind(struct usb_composite_dev *cdev) |
| 85 | { |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static struct usb_composite_driver usbg_driver = { |
| 90 | .name = "g_target", |
| 91 | .dev = &usbg_device_desc, |
| 92 | .strings = usbg_strings, |
| 93 | .max_speed = USB_SPEED_SUPER, |
| 94 | .unbind = guas_unbind, |
| 95 | }; |
| 96 | |
Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 97 | static int usbg_attach_cb(bool connect) |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 98 | { |
Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 99 | int ret = 0; |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 100 | |
Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 101 | if (connect) |
| 102 | ret = usb_composite_probe(&usbg_driver, usb_target_bind); |
| 103 | else |
| 104 | usb_composite_unregister(&usbg_driver); |
| 105 | |
| 106 | return ret; |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static int __init usb_target_gadget_init(void) |
| 110 | { |
| 111 | int ret; |
| 112 | |
Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 113 | ret = f_tcm_init(&usbg_attach_cb); |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 114 | return ret; |
| 115 | } |
| 116 | module_init(usb_target_gadget_init); |
| 117 | |
| 118 | static void __exit usb_target_gadget_exit(void) |
| 119 | { |
Pavankumar Kondeti | a51de95 | 2012-06-25 13:57:11 +0530 | [diff] [blame] | 120 | f_tcm_exit(); |
Sebastian Andrzej Siewior | 464cafd | 2012-05-03 19:51:36 -0700 | [diff] [blame] | 121 | } |
| 122 | module_exit(usb_target_gadget_exit); |
| 123 | |
| 124 | MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>"); |
| 125 | MODULE_DESCRIPTION("usb-gadget fabric"); |
| 126 | MODULE_LICENSE("GPL v2"); |