f_adb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. * Gadget Driver for Android ADB
  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. #ifdef pr_fmt
  18. #undef pr_fmt
  19. #endif
  20. #define pr_fmt(fmt) "["KBUILD_MODNAME"]" fmt
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/poll.h>
  24. #include <linux/delay.h>
  25. #include <linux/wait.h>
  26. #include <linux/err.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/sched.h>
  29. #include <linux/types.h>
  30. #include <linux/device.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/ratelimit.h>
  33. #define ADB_BULK_BUFFER_SIZE 4096
  34. /* number of tx requests to allocate */
  35. #define TX_REQ_MAX 4
  36. static const char adb_shortname[] = "android_adb";
  37. struct adb_dev {
  38. struct usb_function function;
  39. struct usb_composite_dev *cdev;
  40. spinlock_t lock;
  41. struct usb_ep *ep_in;
  42. struct usb_ep *ep_out;
  43. int online;
  44. int error;
  45. atomic_t read_excl;
  46. atomic_t write_excl;
  47. atomic_t open_excl;
  48. struct list_head tx_idle;
  49. wait_queue_head_t read_wq;
  50. wait_queue_head_t write_wq;
  51. struct usb_request *rx_req;
  52. int rx_done;
  53. };
  54. /* __ADB_DEBUG__ start */
  55. struct usb_ep *ep_in = NULL;
  56. struct usb_ep *ep_out = NULL;
  57. int bitdebug_enabled;
  58. unsigned bitdebug_writeCnt = 1;
  59. unsigned bitdebug_readCnt = 0;
  60. struct amessage {
  61. unsigned command; /* command identifier constant */
  62. unsigned arg0; /* first argument */
  63. unsigned arg1; /* second argument */
  64. unsigned data_length; /* length of payload (0 is allowed) */
  65. unsigned data_check; /* checksum of data payload */
  66. unsigned magic; /* command ^ 0xffffffff */
  67. };
  68. struct debuginfo {
  69. unsigned headtoken;
  70. unsigned command; /* command identifier constant */
  71. unsigned msg_check;
  72. unsigned data_check;
  73. unsigned count;
  74. unsigned dummy;
  75. unsigned tailtoken;
  76. };
  77. typedef struct amessage amessage;
  78. typedef struct debuginfo debuginfo;
  79. #define A_SYNC 0x434e5953
  80. #define A_CNXN 0x4e584e43
  81. #define A_OPEN 0x4e45504f
  82. #define A_OKAY 0x59414b4f
  83. #define A_CLSE 0x45534c43
  84. #define A_WRTE 0x45545257
  85. #define A_AUTH 0x48545541
  86. #define A_DBUG 0x41424a42
  87. #define DBGHEADTOKEN 0x13579bdf
  88. #define DBGTAILTOKEN 0xdca86420
  89. /* __ADB_DEBUG__ end */
  90. static struct usb_interface_descriptor adb_interface_desc = {
  91. .bLength = USB_DT_INTERFACE_SIZE,
  92. .bDescriptorType = USB_DT_INTERFACE,
  93. .bInterfaceNumber = 0,
  94. .bNumEndpoints = 2,
  95. .bInterfaceClass = 0xFF,
  96. .bInterfaceSubClass = 0x42,
  97. .bInterfaceProtocol = 1,
  98. };
  99. static struct usb_endpoint_descriptor adb_superspeed_in_desc = {
  100. .bLength = USB_DT_ENDPOINT_SIZE,
  101. .bDescriptorType = USB_DT_ENDPOINT,
  102. .bEndpointAddress = USB_DIR_IN,
  103. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  104. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  105. };
  106. static struct usb_ss_ep_comp_descriptor adb_superspeed_in_comp_desc = {
  107. .bLength = sizeof(adb_superspeed_in_comp_desc),
  108. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  109. /* the following 2 values can be tweaked if necessary */
  110. /* .bMaxBurst = 0, */
  111. /* .bmAttributes = 0, */
  112. };
  113. static struct usb_endpoint_descriptor adb_superspeed_out_desc = {
  114. .bLength = USB_DT_ENDPOINT_SIZE,
  115. .bDescriptorType = USB_DT_ENDPOINT,
  116. .bEndpointAddress = USB_DIR_OUT,
  117. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  118. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  119. };
  120. static struct usb_ss_ep_comp_descriptor adb_superspeed_out_comp_desc = {
  121. .bLength = sizeof(adb_superspeed_out_comp_desc),
  122. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  123. /* the following 2 values can be tweaked if necessary */
  124. /* .bMaxBurst = 0, */
  125. /* .bmAttributes = 0, */
  126. };
  127. static struct usb_endpoint_descriptor adb_highspeed_in_desc = {
  128. .bLength = USB_DT_ENDPOINT_SIZE,
  129. .bDescriptorType = USB_DT_ENDPOINT,
  130. .bEndpointAddress = USB_DIR_IN,
  131. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  132. .wMaxPacketSize = __constant_cpu_to_le16(512),
  133. };
  134. static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
  135. .bLength = USB_DT_ENDPOINT_SIZE,
  136. .bDescriptorType = USB_DT_ENDPOINT,
  137. .bEndpointAddress = USB_DIR_OUT,
  138. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  139. .wMaxPacketSize = __constant_cpu_to_le16(512),
  140. };
  141. static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
  142. .bLength = USB_DT_ENDPOINT_SIZE,
  143. .bDescriptorType = USB_DT_ENDPOINT,
  144. .bEndpointAddress = USB_DIR_IN,
  145. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  146. };
  147. static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {
  148. .bLength = USB_DT_ENDPOINT_SIZE,
  149. .bDescriptorType = USB_DT_ENDPOINT,
  150. .bEndpointAddress = USB_DIR_OUT,
  151. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  152. };
  153. static struct usb_descriptor_header *fs_adb_descs[] = {
  154. (struct usb_descriptor_header *)&adb_interface_desc,
  155. (struct usb_descriptor_header *)&adb_fullspeed_in_desc,
  156. (struct usb_descriptor_header *)&adb_fullspeed_out_desc,
  157. NULL,
  158. };
  159. static struct usb_descriptor_header *hs_adb_descs[] = {
  160. (struct usb_descriptor_header *)&adb_interface_desc,
  161. (struct usb_descriptor_header *)&adb_highspeed_in_desc,
  162. (struct usb_descriptor_header *)&adb_highspeed_out_desc,
  163. NULL,
  164. };
  165. static struct usb_descriptor_header *ss_adb_descs[] = {
  166. (struct usb_descriptor_header *)&adb_interface_desc,
  167. (struct usb_descriptor_header *)&adb_superspeed_in_desc,
  168. (struct usb_descriptor_header *)&adb_superspeed_in_comp_desc,
  169. (struct usb_descriptor_header *)&adb_superspeed_out_desc,
  170. (struct usb_descriptor_header *)&adb_superspeed_out_comp_desc,
  171. NULL,
  172. };
  173. static void adb_debug_read_copy_from_user(char __user *buf, struct usb_request *req)
  174. {
  175. if (sizeof(debuginfo) == req->length) {
  176. unsigned long ret;
  177. ret = copy_from_user(req->buf, buf, req->length);
  178. if (ret != 0) {
  179. pr_err("copy_from_user fail\n");
  180. }
  181. }
  182. }
  183. static void adb_debug_read_copy_to_user(char __user *buf, struct usb_request *req)
  184. {
  185. debuginfo *dbg = (debuginfo *) req->buf;
  186. if (dbg != NULL && dbg->command == A_DBUG && dbg->headtoken == DBGHEADTOKEN
  187. && dbg->tailtoken == DBGTAILTOKEN) {
  188. unsigned long ret;
  189. ret = copy_to_user(buf, req->buf, req->length);
  190. if (ret != 0) {
  191. pr_err("copy_to_user fail\n");
  192. }
  193. printk(KERN_INFO "adb_read A_DBUG (0x%x) (0x%x) (0x%x)\n", dbg->command,
  194. dbg->msg_check, dbg->data_check);
  195. }
  196. }
  197. static void adb_ready_callback(void);
  198. static void adb_closed_callback(void);
  199. /* temporary variable used between adb_open() and adb_gadget_bind() */
  200. static struct adb_dev *_adb_dev;
  201. static inline struct adb_dev *func_to_adb(struct usb_function *f)
  202. {
  203. return container_of(f, struct adb_dev, function);
  204. }
  205. static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
  206. {
  207. struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
  208. if (!req)
  209. return NULL;
  210. /* now allocate buffers for the requests */
  211. req->buf = kmalloc(buffer_size, GFP_KERNEL);
  212. if (!req->buf) {
  213. pr_err("%s %s %d: kmalloc failed\n", __FILE__, __func__, __LINE__);
  214. usb_ep_free_request(ep, req);
  215. return NULL;
  216. }
  217. return req;
  218. }
  219. static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
  220. {
  221. if (req) {
  222. kfree(req->buf);
  223. usb_ep_free_request(ep, req);
  224. }
  225. }
  226. static inline int adb_lock(atomic_t *excl)
  227. {
  228. /*printk(KERN_DEBUG "%s %s %d: excl: %d\n", __FILE__, __func__, __LINE__, atomic_read(excl)); */
  229. if (atomic_inc_return(excl) == 1) {
  230. return 0;
  231. } else {
  232. atomic_dec(excl);
  233. return -1;
  234. }
  235. }
  236. static inline void adb_unlock(atomic_t *excl)
  237. {
  238. /*printk(KERN_DEBUG "%s %s %d: excl: %d\n", __FILE__, __func__, __LINE__, atomic_read(excl)); */
  239. atomic_dec(excl);
  240. }
  241. /* add a request to the tail of a list */
  242. void adb_req_put(struct adb_dev *dev, struct list_head *head, struct usb_request *req)
  243. {
  244. unsigned long flags;
  245. spin_lock_irqsave(&dev->lock, flags);
  246. list_add_tail(&req->list, head);
  247. spin_unlock_irqrestore(&dev->lock, flags);
  248. }
  249. /* remove a request from the head of a list */
  250. struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
  251. {
  252. unsigned long flags;
  253. struct usb_request *req;
  254. spin_lock_irqsave(&dev->lock, flags);
  255. if (list_empty(head)) {
  256. req = 0;
  257. } else {
  258. req = list_first_entry(head, struct usb_request, list);
  259. list_del(&req->list);
  260. }
  261. spin_unlock_irqrestore(&dev->lock, flags);
  262. return req;
  263. }
  264. static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
  265. {
  266. struct adb_dev *dev = _adb_dev;
  267. if (req->status != 0)
  268. dev->error = 1;
  269. adb_req_put(dev, &dev->tx_idle, req);
  270. wake_up(&dev->write_wq);
  271. }
  272. static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
  273. {
  274. struct adb_dev *dev = _adb_dev;
  275. dev->rx_done = 1;
  276. if (req->status != 0 && req->status != -ECONNRESET)
  277. dev->error = 1;
  278. wake_up(&dev->read_wq);
  279. }
  280. static int adb_create_bulk_endpoints(struct adb_dev *dev,
  281. struct usb_endpoint_descriptor *in_desc,
  282. struct usb_endpoint_descriptor *out_desc)
  283. {
  284. struct usb_composite_dev *cdev = dev->cdev;
  285. struct usb_request *req;
  286. struct usb_ep *ep;
  287. int i;
  288. DBG(cdev, "%s %s %d: create_bulk_endpoints dev: %p\n", __FILE__, __func__, __LINE__, dev);
  289. ep = usb_ep_autoconfig(cdev->gadget, in_desc);
  290. /* __ADB_DEBUG__ start */
  291. ep_in = ep;
  292. /* __ADB_DEBUG__ end */
  293. if (!ep) {
  294. DBG(cdev, "%s %s %d: usb_ep_autoconfig for ep_in failed\n", __FILE__, __func__,
  295. __LINE__);
  296. return -ENODEV;
  297. }
  298. DBG(cdev, "%s %s %d: usb_ep_autoconfig for ep_in got %s\n", __FILE__, __func__, __LINE__,
  299. ep->name);
  300. ep->driver_data = dev; /* claim the endpoint */
  301. dev->ep_in = ep;
  302. ep = usb_ep_autoconfig(cdev->gadget, out_desc);
  303. /* __ADB_DEBUG__ start */
  304. ep_out = ep;
  305. /* __ADB_DEBUG__ end */
  306. if (!ep) {
  307. DBG(cdev, "%s %s %d: usb_ep_autoconfig for ep_out failed\n", __FILE__, __func__,
  308. __LINE__);
  309. return -ENODEV;
  310. }
  311. DBG(cdev, "%s %s %d: usb_ep_autoconfig for adb ep_out got %s\n", __FILE__, __func__,
  312. __LINE__, ep->name);
  313. ep->driver_data = dev; /* claim the endpoint */
  314. dev->ep_out = ep;
  315. /* now allocate requests for our endpoints */
  316. req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
  317. if (!req)
  318. goto fail;
  319. req->complete = adb_complete_out;
  320. dev->rx_req = req;
  321. for (i = 0; i < TX_REQ_MAX; i++) {
  322. req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
  323. if (!req)
  324. goto fail;
  325. req->complete = adb_complete_in;
  326. adb_req_put(dev, &dev->tx_idle, req);
  327. }
  328. return 0;
  329. fail:
  330. printk(KERN_ERR "%s %s %d: adb_bind() could not allocate requests\n", __FILE__, __func__,
  331. __LINE__);
  332. return -1;
  333. }
  334. static ssize_t adb_read(struct file *fp, char __user *buf, size_t count, loff_t *pos)
  335. {
  336. struct adb_dev *dev = fp->private_data;
  337. struct usb_request *req;
  338. int r = count, xfer;
  339. int ret;
  340. static DEFINE_RATELIMIT_STATE(ratelimit, 1 * HZ, 10);
  341. pr_debug("%s %s %d: (%d)\n", __FILE__, __func__, __LINE__, (int)count);
  342. if (!_adb_dev)
  343. return -ENODEV;
  344. if (count > ADB_BULK_BUFFER_SIZE) {
  345. pr_err("%s %s %d: count > ADB_BULK_BUFFER_SIZE\n", __FILE__, __func__, __LINE__);
  346. return -EINVAL;
  347. }
  348. if (adb_lock(&dev->read_excl)) {
  349. if (__ratelimit(&ratelimit)) {
  350. pr_err("%s %s %d: Failed due to lock busy\n", __FILE__, __func__, __LINE__);
  351. }
  352. return -EBUSY;
  353. }
  354. /* we will block until we're online */
  355. while (!(dev->online || dev->error)) {
  356. pr_debug("%s %s %d: waiting for online state\n", __FILE__, __func__, __LINE__);
  357. ret = wait_event_interruptible(dev->read_wq, (dev->online || dev->error));
  358. if (ret < 0) {
  359. adb_unlock(&dev->read_excl);
  360. return ret;
  361. }
  362. }
  363. if (dev->error) {
  364. r = -EIO;
  365. goto done;
  366. }
  367. requeue_req:
  368. /* queue a request */
  369. req = dev->rx_req;
  370. req->length = count;
  371. dev->rx_done = 0;
  372. /*
  373. * The MAX_PAYLOAD of adb is 4096bytes defined in system/core/adb/adb.h
  374. * So when meet the request has to read 4096bytes long payload,
  375. * set short_not_ok is 1. Use musb dma mode 1 to speed up the write
  376. * throughput.
  377. */
  378. if (count == 4096)
  379. req->short_not_ok = 1;
  380. else
  381. req->short_not_ok = 0;
  382. if (bitdebug_enabled == 1) {
  383. adb_debug_read_copy_from_user(buf, req);
  384. }
  385. ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
  386. if (ret < 0) {
  387. /* FIXME */
  388. /* Process adbd would try to reconnect when usb has been reset. */
  389. /* It should not send data after endpoint has shutdown. */
  390. /* It is a workaround to reduce adb retry times. */
  391. if (ret == -ESHUTDOWN) {
  392. msleep(150);
  393. }
  394. if (bitdebug_enabled == 1) {
  395. if (ret == -EINPROGRESS) {
  396. adb_debug_read_copy_to_user(buf, req);
  397. goto done;
  398. }
  399. }
  400. pr_err("%s %s %d: failed to queue req %p (%d)\n",
  401. __FILE__, __func__, __LINE__, req, ret);
  402. r = -EIO;
  403. dev->error = 1;
  404. goto done;
  405. } else {
  406. pr_debug("%s %s %d: rx %p queue\n", __FILE__, __func__, __LINE__, req);
  407. }
  408. /* wait for a request to complete */
  409. ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
  410. if (ret < 0) {
  411. if (ret != -ERESTARTSYS)
  412. dev->error = 1;
  413. r = ret;
  414. usb_ep_dequeue(dev->ep_out, req);
  415. goto done;
  416. }
  417. if (!dev->error) {
  418. /* If we got a 0-len packet, throw it back and try again. */
  419. if (req->actual == 0)
  420. goto requeue_req;
  421. pr_debug("%s %s %d: rx %p %d\n", __FILE__, __func__, __LINE__, req, req->actual);
  422. xfer = (req->actual < count) ? req->actual : count;
  423. /* __ADB_DEBUG__ start */
  424. if (bitdebug_enabled == 1) {
  425. if (count == sizeof(amessage)) {
  426. amessage *msg = (amessage *) req->buf;
  427. if (msg != NULL) {
  428. switch (msg->command) {
  429. case A_SYNC:
  430. case A_CNXN:
  431. case A_OPEN:
  432. case A_OKAY:
  433. case A_CLSE:
  434. case A_WRTE:
  435. case A_AUTH:
  436. /* printk(KERN_INFO "adb: adb_read (0x%x) (0x%x) (0x%x) (0x%x) (0x%x) (0x%x)\n", msg->command, msg->arg0, msg->arg1, */
  437. /* msg->data_length, msg->data_check, msg->magic); */
  438. break;
  439. default:
  440. /* printk(KERN_INFO "adb_read msg A_DATA\n"); */
  441. break;
  442. }
  443. }
  444. }
  445. }
  446. /* __ADB_DEBUG__ end */
  447. if (copy_to_user(buf, req->buf, xfer))
  448. r = -EFAULT;
  449. } else
  450. r = -EIO;
  451. done:
  452. adb_unlock(&dev->read_excl);
  453. pr_debug("%s %s %d: returning %d\n", __FILE__, __func__, __LINE__, r);
  454. return r;
  455. }
  456. static ssize_t adb_write(struct file *fp, const char __user *buf, size_t count, loff_t *pos)
  457. {
  458. struct adb_dev *dev = fp->private_data;
  459. struct usb_request *req = 0;
  460. int r = count, xfer;
  461. int ret;
  462. static int flow_state;
  463. bool data;
  464. if (!_adb_dev)
  465. return -ENODEV;
  466. pr_debug("%s %s %d:(%d)\n", __FILE__, __func__, __LINE__, (int)count);
  467. if (adb_lock(&dev->write_excl)) {
  468. return -EBUSY;
  469. }
  470. while (count > 0) {
  471. if (dev->error) {
  472. pr_debug("%s %s %d: dev->error\n", __FILE__, __func__, __LINE__);
  473. r = -EIO;
  474. break;
  475. }
  476. /* get an idle tx request to use */
  477. req = 0;
  478. ret = wait_event_interruptible(dev->write_wq,
  479. (req = adb_req_get(dev, &dev->tx_idle))
  480. || dev->error);
  481. if (ret < 0) {
  482. r = ret;
  483. break;
  484. }
  485. if (req != 0) {
  486. if (count > ADB_BULK_BUFFER_SIZE)
  487. xfer = ADB_BULK_BUFFER_SIZE;
  488. else
  489. xfer = count;
  490. if (copy_from_user(req->buf, buf, xfer)) {
  491. r = -EFAULT;
  492. break;
  493. }
  494. /* __ADB_DEBUG__ start */
  495. data = true;
  496. if (bitdebug_enabled == 1) {
  497. if (count == sizeof(amessage)) {
  498. amessage *msg = (amessage *) req->buf;
  499. if (msg != NULL) {
  500. switch (msg->command) {
  501. case A_SYNC:
  502. case A_CNXN:
  503. case A_OPEN:
  504. case A_OKAY:
  505. case A_CLSE:
  506. case A_WRTE:
  507. case A_AUTH:
  508. /* printk(KERN_INFO "adb_write msg (0x%x) (0x%x) (0x%x) (0x%x) (0x%x) (0x%x)\n", msg->command, msg->arg0, msg->arg1, */
  509. /* msg->data_length, msg->data_check, msg->magic); */
  510. if (flow_state == 0) {
  511. flow_state = 1;
  512. /* no data packet */
  513. if (msg->data_length == 0) {
  514. flow_state = 2;
  515. }
  516. } else {
  517. pr_debug
  518. ("adb_write flow state msg warning\n");
  519. pr_debug
  520. ("adb_write msg (0x%x) (0x%x) (0x%x) (0x%x) (0x%x) (0x%x)\n",
  521. msg->command, msg->arg0,
  522. msg->arg1, msg->data_length,
  523. msg->data_check, msg->magic);
  524. }
  525. data = false;
  526. break;
  527. }
  528. }
  529. } else {
  530. data = true;
  531. }
  532. if (count == sizeof(debuginfo)) {
  533. debuginfo *dbg = (debuginfo *) req->buf;
  534. if (dbg != NULL && dbg->command == A_DBUG
  535. && dbg->headtoken == DBGHEADTOKEN
  536. && dbg->tailtoken == DBGTAILTOKEN) {
  537. /* printk(KERN_INFO "adb_write dbg (0x%x) (0x%x) (0x%x)\n", dbg->command, dbg->msg_check, dbg->data_check); */
  538. if (flow_state == 2) {
  539. flow_state = 0;
  540. } else {
  541. pr_debug
  542. ("adb_write flow state debug warning\n");
  543. pr_debug
  544. ("adb_write dbg (0x%x) (0x%x) (0x%x)\n",
  545. dbg->command, dbg->msg_check,
  546. dbg->data_check);
  547. }
  548. data = false;
  549. if (dbg->count == -1) {
  550. bitdebug_enabled = 0;
  551. bitdebug_writeCnt = 1;
  552. bitdebug_readCnt = 0;
  553. msleep(150);
  554. break;
  555. }
  556. }
  557. }
  558. if (data == true && bitdebug_enabled == 1) {
  559. if (flow_state == 1) {
  560. flow_state = 2;
  561. } else {
  562. pr_debug("adb_write flow state data warning\n");
  563. }
  564. /* printk(KERN_INFO "adb_write data\n"); */
  565. }
  566. } else {
  567. if (count == sizeof(debuginfo)) {
  568. debuginfo *dbg = (debuginfo *) req->buf;
  569. if (dbg != NULL && dbg->command == A_DBUG
  570. && dbg->headtoken == DBGHEADTOKEN
  571. && dbg->tailtoken == DBGTAILTOKEN) {
  572. if (dbg->count == 0) {
  573. bitdebug_enabled = 1;
  574. flow_state = 0;
  575. /* req->length = sizeof(debuginfo); */
  576. msleep(150);
  577. break;
  578. }
  579. }
  580. }
  581. }
  582. /* __ADB_DEBUG__ end */
  583. req->length = xfer;
  584. ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
  585. if (ret < 0) {
  586. pr_debug("%s %s %d: xfer error %d\n", __FILE__, __func__, __LINE__,
  587. ret);
  588. dev->error = 1;
  589. r = -EIO;
  590. break;
  591. }
  592. buf += xfer;
  593. count -= xfer;
  594. /* zero this so we don't try to free it on error exit */
  595. req = 0;
  596. }
  597. }
  598. if (req)
  599. adb_req_put(dev, &dev->tx_idle, req);
  600. adb_unlock(&dev->write_excl);
  601. pr_debug("%s %s %d: returning %d\n", __FILE__, __func__, __LINE__, r);
  602. return r;
  603. }
  604. static int adb_open(struct inode *ip, struct file *fp);
  605. static int adb_release(struct inode *ip, struct file *fp);
  606. /* file operations for ADB device /dev/android_adb */
  607. static struct file_operations adb_fops = {
  608. .owner = THIS_MODULE,
  609. .read = adb_read,
  610. .write = adb_write,
  611. .open = adb_open,
  612. .release = adb_release,
  613. };
  614. static struct miscdevice adb_device = {
  615. .minor = MISC_DYNAMIC_MINOR,
  616. .name = adb_shortname,
  617. .fops = &adb_fops,
  618. };
  619. static int open_release_pair;
  620. static spinlock_t open_lock;
  621. static int adb_open(struct inode *ip, struct file *fp)
  622. {
  623. int ret = 0;
  624. unsigned long flags;
  625. bitdebug_enabled = 0;
  626. spin_lock_irqsave(&open_lock, flags);
  627. pr_debug("[adb]adb_open start, adb_open: %p check adb_release %p, open_release_pair: %d\n",
  628. adb_fops.open, adb_fops.release, open_release_pair);
  629. if (!_adb_dev) {
  630. pr_err("[adb]adb_open _adb_dev is NULL, open_release_pair: %d\n",
  631. open_release_pair);
  632. open_release_pair = 0;
  633. ret = ENODEV;
  634. goto OPEN_END;
  635. }
  636. /*Workaround for being unable to call adb_release from adbd */
  637. if (open_release_pair > 0) {
  638. pr_debug("[XLOG_WARN][adb]open twice, %s %s %d: open_release_pair count: %d\n",
  639. __FILE__, __func__, __LINE__, open_release_pair);
  640. ret = 0;
  641. fp->private_data = _adb_dev;
  642. goto OPEN_END;
  643. }
  644. open_release_pair++;
  645. fp->private_data = _adb_dev;
  646. /* clear the error latch */
  647. _adb_dev->error = 0;
  648. adb_ready_callback();
  649. OPEN_END:
  650. pr_debug("[adb]adb_open end, open_release_pair: %d", open_release_pair);
  651. spin_unlock_irqrestore(&open_lock, flags);
  652. return ret;
  653. }
  654. static int adb_release(struct inode *ip, struct file *fp)
  655. {
  656. int ret = 0;
  657. unsigned long flags;
  658. spin_lock_irqsave(&open_lock, flags);
  659. pr_debug
  660. ("[adb]adb_release start, adb_open: %p check adb_release %p, open_release_pair: %d\n",
  661. adb_fops.open, adb_fops.release, open_release_pair);
  662. if (open_release_pair < 1) {
  663. pr_debug
  664. ("[XLOG_WARN][adb][adb] close an unopened device, %s %s %d: open_release_pair count: %d\n",
  665. __FILE__, __func__, __LINE__, open_release_pair);
  666. ret = -1;
  667. goto RELEASE_END;
  668. }
  669. adb_closed_callback();
  670. open_release_pair--;
  671. RELEASE_END:
  672. pr_debug("[adb]adb_release end, open_release_pair: %d", open_release_pair);
  673. spin_unlock_irqrestore(&open_lock, flags);
  674. return ret;
  675. }
  676. static int adb_function_bind(struct usb_configuration *c, struct usb_function *f)
  677. {
  678. struct usb_composite_dev *cdev = c->cdev;
  679. struct adb_dev *dev = func_to_adb(f);
  680. int id;
  681. int ret;
  682. dev->cdev = cdev;
  683. DBG(cdev, "%s %s %d: dev: %p\n", __FILE__, __func__, __LINE__, dev);
  684. /* allocate interface ID(s) */
  685. id = usb_interface_id(c, f);
  686. if (id < 0)
  687. return id;
  688. adb_interface_desc.bInterfaceNumber = id;
  689. /* allocate endpoints */
  690. ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc, &adb_fullspeed_out_desc);
  691. if (ret)
  692. return ret;
  693. /* support high speed hardware */
  694. if (gadget_is_dualspeed(c->cdev->gadget)) {
  695. adb_highspeed_in_desc.bEndpointAddress = adb_fullspeed_in_desc.bEndpointAddress;
  696. adb_highspeed_out_desc.bEndpointAddress = adb_fullspeed_out_desc.bEndpointAddress;
  697. }
  698. /* support super speed hardware */
  699. if (gadget_is_superspeed(c->cdev->gadget)) {
  700. adb_superspeed_in_desc.bEndpointAddress = adb_fullspeed_in_desc.bEndpointAddress;
  701. adb_superspeed_out_desc.bEndpointAddress = adb_fullspeed_out_desc.bEndpointAddress;
  702. }
  703. DBG(cdev, "%s %s %d: %s speed %s: IN/%s, OUT/%s\n", __FILE__, __func__, __LINE__,
  704. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  705. f->name, dev->ep_in->name, dev->ep_out->name);
  706. return 0;
  707. }
  708. static void adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
  709. {
  710. struct adb_dev *dev = func_to_adb(f);
  711. struct usb_request *req;
  712. dev->online = 0;
  713. dev->error = 1;
  714. wake_up(&dev->read_wq);
  715. adb_request_free(dev->rx_req, dev->ep_out);
  716. while ((req = adb_req_get(dev, &dev->tx_idle)))
  717. adb_request_free(req, dev->ep_in);
  718. }
  719. static int adb_function_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  720. {
  721. struct adb_dev *dev = func_to_adb(f);
  722. struct usb_composite_dev *cdev = f->config->cdev;
  723. int ret;
  724. DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
  725. #ifdef CONFIG_USBIF_COMPLIANCE
  726. if (dev->online){
  727. return 0 ;
  728. }
  729. #endif
  730. ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
  731. if (ret)
  732. return ret;
  733. ret = usb_ep_enable(dev->ep_in);
  734. if (ret)
  735. return ret;
  736. ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
  737. if (ret) {
  738. pr_err("%s %s %d: usb_ep_enable in failed\n", __FILE__, __func__, __LINE__);
  739. return ret;
  740. }
  741. ret = usb_ep_enable(dev->ep_out);
  742. if (ret) {
  743. usb_ep_disable(dev->ep_in);
  744. pr_err("%s %s %d: usb_ep_enable out failed\n", __FILE__, __func__, __LINE__);
  745. return ret;
  746. }
  747. dev->online = 1;
  748. /* readers may be blocked waiting for us to go online */
  749. wake_up(&dev->read_wq);
  750. return 0;
  751. }
  752. static void adb_function_disable(struct usb_function *f)
  753. {
  754. struct adb_dev *dev = func_to_adb(f);
  755. struct usb_composite_dev *cdev = dev->cdev;
  756. DBG(cdev, "%s %s %d: cdev %p\n", __FILE__, __func__, __LINE__, cdev);
  757. dev->online = 0;
  758. dev->error = 1;
  759. usb_ep_disable(dev->ep_in);
  760. usb_ep_disable(dev->ep_out);
  761. /* readers may be blocked waiting for us to go online */
  762. wake_up(&dev->read_wq);
  763. VDBG(cdev, "%s %s %d: %s disabled\n", __FILE__, __func__, __LINE__, dev->function.name);
  764. }
  765. static int adb_bind_config(struct usb_configuration *c)
  766. {
  767. struct adb_dev *dev = _adb_dev;
  768. printk(KERN_INFO "%s %s %d\n", __FILE__, __func__, __LINE__);
  769. dev->cdev = c->cdev;
  770. dev->function.name = "adb";
  771. dev->function.fs_descriptors = fs_adb_descs;
  772. dev->function.hs_descriptors = hs_adb_descs;
  773. if (gadget_is_superspeed(c->cdev->gadget)) {
  774. pr_debug("[ADB] SS ADB DESCS!!\n");
  775. dev->function.ss_descriptors = ss_adb_descs;
  776. }
  777. dev->function.bind = adb_function_bind;
  778. dev->function.unbind = adb_function_unbind;
  779. dev->function.set_alt = adb_function_set_alt;
  780. dev->function.disable = adb_function_disable;
  781. return usb_add_function(c, &dev->function);
  782. }
  783. static int adb_setup(void)
  784. {
  785. struct adb_dev *dev;
  786. int ret;
  787. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  788. if (!dev)
  789. return -ENOMEM;
  790. spin_lock_init(&dev->lock);
  791. spin_lock_init(&open_lock);
  792. init_waitqueue_head(&dev->read_wq);
  793. init_waitqueue_head(&dev->write_wq);
  794. atomic_set(&dev->open_excl, 0);
  795. atomic_set(&dev->read_excl, 0);
  796. atomic_set(&dev->write_excl, 0);
  797. INIT_LIST_HEAD(&dev->tx_idle);
  798. _adb_dev = dev;
  799. ret = misc_register(&adb_device);
  800. if (ret)
  801. goto err;
  802. return 0;
  803. err:
  804. kfree(dev);
  805. printk(KERN_ERR "%s %s %d: adb gadget driver failed to initialize\n", __FILE__, __func__,
  806. __LINE__);
  807. return ret;
  808. }
  809. static void adb_cleanup(void)
  810. {
  811. misc_deregister(&adb_device);
  812. kfree(_adb_dev);
  813. _adb_dev = NULL;
  814. }