android.c 72 KB

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