android.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848
  1. /*
  2. * Gadget Driver for Android
  3. *
  4. * Copyright (C) 2008 Google, Inc.
  5. * Author: Mike Lockwood <lockwood@android.com>
  6. * Benoit Goby <benoit@android.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. #ifdef pr_fmt
  19. #undef pr_fmt
  20. #endif
  21. #define pr_fmt(fmt) "["KBUILD_MODNAME"]" fmt
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/fs.h>
  25. #include <linux/delay.h>
  26. #include <linux/kernel.h>
  27. #include <linux/utsname.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/usb/ch9.h>
  30. #include <linux/usb/composite.h>
  31. #include <linux/usb/gadget.h>
  32. #include <linux/printk.h>
  33. /* Add for HW/SW connect */
  34. #include "mtk_gadget.h"
  35. /* Add for HW/SW connect */
  36. #include "gadget_chips.h"
  37. #include "f_fs.c"
  38. #include "f_audio_source.c"
  39. #ifdef CONFIG_SND_RAWMIDI
  40. #include "f_midi.c"
  41. #endif
  42. #include "f_mass_storage.c"
  43. #include "f_adb.c"
  44. #include "f_mtp.c"
  45. #include "f_accessory.c"
  46. #define USB_ETH_RNDIS y
  47. #include "f_rndis.c"
  48. #include "rndis.c"
  49. #include "f_ecm.c"
  50. #include "f_eem.c"
  51. #include "u_ether.c"
  52. #ifdef CONFIG_MTK_C2K_SUPPORT
  53. #include "viatel_rawbulk.h"
  54. int rawbulk_bind_config(struct usb_configuration *c, int transfer_id);
  55. int rawbulk_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl);
  56. #endif
  57. MODULE_AUTHOR("Mike Lockwood");
  58. MODULE_DESCRIPTION("Android Composite USB Driver");
  59. MODULE_LICENSE("GPL");
  60. MODULE_VERSION("1.0");
  61. static const char longname[] = "Gadget Android";
  62. /* Default vendor and product IDs, overridden by userspace */
  63. #define VENDOR_ID 0x0BB4
  64. #define PRODUCT_ID 0x0001
  65. #ifdef CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  66. #include <mt-plat/mt_boot_common.h>
  67. #define KPOC_USB_FUNC "mtp"
  68. #define KPOC_USB_VENDOR_ID 0x0E8D
  69. #define KPOC_USB_PRODUCT_ID 0x2008
  70. #endif
  71. /* f_midi configuration */
  72. #define MIDI_INPUT_PORTS 1
  73. #define MIDI_OUTPUT_PORTS 1
  74. #define MIDI_BUFFER_SIZE 512
  75. #define MIDI_QUEUE_LENGTH 32
  76. /* Default manufacturer and product string , overridden by userspace */
  77. #define MANUFACTURER_STRING "MediaTek"
  78. #define PRODUCT_STRING "MT65xx Android Phone"
  79. //#define USB_LOG "USB"
  80. struct android_usb_function {
  81. char *name;
  82. void *config;
  83. struct device *dev;
  84. char *dev_name;
  85. struct device_attribute **attributes;
  86. /* for android_dev.enabled_functions */
  87. struct list_head enabled_list;
  88. /* Optional: initialization during gadget bind */
  89. int (*init)(struct android_usb_function *, struct usb_composite_dev *);
  90. /* Optional: cleanup during gadget unbind */
  91. void (*cleanup)(struct android_usb_function *);
  92. /* Optional: called when the function is added the list of
  93. * enabled functions */
  94. void (*enable)(struct android_usb_function *);
  95. /* Optional: called when it is removed */
  96. void (*disable)(struct android_usb_function *);
  97. int (*bind_config)(struct android_usb_function *,
  98. struct usb_configuration *);
  99. /* Optional: called when the configuration is removed */
  100. void (*unbind_config)(struct android_usb_function *,
  101. struct usb_configuration *);
  102. /* Optional: handle ctrl requests before the device is configured */
  103. int (*ctrlrequest)(struct android_usb_function *,
  104. struct usb_composite_dev *,
  105. const struct usb_ctrlrequest *);
  106. };
  107. struct android_dev {
  108. struct android_usb_function **functions;
  109. struct list_head enabled_functions;
  110. struct usb_composite_dev *cdev;
  111. struct device *dev;
  112. void (*setup_complete)(struct usb_ep *ep,
  113. struct usb_request *req);
  114. bool enabled;
  115. int disable_depth;
  116. struct mutex mutex;
  117. bool connected;
  118. bool sw_connected;
  119. struct work_struct work;
  120. char ffs_aliases[256];
  121. int rezero_cmd;
  122. };
  123. static struct class *android_class;
  124. static struct android_dev *_android_dev;
  125. static int android_bind_config(struct usb_configuration *c);
  126. static void android_unbind_config(struct usb_configuration *c);
  127. static int android_setup_config(struct usb_configuration *c, const struct usb_ctrlrequest *ctrl);
  128. /* string IDs are assigned dynamically */
  129. #define STRING_MANUFACTURER_IDX 0
  130. #define STRING_PRODUCT_IDX 1
  131. #define STRING_SERIAL_IDX 2
  132. static char manufacturer_string[256];
  133. static char product_string[256];
  134. static char serial_string[256];
  135. /* String Table */
  136. static struct usb_string strings_dev[] = {
  137. [STRING_MANUFACTURER_IDX].s = manufacturer_string,
  138. [STRING_PRODUCT_IDX].s = product_string,
  139. [STRING_SERIAL_IDX].s = serial_string,
  140. { } /* end of list */
  141. };
  142. static struct usb_gadget_strings stringtab_dev = {
  143. .language = 0x0409, /* en-us */
  144. .strings = strings_dev,
  145. };
  146. static struct usb_gadget_strings *dev_strings[] = {
  147. &stringtab_dev,
  148. NULL,
  149. };
  150. static struct usb_device_descriptor device_desc = {
  151. .bLength = sizeof(device_desc),
  152. .bDescriptorType = USB_DT_DEVICE,
  153. #ifdef CONFIG_USB_MU3D_DRV
  154. .bcdUSB = __constant_cpu_to_le16(0x0300),
  155. #else
  156. .bcdUSB = __constant_cpu_to_le16(0x0200),
  157. #endif
  158. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  159. .idVendor = __constant_cpu_to_le16(VENDOR_ID),
  160. .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
  161. /* initialize the value for REV/bcdDevice, modify by xmtjx*/
  162. // .bcdDevice = __constant_cpu_to_le16(0xffff),
  163. .bcdDevice = __constant_cpu_to_le16(0x0200),
  164. .bNumConfigurations = 1,
  165. };
  166. static struct usb_configuration android_config_driver = {
  167. .label = "android",
  168. .unbind = android_unbind_config,
  169. .setup = android_setup_config,
  170. .bConfigurationValue = 1,
  171. #ifdef CONFIG_USBIF_COMPLIANCE
  172. // USBIF, do we need to set bus powered
  173. .bmAttributes = USB_CONFIG_ATT_ONE,
  174. //.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  175. #else
  176. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  177. #endif
  178. #ifdef CONFIG_USB_MU3D_DRV
  179. .MaxPower = 192, /* Only consume 192ma for passing USB30CV Descriptor Test [USB3.0 devices]*/
  180. #else
  181. .MaxPower = 500, /* 500ma */
  182. #endif
  183. };
  184. static void android_work(struct work_struct *data)
  185. {
  186. struct android_dev *dev = container_of(data, struct android_dev, work);
  187. struct usb_composite_dev *cdev = dev->cdev;
  188. char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
  189. char *connected[2] = { "USB_STATE=CONNECTED", NULL };
  190. char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
  191. /* Add for HW/SW connect */
  192. char *hwdisconnected[2] = { "USB_STATE=HWDISCONNECTED", NULL };
  193. // char *hwconnected[2] = { "USB_STATE=HWCONNECTED", NULL };
  194. /* Add for HW/SW connect */
  195. char *rezero_event[2] = { "USB_STATE=REZEROCMD", NULL };
  196. char *showcdrom_event[2] = { "USB_STATE=SHOWCDROMCMD", NULL };
  197. char **uevent_envp = NULL;
  198. char **uevent_envp_cdrom = NULL;
  199. unsigned long flags;
  200. /* Add for HW/SW connect */
  201. bool is_hwconnected = true;
  202. /* patch for ALPS00345130, if the disconnect followed by hw_disconnect, then the hw_disconnect
  203. will not notify the UsbDeviceManager due to that musb->g.speed == USB_SPEED_UNKNOWN*/
  204. if (!cdev){
  205. return ;
  206. }
  207. if (usb_cable_connected())
  208. is_hwconnected = true;
  209. else
  210. is_hwconnected = false;
  211. pr_notice("[USB]%s: is_hwconnected=%d \n", __func__, is_hwconnected);
  212. /* Add for HW/SW connect */
  213. spin_lock_irqsave(&cdev->lock, flags);
  214. if (cdev->config) {
  215. uevent_envp = configured;
  216. } else if (dev->connected != dev->sw_connected) {
  217. uevent_envp = dev->connected ? connected : disconnected;
  218. }
  219. dev->sw_connected = dev->connected;
  220. if (dev->rezero_cmd == 1) {
  221. uevent_envp_cdrom = rezero_event;
  222. dev->rezero_cmd = 0;
  223. } else if (dev->rezero_cmd == 2) {
  224. uevent_envp_cdrom = showcdrom_event;
  225. dev->rezero_cmd = 0;
  226. }
  227. spin_unlock_irqrestore(&cdev->lock, flags);
  228. if (uevent_envp) {
  229. kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
  230. pr_notice("[USB]%s: sent uevent %s\n", __func__, uevent_envp[0]);
  231. } else {
  232. pr_notice("[USB]%s: did not send uevent (%d %d %p)\n", __func__,
  233. dev->connected, dev->sw_connected, cdev->config);
  234. }
  235. if (!is_hwconnected) {
  236. kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, hwdisconnected);
  237. pr_notice("[USB]%s: sent uevent %s\n", __func__, hwdisconnected[0]);
  238. }
  239. if (uevent_envp_cdrom) {
  240. kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp_cdrom);
  241. pr_notice("[USB]%s: sent uevent %s\n", __func__, uevent_envp_cdrom[0]);
  242. } else {
  243. pr_notice("[USB]%s: did not send zero uevent\n", __func__);
  244. }
  245. }
  246. static void android_enable(struct android_dev *dev)
  247. {
  248. struct usb_composite_dev *cdev = dev->cdev;
  249. if (WARN_ON(!dev->disable_depth))
  250. return;
  251. if (--dev->disable_depth == 0) {
  252. usb_add_config(cdev, &android_config_driver,
  253. android_bind_config);
  254. usb_gadget_connect(cdev->gadget);
  255. }
  256. }
  257. static void android_disable(struct android_dev *dev)
  258. {
  259. struct usb_composite_dev *cdev = dev->cdev;
  260. if (dev->disable_depth++ == 0) {
  261. usb_gadget_disconnect(cdev->gadget);
  262. /* Cancel pending control requests */
  263. usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
  264. usb_remove_config(cdev, &android_config_driver);
  265. }
  266. }
  267. /*-------------------------------------------------------------------------*/
  268. /* Supported functions initialization */
  269. struct functionfs_config {
  270. bool opened;
  271. bool enabled;
  272. struct ffs_data *data;
  273. };
  274. static int ffs_function_init(struct android_usb_function *f,
  275. struct usb_composite_dev *cdev)
  276. {
  277. f->config = kzalloc(sizeof(struct functionfs_config), GFP_KERNEL);
  278. if (!f->config)
  279. return -ENOMEM;
  280. return functionfs_init();
  281. }
  282. static void ffs_function_cleanup(struct android_usb_function *f)
  283. {
  284. functionfs_cleanup();
  285. kfree(f->config);
  286. }
  287. static void ffs_function_enable(struct android_usb_function *f)
  288. {
  289. struct android_dev *dev = _android_dev;
  290. struct functionfs_config *config = f->config;
  291. config->enabled = true;
  292. /* Disable the gadget until the function is ready */
  293. if (!config->opened)
  294. android_disable(dev);
  295. }
  296. static void ffs_function_disable(struct android_usb_function *f)
  297. {
  298. struct android_dev *dev = _android_dev;
  299. struct functionfs_config *config = f->config;
  300. config->enabled = false;
  301. /* Balance the disable that was called in closed_callback */
  302. if (!config->opened)
  303. android_enable(dev);
  304. }
  305. static int ffs_function_bind_config(struct android_usb_function *f,
  306. struct usb_configuration *c)
  307. {
  308. struct functionfs_config *config = f->config;
  309. return functionfs_bind_config(c->cdev, c, config->data);
  310. }
  311. static ssize_t
  312. ffs_aliases_show(struct device *pdev, struct device_attribute *attr, char *buf)
  313. {
  314. struct android_dev *dev = _android_dev;
  315. int ret;
  316. mutex_lock(&dev->mutex);
  317. ret = sprintf(buf, "%s\n", dev->ffs_aliases);
  318. mutex_unlock(&dev->mutex);
  319. return ret;
  320. }
  321. static ssize_t
  322. ffs_aliases_store(struct device *pdev, struct device_attribute *attr,
  323. const char *buf, size_t size)
  324. {
  325. struct android_dev *dev = _android_dev;
  326. char buff[256];
  327. mutex_lock(&dev->mutex);
  328. if (dev->enabled) {
  329. mutex_unlock(&dev->mutex);
  330. return -EBUSY;
  331. }
  332. strlcpy(buff, buf, sizeof(buff));
  333. strlcpy(dev->ffs_aliases, strim(buff), sizeof(dev->ffs_aliases));
  334. mutex_unlock(&dev->mutex);
  335. return size;
  336. }
  337. static DEVICE_ATTR(aliases, S_IRUGO | S_IWUSR, ffs_aliases_show,
  338. ffs_aliases_store);
  339. static struct device_attribute *ffs_function_attributes[] = {
  340. &dev_attr_aliases,
  341. NULL
  342. };
  343. static struct android_usb_function ffs_function = {
  344. .name = "ffs",
  345. .init = ffs_function_init,
  346. .enable = ffs_function_enable,
  347. .disable = ffs_function_disable,
  348. .cleanup = ffs_function_cleanup,
  349. .bind_config = ffs_function_bind_config,
  350. .attributes = ffs_function_attributes,
  351. };
  352. static int functionfs_ready_callback(struct ffs_data *ffs)
  353. {
  354. struct android_dev *dev = _android_dev;
  355. struct functionfs_config *config = ffs_function.config;
  356. int ret = 0;
  357. mutex_lock(&dev->mutex);
  358. ret = functionfs_bind(ffs, dev->cdev);
  359. if (ret)
  360. goto err;
  361. config->data = ffs;
  362. config->opened = true;
  363. if (config->enabled)
  364. android_enable(dev);
  365. err:
  366. mutex_unlock(&dev->mutex);
  367. return ret;
  368. }
  369. static void functionfs_closed_callback(struct ffs_data *ffs)
  370. {
  371. struct android_dev *dev = _android_dev;
  372. struct functionfs_config *config = ffs_function.config;
  373. mutex_lock(&dev->mutex);
  374. if (config->enabled)
  375. android_disable(dev);
  376. config->opened = false;
  377. config->data = NULL;
  378. functionfs_unbind(ffs);
  379. mutex_unlock(&dev->mutex);
  380. }
  381. static void *functionfs_acquire_dev_callback(const char *dev_name)
  382. {
  383. return 0;
  384. }
  385. static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
  386. {
  387. }
  388. struct adb_data {
  389. bool opened;
  390. bool enabled;
  391. };
  392. static int
  393. adb_function_init(struct android_usb_function *f,
  394. struct usb_composite_dev *cdev)
  395. {
  396. f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
  397. if (!f->config)
  398. return -ENOMEM;
  399. return adb_setup();
  400. }
  401. static void adb_function_cleanup(struct android_usb_function *f)
  402. {
  403. adb_cleanup();
  404. kfree(f->config);
  405. }
  406. static int
  407. adb_function_bind_config(struct android_usb_function *f,
  408. struct usb_configuration *c)
  409. {
  410. return adb_bind_config(c);
  411. }
  412. static void adb_android_function_enable(struct android_usb_function *f)
  413. {
  414. /* This patch cause WHQL fail */
  415. #if 0
  416. struct android_dev *dev = _android_dev;
  417. struct adb_data *data = f->config;
  418. data->enabled = true;
  419. /* Disable the gadget until adbd is ready */
  420. if (!data->opened)
  421. android_disable(dev);
  422. #endif
  423. }
  424. static void adb_android_function_disable(struct android_usb_function *f)
  425. {
  426. /* This patch cause WHQL fail */
  427. #if 0
  428. struct android_dev *dev = _android_dev;
  429. struct adb_data *data = f->config;
  430. data->enabled = false;
  431. /* Balance the disable that was called in closed_callback */
  432. if (!data->opened)
  433. android_enable(dev);
  434. #endif
  435. }
  436. static struct android_usb_function adb_function = {
  437. .name = "adb",
  438. .enable = adb_android_function_enable,
  439. .disable = adb_android_function_disable,
  440. .init = adb_function_init,
  441. .cleanup = adb_function_cleanup,
  442. .bind_config = adb_function_bind_config,
  443. };
  444. static void adb_ready_callback(void)
  445. {
  446. /* This patch cause WHQL fail */
  447. #if 0
  448. struct android_dev *dev = _android_dev;
  449. struct adb_data *data = adb_function.config;
  450. mutex_lock(&dev->mutex);
  451. data->opened = true;
  452. if (data->enabled)
  453. android_enable(dev);
  454. mutex_unlock(&dev->mutex);
  455. #endif
  456. }
  457. static void adb_closed_callback(void)
  458. {
  459. /* This patch cause WHQL fail */
  460. #if 0
  461. struct android_dev *dev = _android_dev;
  462. struct adb_data *data = adb_function.config;
  463. mutex_lock(&dev->mutex);
  464. data->opened = false;
  465. if (data->enabled)
  466. android_disable(dev);
  467. mutex_unlock(&dev->mutex);
  468. #endif
  469. }
  470. #define MAX_ACM_INSTANCES 4
  471. struct acm_function_config {
  472. int instances;
  473. int instances_on;
  474. struct usb_function *f_acm[MAX_ACM_INSTANCES];
  475. struct usb_function_instance *f_acm_inst[MAX_ACM_INSTANCES];
  476. int port_index[MAX_ACM_INSTANCES];
  477. int port_index_on[MAX_ACM_INSTANCES];
  478. };
  479. static int
  480. acm_function_init(struct android_usb_function *f,
  481. struct usb_composite_dev *cdev)
  482. {
  483. int i;
  484. int ret;
  485. struct acm_function_config *config;
  486. #ifdef CONFIG_USBIF_COMPLIANCE
  487. /* FIXME, USB_IF workaround */
  488. return 0;
  489. #endif
  490. config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
  491. if (!config)
  492. return -ENOMEM;
  493. f->config = config;
  494. for (i = 0; i < MAX_ACM_INSTANCES; i++) {
  495. config->f_acm_inst[i] = usb_get_function_instance("acm");
  496. if (IS_ERR(config->f_acm_inst[i])) {
  497. ret = PTR_ERR(config->f_acm_inst[i]);
  498. goto err_usb_get_function_instance;
  499. }
  500. config->f_acm[i] = usb_get_function(config->f_acm_inst[i]);
  501. if (IS_ERR(config->f_acm[i])) {
  502. ret = PTR_ERR(config->f_acm[i]);
  503. goto err_usb_get_function;
  504. }
  505. }
  506. return 0;
  507. err_usb_get_function_instance:
  508. pr_err("Could not usb_get_function_instance() %d\n", i);
  509. while (i-- > 0) {
  510. usb_put_function(config->f_acm[i]);
  511. err_usb_get_function:
  512. pr_err("Could not usb_get_function() %d\n", i);
  513. usb_put_function_instance(config->f_acm_inst[i]);
  514. }
  515. return ret;
  516. }
  517. static void acm_function_cleanup(struct android_usb_function *f)
  518. {
  519. int i;
  520. struct acm_function_config *config = f->config;
  521. #ifdef CONFIG_USBIF_COMPLIANCE
  522. /* FIXME, USB_IF workaround */
  523. return 0;
  524. #endif
  525. for (i = 0; i < MAX_ACM_INSTANCES; i++) {
  526. usb_put_function(config->f_acm[i]);
  527. usb_put_function_instance(config->f_acm_inst[i]);
  528. }
  529. kfree(f->config);
  530. f->config = NULL;
  531. }
  532. static int
  533. acm_function_bind_config(struct android_usb_function *f,
  534. struct usb_configuration *c)
  535. {
  536. int i;
  537. int ret = 0;
  538. struct acm_function_config *config = f->config;
  539. /*1st:Modem, 2nd:Modem, 3rd:BT, 4th:MD logger*/
  540. for (i = 0; i < MAX_ACM_INSTANCES; i++) {
  541. if(config->port_index[i] != 0) {
  542. ret = usb_add_function(c, config->f_acm[i]);
  543. if (ret) {
  544. pr_err("Could not bind acm%u config\n", i);
  545. goto err_usb_add_function;
  546. }
  547. pr_info("%s Open /dev/ttyGS%d\n", __func__, i);
  548. config->port_index[i] = 0;
  549. config->port_index_on[i] = 1;
  550. config->instances = 0;
  551. }
  552. }
  553. config->instances_on = config->instances;
  554. for (i = 0; i < config->instances_on; i++) {
  555. ret = usb_add_function(c, config->f_acm[i]);
  556. if (ret) {
  557. pr_err("Could not bind acm%u config\n", i);
  558. goto err_usb_add_function;
  559. }
  560. pr_info("%s Open /dev/ttyGS%d\n", __func__, i);
  561. }
  562. return 0;
  563. err_usb_add_function:
  564. while (i-- > 0)
  565. usb_remove_function(c, config->f_acm[i]);
  566. return ret;
  567. }
  568. static void acm_function_unbind_config(struct android_usb_function *f,
  569. struct usb_configuration *c)
  570. {
  571. //int i;
  572. //struct acm_function_config *config = f->config;
  573. /* REVISIT:
  574. * list_del(&f->list) and f->unbind() are called at unbind_config().
  575. * f->disable() is called at enable_store.
  576. * So directly return this function.
  577. * If does NOT return, f->list is deleted twice and cuased KE.
  578. * ToDo:
  579. * What does the original kernel code look likes?
  580. */
  581. return;
  582. /*
  583. if (config->instances_on != 0) {
  584. for (i = 0; i < config->instances_on; i++) {
  585. pr_notice("%s f_acm[%d]=%p\n", __func__, i, config->f_acm[i]);
  586. usb_remove_function(c, config->f_acm[i]);
  587. }
  588. } else {
  589. for (i = 0; i < MAX_ACM_INSTANCES; i++)
  590. pr_notice("%s port_index_on=%d\n", __func__, config->port_index_on[i]);
  591. if (config->port_index_on[i] != 0) {
  592. usb_remove_function(c, config->f_acm[i]);
  593. }
  594. }
  595. */
  596. }
  597. static ssize_t acm_instances_show(struct device *dev,
  598. struct device_attribute *attr, char *buf)
  599. {
  600. struct android_usb_function *f = dev_get_drvdata(dev);
  601. struct acm_function_config *config = f->config;
  602. return sprintf(buf, "%d\n", config->instances);
  603. }
  604. static ssize_t acm_instances_store(struct device *dev,
  605. struct device_attribute *attr, const char *buf, size_t size)
  606. {
  607. struct android_usb_function *f = dev_get_drvdata(dev);
  608. struct acm_function_config *config = f->config;
  609. int value;
  610. sscanf(buf, "%d", &value);
  611. if (value > MAX_ACM_INSTANCES)
  612. value = MAX_ACM_INSTANCES;
  613. config->instances = value;
  614. return size;
  615. }
  616. static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show,
  617. acm_instances_store);
  618. static ssize_t acm_port_index_show(struct device *dev,
  619. struct device_attribute *attr, char *buf)
  620. {
  621. struct android_usb_function *f = dev_get_drvdata(dev);
  622. struct acm_function_config *config = f->config;
  623. return sprintf(buf, "%d,%d,%d,%d\n", config->port_index[0], config->port_index[1],
  624. config->port_index[2], config->port_index[3]);
  625. }
  626. static ssize_t acm_port_index_store(struct device *dev,
  627. struct device_attribute *attr, const char *buf, size_t size)
  628. {
  629. struct android_usb_function *f = dev_get_drvdata(dev);
  630. struct acm_function_config *config = f->config;
  631. int val[MAX_ACM_INSTANCES]={0};
  632. int num = 0;
  633. int tmp = 0;
  634. num = sscanf(buf, "%d,%d,%d,%d", &(val[0]), &(val[1]), &(val[2]), &(val[3]));
  635. pr_notice("%s [0]=%d,[1]=%d,[2]=%d,[3]=%d, num=%d\n", __func__, val[0], val[1], \
  636. val[2], val[3], num);
  637. /* Set all port_index as 0*/
  638. for (tmp = 0; tmp < MAX_ACM_INSTANCES; tmp ++)
  639. config->port_index[tmp] = 0;
  640. for (tmp = 0; tmp < num; tmp++) {
  641. int port = (val[tmp] > MAX_ACM_INSTANCES || val[tmp] < 1) ? 0 : val[tmp]-1;
  642. config->port_index[port] = 1;
  643. }
  644. return size;
  645. }
  646. static DEVICE_ATTR(port_index, S_IRUGO | S_IWUSR, acm_port_index_show,
  647. acm_port_index_store);
  648. static struct device_attribute *acm_function_attributes[] = {
  649. &dev_attr_instances,
  650. &dev_attr_port_index, /*Only open the specific port*/
  651. NULL
  652. };
  653. static struct android_usb_function acm_function = {
  654. .name = "acm",
  655. .init = acm_function_init,
  656. .cleanup = acm_function_cleanup,
  657. .bind_config = acm_function_bind_config,
  658. .unbind_config = acm_function_unbind_config,
  659. .attributes = acm_function_attributes,
  660. };
  661. #ifdef CONFIG_USB_F_LOOPBACK
  662. #define MAX_LOOPBACK_INSTANCES 1
  663. struct loopback_function_config {
  664. int port_num;
  665. struct usb_function *f_lp[MAX_LOOPBACK_INSTANCES];
  666. struct usb_function_instance *f_lp_inst[MAX_LOOPBACK_INSTANCES];
  667. };
  668. static int loopback_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
  669. {
  670. int i;
  671. int ret;
  672. struct loopback_function_config *config;
  673. config = kzalloc(sizeof(struct loopback_function_config), GFP_KERNEL);
  674. if (!config)
  675. return -ENOMEM;
  676. f->config = config;
  677. for (i = 0; i < MAX_LOOPBACK_INSTANCES; i++) {
  678. config->f_lp_inst[i] = usb_get_function_instance("loopback");
  679. if (IS_ERR(config->f_lp_inst[i])) {
  680. ret = PTR_ERR(config->f_lp_inst[i]);
  681. goto err_usb_get_function_instance;
  682. }
  683. config->f_lp[i] = usb_get_function(config->f_lp_inst[i]);
  684. if (IS_ERR(config->f_lp[i])) {
  685. ret = PTR_ERR(config->f_lp[i]);
  686. goto err_usb_get_function;
  687. }
  688. }
  689. return 0;
  690. err_usb_get_function_instance:
  691. pr_err("Could not usb_get_function_instance() %d\n", i);
  692. while (i-- > 0) {
  693. usb_put_function(config->f_lp[i]);
  694. err_usb_get_function:
  695. pr_err("Could not usb_get_function() %d\n", i);
  696. usb_put_function_instance(config->f_lp_inst[i]);
  697. }
  698. return ret;
  699. }
  700. static void loopback_function_cleanup(struct android_usb_function *f)
  701. {
  702. int i;
  703. struct loopback_function_config *config = f->config;
  704. for (i = 0; i < MAX_LOOPBACK_INSTANCES; i++) {
  705. usb_put_function(config->f_lp[i]);
  706. usb_put_function_instance(config->f_lp_inst[i]);
  707. }
  708. kfree(f->config);
  709. f->config = NULL;
  710. }
  711. static int
  712. loopback_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
  713. {
  714. int ret = 0;
  715. struct loopback_function_config *config = f->config;
  716. ret = usb_add_function(c, config->f_lp[config->port_num]);
  717. if (ret) {
  718. pr_err("Could not bind loopback%u config\n", config->port_num);
  719. goto err_usb_add_function;
  720. }
  721. pr_info("%s Open loopback\n", __func__);
  722. return 0;
  723. err_usb_add_function:
  724. usb_remove_function(c, config->f_lp[config->port_num]);
  725. return ret;
  726. }
  727. static struct android_usb_function loopback_function = {
  728. .name = "loopback",
  729. .init = loopback_function_init,
  730. .cleanup = loopback_function_cleanup,
  731. .bind_config = loopback_function_bind_config,
  732. };
  733. #endif
  734. #define MAX_SERIAL_INSTANCES 4
  735. struct serial_function_config {
  736. int port_num;
  737. struct usb_function *f_ser[MAX_SERIAL_INSTANCES];
  738. struct usb_function_instance *f_ser_inst[MAX_SERIAL_INSTANCES];
  739. };
  740. static int serial_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
  741. {
  742. int i;
  743. int ret;
  744. struct serial_function_config *config;
  745. #ifdef CONFIG_USBIF_COMPLIANCE
  746. /* FIXME, USB_IF workaround */
  747. return 0;
  748. #endif
  749. config = kzalloc(sizeof(struct serial_function_config), GFP_KERNEL);
  750. if (!config)
  751. return -ENOMEM;
  752. f->config = config;
  753. for (i = 0; i < MAX_SERIAL_INSTANCES; i++) {
  754. config->f_ser_inst[i] = usb_get_function_instance("gser");
  755. if (IS_ERR(config->f_ser_inst[i])) {
  756. ret = PTR_ERR(config->f_ser_inst[i]);
  757. goto err_usb_get_function_instance;
  758. }
  759. config->f_ser[i] = usb_get_function(config->f_ser_inst[i]);
  760. if (IS_ERR(config->f_ser[i])) {
  761. ret = PTR_ERR(config->f_ser[i]);
  762. goto err_usb_get_function;
  763. }
  764. }
  765. return 0;
  766. err_usb_get_function_instance:
  767. pr_err("Could not usb_get_function_instance() %d\n", i);
  768. while (i-- > 0) {
  769. usb_put_function(config->f_ser[i]);
  770. err_usb_get_function:
  771. pr_err("Could not usb_get_function() %d\n", i);
  772. usb_put_function_instance(config->f_ser_inst[i]);
  773. }
  774. return ret;
  775. }
  776. static void serial_function_cleanup(struct android_usb_function *f)
  777. {
  778. int i;
  779. struct serial_function_config *config = f->config;
  780. #ifdef CONFIG_USBIF_COMPLIANCE
  781. /* FIXME, USB_IF workaround */
  782. return 0;
  783. #endif
  784. for (i = 0; i < MAX_SERIAL_INSTANCES; i++) {
  785. usb_put_function(config->f_ser[i]);
  786. usb_put_function_instance(config->f_ser_inst[i]);
  787. }
  788. kfree(f->config);
  789. f->config = NULL;
  790. }
  791. static int
  792. serial_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
  793. {
  794. int ret = 0;
  795. struct serial_function_config *config = f->config;
  796. ret = usb_add_function(c, config->f_ser[config->port_num]);
  797. if (ret) {
  798. pr_err("Could not bind ser%u config\n", config->port_num);
  799. goto err_usb_add_function;
  800. }
  801. pr_info("%s Open /dev/ttyGS%d\n", __func__, config->port_num);
  802. return 0;
  803. err_usb_add_function:
  804. usb_remove_function(c, config->f_ser[config->port_num]);
  805. return ret;
  806. }
  807. static ssize_t serial_port_show(struct device *dev,
  808. struct device_attribute *attr, char *buf)
  809. {
  810. struct android_usb_function *f = dev_get_drvdata(dev);
  811. struct serial_function_config *config = f->config;
  812. return sprintf(buf, "%d\n", config->port_num);
  813. }
  814. static ssize_t serial_port_store(struct device *dev,
  815. struct device_attribute *attr, const char *buf, size_t size)
  816. {
  817. struct android_usb_function *f = dev_get_drvdata(dev);
  818. struct serial_function_config *config = f->config;
  819. int value;
  820. sscanf(buf, "%d", &value);
  821. if (value > MAX_SERIAL_INSTANCES)
  822. value = MAX_SERIAL_INSTANCES;
  823. config->port_num = value;
  824. return size;
  825. }
  826. static DEVICE_ATTR(port, S_IRUGO | S_IWUSR, serial_port_show, serial_port_store);
  827. static struct device_attribute *serial_function_attributes[] = { &dev_attr_port, NULL };
  828. static struct android_usb_function serial_function = {
  829. .name = "gser",
  830. .init = serial_function_init,
  831. .cleanup = serial_function_cleanup,
  832. .bind_config = serial_function_bind_config,
  833. .attributes = serial_function_attributes,
  834. };
  835. static int
  836. mtp_function_init(struct android_usb_function *f,
  837. struct usb_composite_dev *cdev)
  838. {
  839. return mtp_setup();
  840. }
  841. static void mtp_function_cleanup(struct android_usb_function *f)
  842. {
  843. mtp_cleanup();
  844. }
  845. static int
  846. mtp_function_bind_config(struct android_usb_function *f,
  847. struct usb_configuration *c)
  848. {
  849. return mtp_bind_config(c, false);
  850. }
  851. static int
  852. ptp_function_init(struct android_usb_function *f,
  853. struct usb_composite_dev *cdev)
  854. {
  855. /* nothing to do - initialization is handled by mtp_function_init */
  856. return 0;
  857. }
  858. static void ptp_function_cleanup(struct android_usb_function *f)
  859. {
  860. /* nothing to do - cleanup is handled by mtp_function_cleanup */
  861. }
  862. static int
  863. ptp_function_bind_config(struct android_usb_function *f,
  864. struct usb_configuration *c)
  865. {
  866. return mtp_bind_config(c, true);
  867. }
  868. static int mtp_function_ctrlrequest(struct android_usb_function *f,
  869. struct usb_composite_dev *cdev,
  870. const struct usb_ctrlrequest *c)
  871. {
  872. /* MTP MSFT OS Descriptor */
  873. struct android_dev *dev = _android_dev;
  874. struct android_usb_function *f_count;
  875. int functions_no=0;
  876. char usb_function_string[32];
  877. char * buff = usb_function_string;
  878. list_for_each_entry(f_count, &dev->enabled_functions, enabled_list)
  879. {
  880. functions_no++;
  881. buff += sprintf(buff, "%s,", f_count->name);
  882. }
  883. *(buff-1) = '\n';
  884. mtp_read_usb_functions(functions_no, usb_function_string);
  885. return mtp_ctrlrequest(cdev, c);
  886. }
  887. static int ptp_function_ctrlrequest(struct android_usb_function *f,
  888. struct usb_composite_dev *cdev,
  889. const struct usb_ctrlrequest *c)
  890. {
  891. return ptp_ctrlrequest(cdev, c);
  892. }
  893. static struct android_usb_function mtp_function = {
  894. .name = "mtp",
  895. .init = mtp_function_init,
  896. .cleanup = mtp_function_cleanup,
  897. .bind_config = mtp_function_bind_config,
  898. .ctrlrequest = mtp_function_ctrlrequest,
  899. };
  900. /* PTP function is same as MTP with slightly different interface descriptor */
  901. static struct android_usb_function ptp_function = {
  902. .name = "ptp",
  903. .init = ptp_function_init,
  904. .cleanup = ptp_function_cleanup,
  905. .bind_config = ptp_function_bind_config,
  906. .ctrlrequest = ptp_function_ctrlrequest, //ALPS01832160
  907. };
  908. struct ecm_function_config {
  909. u8 ethaddr[ETH_ALEN];
  910. struct eth_dev *dev;
  911. };
  912. static int ecm_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
  913. {
  914. f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
  915. if (!f->config)
  916. return -ENOMEM;
  917. return 0;
  918. }
  919. static void ecm_function_cleanup(struct android_usb_function *f)
  920. {
  921. kfree(f->config);
  922. f->config = NULL;
  923. }
  924. static int
  925. ecm_function_bind_config(struct android_usb_function *f,
  926. struct usb_configuration *c)
  927. {
  928. int ret;
  929. struct eth_dev *dev;
  930. struct ecm_function_config *ecm = f->config;
  931. if (!ecm) {
  932. pr_err("%s: ecm_pdata\n", __func__);
  933. return -1;
  934. }
  935. pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
  936. ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
  937. ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
  938. dev = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "rndis");
  939. if (IS_ERR(dev)) {
  940. ret = PTR_ERR(dev);
  941. pr_err("%s: gether_setup failed\n", __func__);
  942. return ret;
  943. }
  944. ecm->dev = dev;
  945. return ecm_bind_config(c, ecm->ethaddr, ecm->dev);
  946. }
  947. static void ecm_function_unbind_config(struct android_usb_function *f,
  948. struct usb_configuration *c)
  949. {
  950. struct ecm_function_config *ecm = f->config;
  951. gether_cleanup(ecm->dev);
  952. }
  953. static ssize_t ecm_ethaddr_show(struct device *dev,
  954. struct device_attribute *attr, char *buf)
  955. {
  956. struct android_usb_function *f = dev_get_drvdata(dev);
  957. struct ecm_function_config *ecm = f->config;
  958. return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  959. ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
  960. ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
  961. }
  962. static ssize_t ecm_ethaddr_store(struct device *dev,
  963. struct device_attribute *attr, const char *buf, size_t size)
  964. {
  965. struct android_usb_function *f = dev_get_drvdata(dev);
  966. struct ecm_function_config *ecm = f->config;
  967. if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  968. (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
  969. (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
  970. (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
  971. return size;
  972. return -EINVAL;
  973. }
  974. static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
  975. ecm_ethaddr_store);
  976. static struct device_attribute *ecm_function_attributes[] = {
  977. &dev_attr_ecm_ethaddr,
  978. NULL
  979. };
  980. static struct android_usb_function ecm_function = {
  981. .name = "ecm",
  982. .init = ecm_function_init,
  983. .cleanup = ecm_function_cleanup,
  984. .bind_config = ecm_function_bind_config,
  985. .unbind_config = ecm_function_unbind_config,
  986. .attributes = ecm_function_attributes,
  987. };
  988. struct eem_function_config {
  989. u8 ethaddr[ETH_ALEN];
  990. char manufacturer[256];
  991. struct eth_dev *dev;
  992. };
  993. static int
  994. eem_function_init(struct android_usb_function *f,
  995. struct usb_composite_dev *cdev)
  996. {
  997. f->config = kzalloc(sizeof(struct eem_function_config), GFP_KERNEL);
  998. if (!f->config)
  999. return -ENOMEM;
  1000. return 0;
  1001. }
  1002. static void eem_function_cleanup(struct android_usb_function *f)
  1003. {
  1004. kfree(f->config);
  1005. f->config = NULL;
  1006. }
  1007. static int
  1008. eem_function_bind_config(struct android_usb_function *f,
  1009. struct usb_configuration *c)
  1010. {
  1011. int ret;
  1012. struct eth_dev *dev;
  1013. struct eem_function_config *eem = f->config;
  1014. pr_notice("[USB]%s: \n", __func__);
  1015. if (!eem) {
  1016. pr_err("%s: rndis_pdata\n", __func__);
  1017. return -1;
  1018. }
  1019. pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
  1020. eem->ethaddr[0], eem->ethaddr[1], eem->ethaddr[2],
  1021. eem->ethaddr[3], eem->ethaddr[4], eem->ethaddr[5]);
  1022. dev = gether_setup_name(c->cdev->gadget, eem->ethaddr, "rndis");
  1023. if (IS_ERR(dev)) {
  1024. ret = PTR_ERR(dev);
  1025. pr_err("%s: gether_setup failed\n", __func__);
  1026. return ret;
  1027. }
  1028. eem->dev = dev;
  1029. return eem_bind_config(c, eem->dev);
  1030. }
  1031. static void eem_function_unbind_config(struct android_usb_function *f,
  1032. struct usb_configuration *c)
  1033. {
  1034. struct eem_function_config *eem = f->config;
  1035. gether_cleanup(eem->dev);
  1036. }
  1037. static ssize_t eem_ethaddr_show(struct device *dev,
  1038. struct device_attribute *attr, char *buf)
  1039. {
  1040. struct android_usb_function *f = dev_get_drvdata(dev);
  1041. struct eem_function_config *eem = f->config;
  1042. return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  1043. eem->ethaddr[0], eem->ethaddr[1], eem->ethaddr[2],
  1044. eem->ethaddr[3], eem->ethaddr[4], eem->ethaddr[5]);
  1045. }
  1046. static ssize_t eem_ethaddr_store(struct device *dev,
  1047. struct device_attribute *attr, const char *buf, size_t size)
  1048. {
  1049. struct android_usb_function *f = dev_get_drvdata(dev);
  1050. struct eem_function_config *eem = f->config;
  1051. if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  1052. (int *)&eem->ethaddr[0], (int *)&eem->ethaddr[1],
  1053. (int *)&eem->ethaddr[2], (int *)&eem->ethaddr[3],
  1054. (int *)&eem->ethaddr[4], (int *)&eem->ethaddr[5]) == 6)
  1055. return size;
  1056. return -EINVAL;
  1057. }
  1058. static DEVICE_ATTR(eem_ethaddr, S_IRUGO | S_IWUSR, eem_ethaddr_show,
  1059. eem_ethaddr_store);
  1060. static struct device_attribute *eem_function_attributes[] = {
  1061. &dev_attr_eem_ethaddr,
  1062. NULL
  1063. };
  1064. static struct android_usb_function eem_function = {
  1065. .name = "eem",
  1066. .init = eem_function_init,
  1067. .cleanup = eem_function_cleanup,
  1068. .bind_config = eem_function_bind_config,
  1069. .unbind_config = eem_function_unbind_config,
  1070. .attributes = eem_function_attributes,
  1071. };
  1072. struct rndis_function_config {
  1073. u8 ethaddr[ETH_ALEN];
  1074. u32 vendorID;
  1075. char manufacturer[256];
  1076. /* "Wireless" RNDIS; auto-detected by Windows */
  1077. bool wceis;
  1078. struct eth_dev *dev;
  1079. };
  1080. static int
  1081. rndis_function_init(struct android_usb_function *f,
  1082. struct usb_composite_dev *cdev)
  1083. {
  1084. f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
  1085. if (!f->config)
  1086. return -ENOMEM;
  1087. return 0;
  1088. }
  1089. static void rndis_function_cleanup(struct android_usb_function *f)
  1090. {
  1091. kfree(f->config);
  1092. f->config = NULL;
  1093. }
  1094. static int
  1095. rndis_function_bind_config(struct android_usb_function *f,
  1096. struct usb_configuration *c)
  1097. {
  1098. int ret;
  1099. struct eth_dev *dev;
  1100. struct rndis_function_config *rndis = f->config;
  1101. pr_notice("[USB]%s: \n", __func__);
  1102. if (!rndis) {
  1103. pr_err("%s: rndis_pdata\n", __func__);
  1104. return -1;
  1105. }
  1106. pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
  1107. rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
  1108. rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
  1109. dev = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
  1110. if (IS_ERR(dev)) {
  1111. ret = PTR_ERR(dev);
  1112. pr_err("%s: gether_setup failed\n", __func__);
  1113. return ret;
  1114. }
  1115. rndis->dev = dev;
  1116. if (rndis->wceis) {
  1117. /* "Wireless" RNDIS; auto-detected by Windows */
  1118. rndis_iad_descriptor.bFunctionClass =
  1119. USB_CLASS_WIRELESS_CONTROLLER;
  1120. rndis_iad_descriptor.bFunctionSubClass = 0x01;
  1121. rndis_iad_descriptor.bFunctionProtocol = 0x03;
  1122. rndis_control_intf.bInterfaceClass =
  1123. USB_CLASS_WIRELESS_CONTROLLER;
  1124. rndis_control_intf.bInterfaceSubClass = 0x01;
  1125. rndis_control_intf.bInterfaceProtocol = 0x03;
  1126. }
  1127. return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
  1128. rndis->manufacturer, rndis->dev);
  1129. }
  1130. static void rndis_function_unbind_config(struct android_usb_function *f,
  1131. struct usb_configuration *c)
  1132. {
  1133. struct rndis_function_config *rndis = f->config;
  1134. gether_cleanup(rndis->dev);
  1135. }
  1136. static ssize_t rndis_manufacturer_show(struct device *dev,
  1137. struct device_attribute *attr, char *buf)
  1138. {
  1139. struct android_usb_function *f = dev_get_drvdata(dev);
  1140. struct rndis_function_config *config = f->config;
  1141. return sprintf(buf, "%s\n", config->manufacturer);
  1142. }
  1143. static ssize_t rndis_manufacturer_store(struct device *dev,
  1144. struct device_attribute *attr, const char *buf, size_t size)
  1145. {
  1146. struct android_usb_function *f = dev_get_drvdata(dev);
  1147. struct rndis_function_config *config = f->config;
  1148. if (size >= sizeof(config->manufacturer))
  1149. return -EINVAL;
  1150. if (sscanf(buf, "%s", config->manufacturer) == 1)
  1151. return size;
  1152. return -1;
  1153. }
  1154. static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
  1155. rndis_manufacturer_store);
  1156. static ssize_t rndis_wceis_show(struct device *dev,
  1157. struct device_attribute *attr, char *buf)
  1158. {
  1159. struct android_usb_function *f = dev_get_drvdata(dev);
  1160. struct rndis_function_config *config = f->config;
  1161. return sprintf(buf, "%d\n", config->wceis);
  1162. }
  1163. static ssize_t rndis_wceis_store(struct device *dev,
  1164. struct device_attribute *attr, const char *buf, size_t size)
  1165. {
  1166. struct android_usb_function *f = dev_get_drvdata(dev);
  1167. struct rndis_function_config *config = f->config;
  1168. int value;
  1169. if (sscanf(buf, "%d", &value) == 1) {
  1170. config->wceis = value;
  1171. return size;
  1172. }
  1173. return -EINVAL;
  1174. }
  1175. static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
  1176. rndis_wceis_store);
  1177. static ssize_t rndis_ethaddr_show(struct device *dev,
  1178. struct device_attribute *attr, char *buf)
  1179. {
  1180. struct android_usb_function *f = dev_get_drvdata(dev);
  1181. struct rndis_function_config *rndis = f->config;
  1182. return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  1183. rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
  1184. rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
  1185. }
  1186. static ssize_t rndis_ethaddr_store(struct device *dev,
  1187. struct device_attribute *attr, const char *buf, size_t size)
  1188. {
  1189. struct android_usb_function *f = dev_get_drvdata(dev);
  1190. struct rndis_function_config *rndis = f->config;
  1191. if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  1192. (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
  1193. (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
  1194. (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
  1195. return size;
  1196. return -EINVAL;
  1197. }
  1198. static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
  1199. rndis_ethaddr_store);
  1200. static ssize_t rndis_vendorID_show(struct device *dev,
  1201. struct device_attribute *attr, char *buf)
  1202. {
  1203. struct android_usb_function *f = dev_get_drvdata(dev);
  1204. struct rndis_function_config *config = f->config;
  1205. return sprintf(buf, "%04x\n", config->vendorID);
  1206. }
  1207. static ssize_t rndis_vendorID_store(struct device *dev,
  1208. struct device_attribute *attr, const char *buf, size_t size)
  1209. {
  1210. struct android_usb_function *f = dev_get_drvdata(dev);
  1211. struct rndis_function_config *config = f->config;
  1212. int value;
  1213. if (sscanf(buf, "%04x", &value) == 1) {
  1214. config->vendorID = value;
  1215. return size;
  1216. }
  1217. return -EINVAL;
  1218. }
  1219. static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
  1220. rndis_vendorID_store);
  1221. static struct device_attribute *rndis_function_attributes[] = {
  1222. &dev_attr_manufacturer,
  1223. &dev_attr_wceis,
  1224. &dev_attr_ethaddr,
  1225. &dev_attr_vendorID,
  1226. NULL
  1227. };
  1228. static struct android_usb_function rndis_function = {
  1229. .name = "rndis",
  1230. .init = rndis_function_init,
  1231. .cleanup = rndis_function_cleanup,
  1232. .bind_config = rndis_function_bind_config,
  1233. .unbind_config = rndis_function_unbind_config,
  1234. .attributes = rndis_function_attributes,
  1235. };
  1236. struct mass_storage_function_config {
  1237. struct fsg_config fsg;
  1238. struct fsg_common *common;
  1239. };
  1240. static int mass_storage_function_init(struct android_usb_function *f,
  1241. struct usb_composite_dev *cdev)
  1242. {
  1243. struct mass_storage_function_config *config;
  1244. struct fsg_common *common;
  1245. int err;
  1246. int i;
  1247. config = kzalloc(sizeof(struct mass_storage_function_config),
  1248. GFP_KERNEL);
  1249. if (!config)
  1250. return -ENOMEM;
  1251. #ifdef CONFIG_MTK_MULTI_STORAGE_SUPPORT
  1252. #ifdef CONFIG_MTK_SHARED_SDCARD
  1253. #define NLUN_STORAGE 1
  1254. #else
  1255. #define NLUN_STORAGE 2
  1256. #endif
  1257. #else
  1258. #define NLUN_STORAGE 1
  1259. #endif
  1260. #ifdef CONFIG_MTK_ICUSB_SUPPORT
  1261. #define NLUN_ICUSB (1)
  1262. #else
  1263. #define NLUN_ICUSB (0)
  1264. #endif
  1265. config->fsg.nluns = NLUN_STORAGE + NLUN_ICUSB;
  1266. for(i = 0; i < config->fsg.nluns; i++) {
  1267. config->fsg.luns[i].removable = 1;
  1268. config->fsg.luns[i].nofua = 1;
  1269. }
  1270. common = fsg_common_init(NULL, cdev, &config->fsg);
  1271. if (IS_ERR(common)) {
  1272. kfree(config);
  1273. return PTR_ERR(common);
  1274. }
  1275. err = sysfs_create_link(&f->dev->kobj,
  1276. &common->luns[0].dev.kobj,
  1277. "lun");
  1278. if (err) {
  1279. kfree(config);
  1280. return err;
  1281. }
  1282. /*
  1283. * "i" starts from "1", cuz dont want to change the naming of
  1284. * the original path of "lun0".
  1285. */
  1286. for(i = 1; i < config->fsg.nluns; i++) {
  1287. char string_lun[5]={0};
  1288. sprintf(string_lun, "lun%d",i);
  1289. err = sysfs_create_link(&f->dev->kobj,
  1290. &common->luns[i].dev.kobj,
  1291. string_lun);
  1292. if (err) {
  1293. kfree(config);
  1294. return err;
  1295. }
  1296. }
  1297. config->common = common;
  1298. f->config = config;
  1299. return 0;
  1300. }
  1301. static void mass_storage_function_cleanup(struct android_usb_function *f)
  1302. {
  1303. kfree(f->config);
  1304. f->config = NULL;
  1305. }
  1306. static int mass_storage_function_bind_config(struct android_usb_function *f,
  1307. struct usb_configuration *c)
  1308. {
  1309. struct mass_storage_function_config *config = f->config;
  1310. return fsg_bind_config(c->cdev, c, config->common);
  1311. }
  1312. static ssize_t mass_storage_inquiry_show(struct device *dev,
  1313. struct device_attribute *attr, char *buf)
  1314. {
  1315. struct android_usb_function *f = dev_get_drvdata(dev);
  1316. struct mass_storage_function_config *config = f->config;
  1317. return sprintf(buf, "%s\n", config->common->inquiry_string);
  1318. }
  1319. static ssize_t mass_storage_inquiry_store(struct device *dev,
  1320. struct device_attribute *attr, const char *buf, size_t size)
  1321. {
  1322. struct android_usb_function *f = dev_get_drvdata(dev);
  1323. struct mass_storage_function_config *config = f->config;
  1324. if (size >= sizeof(config->common->inquiry_string))
  1325. return -EINVAL;
  1326. if (sscanf(buf, "%s", config->common->inquiry_string) != 1)
  1327. return -EINVAL;
  1328. return size;
  1329. }
  1330. static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
  1331. mass_storage_inquiry_show,
  1332. mass_storage_inquiry_store);
  1333. static ssize_t mass_storage_bicr_show(struct device *dev,
  1334. struct device_attribute *attr, char *buf)
  1335. {
  1336. struct android_usb_function *f = dev_get_drvdata(dev);
  1337. struct mass_storage_function_config *config = f->config;
  1338. return sprintf(buf, "%d\n", config->common->bicr);
  1339. }
  1340. static ssize_t mass_storage_bicr_store(struct device *dev,
  1341. struct device_attribute *attr, const char *buf, size_t size)
  1342. {
  1343. struct android_usb_function *f = dev_get_drvdata(dev);
  1344. struct mass_storage_function_config *config = f->config;
  1345. if (size >= sizeof(config->common->bicr))
  1346. return -EINVAL;
  1347. if (sscanf(buf, "%d", &config->common->bicr) != 1)
  1348. return -EINVAL;
  1349. /* Set Lun[0] is a CDROM when enable bicr.*/
  1350. if (!strcmp(buf, "1"))
  1351. config->common->luns[0].cdrom = 1;
  1352. else {
  1353. /*Reset the value. Clean the cdrom's parameters*/
  1354. config->common->luns[0].cdrom = 0;
  1355. config->common->luns[0].blkbits = 0;
  1356. config->common->luns[0].blksize = 0;
  1357. config->common->luns[0].num_sectors = 0;
  1358. }
  1359. return size;
  1360. }
  1361. static DEVICE_ATTR(bicr, S_IRUGO | S_IWUSR,
  1362. mass_storage_bicr_show,
  1363. mass_storage_bicr_store);
  1364. static struct device_attribute *mass_storage_function_attributes[] = {
  1365. &dev_attr_inquiry_string,
  1366. &dev_attr_bicr,
  1367. NULL
  1368. };
  1369. static struct android_usb_function mass_storage_function = {
  1370. .name = "mass_storage",
  1371. .init = mass_storage_function_init,
  1372. .cleanup = mass_storage_function_cleanup,
  1373. .bind_config = mass_storage_function_bind_config,
  1374. .attributes = mass_storage_function_attributes,
  1375. };
  1376. static int accessory_function_init(struct android_usb_function *f,
  1377. struct usb_composite_dev *cdev)
  1378. {
  1379. return acc_setup();
  1380. }
  1381. static void accessory_function_cleanup(struct android_usb_function *f)
  1382. {
  1383. acc_cleanup();
  1384. }
  1385. static int accessory_function_bind_config(struct android_usb_function *f,
  1386. struct usb_configuration *c)
  1387. {
  1388. return acc_bind_config(c);
  1389. }
  1390. static int accessory_function_ctrlrequest(struct android_usb_function *f,
  1391. struct usb_composite_dev *cdev,
  1392. const struct usb_ctrlrequest *c)
  1393. {
  1394. return acc_ctrlrequest(cdev, c);
  1395. }
  1396. static struct android_usb_function accessory_function = {
  1397. .name = "accessory",
  1398. .init = accessory_function_init,
  1399. .cleanup = accessory_function_cleanup,
  1400. .bind_config = accessory_function_bind_config,
  1401. .ctrlrequest = accessory_function_ctrlrequest,
  1402. };
  1403. static int audio_source_function_init(struct android_usb_function *f,
  1404. struct usb_composite_dev *cdev)
  1405. {
  1406. struct audio_source_config *config;
  1407. config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
  1408. if (!config)
  1409. return -ENOMEM;
  1410. config->card = -1;
  1411. config->device = -1;
  1412. f->config = config;
  1413. return 0;
  1414. }
  1415. static void audio_source_function_cleanup(struct android_usb_function *f)
  1416. {
  1417. kfree(f->config);
  1418. }
  1419. static int audio_source_function_bind_config(struct android_usb_function *f,
  1420. struct usb_configuration *c)
  1421. {
  1422. struct audio_source_config *config = f->config;
  1423. return audio_source_bind_config(c, config);
  1424. }
  1425. static void audio_source_function_unbind_config(struct android_usb_function *f,
  1426. struct usb_configuration *c)
  1427. {
  1428. struct audio_source_config *config = f->config;
  1429. config->card = -1;
  1430. config->device = -1;
  1431. }
  1432. static ssize_t audio_source_pcm_show(struct device *dev,
  1433. struct device_attribute *attr, char *buf)
  1434. {
  1435. struct android_usb_function *f = dev_get_drvdata(dev);
  1436. struct audio_source_config *config = f->config;
  1437. /* print PCM card and device numbers */
  1438. return sprintf(buf, "%d %d\n", config->card, config->device);
  1439. }
  1440. static DEVICE_ATTR(pcm, S_IRUGO, audio_source_pcm_show, NULL);
  1441. static struct device_attribute *audio_source_function_attributes[] = {
  1442. &dev_attr_pcm,
  1443. NULL
  1444. };
  1445. static struct android_usb_function audio_source_function = {
  1446. .name = "audio_source",
  1447. .init = audio_source_function_init,
  1448. .cleanup = audio_source_function_cleanup,
  1449. .bind_config = audio_source_function_bind_config,
  1450. .unbind_config = audio_source_function_unbind_config,
  1451. .attributes = audio_source_function_attributes,
  1452. };
  1453. #ifndef CONFIG_MTK_ECCCI_C2K
  1454. #ifdef CONFIG_MTK_C2K_SUPPORT
  1455. static int rawbulk_function_init(struct android_usb_function *f,
  1456. struct usb_composite_dev *cdev)
  1457. {
  1458. return 0;
  1459. }
  1460. static void rawbulk_function_cleanup(struct android_usb_function *f)
  1461. {
  1462. ;
  1463. }
  1464. static int rawbulk_function_bind_config(struct android_usb_function *f,
  1465. struct usb_configuration *c)
  1466. {
  1467. char *i = f->name + strlen("via_");
  1468. if (!strncmp(i, "modem", 5))
  1469. return rawbulk_bind_config(c, RAWBULK_TID_MODEM);
  1470. else if (!strncmp(i, "ets", 3))
  1471. return rawbulk_bind_config(c, RAWBULK_TID_ETS);
  1472. else if (!strncmp(i, "atc", 3))
  1473. return rawbulk_bind_config(c, RAWBULK_TID_AT);
  1474. else if (!strncmp(i, "pcv", 3))
  1475. return rawbulk_bind_config(c, RAWBULK_TID_PCV);
  1476. else if (!strncmp(i, "gps", 3))
  1477. return rawbulk_bind_config(c, RAWBULK_TID_GPS);
  1478. return -EINVAL;
  1479. }
  1480. static int rawbulk_function_modem_ctrlrequest(struct android_usb_function *f,
  1481. struct usb_composite_dev *cdev,
  1482. const struct usb_ctrlrequest *c)
  1483. {
  1484. if ((c->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE &&
  1485. (c->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
  1486. struct rawbulk_function *fn = rawbulk_lookup_function(RAWBULK_TID_MODEM);
  1487. return rawbulk_function_setup(&fn->function, c);
  1488. }
  1489. return -1;
  1490. }
  1491. static struct android_usb_function rawbulk_modem_function = {
  1492. .name = "via_modem",
  1493. .init = rawbulk_function_init,
  1494. .cleanup = rawbulk_function_cleanup,
  1495. .bind_config = rawbulk_function_bind_config,
  1496. .ctrlrequest = rawbulk_function_modem_ctrlrequest,
  1497. };
  1498. static struct android_usb_function rawbulk_ets_function = {
  1499. .name = "via_ets",
  1500. .init = rawbulk_function_init,
  1501. .cleanup = rawbulk_function_cleanup,
  1502. .bind_config = rawbulk_function_bind_config,
  1503. };
  1504. static struct android_usb_function rawbulk_atc_function = {
  1505. .name = "via_atc",
  1506. .init = rawbulk_function_init,
  1507. .cleanup = rawbulk_function_cleanup,
  1508. .bind_config = rawbulk_function_bind_config,
  1509. };
  1510. static struct android_usb_function rawbulk_pcv_function = {
  1511. .name = "via_pcv",
  1512. .init = rawbulk_function_init,
  1513. .cleanup = rawbulk_function_cleanup,
  1514. .bind_config = rawbulk_function_bind_config,
  1515. };
  1516. static struct android_usb_function rawbulk_gps_function = {
  1517. .name = "via_gps",
  1518. .init = rawbulk_function_init,
  1519. .cleanup = rawbulk_function_cleanup,
  1520. .bind_config = rawbulk_function_bind_config,
  1521. };
  1522. #endif
  1523. #endif
  1524. #ifdef CONFIG_SND_RAWMIDI
  1525. static int midi_function_init(struct android_usb_function *f,
  1526. struct usb_composite_dev *cdev)
  1527. {
  1528. struct midi_alsa_config *config;
  1529. config = kzalloc(sizeof(struct midi_alsa_config), GFP_KERNEL);
  1530. f->config = config;
  1531. if (!config)
  1532. return -ENOMEM;
  1533. config->card = -1;
  1534. config->device = -1;
  1535. return 0;
  1536. }
  1537. static void midi_function_cleanup(struct android_usb_function *f)
  1538. {
  1539. kfree(f->config);
  1540. }
  1541. static int midi_function_bind_config(struct android_usb_function *f,
  1542. struct usb_configuration *c)
  1543. {
  1544. struct midi_alsa_config *config = f->config;
  1545. return f_midi_bind_config(c, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  1546. MIDI_INPUT_PORTS, MIDI_OUTPUT_PORTS, MIDI_BUFFER_SIZE,
  1547. MIDI_QUEUE_LENGTH, config);
  1548. }
  1549. static ssize_t midi_alsa_show(struct device *dev,
  1550. struct device_attribute *attr, char *buf)
  1551. {
  1552. struct android_usb_function *f = dev_get_drvdata(dev);
  1553. struct midi_alsa_config *config = f->config;
  1554. /* print ALSA card and device numbers */
  1555. return sprintf(buf, "%d %d\n", config->card, config->device);
  1556. }
  1557. static DEVICE_ATTR(alsa, S_IRUGO, midi_alsa_show, NULL);
  1558. static struct device_attribute *midi_function_attributes[] = {
  1559. &dev_attr_alsa,
  1560. NULL
  1561. };
  1562. static struct android_usb_function midi_function = {
  1563. .name = "midi",
  1564. .init = midi_function_init,
  1565. .cleanup = midi_function_cleanup,
  1566. .bind_config = midi_function_bind_config,
  1567. .attributes = midi_function_attributes,
  1568. };
  1569. #endif
  1570. static struct android_usb_function *supported_functions[] = {
  1571. &ffs_function,
  1572. &adb_function,
  1573. &acm_function,
  1574. &mtp_function,
  1575. &ptp_function,
  1576. #ifndef CONFIG_USBIF_COMPLIANCE
  1577. &ecm_function,
  1578. &eem_function,
  1579. #endif
  1580. &serial_function,
  1581. &rndis_function,
  1582. &mass_storage_function,
  1583. &accessory_function,
  1584. &audio_source_function,
  1585. #ifdef CONFIG_SND_RAWMIDI
  1586. &midi_function,
  1587. #endif
  1588. #ifndef CONFIG_MTK_ECCCI_C2K
  1589. #ifdef CONFIG_MTK_C2K_SUPPORT
  1590. &rawbulk_modem_function,
  1591. &rawbulk_ets_function,
  1592. &rawbulk_atc_function,
  1593. &rawbulk_pcv_function,
  1594. &rawbulk_gps_function,
  1595. #endif
  1596. #endif
  1597. #ifdef CONFIG_USB_F_LOOPBACK
  1598. &loopback_function,
  1599. #endif
  1600. NULL
  1601. };
  1602. static int android_init_functions(struct android_usb_function **functions,
  1603. struct usb_composite_dev *cdev)
  1604. {
  1605. struct android_dev *dev = _android_dev;
  1606. struct android_usb_function *f;
  1607. struct device_attribute **attrs;
  1608. struct device_attribute *attr;
  1609. int err;
  1610. #ifdef CONFIG_USBIF_COMPLIANCE
  1611. int index = 1;
  1612. #else
  1613. int index = 0;
  1614. #endif
  1615. for (; (f = *functions++); index++) {
  1616. f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
  1617. /* Added for USB Develpment debug, more log for more debuging help */
  1618. pr_notice("[USB]%s: f->dev_name = %s, f->name = %s\n", __func__, f->dev_name, f->name);
  1619. /* Added for USB Develpment debug, more log for more debuging help */
  1620. f->dev = device_create(android_class, dev->dev,
  1621. MKDEV(0, index), f, f->dev_name);
  1622. if (IS_ERR(f->dev)) {
  1623. pr_err("%s: Failed to create dev %s", __func__,
  1624. f->dev_name);
  1625. err = PTR_ERR(f->dev);
  1626. goto err_create;
  1627. }
  1628. if (f->init) {
  1629. err = f->init(f, cdev);
  1630. if (err) {
  1631. pr_err("%s: Failed to init %s", __func__,
  1632. f->name);
  1633. goto err_out;
  1634. } else {
  1635. pr_notice("[USB]%s: init %s success!!\n", __func__, f->name);
  1636. }
  1637. }
  1638. attrs = f->attributes;
  1639. if (attrs) {
  1640. while ((attr = *attrs++) && !err)
  1641. err = device_create_file(f->dev, attr);
  1642. }
  1643. if (err) {
  1644. pr_err("%s: Failed to create function %s attributes",
  1645. __func__, f->name);
  1646. goto err_out;
  1647. }
  1648. }
  1649. return 0;
  1650. err_out:
  1651. device_destroy(android_class, f->dev->devt);
  1652. err_create:
  1653. kfree(f->dev_name);
  1654. return err;
  1655. }
  1656. static void android_cleanup_functions(struct android_usb_function **functions)
  1657. {
  1658. struct android_usb_function *f;
  1659. while (*functions) {
  1660. f = *functions++;
  1661. if (f->dev) {
  1662. device_destroy(android_class, f->dev->devt);
  1663. kfree(f->dev_name);
  1664. }
  1665. if (f->cleanup)
  1666. f->cleanup(f);
  1667. }
  1668. }
  1669. static int
  1670. android_bind_enabled_functions(struct android_dev *dev,
  1671. struct usb_configuration *c)
  1672. {
  1673. struct android_usb_function *f;
  1674. int ret;
  1675. /* Added for USB Develpment debug, more log for more debuging help */
  1676. pr_notice("[USB]%s: ", __func__);
  1677. /* Added for USB Develpment debug, more log for more debuging help */
  1678. list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
  1679. pr_notice("[USB]bind_config function '%s'/%p\n", f->name, f);
  1680. ret = f->bind_config(f, c);
  1681. if (ret) {
  1682. pr_err("%s: %s failed", __func__, f->name);
  1683. return ret;
  1684. }
  1685. }
  1686. return 0;
  1687. }
  1688. static void
  1689. android_unbind_enabled_functions(struct android_dev *dev,
  1690. struct usb_configuration *c)
  1691. {
  1692. struct android_usb_function *f;
  1693. list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
  1694. pr_notice("[USB]unbind_config function '%s'/%p\n", f->name, f);
  1695. if (f->unbind_config)
  1696. f->unbind_config(f, c);
  1697. }
  1698. }
  1699. static int android_enable_function(struct android_dev *dev, char *name)
  1700. {
  1701. struct android_usb_function **functions = dev->functions;
  1702. struct android_usb_function *f;
  1703. while ((f = *functions++)) {
  1704. /* Added for USB Develpment debug, more log for more debuging help */
  1705. pr_notice("[USB]%s: name = %s, f->name=%s \n", __func__, name, f->name);
  1706. /* Added for USB Develpment debug, more log for more debuging help */
  1707. if (!strcmp(name, f->name)) {
  1708. list_add_tail(&f->enabled_list,
  1709. &dev->enabled_functions);
  1710. return 0;
  1711. }
  1712. }
  1713. return -EINVAL;
  1714. }
  1715. /*-------------------------------------------------------------------------*/
  1716. /* /sys/class/android_usb/android%d/ interface */
  1717. static ssize_t
  1718. functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
  1719. {
  1720. struct android_dev *dev = dev_get_drvdata(pdev);
  1721. struct android_usb_function *f;
  1722. char *buff = buf;
  1723. /* Added for USB Develpment debug, more log for more debuging help */
  1724. pr_notice("[USB]%s: ", __func__);
  1725. /* Added for USB Develpment debug, more log for more debuging help */
  1726. mutex_lock(&dev->mutex);
  1727. list_for_each_entry(f, &dev->enabled_functions, enabled_list)
  1728. buff += sprintf(buff, "%s,", f->name);
  1729. mutex_unlock(&dev->mutex);
  1730. if (buff != buf)
  1731. *(buff-1) = '\n';
  1732. return buff - buf;
  1733. }
  1734. static ssize_t
  1735. functions_store(struct device *pdev, struct device_attribute *attr,
  1736. const char *buff, size_t size)
  1737. {
  1738. struct android_dev *dev = dev_get_drvdata(pdev);
  1739. char *name;
  1740. char buf[256], *b;
  1741. char aliases[256], *a;
  1742. int err;
  1743. int is_ffs;
  1744. int ffs_enabled = 0;
  1745. mutex_lock(&dev->mutex);
  1746. if (dev->enabled) {
  1747. mutex_unlock(&dev->mutex);
  1748. return -EBUSY;
  1749. }
  1750. INIT_LIST_HEAD(&dev->enabled_functions);
  1751. #ifdef CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  1752. if (get_boot_mode() == KERNEL_POWER_OFF_CHARGING_BOOT || get_boot_mode() == LOW_POWER_OFF_CHARGING_BOOT) {
  1753. pr_notice("[USB]KPOC, func%s\n", KPOC_USB_FUNC);
  1754. err = android_enable_function(dev, KPOC_USB_FUNC);
  1755. if (err)
  1756. pr_err("android_usb: Cannot enable '%s' (%d)",
  1757. KPOC_USB_FUNC, err);
  1758. mutex_unlock(&dev->mutex);
  1759. return size;
  1760. }
  1761. #endif
  1762. /* Added for USB Develpment debug, more log for more debuging help */
  1763. pr_notice("[USB]%s\n", __func__);
  1764. /* Added for USB Develpment debug, more log for more debuging help */
  1765. strlcpy(buf, buff, sizeof(buf));
  1766. b = strim(buf);
  1767. while (b) {
  1768. name = strsep(&b, ",");
  1769. /* Added for USB Develpment debug, more log for more debuging help */
  1770. pr_notice("[USB]%s: name = %s \n", __func__, name);
  1771. /* Added for USB Develpment debug, more log for more debuging help */
  1772. if (!name)
  1773. continue;
  1774. is_ffs = 0;
  1775. strlcpy(aliases, dev->ffs_aliases, sizeof(aliases));
  1776. a = aliases;
  1777. while (a) {
  1778. char *alias = strsep(&a, ",");
  1779. if (alias && !strcmp(name, alias)) {
  1780. is_ffs = 1;
  1781. break;
  1782. }
  1783. }
  1784. if (is_ffs) {
  1785. if (ffs_enabled)
  1786. continue;
  1787. err = android_enable_function(dev, "ffs");
  1788. if (err)
  1789. pr_err("android_usb: Cannot enable ffs (%d)",
  1790. err);
  1791. else
  1792. ffs_enabled = 1;
  1793. continue;
  1794. }
  1795. err = android_enable_function(dev, name);
  1796. if (err)
  1797. pr_err("android_usb: Cannot enable '%s' (%d)",
  1798. name, err);
  1799. }
  1800. mutex_unlock(&dev->mutex);
  1801. return size;
  1802. }
  1803. static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
  1804. char *buf)
  1805. {
  1806. struct android_dev *dev = dev_get_drvdata(pdev);
  1807. return sprintf(buf, "%d\n", dev->enabled);
  1808. }
  1809. static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
  1810. const char *buff, size_t size)
  1811. {
  1812. struct android_dev *dev = dev_get_drvdata(pdev);
  1813. struct usb_composite_dev *cdev = dev->cdev;
  1814. struct android_usb_function *f;
  1815. int enabled = 0;
  1816. if (!cdev)
  1817. return -ENODEV;
  1818. mutex_lock(&dev->mutex);
  1819. /* Added for USB Develpment debug, more log for more debuging help */
  1820. pr_notice("[USB]%s: device_attr->attr.name: %s\n", __func__, attr->attr.name);
  1821. /* Added for USB Develpment debug, more log for more debuging help */
  1822. sscanf(buff, "%d", &enabled);
  1823. if (enabled && !dev->enabled) {
  1824. /* ALPS01770952
  1825. * Reset next_string_id to 0 before enabling the gadget driver.
  1826. * Otherwise, after a large number of enable/disable cycles,
  1827. * function bind will fail because we cannot allocate new string ids.
  1828. * String ids cannot be larger than 254 per USB spec.
  1829. * 0~15 are reserved for usb device descriptor
  1830. * 16~254 are for functions.
  1831. */
  1832. cdev->next_string_id = 0x10;
  1833. /*
  1834. * Update values in composite driver's copy of
  1835. * device descriptor.
  1836. */
  1837. cdev->desc.idVendor = device_desc.idVendor;
  1838. cdev->desc.idProduct = device_desc.idProduct;
  1839. #ifdef CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  1840. if (get_boot_mode() == KERNEL_POWER_OFF_CHARGING_BOOT || get_boot_mode() == LOW_POWER_OFF_CHARGING_BOOT) {
  1841. pr_notice("[USB]KPOC, vid:%d, pid:%d\n", KPOC_USB_VENDOR_ID, KPOC_USB_PRODUCT_ID);
  1842. cdev->desc.idVendor = __constant_cpu_to_le16(KPOC_USB_VENDOR_ID);
  1843. cdev->desc.idProduct = __constant_cpu_to_le16(KPOC_USB_PRODUCT_ID);
  1844. }
  1845. #endif
  1846. cdev->desc.bcdDevice = device_desc.bcdDevice;
  1847. cdev->desc.bDeviceClass = device_desc.bDeviceClass;
  1848. cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
  1849. cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
  1850. /* special case for meta mode */
  1851. if (serial_string[0] == 0x0) {
  1852. cdev->desc.iSerialNumber = 0;
  1853. } else {
  1854. cdev->desc.iSerialNumber = device_desc.iSerialNumber;
  1855. }
  1856. list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
  1857. pr_notice("[USB]enable function '%s'/%p\n", f->name, f);
  1858. if (f->enable)
  1859. f->enable(f);
  1860. }
  1861. android_enable(dev);
  1862. dev->enabled = true;
  1863. /* Added for USB Develpment debug, more log for more debuging help */
  1864. pr_notice("[USB]%s: enable 0->1 case, device_desc.idVendor = 0x%x, device_desc.idProduct = 0x%x\n", __func__, device_desc.idVendor, device_desc.idProduct);
  1865. /* Added for USB Develpment debug, more log for more debuging help */
  1866. } else if (!enabled && dev->enabled) {
  1867. /* Added for USB Develpment debug, more log for more debuging help */
  1868. pr_notice("[USB]%s: enable 1->0 case, device_desc.idVendor = 0x%x, device_desc.idProduct = 0x%x\n", __func__, device_desc.idVendor, device_desc.idProduct);
  1869. /* Added for USB Develpment debug, more log for more debuging help */
  1870. android_disable(dev);
  1871. list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
  1872. pr_notice("[USB]disable function '%s'/%p\n", f->name, f);
  1873. if (f->disable) {
  1874. f->disable(f);
  1875. }
  1876. }
  1877. dev->enabled = false;
  1878. } else {
  1879. pr_err("android_usb: already %s\n",
  1880. dev->enabled ? "enabled" : "disabled");
  1881. /* Add for HW/SW connect */
  1882. if (!usb_cable_connected()) {
  1883. schedule_work(&dev->work);
  1884. pr_notice("[USB]%s: enable 0->0 case - no usb cable", __func__);
  1885. }
  1886. /* Add for HW/SW connect */
  1887. }
  1888. mutex_unlock(&dev->mutex);
  1889. return size;
  1890. }
  1891. static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
  1892. char *buf)
  1893. {
  1894. struct android_dev *dev = dev_get_drvdata(pdev);
  1895. struct usb_composite_dev *cdev = dev->cdev;
  1896. char *state = "DISCONNECTED";
  1897. unsigned long flags;
  1898. if (!cdev)
  1899. goto out;
  1900. spin_lock_irqsave(&cdev->lock, flags);
  1901. if (cdev->config)
  1902. state = "CONFIGURED";
  1903. else if (dev->connected)
  1904. state = "CONNECTED";
  1905. spin_unlock_irqrestore(&cdev->lock, flags);
  1906. out:
  1907. return sprintf(buf, "%s\n", state);
  1908. }
  1909. #define DESCRIPTOR_ATTR(field, format_string) \
  1910. static ssize_t \
  1911. field ## _show(struct device *dev, struct device_attribute *attr, \
  1912. char *buf) \
  1913. { \
  1914. return sprintf(buf, format_string, device_desc.field); \
  1915. } \
  1916. static ssize_t \
  1917. field ## _store(struct device *dev, struct device_attribute *attr, \
  1918. const char *buf, size_t size) \
  1919. { \
  1920. int value; \
  1921. if (sscanf(buf, format_string, &value) == 1) { \
  1922. device_desc.field = value; \
  1923. return size; \
  1924. } \
  1925. return -1; \
  1926. } \
  1927. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
  1928. #define DESCRIPTOR_STRING_ATTR(field, buffer) \
  1929. static ssize_t \
  1930. field ## _show(struct device *dev, struct device_attribute *attr, \
  1931. char *buf) \
  1932. { \
  1933. return sprintf(buf, "%s", buffer); \
  1934. } \
  1935. static ssize_t \
  1936. field ## _store(struct device *dev, struct device_attribute *attr, \
  1937. const char *buf, size_t size) \
  1938. { \
  1939. if (size >= sizeof(buffer)) \
  1940. return -EINVAL; \
  1941. if (sscanf(buf, "%s", buffer) == 1) { \
  1942. return size; \
  1943. } \
  1944. return -1; \
  1945. } \
  1946. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
  1947. DESCRIPTOR_ATTR(idVendor, "%04x\n")
  1948. DESCRIPTOR_ATTR(idProduct, "%04x\n")
  1949. DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
  1950. DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
  1951. DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
  1952. DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
  1953. DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
  1954. DESCRIPTOR_STRING_ATTR(iProduct, product_string)
  1955. DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
  1956. static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
  1957. functions_store);
  1958. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
  1959. static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
  1960. static struct device_attribute *android_usb_attributes[] = {
  1961. &dev_attr_idVendor,
  1962. &dev_attr_idProduct,
  1963. &dev_attr_bcdDevice,
  1964. &dev_attr_bDeviceClass,
  1965. &dev_attr_bDeviceSubClass,
  1966. &dev_attr_bDeviceProtocol,
  1967. &dev_attr_iManufacturer,
  1968. &dev_attr_iProduct,
  1969. &dev_attr_iSerial,
  1970. &dev_attr_functions,
  1971. &dev_attr_enable,
  1972. &dev_attr_state,
  1973. NULL
  1974. };
  1975. /*-------------------------------------------------------------------------*/
  1976. /* Composite driver */
  1977. static int android_bind_config(struct usb_configuration *c)
  1978. {
  1979. struct android_dev *dev = _android_dev;
  1980. int ret = 0;
  1981. ret = android_bind_enabled_functions(dev, c);
  1982. if (ret)
  1983. return ret;
  1984. return 0;
  1985. }
  1986. static void android_unbind_config(struct usb_configuration *c)
  1987. {
  1988. struct android_dev *dev = _android_dev;
  1989. android_unbind_enabled_functions(dev, c);
  1990. }
  1991. static int android_setup_config(struct usb_configuration *c, const struct usb_ctrlrequest *ctrl)
  1992. {
  1993. int handled = -EINVAL;
  1994. const u8 recip = ctrl->bRequestType & USB_RECIP_MASK;
  1995. pr_notice("%s bRequestType=%x, bRequest=%x, recip=%x\n", __func__, ctrl->bRequestType, ctrl->bRequest, recip);
  1996. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1997. switch (ctrl->bRequest)
  1998. {
  1999. case USB_REQ_CLEAR_FEATURE:
  2000. switch (recip)
  2001. {
  2002. case USB_RECIP_DEVICE:
  2003. switch (ctrl->wValue)
  2004. {
  2005. case USB_DEVICE_U1_ENABLE:
  2006. handled = 1;
  2007. pr_notice("Clear Feature->U1 Enable\n");
  2008. break;
  2009. case USB_DEVICE_U2_ENABLE:
  2010. handled = 1;
  2011. pr_notice("Clear Feature->U2 Enable\n");
  2012. break;
  2013. default:
  2014. handled = -EINVAL;
  2015. break;
  2016. }
  2017. break;
  2018. default:
  2019. handled = -EINVAL;
  2020. break;
  2021. }
  2022. break;
  2023. case USB_REQ_SET_FEATURE:
  2024. switch (recip)
  2025. {
  2026. case USB_RECIP_DEVICE:
  2027. switch (ctrl->wValue)
  2028. {
  2029. case USB_DEVICE_U1_ENABLE:
  2030. pr_notice("Set Feature->U1 Enable\n");
  2031. handled = 1;
  2032. break;
  2033. case USB_DEVICE_U2_ENABLE:
  2034. pr_notice("Set Feature->U2 Enable\n");
  2035. handled = 1;
  2036. break;
  2037. default:
  2038. handled = -EINVAL;
  2039. break;
  2040. }
  2041. break;
  2042. default:
  2043. handled = -EINVAL;
  2044. break;
  2045. }
  2046. break;
  2047. default:
  2048. handled = -EINVAL;
  2049. break;
  2050. }
  2051. }
  2052. return handled;
  2053. }
  2054. static int android_bind(struct usb_composite_dev *cdev)
  2055. {
  2056. struct android_dev *dev = _android_dev;
  2057. struct usb_gadget *gadget = cdev->gadget;
  2058. int id, ret;
  2059. /* Save the default handler */
  2060. dev->setup_complete = cdev->req->complete;
  2061. /*
  2062. * Start disconnected. Userspace will connect the gadget once
  2063. * it is done configuring the functions.
  2064. */
  2065. usb_gadget_disconnect(gadget);
  2066. ret = android_init_functions(dev->functions, cdev);
  2067. if (ret)
  2068. return ret;
  2069. /* Allocate string descriptor numbers ... note that string
  2070. * contents can be overridden by the composite_dev glue.
  2071. */
  2072. id = usb_string_id(cdev);
  2073. if (id < 0)
  2074. return id;
  2075. strings_dev[STRING_MANUFACTURER_IDX].id = id;
  2076. device_desc.iManufacturer = id;
  2077. id = usb_string_id(cdev);
  2078. if (id < 0)
  2079. return id;
  2080. strings_dev[STRING_PRODUCT_IDX].id = id;
  2081. device_desc.iProduct = id;
  2082. /* Default strings - should be updated by userspace */
  2083. strncpy(manufacturer_string, MANUFACTURER_STRING, sizeof(manufacturer_string) - 1);
  2084. strncpy(product_string, PRODUCT_STRING, sizeof(product_string) - 1);
  2085. strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
  2086. id = usb_string_id(cdev);
  2087. if (id < 0)
  2088. return id;
  2089. strings_dev[STRING_SERIAL_IDX].id = id;
  2090. device_desc.iSerialNumber = id;
  2091. #ifdef CONFIG_USBIF_COMPLIANCE
  2092. usb_gadget_clear_selfpowered(gadget);
  2093. #else
  2094. usb_gadget_set_selfpowered(gadget);
  2095. #endif
  2096. dev->cdev = cdev;
  2097. return 0;
  2098. }
  2099. static int android_usb_unbind(struct usb_composite_dev *cdev)
  2100. {
  2101. struct android_dev *dev = _android_dev;
  2102. cancel_work_sync(&dev->work);
  2103. android_cleanup_functions(dev->functions);
  2104. return 0;
  2105. }
  2106. /* HACK: android needs to override setup for accessory to work */
  2107. static int (*composite_setup_func)(struct usb_gadget *gadget, const struct usb_ctrlrequest *c);
  2108. extern void composite_setup_complete(struct usb_ep *ep, struct usb_request *req);
  2109. static int
  2110. android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
  2111. {
  2112. struct android_dev *dev = _android_dev;
  2113. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  2114. struct usb_request *req = cdev->req;
  2115. struct android_usb_function *f;
  2116. int value = -EOPNOTSUPP;
  2117. unsigned long flags;
  2118. static DEFINE_RATELIMIT_STATE(ratelimit, 1 * HZ, 5);
  2119. if (__ratelimit(&ratelimit))
  2120. pr_notice("[USB][ratelimit]%s,\n", __func__);
  2121. req->zero = 0;
  2122. req->complete = composite_setup_complete;
  2123. req->length = 0;
  2124. req->complete = dev->setup_complete;
  2125. gadget->ep0->driver_data = cdev;
  2126. list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
  2127. if (f->ctrlrequest) {
  2128. value = f->ctrlrequest(f, cdev, c);
  2129. if (value >= 0)
  2130. break;
  2131. }
  2132. }
  2133. /* Special case the accessory function.
  2134. * It needs to handle control requests before it is enabled.
  2135. */
  2136. if (value < 0)
  2137. value = acc_ctrlrequest(cdev, c);
  2138. if (value < 0)
  2139. value = composite_setup_func(gadget, c);
  2140. spin_lock_irqsave(&cdev->lock, flags);
  2141. if (!dev->connected) {
  2142. dev->connected = 1;
  2143. schedule_work(&dev->work);
  2144. } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
  2145. cdev->config) {
  2146. schedule_work(&dev->work);
  2147. }
  2148. spin_unlock_irqrestore(&cdev->lock, flags);
  2149. return value;
  2150. }
  2151. static void android_disconnect(struct usb_composite_dev *cdev)
  2152. {
  2153. struct android_dev *dev = _android_dev;
  2154. /* Added for USB Develpment debug, more log for more debuging help */
  2155. pr_notice("[USB]%s: \n", __func__);
  2156. /* Added for USB Develpment debug, more log for more debuging help */
  2157. /* accessory HID support can be active while the
  2158. accessory function is not actually enabled,
  2159. so we need to inform it when we are disconnected.
  2160. */
  2161. acc_disconnect();
  2162. dev->connected = 0;
  2163. schedule_work(&dev->work);
  2164. /* Added for USB Develpment debug, more log for more debuging help */
  2165. pr_notice("[USB]%s: dev->connected = %d \n", __func__, dev->connected);
  2166. /* Added for USB Develpment debug, more log for more debuging help */
  2167. }
  2168. static struct usb_composite_driver android_usb_driver = {
  2169. .name = "android_usb",
  2170. .dev = &device_desc,
  2171. .strings = dev_strings,
  2172. .bind = android_bind,
  2173. .unbind = android_usb_unbind,
  2174. .disconnect = android_disconnect,
  2175. #ifdef CONFIG_USB_MU3D_DRV
  2176. .max_speed = USB_SPEED_SUPER
  2177. #else
  2178. .max_speed = USB_SPEED_HIGH
  2179. #endif
  2180. };
  2181. static int android_create_device(struct android_dev *dev)
  2182. {
  2183. struct device_attribute **attrs = android_usb_attributes;
  2184. struct device_attribute *attr;
  2185. int err;
  2186. dev->dev = device_create(android_class, NULL,
  2187. MKDEV(0, 0), NULL, "android0");
  2188. if (IS_ERR(dev->dev))
  2189. return PTR_ERR(dev->dev);
  2190. dev_set_drvdata(dev->dev, dev);
  2191. while ((attr = *attrs++)) {
  2192. err = device_create_file(dev->dev, attr);
  2193. if (err) {
  2194. device_destroy(android_class, dev->dev->devt);
  2195. return err;
  2196. }
  2197. }
  2198. return 0;
  2199. }
  2200. #ifdef CONFIG_USBIF_COMPLIANCE
  2201. #include <linux/proc_fs.h>
  2202. #include <asm/uaccess.h>
  2203. #include <linux/seq_file.h>
  2204. static int andoid_usbif_driver_on = 0 ;
  2205. static int android_start(void)
  2206. {
  2207. struct android_dev *dev;
  2208. int err;
  2209. pr_notice("android_start ===> \n");
  2210. err = usb_composite_probe(&android_usb_driver);
  2211. if (err) {
  2212. pr_err("%s: failed to probe driver %d", __func__, err);
  2213. }
  2214. /* HACK: exchange composite's setup with ours */
  2215. composite_setup_func = android_usb_driver.gadget_driver.setup;
  2216. android_usb_driver.gadget_driver.setup = android_setup;
  2217. pr_notice("android_start <=== \n");
  2218. return err;
  2219. }
  2220. static int android_stop(void)
  2221. {
  2222. pr_notice("android_stop ===> \n");
  2223. usb_composite_unregister(&android_usb_driver);
  2224. pr_notice("android_stop <=== \n");
  2225. }
  2226. static int andoid_usbif_proc_show(struct seq_file *seq, void *v)
  2227. {
  2228. seq_printf(seq, "andoid_usbif_proc_show, andoid_usbif_driver_on is %d (on:1, off:0)\n", andoid_usbif_driver_on);
  2229. return 0;
  2230. }
  2231. static int andoid_usbif_proc_open(struct inode *inode, struct file *file)
  2232. {
  2233. return single_open(file, andoid_usbif_proc_show, inode->i_private);
  2234. }
  2235. static ssize_t andoid_usbif_proc_write(struct file *file, const char __user *buf, size_t length, loff_t *ppos)
  2236. {
  2237. int ret ;
  2238. char msg[32] ;
  2239. int result;
  2240. int status;
  2241. struct device *dev ;
  2242. int irq ;
  2243. struct resource *iomem;
  2244. void __iomem *base;
  2245. struct musb *musb ;
  2246. void __iomem *ctrl_base;
  2247. if (length >= sizeof(msg)) {
  2248. pr_notice("andoid_usbif_proc_write length error, the error len is %d\n", (unsigned int)length);
  2249. return -EINVAL;
  2250. }
  2251. if (copy_from_user(msg, buf, length))
  2252. return -EFAULT;
  2253. msg[length] = 0 ;
  2254. pr_notice("andoid_usbif_proc_write: %s, current driver on/off: %d\n", msg, andoid_usbif_driver_on);
  2255. if ((msg[0] == '1') && (andoid_usbif_driver_on == 0)){
  2256. pr_notice("start usb android driver ===> \n");
  2257. printk("start usb android driver ===> \n");
  2258. android_start() ;
  2259. andoid_usbif_driver_on = 1 ;
  2260. pr_notice("start usb android driver <=== \n");
  2261. }else if ((msg[0] == '0') && (andoid_usbif_driver_on == 1)){
  2262. pr_notice("stop usb android driver ===> \n");
  2263. printk("stop usb android driver ===> \n");
  2264. andoid_usbif_driver_on = 0 ;
  2265. android_stop() ;
  2266. pr_notice("stop usb android driver <=== \n");
  2267. }
  2268. return length;
  2269. }
  2270. static const struct file_operations andoid_usbif_proc_fops = {
  2271. .owner = THIS_MODULE,
  2272. .open = andoid_usbif_proc_open,
  2273. .write = andoid_usbif_proc_write,
  2274. .read = seq_read,
  2275. .llseek = seq_lseek,
  2276. };
  2277. static int __init init(void)
  2278. {
  2279. struct android_dev *dev;
  2280. int err;
  2281. struct proc_dir_entry *prEntry;
  2282. android_class = class_create(THIS_MODULE, "android_usb");
  2283. if (IS_ERR(android_class))
  2284. return PTR_ERR(android_class);
  2285. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2286. if (!dev) {
  2287. err = -ENOMEM;
  2288. goto err_dev;
  2289. }
  2290. dev->disable_depth = 1;
  2291. dev->functions = supported_functions;
  2292. INIT_LIST_HEAD(&dev->enabled_functions);
  2293. INIT_WORK(&dev->work, android_work);
  2294. mutex_init(&dev->mutex);
  2295. err = android_create_device(dev);
  2296. if (err) {
  2297. pr_err("%s: failed to create android device %d", __func__, err);
  2298. goto err_create;
  2299. }
  2300. _android_dev = dev;
  2301. prEntry = proc_create("android_usbif_init", 0666, NULL, &andoid_usbif_proc_fops);
  2302. if (prEntry)
  2303. {
  2304. printk("create the android_usbif_init proc OK!\n") ;
  2305. }else{
  2306. printk("[ERROR] create the android_usbif_init proc FAIL\n") ;
  2307. }
  2308. // set android up at boot up
  2309. android_start() ;
  2310. andoid_usbif_driver_on = 1 ;
  2311. return 0;
  2312. err_create:
  2313. kfree(dev);
  2314. err_dev:
  2315. class_destroy(android_class);
  2316. return err;
  2317. }
  2318. late_initcall(init);
  2319. static void __exit cleanup(void)
  2320. {
  2321. printk("[U3D] android cleanup ===> \n") ;
  2322. class_destroy(android_class);
  2323. kfree(_android_dev);
  2324. _android_dev = NULL;
  2325. printk("[U3D] android cleanup <=== \n") ;
  2326. }
  2327. module_exit(cleanup);
  2328. #else
  2329. static int __init init(void)
  2330. {
  2331. struct android_dev *dev;
  2332. int err;
  2333. android_class = class_create(THIS_MODULE, "android_usb");
  2334. if (IS_ERR(android_class))
  2335. return PTR_ERR(android_class);
  2336. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2337. if (!dev) {
  2338. err = -ENOMEM;
  2339. goto err_dev;
  2340. }
  2341. dev->disable_depth = 1;
  2342. dev->functions = supported_functions;
  2343. INIT_LIST_HEAD(&dev->enabled_functions);
  2344. INIT_WORK(&dev->work, android_work);
  2345. mutex_init(&dev->mutex);
  2346. err = android_create_device(dev);
  2347. if (err) {
  2348. pr_err("%s: failed to create android device %d", __func__, err);
  2349. goto err_create;
  2350. }
  2351. _android_dev = dev;
  2352. err = usb_composite_probe(&android_usb_driver);
  2353. if (err) {
  2354. pr_err("%s: failed to probe driver %d", __func__, err);
  2355. goto err_probe;
  2356. }
  2357. /* HACK: exchange composite's setup with ours */
  2358. composite_setup_func = android_usb_driver.gadget_driver.setup;
  2359. android_usb_driver.gadget_driver.setup = android_setup;
  2360. return 0;
  2361. err_probe:
  2362. device_destroy(android_class, dev->dev->devt);
  2363. err_create:
  2364. kfree(dev);
  2365. err_dev:
  2366. class_destroy(android_class);
  2367. return err;
  2368. }
  2369. late_initcall(init);
  2370. static void __exit cleanup(void)
  2371. {
  2372. usb_composite_unregister(&android_usb_driver);
  2373. class_destroy(android_class);
  2374. kfree(_android_dev);
  2375. _android_dev = NULL;
  2376. }
  2377. module_exit(cleanup);
  2378. #endif