f_adb.c 17 KB

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