f_adb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/poll.h>
  20. #include <linux/delay.h>
  21. #include <linux/wait.h>
  22. #include <linux/err.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/sched.h>
  25. #include <linux/types.h>
  26. #include <linux/device.h>
  27. #include <linux/miscdevice.h>
  28. #include <net/sock.h>
  29. #include <linux/netlink.h>
  30. #include <linux/usb/composite.h>
  31. #include <linux/usb/functionfs.h>
  32. #include "u_f.h"
  33. #define ADB_BULK_BUFFER_SIZE 16384
  34. /* number of tx requests to allocate */
  35. #define TX_REQ_MAX 4
  36. #define DRIVER_NAME "adb"
  37. static const char adb_shortname[] = "android_adb";
  38. struct adb_dev {
  39. struct usb_function function;
  40. struct usb_composite_dev *cdev;
  41. spinlock_t lock;
  42. struct usb_ep *ep_in;
  43. struct usb_ep *ep_out;
  44. int online;
  45. int error;
  46. atomic_t read_excl;
  47. atomic_t write_excl;
  48. atomic_t open_excl;
  49. struct list_head tx_idle;
  50. struct list_head rx_idle;
  51. struct list_head rx_used;
  52. wait_queue_head_t read_wq;
  53. wait_queue_head_t write_wq;
  54. struct usb_request *rx_req;
  55. int rx_done;
  56. int cur_read_pos;
  57. };
  58. static struct usb_interface_descriptor adb_interface_desc = {
  59. .bLength = USB_DT_INTERFACE_SIZE,
  60. .bDescriptorType = USB_DT_INTERFACE,
  61. .bInterfaceNumber = 0,
  62. .bNumEndpoints = 2,
  63. .bInterfaceClass = 0xFF,
  64. .bInterfaceSubClass = 0x42,
  65. .bInterfaceProtocol = 1,
  66. };
  67. static struct usb_endpoint_descriptor adb_highspeed_in_desc = {
  68. .bLength = USB_DT_ENDPOINT_SIZE,
  69. .bDescriptorType = USB_DT_ENDPOINT,
  70. .bEndpointAddress = USB_DIR_IN,
  71. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  72. .wMaxPacketSize = __constant_cpu_to_le16(512),
  73. };
  74. static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
  75. .bLength = USB_DT_ENDPOINT_SIZE,
  76. .bDescriptorType = USB_DT_ENDPOINT,
  77. .bEndpointAddress = USB_DIR_OUT,
  78. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  79. .wMaxPacketSize = __constant_cpu_to_le16(512),
  80. };
  81. static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
  82. .bLength = USB_DT_ENDPOINT_SIZE,
  83. .bDescriptorType = USB_DT_ENDPOINT,
  84. .bEndpointAddress = USB_DIR_IN,
  85. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  86. };
  87. static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {
  88. .bLength = USB_DT_ENDPOINT_SIZE,
  89. .bDescriptorType = USB_DT_ENDPOINT,
  90. .bEndpointAddress = USB_DIR_OUT,
  91. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  92. };
  93. #if 0
  94. static struct usb_descriptor_header *fs_adb_descs[] = {
  95. (struct usb_descriptor_header *) &adb_interface_desc,
  96. (struct usb_descriptor_header *) &adb_fullspeed_in_desc,
  97. (struct usb_descriptor_header *) &adb_fullspeed_out_desc,
  98. NULL,
  99. };
  100. #endif
  101. static struct usb_descriptor_header *hs_adb_descs[] = {
  102. (struct usb_descriptor_header *) &adb_interface_desc,
  103. (struct usb_descriptor_header *) &adb_highspeed_in_desc,
  104. (struct usb_descriptor_header *) &adb_highspeed_out_desc,
  105. NULL,
  106. };
  107. #define ADB_INTERFACE_STRING_INDEX 0
  108. static struct usb_string adb_string_defs[] = {
  109. [ADB_INTERFACE_STRING_INDEX].s = "ADB Interface",
  110. { }, /* end of list */
  111. };
  112. static struct usb_gadget_strings adb_string_table = {
  113. .language = 0x0409, /* en-US */
  114. .strings = adb_string_defs,
  115. };
  116. static struct usb_gadget_strings *adb_strings[] = {
  117. &adb_string_table,
  118. NULL,
  119. };
  120. //static void adb_ready_callback(void);
  121. //static void adb_closed_callback(void);
  122. /* temporary variable used between adb_open() and adb_gadget_bind() */
  123. static struct adb_dev *_adb_dev;
  124. static inline struct adb_dev *func_to_adb(struct usb_function *f)
  125. {
  126. return container_of(f, struct adb_dev, function);
  127. }
  128. static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
  129. {
  130. struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
  131. if (!req)
  132. return NULL;
  133. /* now allocate buffers for the requests */
  134. req->buf = kmalloc(buffer_size, GFP_KERNEL);
  135. if (!req->buf) {
  136. usb_ep_free_request(ep, req);
  137. return NULL;
  138. }
  139. return req;
  140. }
  141. static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
  142. {
  143. if (req) {
  144. kfree(req->buf);
  145. usb_ep_free_request(ep, req);
  146. }
  147. }
  148. static inline int adb_lock(atomic_t *excl)
  149. {
  150. if (atomic_inc_return(excl) == 1) {
  151. return 0;
  152. } else {
  153. atomic_dec(excl);
  154. return -1;
  155. }
  156. }
  157. static inline void adb_unlock(atomic_t *excl)
  158. {
  159. atomic_dec(excl);
  160. }
  161. /* add a request to the tail of a list */
  162. void adb_req_put(struct adb_dev *dev, struct list_head *head,
  163. struct usb_request *req)
  164. {
  165. unsigned long flags;
  166. spin_lock_irqsave(&dev->lock, flags);
  167. list_add_tail(&req->list, head);
  168. spin_unlock_irqrestore(&dev->lock, flags);
  169. }
  170. /* remove a request from the head of a list */
  171. struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
  172. {
  173. unsigned long flags;
  174. struct usb_request *req;
  175. spin_lock_irqsave(&dev->lock, flags);
  176. if (list_empty(head)) {
  177. req = 0;
  178. } else {
  179. req = list_first_entry(head, struct usb_request, list);
  180. list_del(&req->list);
  181. }
  182. spin_unlock_irqrestore(&dev->lock, flags);
  183. return req;
  184. }
  185. static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
  186. {
  187. struct adb_dev *dev = _adb_dev;
  188. if (req->status != 0)
  189. dev->error = 1;
  190. adb_req_put(dev, &dev->tx_idle, req);
  191. wake_up(&dev->write_wq);
  192. }
  193. static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
  194. {
  195. struct adb_dev *dev = _adb_dev;
  196. dev->rx_done = 1;
  197. #if 0
  198. if (req->status != 0 && req->status != -ECONNRESET)
  199. dev->error = 1;
  200. #else
  201. if (req) {
  202. if (req->status != 0){//printk("+++%s:%d+++req->status=%d\n", __func__, __LINE__, req->status);
  203. adb_req_put(dev, &dev->rx_idle, req);
  204. dev->error = 1;
  205. } else {
  206. if (req->actual > 0)
  207. adb_req_put(dev, &dev->rx_used, req);
  208. else {//printk("+++%s:%d+++ zero len \n", __func__, __LINE__);
  209. adb_req_put(dev, &dev->rx_idle, req);
  210. }
  211. }
  212. }
  213. #endif
  214. wake_up(&dev->read_wq);
  215. }
  216. static int adb_create_bulk_endpoints(struct adb_dev *dev,
  217. struct usb_endpoint_descriptor *in_desc,
  218. struct usb_endpoint_descriptor *out_desc)
  219. {
  220. struct usb_composite_dev *cdev = dev->cdev;
  221. struct usb_request *req;
  222. struct usb_ep *ep;
  223. int i;
  224. DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
  225. ep = usb_ep_autoconfig(cdev->gadget, in_desc);
  226. if (!ep) {
  227. DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
  228. return -ENODEV;
  229. }
  230. DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
  231. ep->driver_data = dev; /* claim the endpoint */
  232. dev->ep_in = ep;
  233. ep = usb_ep_autoconfig(cdev->gadget, out_desc);
  234. if (!ep) {
  235. DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
  236. return -ENODEV;
  237. }
  238. DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
  239. ep->driver_data = dev; /* claim the endpoint */
  240. dev->ep_out = ep;
  241. #if 0
  242. /* now allocate requests for our endpoints */
  243. req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
  244. if (!req)
  245. goto fail;
  246. req->complete = adb_complete_out;
  247. dev->rx_req = req;
  248. #else
  249. for (i = 0; i < TX_REQ_MAX; i++) {
  250. req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
  251. if (!req)
  252. goto fail;
  253. req->complete = adb_complete_out;
  254. adb_req_put(dev, &dev->rx_idle, req);
  255. }
  256. #endif
  257. for (i = 0; i < TX_REQ_MAX; i++) {
  258. req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
  259. if (!req)
  260. goto fail;
  261. req->complete = adb_complete_in;
  262. adb_req_put(dev, &dev->tx_idle, req);
  263. }
  264. return 0;
  265. fail:
  266. printk(KERN_ERR "adb_bind() could not allocate requests\n");
  267. return -1;
  268. }
  269. static int adb_rx_submit(struct adb_dev *dev, struct usb_request *req)
  270. {
  271. int ret, r;
  272. dev->rx_done = 0;
  273. ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
  274. if (ret < 0) {
  275. pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
  276. r = -EIO;
  277. dev->error = 1;
  278. goto done;
  279. } else {
  280. pr_debug("rx %p queue\n", req);
  281. }
  282. done:
  283. pr_debug("adb_read returning %d\n", r);
  284. return r;
  285. }
  286. #if 1
  287. static ssize_t adb_read(struct file *fp, char __user *buf,
  288. size_t count, loff_t *pos)
  289. {
  290. struct adb_dev *dev = fp->private_data;
  291. struct usb_request *req = NULL;
  292. int r = 0, xfer, cur_actual = 0;
  293. int ret;
  294. //static int cur_pos = 0;
  295. unsigned long flags;
  296. pr_debug("adb_read(%d) \n", count);
  297. if (!dev)
  298. return -ENODEV;
  299. if (count <= 0)
  300. return 0;
  301. if (adb_lock(&dev->read_excl))
  302. return -EBUSY;
  303. while (!(dev->online || dev->error)) {
  304. pr_debug("adb_read: waiting for online state\n");
  305. ret = wait_event_interruptible(dev->read_wq,
  306. (dev->online || dev->error));
  307. if (ret < 0) {
  308. adb_unlock(&dev->read_excl);
  309. return ret;
  310. }
  311. }
  312. if (dev->error) {
  313. r = -EIO;
  314. goto done;
  315. }
  316. req = NULL;
  317. ret = wait_event_interruptible(dev->read_wq, (req = adb_req_get(dev, &dev->rx_used)) != NULL);
  318. if (ret < 0) {
  319. r = ret;
  320. if (req)
  321. usb_ep_dequeue(dev->ep_out, req);
  322. goto done;
  323. }
  324. if (!dev->error && req) {
  325. if (req->actual > dev->cur_read_pos) {
  326. cur_actual = req->actual - dev->cur_read_pos;
  327. xfer = (cur_actual <= count) ? cur_actual : count;
  328. r = xfer;
  329. if (copy_to_user(buf, (req->buf + dev->cur_read_pos), xfer))
  330. r = -EFAULT;
  331. }
  332. if (count >= cur_actual) {
  333. adb_rx_submit(dev, req);//the req is read completely, return to the usb core
  334. dev->cur_read_pos = 0;
  335. while ((req = adb_req_get(dev, &dev->rx_idle))) {
  336. req->length = ADB_BULK_BUFFER_SIZE;
  337. adb_rx_submit(dev, req);
  338. }
  339. } else {
  340. dev->cur_read_pos += count;
  341. spin_lock_irqsave(&dev->lock, flags);//the request buffer isn't completely read ,return the req to the head of the list of rx_used
  342. list_add(&req->list, &dev->rx_used);
  343. spin_unlock_irqrestore(&dev->lock, flags);
  344. }
  345. } else {// IO error
  346. if (dev->error)
  347. r = -EIO;
  348. if (dev->online == 0)
  349. r = -ENODEV;
  350. if (req) {
  351. spin_lock_irqsave(&dev->lock, flags);
  352. list_add(&req->list, &dev->rx_used);
  353. spin_unlock_irqrestore(&dev->lock, flags);
  354. }
  355. }
  356. done:
  357. adb_unlock(&dev->read_excl);
  358. pr_debug("adb_read returning %d\n", r);
  359. return r;
  360. }
  361. #else
  362. static ssize_t adb_read(struct file *fp, char __user *buf,
  363. size_t count, loff_t *pos)
  364. {
  365. struct adb_dev *dev = fp->private_data;
  366. struct usb_request *req;
  367. int r = count, xfer;
  368. int ret;
  369. printk("adb_read(%d)\n", count);
  370. if (!_adb_dev)
  371. return -ENODEV;
  372. if (count > ADB_BULK_BUFFER_SIZE)
  373. return -EINVAL;
  374. if (adb_lock(&dev->read_excl))
  375. return -EBUSY;
  376. /* we will block until we're online */
  377. while (!(dev->online || dev->error)) {
  378. printk("adb_read: waiting for online state\n");
  379. ret = wait_event_interruptible(dev->read_wq,
  380. (dev->online || dev->error));
  381. if (ret < 0) {
  382. adb_unlock(&dev->read_excl);
  383. return ret;
  384. }
  385. }
  386. if (dev->error) {
  387. r = -EIO;
  388. goto done;
  389. }
  390. requeue_req:
  391. /* queue a request */
  392. req = dev->rx_req;
  393. req->length = count;
  394. dev->rx_done = 0;
  395. ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
  396. if (ret < 0) {
  397. printk("adb_read: failed to queue req %p (%d)\n", req, ret);
  398. r = -EIO;
  399. dev->error = 1;
  400. goto done;
  401. } else {
  402. printk("rx %p queue\n", req);
  403. }
  404. /* wait for a request to complete */
  405. ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
  406. if (ret < 0) {
  407. if (ret != -ERESTARTSYS)
  408. dev->error = 1;
  409. r = ret;
  410. usb_ep_dequeue(dev->ep_out, req);
  411. goto done;
  412. }
  413. if (!dev->error) {
  414. /* If we got a 0-len packet, throw it back and try again. */
  415. if (req->actual == 0)
  416. goto requeue_req;
  417. printk("rx %p %d\n", req, req->actual);
  418. xfer = (req->actual < count) ? req->actual : count;
  419. if (copy_to_user(buf, req->buf, xfer))
  420. r = -EFAULT;
  421. } else
  422. r = -EIO;
  423. done:
  424. adb_unlock(&dev->read_excl);
  425. printk("adb_read returning %d\n", r);
  426. return r;
  427. }
  428. #endif
  429. static ssize_t adb_write(struct file *fp, const char __user *buf,
  430. size_t count, loff_t *pos)
  431. {
  432. struct adb_dev *dev = fp->private_data;
  433. struct usb_request *req = 0;
  434. int r = count, xfer;
  435. int ret;
  436. if (!_adb_dev)
  437. return -ENODEV;
  438. pr_debug("adb_write(%d)\n", count);
  439. if (adb_lock(&dev->write_excl))
  440. return -EBUSY;
  441. while (count > 0) {
  442. if (dev->error) {
  443. pr_debug("adb_write dev->error\n");
  444. r = -EIO;
  445. break;
  446. }
  447. /* get an idle tx request to use */
  448. req = 0;
  449. ret = wait_event_interruptible(dev->write_wq,
  450. (req = adb_req_get(dev, &dev->tx_idle)) || dev->error);
  451. if (ret < 0) {
  452. r = ret;
  453. break;
  454. }
  455. if (req != 0) {
  456. if (count > ADB_BULK_BUFFER_SIZE)
  457. xfer = ADB_BULK_BUFFER_SIZE;
  458. else
  459. xfer = count;
  460. if (copy_from_user(req->buf, buf, xfer)) {
  461. r = -EFAULT;
  462. break;
  463. }
  464. req->length = xfer;
  465. ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
  466. if (ret < 0) {
  467. pr_debug("adb_write: xfer error %d\n", ret);
  468. dev->error = 1;
  469. r = -EIO;
  470. break;
  471. }
  472. buf += xfer;
  473. count -= xfer;
  474. /* zero this so we don't try to free it on error exit */
  475. req = 0;
  476. }
  477. }
  478. if (req)
  479. adb_req_put(dev, &dev->tx_idle, req);
  480. adb_unlock(&dev->write_excl);
  481. pr_debug("adb_write returning %d\n", r);
  482. return r;
  483. }
  484. static int adb_open(struct inode *ip, struct file *fp)
  485. {
  486. pr_info("adb_open\n");
  487. if (!_adb_dev)
  488. return -ENODEV;
  489. if (adb_lock(&_adb_dev->open_excl))
  490. return -EBUSY;
  491. fp->private_data = _adb_dev;
  492. /* clear the error latch */
  493. _adb_dev->error = 0;
  494. //adb_ready_callback();
  495. return 0;
  496. }
  497. static int adb_release(struct inode *ip, struct file *fp)
  498. {
  499. pr_info("adb_release\n");
  500. //adb_closed_callback();
  501. adb_unlock(&_adb_dev->open_excl);
  502. return 0;
  503. }
  504. /* file operations for ADB device /dev/android_adb */
  505. static const struct file_operations adb_fops = {
  506. .owner = THIS_MODULE,
  507. .read = adb_read,
  508. .write = adb_write,
  509. .open = adb_open,
  510. .release = adb_release,
  511. };
  512. static struct miscdevice adb_device = {
  513. .minor = MISC_DYNAMIC_MINOR,
  514. .name = adb_shortname,
  515. .fops = &adb_fops,
  516. };
  517. static int
  518. adb_function_bind(struct usb_configuration *c, struct usb_function *f)
  519. {
  520. struct usb_composite_dev *cdev = c->cdev;
  521. struct adb_dev *dev = func_to_adb(f);
  522. int id;
  523. int ret;
  524. dev->cdev = cdev;
  525. DBG(cdev, "adb_function_bind dev: %p\n", dev);
  526. /* allocate interface ID(s) */
  527. id = usb_interface_id(c, f);
  528. if (id < 0)
  529. return id;
  530. adb_interface_desc.bInterfaceNumber = id;
  531. /* allocate endpoints */
  532. ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
  533. &adb_fullspeed_out_desc);
  534. if (ret)
  535. return ret;
  536. /* support high speed hardware */
  537. if (gadget_is_dualspeed(c->cdev->gadget)) {
  538. adb_highspeed_in_desc.bEndpointAddress =
  539. adb_fullspeed_in_desc.bEndpointAddress;
  540. adb_highspeed_out_desc.bEndpointAddress =
  541. adb_fullspeed_out_desc.bEndpointAddress;
  542. dev->function.hs_descriptors = usb_copy_descriptors(hs_adb_descs);
  543. }
  544. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  545. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  546. f->name, dev->ep_in->name, dev->ep_out->name);
  547. return 0;
  548. }
  549. static void
  550. adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
  551. {
  552. struct adb_dev *dev = func_to_adb(f);
  553. struct usb_request *req;
  554. dev->online = 0;
  555. dev->error = 1;
  556. wake_up(&dev->read_wq);
  557. #if 0
  558. adb_request_free(dev->rx_req, dev->ep_out);
  559. #else
  560. while ((req = adb_req_get(dev, &dev->rx_idle)))
  561. adb_request_free(req, dev->ep_out);
  562. req = NULL;
  563. while ((req = adb_req_get(dev, &dev->rx_used)))
  564. adb_request_free(req, dev->ep_out);
  565. #endif
  566. req = NULL;
  567. while ((req = adb_req_get(dev, &dev->tx_idle)))
  568. adb_request_free(req, dev->ep_in);
  569. }
  570. static int adb_function_set_alt(struct usb_function *f,
  571. unsigned intf, unsigned alt)
  572. {
  573. struct adb_dev *dev = func_to_adb(f);
  574. struct usb_composite_dev *cdev = f->config->cdev;
  575. int ret;
  576. struct usb_request *req = NULL;
  577. DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);printk("adb_function_set_alt intf: %d alt: %d\n", intf, alt);
  578. ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
  579. if (ret)
  580. return ret;
  581. ret = usb_ep_enable(dev->ep_in);
  582. if (ret)
  583. return ret;
  584. ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
  585. if (ret)
  586. return ret;
  587. ret = usb_ep_enable(dev->ep_out);
  588. if (ret) {
  589. usb_ep_disable(dev->ep_in);
  590. return ret;
  591. }
  592. dev->online = 1;
  593. while ((req = adb_req_get(dev, &dev->rx_used))) {
  594. adb_req_put(dev, &dev->rx_idle, req);
  595. }
  596. while ((req = adb_req_get(dev, &dev->rx_idle))) {
  597. req->length = ADB_BULK_BUFFER_SIZE;
  598. adb_rx_submit(dev, req);
  599. }
  600. /* readers may be blocked waiting for us to go online */
  601. wake_up(&dev->read_wq);
  602. return 0;
  603. }
  604. static void adb_function_suspend(struct usb_function *f)
  605. {
  606. struct adb_dev *dev = func_to_adb(f);
  607. struct usb_composite_dev *cdev = dev->cdev;
  608. printk("adb_function_suspend cdev %p\n", cdev);
  609. dev->online = 0;
  610. dev->error = 1;
  611. usb_ep_disable(dev->ep_in);
  612. usb_ep_disable(dev->ep_out);
  613. /* readers may be blocked waiting for us to go online */
  614. wake_up(&dev->read_wq);
  615. printk("%s suspend\n", dev->function.name);
  616. }
  617. static void adb_function_disable(struct usb_function *f)
  618. {
  619. struct adb_dev *dev = func_to_adb(f);
  620. struct usb_composite_dev *cdev = dev->cdev;
  621. DBG(cdev, "adb_function_disable cdev %p\n", cdev);printk("adb_function_disable cdev %p\n", cdev);
  622. dev->online = 0;
  623. dev->error = 1;
  624. usb_ep_disable(dev->ep_in);
  625. usb_ep_disable(dev->ep_out);
  626. /* readers may be blocked waiting for us to go online */
  627. wake_up(&dev->read_wq);
  628. VDBG(cdev, "%s disabled\n", dev->function.name);printk("%s disabled\n", dev->function.name);
  629. }
  630. #if 0
  631. static int adb_bind_config(struct usb_configuration *c)
  632. {
  633. struct adb_dev *dev = _adb_dev;
  634. printk(KERN_INFO "adb_bind_config\n");
  635. printk("%s:%d\n", __func__, __LINE__);
  636. dev->cdev = c->cdev;
  637. dev->function.name = "adb";
  638. dev->function.strings = adb_strings;
  639. dev->function.descriptors = fs_adb_descs;
  640. dev->function.hs_descriptors = hs_adb_descs;
  641. dev->function.bind = adb_function_bind;
  642. dev->function.unbind = adb_function_unbind;
  643. dev->function.set_alt = adb_function_set_alt;
  644. dev->function.disable = adb_function_disable;
  645. printk("%s:%d\n", __func__, __LINE__);
  646. return usb_add_function(c, &dev->function);
  647. }
  648. #endif
  649. static int adb_setup(struct adb_dev *dev)
  650. {
  651. int ret;
  652. spin_lock_init(&dev->lock);
  653. init_waitqueue_head(&dev->read_wq);
  654. init_waitqueue_head(&dev->write_wq);
  655. atomic_set(&dev->open_excl, 0);
  656. atomic_set(&dev->read_excl, 0);
  657. atomic_set(&dev->write_excl, 0);
  658. INIT_LIST_HEAD(&dev->tx_idle);
  659. INIT_LIST_HEAD(&dev->rx_idle);
  660. INIT_LIST_HEAD(&dev->rx_used);
  661. ret = misc_register(&adb_device);
  662. if (ret)
  663. goto err;
  664. _adb_dev = dev;
  665. return 0;
  666. err:
  667. kfree(dev);
  668. printk(KERN_ERR "adb gadget driver failed to initialize\n");
  669. return ret;
  670. }
  671. static void adb_cleanup(struct adb_dev *dev)
  672. {
  673. misc_deregister(&adb_device);
  674. if (_adb_dev)
  675. kfree(_adb_dev);
  676. _adb_dev = NULL;
  677. }
  678. static void adb_free(struct usb_function *f)
  679. {
  680. (void)f;
  681. }
  682. struct f_adb_opts {
  683. struct usb_function_instance func_inst;
  684. struct adb_dev *dev;
  685. char *name;
  686. int refcnt;
  687. struct mutex lock;
  688. };
  689. static struct f_adb_opts *to_adb_opts(struct config_item *item)
  690. {
  691. return container_of(to_config_group(item), struct f_adb_opts,
  692. func_inst.group);
  693. }
  694. static void adb_attr_release(struct config_item *item)
  695. {
  696. struct f_adb_opts *opts = to_adb_opts(item);
  697. usb_put_function_instance(&opts->func_inst);
  698. }
  699. static struct configfs_item_operations adb_item_ops = {
  700. .release = adb_attr_release,
  701. };
  702. static struct config_item_type adb_func_type = {
  703. .ct_item_ops = &adb_item_ops,
  704. .ct_owner = THIS_MODULE,
  705. };
  706. static void adb_free_inst(struct usb_function_instance *fi)
  707. {
  708. struct f_adb_opts *opts;
  709. opts = container_of(fi, struct f_adb_opts, func_inst);
  710. if (NULL != opts->name)
  711. kfree(opts->name);
  712. adb_cleanup(opts->dev);
  713. kfree(opts);
  714. }
  715. static struct usb_function_instance *adb_alloc_inst(void)
  716. {
  717. struct f_adb_opts *opts = NULL;
  718. opts = kzalloc(sizeof(struct f_adb_opts), GFP_KERNEL);
  719. if (!opts)
  720. return ERR_PTR(-ENOMEM);
  721. //fi_iap->func_inst.set_inst_name = iap_set_inst_name;
  722. opts->func_inst.free_func_inst = adb_free_inst;
  723. mutex_init(&opts->lock);
  724. config_group_init_type_name(&opts->func_inst.group,
  725. "", &adb_func_type);
  726. return &opts->func_inst;
  727. }
  728. struct usb_function *adb_alloc(struct usb_function_instance *fi)
  729. {
  730. struct f_adb_opts *opts = container_of(fi, struct f_adb_opts, func_inst);
  731. struct adb_dev *dev;
  732. int ret = -1;
  733. dev = kzalloc(sizeof *dev, GFP_KERNEL);
  734. if (!dev)
  735. return ERR_PTR(-ENOMEM);
  736. ret = adb_setup(dev);
  737. if (ret) {
  738. pr_err("Error setting adb\n");
  739. return ERR_PTR(ret);
  740. }
  741. opts = container_of(fi, struct f_adb_opts, func_inst);
  742. mutex_lock(&opts->lock);
  743. opts->dev = dev;
  744. opts->refcnt++;
  745. mutex_unlock(&opts->lock);
  746. dev = opts->dev;
  747. dev->function.name = DRIVER_NAME;
  748. dev->function.strings = adb_strings;
  749. dev->function.bind = adb_function_bind;
  750. dev->function.unbind = adb_function_unbind;
  751. dev->function.set_alt = adb_function_set_alt;
  752. dev->function.disable = adb_function_disable;
  753. dev->function.suspend = adb_function_suspend;
  754. dev->function.free_func = adb_free;
  755. return &dev->function;
  756. }
  757. DECLARE_USB_FUNCTION_INIT(adb, adb_alloc_inst, adb_alloc);
  758. MODULE_LICENSE("GPL");