android.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /*
  2. * Gadget Driver for Android
  3. *
  4. * Copyright (C) 2008 Google, Inc.
  5. * Author: Mike Lockwood <lockwood@android.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. /* #define DEBUG */
  18. /* #define VERBOSE_DEBUG */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/fs.h>
  22. #include <linux/delay.h>
  23. #include <linux/kernel.h>
  24. #include <linux/utsname.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/usb/ch9.h>
  27. #include <linux/usb/composite.h>
  28. #include <linux/usb/gadget.h>
  29. #include "gadget_chips.h"
  30. /*
  31. * Kbuild is not very cooperative with respect to linking separately
  32. * compiled library objects into one module. So for now we won't use
  33. * separate compilation ... ensuring init/exit sections work to shrink
  34. * the runtime footprint, and giving us at least some parts of what
  35. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  36. */
  37. #include "usbstring.c"
  38. #include "config.c"
  39. #include "epautoconf.c"
  40. #include "composite.c"
  41. #include "f_mass_storage.c"
  42. #include "u_serial.c"
  43. #include "f_acm.c"
  44. #include "f_adb.c"
  45. #include "f_mtp.c"
  46. #include "f_accessory.c"
  47. #define USB_ETH_RNDIS y
  48. #include "f_rndis.c"
  49. #include "rndis.c"
  50. #include "u_ether.c"
  51. MODULE_AUTHOR("Mike Lockwood");
  52. MODULE_DESCRIPTION("Android Composite USB Driver");
  53. MODULE_LICENSE("GPL");
  54. MODULE_VERSION("1.0");
  55. static const char longname[] = "Gadget Android";
  56. /* Default vendor and product IDs, overridden by userspace */
  57. #define VENDOR_ID 0x18D1
  58. #define PRODUCT_ID 0x0001
  59. struct android_usb_function {
  60. char *name;
  61. void *config;
  62. struct device *dev;
  63. char *dev_name;
  64. struct device_attribute **attributes;
  65. /* for android_dev.enabled_functions */
  66. struct list_head enabled_list;
  67. /* Optional: initialization during gadget bind */
  68. int (*init)(struct android_usb_function *, struct usb_composite_dev *);
  69. /* Optional: cleanup during gadget unbind */
  70. void (*cleanup)(struct android_usb_function *);
  71. /* Optional: called when the function is added the list of
  72. * enabled functions */
  73. void (*enable)(struct android_usb_function *);
  74. /* Optional: called when it is removed */
  75. void (*disable)(struct android_usb_function *);
  76. int (*bind_config)(struct android_usb_function *, struct usb_configuration *);
  77. /* Optional: called when the configuration is removed */
  78. void (*unbind_config)(struct android_usb_function *, struct usb_configuration *);
  79. /* Optional: handle ctrl requests before the device is configured */
  80. int (*ctrlrequest)(struct android_usb_function *,
  81. struct usb_composite_dev *,
  82. const struct usb_ctrlrequest *);
  83. };
  84. struct android_dev {
  85. struct android_usb_function **functions;
  86. struct list_head enabled_functions;
  87. struct usb_composite_dev *cdev;
  88. struct device *dev;
  89. bool enabled;
  90. int disable_depth;
  91. struct mutex mutex;
  92. bool connected;
  93. bool sw_connected;
  94. struct work_struct work;
  95. };
  96. static struct class *android_class;
  97. static struct android_dev *_android_dev;
  98. static int android_bind_config(struct usb_configuration *c);
  99. static void android_unbind_config(struct usb_configuration *c);
  100. /* string IDs are assigned dynamically */
  101. #define STRING_MANUFACTURER_IDX 0
  102. #define STRING_PRODUCT_IDX 1
  103. #define STRING_SERIAL_IDX 2
  104. static char manufacturer_string[256];
  105. static char product_string[256];
  106. static char serial_string[256];
  107. /* String Table */
  108. static struct usb_string strings_dev[] = {
  109. [STRING_MANUFACTURER_IDX].s = manufacturer_string,
  110. [STRING_PRODUCT_IDX].s = product_string,
  111. [STRING_SERIAL_IDX].s = serial_string,
  112. { } /* end of list */
  113. };
  114. static struct usb_gadget_strings stringtab_dev = {
  115. .language = 0x0409, /* en-us */
  116. .strings = strings_dev,
  117. };
  118. static struct usb_gadget_strings *dev_strings[] = {
  119. &stringtab_dev,
  120. NULL,
  121. };
  122. static struct usb_device_descriptor device_desc = {
  123. .bLength = sizeof(device_desc),
  124. .bDescriptorType = USB_DT_DEVICE,
  125. .bcdUSB = __constant_cpu_to_le16(0x0200),
  126. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  127. .idVendor = __constant_cpu_to_le16(VENDOR_ID),
  128. .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
  129. .bcdDevice = __constant_cpu_to_le16(0xffff),
  130. .bNumConfigurations = 1,
  131. };
  132. static struct usb_configuration android_config_driver = {
  133. .label = "android",
  134. .unbind = android_unbind_config,
  135. .bConfigurationValue = 1,
  136. };
  137. static void android_work(struct work_struct *data)
  138. {
  139. struct android_dev *dev = container_of(data, struct android_dev, work);
  140. struct usb_composite_dev *cdev = dev->cdev;
  141. char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
  142. char *connected[2] = { "USB_STATE=CONNECTED", NULL };
  143. char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
  144. char **uevent_envp = NULL;
  145. unsigned long flags;
  146. spin_lock_irqsave(&cdev->lock, flags);
  147. if (cdev->config)
  148. uevent_envp = configured;
  149. else if (dev->connected != dev->sw_connected)
  150. uevent_envp = dev->connected ? connected : disconnected;
  151. dev->sw_connected = dev->connected;
  152. spin_unlock_irqrestore(&cdev->lock, flags);
  153. if (uevent_envp) {
  154. kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
  155. pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
  156. } else {
  157. pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
  158. dev->connected, dev->sw_connected, cdev->config);
  159. }
  160. }
  161. static void android_enable(struct android_dev *dev)
  162. {
  163. struct usb_composite_dev *cdev = dev->cdev;
  164. BUG_ON(!mutex_is_locked(&dev->mutex));
  165. BUG_ON(!dev->disable_depth);
  166. if (--dev->disable_depth == 0) {
  167. usb_add_config(cdev, &android_config_driver,
  168. android_bind_config);
  169. usb_gadget_connect(cdev->gadget);
  170. }
  171. }
  172. static void android_disable(struct android_dev *dev)
  173. {
  174. struct usb_composite_dev *cdev = dev->cdev;
  175. BUG_ON(!mutex_is_locked(&dev->mutex));
  176. if (dev->disable_depth++ == 0) {
  177. usb_gadget_disconnect(cdev->gadget);
  178. /* Cancel pending control requests */
  179. usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
  180. usb_remove_config(cdev, &android_config_driver);
  181. }
  182. }
  183. /*-------------------------------------------------------------------------*/
  184. /* Supported functions initialization */
  185. struct adb_data {
  186. bool opened;
  187. bool enabled;
  188. };
  189. static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
  190. {
  191. f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
  192. if (!f->config)
  193. return -ENOMEM;
  194. return adb_setup();
  195. }
  196. static void adb_function_cleanup(struct android_usb_function *f)
  197. {
  198. adb_cleanup();
  199. kfree(f->config);
  200. }
  201. static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
  202. {
  203. return adb_bind_config(c);
  204. }
  205. static void adb_android_function_enable(struct android_usb_function *f)
  206. {
  207. struct android_dev *dev = _android_dev;
  208. struct adb_data *data = f->config;
  209. data->enabled = true;
  210. /* Disable the gadget until adbd is ready */
  211. if (!data->opened)
  212. android_disable(dev);
  213. }
  214. static void adb_android_function_disable(struct android_usb_function *f)
  215. {
  216. struct android_dev *dev = _android_dev;
  217. struct adb_data *data = f->config;
  218. data->enabled = false;
  219. /* Balance the disable that was called in closed_callback */
  220. if (!data->opened)
  221. android_enable(dev);
  222. }
  223. static struct android_usb_function adb_function = {
  224. .name = "adb",
  225. .enable = adb_android_function_enable,
  226. .disable = adb_android_function_disable,
  227. .init = adb_function_init,
  228. .cleanup = adb_function_cleanup,
  229. .bind_config = adb_function_bind_config,
  230. };
  231. static void adb_ready_callback(void)
  232. {
  233. struct android_dev *dev = _android_dev;
  234. struct adb_data *data = adb_function.config;
  235. mutex_lock(&dev->mutex);
  236. data->opened = true;
  237. if (data->enabled)
  238. android_enable(dev);
  239. mutex_unlock(&dev->mutex);
  240. }
  241. static void adb_closed_callback(void)
  242. {
  243. struct android_dev *dev = _android_dev;
  244. struct adb_data *data = adb_function.config;
  245. mutex_lock(&dev->mutex);
  246. data->opened = false;
  247. if (data->enabled)
  248. android_disable(dev);
  249. mutex_unlock(&dev->mutex);
  250. }
  251. #define MAX_ACM_INSTANCES 4
  252. struct acm_function_config {
  253. int instances;
  254. };
  255. static int acm_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
  256. {
  257. f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
  258. if (!f->config)
  259. return -ENOMEM;
  260. return gserial_setup(cdev->gadget, MAX_ACM_INSTANCES);
  261. }
  262. static void acm_function_cleanup(struct android_usb_function *f)
  263. {
  264. gserial_cleanup();
  265. kfree(f->config);
  266. f->config = NULL;
  267. }
  268. static int acm_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
  269. {
  270. int i;
  271. int ret = 0;
  272. struct acm_function_config *config = f->config;
  273. for (i = 0; i < config->instances; i++) {
  274. ret = acm_bind_config(c, i);
  275. if (ret) {
  276. pr_err("Could not bind acm%u config\n", i);
  277. break;
  278. }
  279. }
  280. return ret;
  281. }
  282. static ssize_t acm_instances_show(struct device *dev,
  283. struct device_attribute *attr, char *buf)
  284. {
  285. struct android_usb_function *f = dev_get_drvdata(dev);
  286. struct acm_function_config *config = f->config;
  287. return sprintf(buf, "%d\n", config->instances);
  288. }
  289. static ssize_t acm_instances_store(struct device *dev,
  290. struct device_attribute *attr, const char *buf, size_t size)
  291. {
  292. struct android_usb_function *f = dev_get_drvdata(dev);
  293. struct acm_function_config *config = f->config;
  294. int value;
  295. sscanf(buf, "%d", &value);
  296. if (value > MAX_ACM_INSTANCES)
  297. value = MAX_ACM_INSTANCES;
  298. config->instances = value;
  299. return size;
  300. }
  301. static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show, acm_instances_store);
  302. static struct device_attribute *acm_function_attributes[] = { &dev_attr_instances, NULL };
  303. static struct android_usb_function acm_function = {
  304. .name = "acm",
  305. .init = acm_function_init,
  306. .cleanup = acm_function_cleanup,
  307. .bind_config = acm_function_bind_config,
  308. .attributes = acm_function_attributes,
  309. };
  310. static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
  311. {
  312. return mtp_setup();
  313. }
  314. static void mtp_function_cleanup(struct android_usb_function *f)
  315. {
  316. mtp_cleanup();
  317. }
  318. static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
  319. {
  320. return mtp_bind_config(c, false);
  321. }
  322. static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
  323. {
  324. /* nothing to do - initialization is handled by mtp_function_init */
  325. return 0;
  326. }
  327. static void ptp_function_cleanup(struct android_usb_function *f)
  328. {
  329. /* nothing to do - cleanup is handled by mtp_function_cleanup */
  330. }
  331. static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
  332. {
  333. return mtp_bind_config(c, true);
  334. }
  335. static int mtp_function_ctrlrequest(struct android_usb_function *f,
  336. struct usb_composite_dev *cdev,
  337. const struct usb_ctrlrequest *c)
  338. {
  339. return mtp_ctrlrequest(cdev, c);
  340. }
  341. static struct android_usb_function mtp_function = {
  342. .name = "mtp",
  343. .init = mtp_function_init,
  344. .cleanup = mtp_function_cleanup,
  345. .bind_config = mtp_function_bind_config,
  346. .ctrlrequest = mtp_function_ctrlrequest,
  347. };
  348. /* PTP function is same as MTP with slightly different interface descriptor */
  349. static struct android_usb_function ptp_function = {
  350. .name = "ptp",
  351. .init = ptp_function_init,
  352. .cleanup = ptp_function_cleanup,
  353. .bind_config = ptp_function_bind_config,
  354. };
  355. struct rndis_function_config {
  356. u8 ethaddr[ETH_ALEN];
  357. u32 vendorID;
  358. char manufacturer[256];
  359. bool wceis;
  360. };
  361. static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
  362. {
  363. f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
  364. if (!f->config)
  365. return -ENOMEM;
  366. return 0;
  367. }
  368. static void rndis_function_cleanup(struct android_usb_function *f)
  369. {
  370. kfree(f->config);
  371. f->config = NULL;
  372. }
  373. static int rndis_function_bind_config(struct android_usb_function *f,
  374. struct usb_configuration *c)
  375. {
  376. int ret;
  377. struct rndis_function_config *rndis = f->config;
  378. if (!rndis) {
  379. pr_err("%s: rndis_pdata\n", __func__);
  380. return -1;
  381. }
  382. pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
  383. rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
  384. rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
  385. ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
  386. if (ret) {
  387. pr_err("%s: gether_setup failed\n", __func__);
  388. return ret;
  389. }
  390. if (rndis->wceis) {
  391. /* "Wireless" RNDIS; auto-detected by Windows */
  392. rndis_iad_descriptor.bFunctionClass =
  393. USB_CLASS_WIRELESS_CONTROLLER;
  394. rndis_iad_descriptor.bFunctionSubClass = 0x01;
  395. rndis_iad_descriptor.bFunctionProtocol = 0x03;
  396. rndis_control_intf.bInterfaceClass =
  397. USB_CLASS_WIRELESS_CONTROLLER;
  398. rndis_control_intf.bInterfaceSubClass = 0x01;
  399. rndis_control_intf.bInterfaceProtocol = 0x03;
  400. }
  401. return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
  402. rndis->manufacturer);
  403. }
  404. static void rndis_function_unbind_config(struct android_usb_function *f,
  405. struct usb_configuration *c)
  406. {
  407. gether_cleanup();
  408. }
  409. static ssize_t rndis_manufacturer_show(struct device *dev,
  410. struct device_attribute *attr, char *buf)
  411. {
  412. struct android_usb_function *f = dev_get_drvdata(dev);
  413. struct rndis_function_config *config = f->config;
  414. return sprintf(buf, "%s\n", config->manufacturer);
  415. }
  416. static ssize_t rndis_manufacturer_store(struct device *dev,
  417. struct device_attribute *attr, const char *buf, size_t size)
  418. {
  419. struct android_usb_function *f = dev_get_drvdata(dev);
  420. struct rndis_function_config *config = f->config;
  421. if (size >= sizeof(config->manufacturer))
  422. return -EINVAL;
  423. if (sscanf(buf, "%s", config->manufacturer) == 1)
  424. return size;
  425. return -1;
  426. }
  427. static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
  428. rndis_manufacturer_store);
  429. static ssize_t rndis_wceis_show(struct device *dev,
  430. struct device_attribute *attr, char *buf)
  431. {
  432. struct android_usb_function *f = dev_get_drvdata(dev);
  433. struct rndis_function_config *config = f->config;
  434. return sprintf(buf, "%d\n", config->wceis);
  435. }
  436. static ssize_t rndis_wceis_store(struct device *dev,
  437. struct device_attribute *attr, const char *buf, size_t size)
  438. {
  439. struct android_usb_function *f = dev_get_drvdata(dev);
  440. struct rndis_function_config *config = f->config;
  441. int value;
  442. if (sscanf(buf, "%d", &value) == 1) {
  443. config->wceis = value;
  444. return size;
  445. }
  446. return -EINVAL;
  447. }
  448. static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
  449. rndis_wceis_store);
  450. static ssize_t rndis_ethaddr_show(struct device *dev,
  451. struct device_attribute *attr, char *buf)
  452. {
  453. struct android_usb_function *f = dev_get_drvdata(dev);
  454. struct rndis_function_config *rndis = f->config;
  455. return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  456. rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
  457. rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
  458. }
  459. static ssize_t rndis_ethaddr_store(struct device *dev,
  460. struct device_attribute *attr, const char *buf, size_t size)
  461. {
  462. struct android_usb_function *f = dev_get_drvdata(dev);
  463. struct rndis_function_config *rndis = f->config;
  464. if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  465. (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
  466. (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
  467. (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
  468. return size;
  469. return -EINVAL;
  470. }
  471. static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
  472. rndis_ethaddr_store);
  473. static ssize_t rndis_vendorID_show(struct device *dev,
  474. struct device_attribute *attr, char *buf)
  475. {
  476. struct android_usb_function *f = dev_get_drvdata(dev);
  477. struct rndis_function_config *config = f->config;
  478. return sprintf(buf, "%04x\n", config->vendorID);
  479. }
  480. static ssize_t rndis_vendorID_store(struct device *dev,
  481. struct device_attribute *attr, const char *buf, size_t size)
  482. {
  483. struct android_usb_function *f = dev_get_drvdata(dev);
  484. struct rndis_function_config *config = f->config;
  485. int value;
  486. if (sscanf(buf, "%04x", &value) == 1) {
  487. config->vendorID = value;
  488. return size;
  489. }
  490. return -EINVAL;
  491. }
  492. static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
  493. rndis_vendorID_store);
  494. static struct device_attribute *rndis_function_attributes[] = {
  495. &dev_attr_manufacturer,
  496. &dev_attr_wceis,
  497. &dev_attr_ethaddr,
  498. &dev_attr_vendorID,
  499. NULL
  500. };
  501. static struct android_usb_function rndis_function = {
  502. .name = "rndis",
  503. .init = rndis_function_init,
  504. .cleanup = rndis_function_cleanup,
  505. .bind_config = rndis_function_bind_config,
  506. .unbind_config = rndis_function_unbind_config,
  507. .attributes = rndis_function_attributes,
  508. };
  509. struct mass_storage_function_config {
  510. struct fsg_config fsg;
  511. struct fsg_common *common;
  512. };
  513. static int mass_storage_function_init(struct android_usb_function *f,
  514. struct usb_composite_dev *cdev)
  515. {
  516. struct mass_storage_function_config *config;
  517. struct fsg_common *common;
  518. int err;
  519. config = kzalloc(sizeof(struct mass_storage_function_config),
  520. GFP_KERNEL);
  521. if (!config)
  522. return -ENOMEM;
  523. config->fsg.nluns = 1;
  524. config->fsg.luns[0].removable = 1;
  525. common = fsg_common_init(NULL, cdev, &config->fsg);
  526. if (IS_ERR(common)) {
  527. kfree(config);
  528. return PTR_ERR(common);
  529. }
  530. err = sysfs_create_link(&f->dev->kobj,
  531. &common->luns[0].dev.kobj,
  532. "lun");
  533. if (err) {
  534. kfree(config);
  535. return err;
  536. }
  537. config->common = common;
  538. f->config = config;
  539. return 0;
  540. }
  541. static void mass_storage_function_cleanup(struct android_usb_function *f)
  542. {
  543. kfree(f->config);
  544. f->config = NULL;
  545. }
  546. static int mass_storage_function_bind_config(struct android_usb_function *f,
  547. struct usb_configuration *c)
  548. {
  549. struct mass_storage_function_config *config = f->config;
  550. return fsg_bind_config(c->cdev, c, config->common);
  551. }
  552. static ssize_t mass_storage_inquiry_show(struct device *dev,
  553. struct device_attribute *attr, char *buf)
  554. {
  555. struct android_usb_function *f = dev_get_drvdata(dev);
  556. struct mass_storage_function_config *config = f->config;
  557. return sprintf(buf, "%s\n", config->common->inquiry_string);
  558. }
  559. static ssize_t mass_storage_inquiry_store(struct device *dev,
  560. struct device_attribute *attr, const char *buf, size_t size)
  561. {
  562. struct android_usb_function *f = dev_get_drvdata(dev);
  563. struct mass_storage_function_config *config = f->config;
  564. if (size >= sizeof(config->common->inquiry_string))
  565. return -EINVAL;
  566. if (sscanf(buf, "%s", config->common->inquiry_string) != 1)
  567. return -EINVAL;
  568. return size;
  569. }
  570. static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
  571. mass_storage_inquiry_show,
  572. mass_storage_inquiry_store);
  573. static struct device_attribute *mass_storage_function_attributes[] = {
  574. &dev_attr_inquiry_string,
  575. NULL
  576. };
  577. static struct android_usb_function mass_storage_function = {
  578. .name = "mass_storage",
  579. .init = mass_storage_function_init,
  580. .cleanup = mass_storage_function_cleanup,
  581. .bind_config = mass_storage_function_bind_config,
  582. .attributes = mass_storage_function_attributes,
  583. };
  584. static int accessory_function_init(struct android_usb_function *f,
  585. struct usb_composite_dev *cdev)
  586. {
  587. return acc_setup();
  588. }
  589. static void accessory_function_cleanup(struct android_usb_function *f)
  590. {
  591. acc_cleanup();
  592. }
  593. static int accessory_function_bind_config(struct android_usb_function *f,
  594. struct usb_configuration *c)
  595. {
  596. return acc_bind_config(c);
  597. }
  598. static int accessory_function_ctrlrequest(struct android_usb_function *f,
  599. struct usb_composite_dev *cdev,
  600. const struct usb_ctrlrequest *c)
  601. {
  602. return acc_ctrlrequest(cdev, c);
  603. }
  604. static struct android_usb_function accessory_function = {
  605. .name = "accessory",
  606. .init = accessory_function_init,
  607. .cleanup = accessory_function_cleanup,
  608. .bind_config = accessory_function_bind_config,
  609. .ctrlrequest = accessory_function_ctrlrequest,
  610. };
  611. static struct android_usb_function *supported_functions[] = {
  612. &adb_function,
  613. &acm_function,
  614. &mtp_function,
  615. &ptp_function,
  616. &rndis_function,
  617. &mass_storage_function,
  618. &accessory_function,
  619. NULL
  620. };
  621. static int android_init_functions(struct android_usb_function **functions,
  622. struct usb_composite_dev *cdev)
  623. {
  624. struct android_dev *dev = _android_dev;
  625. struct android_usb_function *f;
  626. struct device_attribute **attrs;
  627. struct device_attribute *attr;
  628. int err;
  629. int index = 0;
  630. for (; (f = *functions++); index++) {
  631. f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
  632. f->dev = device_create(android_class, dev->dev,
  633. MKDEV(0, index), f, f->dev_name);
  634. if (IS_ERR(f->dev)) {
  635. pr_err("%s: Failed to create dev %s", __func__,
  636. f->dev_name);
  637. err = PTR_ERR(f->dev);
  638. goto err_create;
  639. }
  640. if (f->init) {
  641. err = f->init(f, cdev);
  642. if (err) {
  643. pr_err("%s: Failed to init %s", __func__,
  644. f->name);
  645. goto err_out;
  646. }
  647. }
  648. attrs = f->attributes;
  649. if (attrs) {
  650. while ((attr = *attrs++) && !err)
  651. err = device_create_file(f->dev, attr);
  652. }
  653. if (err) {
  654. pr_err("%s: Failed to create function %s attributes",
  655. __func__, f->name);
  656. goto err_out;
  657. }
  658. }
  659. return 0;
  660. err_out:
  661. device_destroy(android_class, f->dev->devt);
  662. err_create:
  663. kfree(f->dev_name);
  664. return err;
  665. }
  666. static void android_cleanup_functions(struct android_usb_function **functions)
  667. {
  668. struct android_usb_function *f;
  669. while (*functions) {
  670. f = *functions++;
  671. if (f->dev) {
  672. device_destroy(android_class, f->dev->devt);
  673. kfree(f->dev_name);
  674. }
  675. if (f->cleanup)
  676. f->cleanup(f);
  677. }
  678. }
  679. static int
  680. android_bind_enabled_functions(struct android_dev *dev,
  681. struct usb_configuration *c)
  682. {
  683. struct android_usb_function *f;
  684. int ret;
  685. list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
  686. ret = f->bind_config(f, c);
  687. if (ret) {
  688. pr_err("%s: %s failed", __func__, f->name);
  689. return ret;
  690. }
  691. }
  692. return 0;
  693. }
  694. static void
  695. android_unbind_enabled_functions(struct android_dev *dev,
  696. struct usb_configuration *c)
  697. {
  698. struct android_usb_function *f;
  699. list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
  700. if (f->unbind_config)
  701. f->unbind_config(f, c);
  702. }
  703. }
  704. static int android_enable_function(struct android_dev *dev, char *name)
  705. {
  706. struct android_usb_function **functions = dev->functions;
  707. struct android_usb_function *f;
  708. while ((f = *functions++)) {
  709. if (!strcmp(name, f->name)) {
  710. list_add_tail(&f->enabled_list, &dev->enabled_functions);
  711. return 0;
  712. }
  713. }
  714. return -EINVAL;
  715. }
  716. /*-------------------------------------------------------------------------*/
  717. /* /sys/class/android_usb/android%d/ interface */
  718. static ssize_t
  719. functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
  720. {
  721. struct android_dev *dev = dev_get_drvdata(pdev);
  722. struct android_usb_function *f;
  723. char *buff = buf;
  724. mutex_lock(&dev->mutex);
  725. list_for_each_entry(f, &dev->enabled_functions, enabled_list)
  726. buff += sprintf(buff, "%s,", f->name);
  727. mutex_unlock(&dev->mutex);
  728. if (buff != buf)
  729. *(buff-1) = '\n';
  730. return buff - buf;
  731. }
  732. static ssize_t
  733. functions_store(struct device *pdev, struct device_attribute *attr,
  734. const char *buff, size_t size)
  735. {
  736. struct android_dev *dev = dev_get_drvdata(pdev);
  737. char *name;
  738. char buf[256], *b;
  739. int err;
  740. mutex_lock(&dev->mutex);
  741. if (dev->enabled) {
  742. mutex_unlock(&dev->mutex);
  743. return -EBUSY;
  744. }
  745. INIT_LIST_HEAD(&dev->enabled_functions);
  746. strncpy(buf, buff, sizeof(buf));
  747. b = strim(buf);
  748. while (b) {
  749. name = strsep(&b, ",");
  750. if (name) {
  751. err = android_enable_function(dev, name);
  752. if (err)
  753. pr_err("android_usb: Cannot enable '%s'", name);
  754. }
  755. }
  756. mutex_unlock(&dev->mutex);
  757. return size;
  758. }
  759. static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
  760. char *buf)
  761. {
  762. struct android_dev *dev = dev_get_drvdata(pdev);
  763. return sprintf(buf, "%d\n", dev->enabled);
  764. }
  765. static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
  766. const char *buff, size_t size)
  767. {
  768. struct android_dev *dev = dev_get_drvdata(pdev);
  769. struct usb_composite_dev *cdev = dev->cdev;
  770. struct android_usb_function *f;
  771. int enabled = 0;
  772. mutex_lock(&dev->mutex);
  773. sscanf(buff, "%d", &enabled);
  774. if (enabled && !dev->enabled) {
  775. /* update values in composite driver's copy of device descriptor */
  776. cdev->desc.idVendor = device_desc.idVendor;
  777. cdev->desc.idProduct = device_desc.idProduct;
  778. cdev->desc.bcdDevice = device_desc.bcdDevice;
  779. cdev->desc.bDeviceClass = device_desc.bDeviceClass;
  780. cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
  781. cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
  782. list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
  783. if (f->enable)
  784. f->enable(f);
  785. }
  786. android_enable(dev);
  787. dev->enabled = true;
  788. } else if (!enabled && dev->enabled) {
  789. android_disable(dev);
  790. list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
  791. if (f->disable)
  792. f->disable(f);
  793. }
  794. dev->enabled = false;
  795. } else {
  796. pr_err("android_usb: already %s\n",
  797. dev->enabled ? "enabled" : "disabled");
  798. }
  799. mutex_unlock(&dev->mutex);
  800. return size;
  801. }
  802. static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
  803. char *buf)
  804. {
  805. struct android_dev *dev = dev_get_drvdata(pdev);
  806. struct usb_composite_dev *cdev = dev->cdev;
  807. char *state = "DISCONNECTED";
  808. unsigned long flags;
  809. if (!cdev)
  810. goto out;
  811. spin_lock_irqsave(&cdev->lock, flags);
  812. if (cdev->config)
  813. state = "CONFIGURED";
  814. else if (dev->connected)
  815. state = "CONNECTED";
  816. spin_unlock_irqrestore(&cdev->lock, flags);
  817. out:
  818. return sprintf(buf, "%s\n", state);
  819. }
  820. #define DESCRIPTOR_ATTR(field, format_string) \
  821. static ssize_t \
  822. field ## _show(struct device *dev, struct device_attribute *attr, \
  823. char *buf) \
  824. { \
  825. return sprintf(buf, format_string, device_desc.field); \
  826. } \
  827. static ssize_t \
  828. field ## _store(struct device *dev, struct device_attribute *attr, \
  829. const char *buf, size_t size) \
  830. { \
  831. int value; \
  832. if (sscanf(buf, format_string, &value) == 1) { \
  833. device_desc.field = value; \
  834. return size; \
  835. } \
  836. return -1; \
  837. } \
  838. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
  839. #define DESCRIPTOR_STRING_ATTR(field, buffer) \
  840. static ssize_t \
  841. field ## _show(struct device *dev, struct device_attribute *attr, \
  842. char *buf) \
  843. { \
  844. return sprintf(buf, "%s", buffer); \
  845. } \
  846. static ssize_t \
  847. field ## _store(struct device *dev, struct device_attribute *attr, \
  848. const char *buf, size_t size) \
  849. { \
  850. if (size >= sizeof(buffer)) return -EINVAL; \
  851. if (sscanf(buf, "%s", buffer) == 1) { \
  852. return size; \
  853. } \
  854. return -1; \
  855. } \
  856. static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
  857. DESCRIPTOR_ATTR(idVendor, "%04x\n")
  858. DESCRIPTOR_ATTR(idProduct, "%04x\n")
  859. DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
  860. DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
  861. DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
  862. DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
  863. DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
  864. DESCRIPTOR_STRING_ATTR(iProduct, product_string)
  865. DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
  866. static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
  867. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
  868. static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
  869. static struct device_attribute *android_usb_attributes[] = {
  870. &dev_attr_idVendor,
  871. &dev_attr_idProduct,
  872. &dev_attr_bcdDevice,
  873. &dev_attr_bDeviceClass,
  874. &dev_attr_bDeviceSubClass,
  875. &dev_attr_bDeviceProtocol,
  876. &dev_attr_iManufacturer,
  877. &dev_attr_iProduct,
  878. &dev_attr_iSerial,
  879. &dev_attr_functions,
  880. &dev_attr_enable,
  881. &dev_attr_state,
  882. NULL
  883. };
  884. /*-------------------------------------------------------------------------*/
  885. /* Composite driver */
  886. static int android_bind_config(struct usb_configuration *c)
  887. {
  888. struct android_dev *dev = _android_dev;
  889. int ret = 0;
  890. ret = android_bind_enabled_functions(dev, c);
  891. if (ret)
  892. return ret;
  893. return 0;
  894. }
  895. static void android_unbind_config(struct usb_configuration *c)
  896. {
  897. struct android_dev *dev = _android_dev;
  898. android_unbind_enabled_functions(dev, c);
  899. }
  900. static int android_bind(struct usb_composite_dev *cdev)
  901. {
  902. struct android_dev *dev = _android_dev;
  903. struct usb_gadget *gadget = cdev->gadget;
  904. int gcnum, id, ret;
  905. usb_gadget_disconnect(gadget);
  906. ret = android_init_functions(dev->functions, cdev);
  907. if (ret)
  908. return ret;
  909. /* Allocate string descriptor numbers ... note that string
  910. * contents can be overridden by the composite_dev glue.
  911. */
  912. id = usb_string_id(cdev);
  913. if (id < 0)
  914. return id;
  915. strings_dev[STRING_MANUFACTURER_IDX].id = id;
  916. device_desc.iManufacturer = id;
  917. id = usb_string_id(cdev);
  918. if (id < 0)
  919. return id;
  920. strings_dev[STRING_PRODUCT_IDX].id = id;
  921. device_desc.iProduct = id;
  922. /* Default strings - should be updated by userspace */
  923. strncpy(manufacturer_string, "Android", sizeof(manufacturer_string) - 1);
  924. strncpy(product_string, "Android", sizeof(product_string) - 1);
  925. strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
  926. id = usb_string_id(cdev);
  927. if (id < 0)
  928. return id;
  929. strings_dev[STRING_SERIAL_IDX].id = id;
  930. device_desc.iSerialNumber = id;
  931. gcnum = usb_gadget_controller_number(gadget);
  932. if (gcnum >= 0)
  933. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  934. else {
  935. /* gadget zero is so simple (for now, no altsettings) that
  936. * it SHOULD NOT have problems with bulk-capable hardware.
  937. * so just warn about unrcognized controllers -- don't panic.
  938. *
  939. * things like configuration and altsetting numbering
  940. * can need hardware-specific attention though.
  941. */
  942. pr_warning("%s: controller '%s' not recognized\n",
  943. longname, gadget->name);
  944. device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
  945. }
  946. dev->cdev = cdev;
  947. return 0;
  948. }
  949. static int android_usb_unbind(struct usb_composite_dev *cdev)
  950. {
  951. struct android_dev *dev = _android_dev;
  952. cancel_work_sync(&dev->work);
  953. android_cleanup_functions(dev->functions);
  954. return 0;
  955. }
  956. static struct usb_composite_driver android_usb_driver = {
  957. .name = "android_usb",
  958. .dev = &device_desc,
  959. .strings = dev_strings,
  960. .unbind = android_usb_unbind,
  961. };
  962. static int
  963. android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
  964. {
  965. struct android_dev *dev = _android_dev;
  966. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  967. struct usb_request *req = cdev->req;
  968. struct android_usb_function *f;
  969. int value = -EOPNOTSUPP;
  970. unsigned long flags;
  971. req->zero = 0;
  972. req->complete = composite_setup_complete;
  973. req->length = 0;
  974. gadget->ep0->driver_data = cdev;
  975. list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
  976. if (f->ctrlrequest) {
  977. value = f->ctrlrequest(f, cdev, c);
  978. if (value >= 0)
  979. break;
  980. }
  981. }
  982. /* Special case the accessory function.
  983. * It needs to handle control requests before it is enabled.
  984. */
  985. if (value < 0)
  986. value = acc_ctrlrequest(cdev, c);
  987. if (value < 0)
  988. value = composite_setup(gadget, c);
  989. spin_lock_irqsave(&cdev->lock, flags);
  990. if (!dev->connected) {
  991. dev->connected = 1;
  992. schedule_work(&dev->work);
  993. }
  994. else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
  995. schedule_work(&dev->work);
  996. }
  997. spin_unlock_irqrestore(&cdev->lock, flags);
  998. return value;
  999. }
  1000. static void android_disconnect(struct usb_gadget *gadget)
  1001. {
  1002. struct android_dev *dev = _android_dev;
  1003. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1004. unsigned long flags;
  1005. composite_disconnect(gadget);
  1006. spin_lock_irqsave(&cdev->lock, flags);
  1007. dev->connected = 0;
  1008. schedule_work(&dev->work);
  1009. spin_unlock_irqrestore(&cdev->lock, flags);
  1010. }
  1011. static int android_create_device(struct android_dev *dev)
  1012. {
  1013. struct device_attribute **attrs = android_usb_attributes;
  1014. struct device_attribute *attr;
  1015. int err;
  1016. dev->dev = device_create(android_class, NULL,
  1017. MKDEV(0, 0), NULL, "android0");
  1018. if (IS_ERR(dev->dev))
  1019. return PTR_ERR(dev->dev);
  1020. dev_set_drvdata(dev->dev, dev);
  1021. while ((attr = *attrs++)) {
  1022. err = device_create_file(dev->dev, attr);
  1023. if (err) {
  1024. device_destroy(android_class, dev->dev->devt);
  1025. return err;
  1026. }
  1027. }
  1028. return 0;
  1029. }
  1030. static int __init init(void)
  1031. {
  1032. struct android_dev *dev;
  1033. int err;
  1034. android_class = class_create(THIS_MODULE, "android_usb");
  1035. if (IS_ERR(android_class))
  1036. return PTR_ERR(android_class);
  1037. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1038. if (!dev)
  1039. return -ENOMEM;
  1040. dev->disable_depth = 1;
  1041. dev->functions = supported_functions;
  1042. INIT_LIST_HEAD(&dev->enabled_functions);
  1043. INIT_WORK(&dev->work, android_work);
  1044. mutex_init(&dev->mutex);
  1045. err = android_create_device(dev);
  1046. if (err) {
  1047. class_destroy(android_class);
  1048. kfree(dev);
  1049. return err;
  1050. }
  1051. _android_dev = dev;
  1052. /* Override composite driver functions */
  1053. composite_driver.setup = android_setup;
  1054. composite_driver.disconnect = android_disconnect;
  1055. return usb_composite_probe(&android_usb_driver, android_bind);
  1056. }
  1057. module_init(init);
  1058. static void __exit cleanup(void)
  1059. {
  1060. usb_composite_unregister(&android_usb_driver);
  1061. class_destroy(android_class);
  1062. kfree(_android_dev);
  1063. _android_dev = NULL;
  1064. }
  1065. module_exit(cleanup);