f_iap.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. */
  3. #include <linux/module.h>
  4. #include <linux/init.h>
  5. #include <linux/poll.h>
  6. #include <linux/delay.h>
  7. #include <linux/wait.h>
  8. #include <linux/err.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/sched.h>
  11. #include <linux/types.h>
  12. #include <linux/device.h>
  13. #include <linux/miscdevice.h>
  14. #include <net/sock.h>
  15. #include <linux/netlink.h>
  16. #include <linux/usb/composite.h>
  17. #include <linux/usb/functionfs.h>
  18. #include "u_f.h"
  19. #define iAP2_BULK_BUFFER_SIZE 16384
  20. #define TX_REQ_MAX 4
  21. #define NETLINK_USB_DEV_READY 25
  22. #define DRIVER_NAME "iap"
  23. #define MAX_INST_NAME_LEN 40
  24. static const char iap2_shortname[] = "iap";
  25. struct iap2_dev {
  26. struct usb_function function;
  27. struct usb_composite_dev *cdev;
  28. spinlock_t lock;
  29. struct usb_ep *ep_in;
  30. struct usb_ep *ep_out;
  31. int online;
  32. int error;
  33. atomic_t read_excl;
  34. atomic_t write_excl;
  35. atomic_t open_excl;
  36. struct list_head tx_idle;
  37. struct list_head rx_idle;
  38. struct list_head rx_used;
  39. wait_queue_head_t read_wq;
  40. wait_queue_head_t write_wq;
  41. int rx_done;
  42. int cur_read_pos;
  43. struct sock *netlink_handle;
  44. };
  45. static struct usb_interface_descriptor iap2_interface_desc = {
  46. .bLength = USB_DT_INTERFACE_SIZE,
  47. .bDescriptorType = USB_DT_INTERFACE,
  48. .bInterfaceNumber = 0,
  49. .bNumEndpoints = 2,
  50. .bInterfaceClass = 0xFF,
  51. .bInterfaceSubClass = 0xF0,
  52. .bInterfaceProtocol = 0,
  53. };
  54. static struct usb_endpoint_descriptor iap2_superspeed_in_desc = {
  55. .bLength = USB_DT_ENDPOINT_SIZE,
  56. .bDescriptorType = USB_DT_ENDPOINT,
  57. .bEndpointAddress = USB_DIR_IN,
  58. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  59. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  60. };
  61. static struct usb_endpoint_descriptor iap2_superspeed_out_desc = {
  62. .bLength = USB_DT_ENDPOINT_SIZE,
  63. .bDescriptorType = USB_DT_ENDPOINT,
  64. .bEndpointAddress = USB_DIR_OUT,
  65. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  66. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  67. };
  68. static struct usb_ss_ep_comp_descriptor iap2_superspeed_bulk_comp_desc = {
  69. .bLength = sizeof iap2_superspeed_bulk_comp_desc,
  70. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  71. /* the following 2 values can be tweaked if necessary */
  72. /* .bMaxBurst = 0, */
  73. /* .bmAttributes = 0, */
  74. };
  75. static struct usb_endpoint_descriptor iap2_highspeed_in_desc = {
  76. .bLength = USB_DT_ENDPOINT_SIZE,
  77. .bDescriptorType = USB_DT_ENDPOINT,
  78. .bEndpointAddress = USB_DIR_IN,
  79. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  80. .wMaxPacketSize = __constant_cpu_to_le16(512),
  81. };
  82. static struct usb_endpoint_descriptor iap2_highspeed_out_desc = {
  83. .bLength = USB_DT_ENDPOINT_SIZE,
  84. .bDescriptorType = USB_DT_ENDPOINT,
  85. .bEndpointAddress = USB_DIR_OUT,
  86. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  87. .wMaxPacketSize = __constant_cpu_to_le16(512),
  88. };
  89. static struct usb_endpoint_descriptor iap2_fullspeed_in_desc = {
  90. .bLength = USB_DT_ENDPOINT_SIZE,
  91. .bDescriptorType = USB_DT_ENDPOINT,
  92. .bEndpointAddress = USB_DIR_IN,
  93. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  94. };
  95. static struct usb_endpoint_descriptor iap2_fullspeed_out_desc = {
  96. .bLength = USB_DT_ENDPOINT_SIZE,
  97. .bDescriptorType = USB_DT_ENDPOINT,
  98. .bEndpointAddress = USB_DIR_OUT,
  99. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  100. };
  101. static struct usb_descriptor_header *fs_iap2_descs[] = {
  102. (struct usb_descriptor_header *) &iap2_interface_desc,
  103. (struct usb_descriptor_header *) &iap2_fullspeed_in_desc,
  104. (struct usb_descriptor_header *) &iap2_fullspeed_out_desc,
  105. NULL,
  106. };
  107. static struct usb_descriptor_header *hs_iap2_descs[] = {
  108. (struct usb_descriptor_header *) &iap2_interface_desc,
  109. (struct usb_descriptor_header *) &iap2_highspeed_in_desc,
  110. (struct usb_descriptor_header *) &iap2_highspeed_out_desc,
  111. NULL,
  112. };
  113. static struct usb_descriptor_header *ss_iap2_descs[] = {
  114. (struct usb_descriptor_header *) &iap2_interface_desc,
  115. (struct usb_descriptor_header *) &iap2_superspeed_in_desc,
  116. (struct usb_descriptor_header *) &iap2_superspeed_bulk_comp_desc,
  117. (struct usb_descriptor_header *) &iap2_superspeed_out_desc,
  118. (struct usb_descriptor_header *) &iap2_superspeed_bulk_comp_desc,
  119. NULL,
  120. };
  121. #define STRING_CTRL_IDX 0
  122. static struct usb_string iap_string_defs[] = {
  123. [STRING_CTRL_IDX].s = "iAP Interface",
  124. { } /* end of list */
  125. };
  126. static struct usb_gadget_strings iap_string_table = {
  127. .language = 0x0409, /* en-us */
  128. .strings = iap_string_defs,
  129. };
  130. static struct usb_gadget_strings *iap_strings[] = {
  131. &iap_string_table,
  132. NULL,
  133. };
  134. static struct iap2_dev *_iap2_dev;
  135. static int usb_dev_ready_notify(struct iap2_dev *dev, int is_ready)
  136. {
  137. struct sk_buff *nl_skb;
  138. struct nlmsghdr *nlh;
  139. char *buf_on = "connected";
  140. char *buf_off = "disconnected";
  141. char *buf = NULL;
  142. int len = 0;
  143. int ret;
  144. if (!dev || !dev->netlink_handle)
  145. return -1;
  146. if (is_ready) {
  147. buf = buf_on;printk("%s:%d %s\n", __func__, __LINE__, buf_on);
  148. } else {
  149. buf = buf_off;printk("%s:%d %s\n", __func__, __LINE__, buf_off);
  150. }
  151. len = strlen(buf) + 1;
  152. nl_skb = nlmsg_new(len, GFP_ATOMIC);
  153. if(!nl_skb) {
  154. printk("netlink_alloc_skb error\n");
  155. return -1;
  156. }
  157. nlh = nlmsg_put(nl_skb, 0, 0, NETLINK_USB_DEV_READY, len, 0);
  158. if(nlh == NULL) {
  159. printk("nlmsg_put() error\n");
  160. nlmsg_free(nl_skb);
  161. return -1;
  162. }
  163. memcpy(nlmsg_data(nlh), buf, len);
  164. ret = netlink_unicast(dev->netlink_handle, nl_skb, 52, MSG_DONTWAIT);
  165. return ret;
  166. }
  167. static inline struct iap2_dev *func_to_iap2(struct usb_function *f)
  168. {
  169. return container_of(f, struct iap2_dev, function);
  170. }
  171. static struct usb_request *iap2_request_new(struct usb_ep *ep, int buffer_size)
  172. {
  173. struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
  174. if (!req)
  175. return NULL;
  176. req->buf = kmalloc(buffer_size, GFP_KERNEL);
  177. if (!req->buf) {
  178. usb_ep_free_request(ep, req);
  179. return NULL;
  180. }
  181. return req;
  182. }
  183. static void iap2_request_free(struct usb_request *req, struct usb_ep *ep)
  184. {
  185. if (req) {
  186. kfree(req->buf);
  187. usb_ep_free_request(ep, req);
  188. }
  189. }
  190. static inline int iap2_lock(atomic_t *excl)
  191. {
  192. if (atomic_inc_return(excl) == 1) {
  193. return 0;
  194. } else {
  195. atomic_dec(excl);
  196. return -1;
  197. }
  198. }
  199. static inline void iap2_unlock(atomic_t *excl)
  200. {
  201. atomic_dec(excl);
  202. }
  203. void iap2_req_put(struct iap2_dev *dev, struct list_head *head,
  204. struct usb_request *req)
  205. {
  206. unsigned long flags;
  207. spin_lock_irqsave(&dev->lock, flags);
  208. list_add_tail(&req->list, head);
  209. spin_unlock_irqrestore(&dev->lock, flags);
  210. }
  211. /* remove a request from the head of a list */
  212. struct usb_request *iap2_req_get(struct iap2_dev *dev, struct list_head *head)
  213. {
  214. unsigned long flags;
  215. struct usb_request *req;
  216. spin_lock_irqsave(&dev->lock, flags);
  217. if (list_empty(head)) {
  218. req = 0;
  219. } else {
  220. req = list_first_entry(head, struct usb_request, list);
  221. list_del(&req->list);
  222. }
  223. spin_unlock_irqrestore(&dev->lock, flags);
  224. return req;
  225. }
  226. static void iap2_complete_in(struct usb_ep *ep, struct usb_request *req)
  227. {
  228. struct iap2_dev *dev = (struct iap2_dev *)(req->context);
  229. if (req->status != 0){//printk("+++%s:%d+++req->status=%d\n", __func__, __LINE__, req->status);
  230. dev->error = 1;
  231. }
  232. iap2_req_put(dev, &dev->tx_idle, req);
  233. wake_up(&dev->write_wq);
  234. }
  235. static void iap2_complete_out(struct usb_ep *ep, struct usb_request *req)
  236. {
  237. struct iap2_dev *dev = (struct iap2_dev *)(req->context);
  238. //unsigned long flags;
  239. dev->rx_done = 1;
  240. if (req) {
  241. if (req->status != 0){//printk("+++%s:%d+++req->status=%d\n", __func__, __LINE__, req->status);
  242. iap2_req_put(dev, &dev->rx_idle, req);
  243. dev->error = 1;
  244. } else {
  245. iap2_req_put(dev, &dev->rx_used, req);
  246. }
  247. }
  248. wake_up(&dev->read_wq);
  249. }
  250. static int iap2_create_bulk_endpoints(struct iap2_dev *dev,
  251. struct usb_endpoint_descriptor *in_desc,
  252. struct usb_endpoint_descriptor *out_desc)
  253. {
  254. struct usb_composite_dev *cdev = dev->cdev;
  255. struct usb_request *req;
  256. struct usb_ep *ep;
  257. int i;
  258. DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
  259. ep = usb_ep_autoconfig(cdev->gadget, in_desc);
  260. if (!ep) {
  261. DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
  262. return -ENODEV;
  263. }
  264. DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
  265. ep->driver_data = dev;
  266. ep->desc = in_desc;
  267. dev->ep_in = ep;
  268. ep = usb_ep_autoconfig(cdev->gadget, out_desc);
  269. if (!ep) {
  270. DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
  271. return -ENODEV;
  272. }
  273. DBG(cdev, "usb_ep_autoconfig for iap2 ep_out got %s\n", ep->name);
  274. ep->driver_data = dev;
  275. ep->desc = out_desc;
  276. dev->ep_out = ep;
  277. for (i = 0; i < TX_REQ_MAX; i++) {
  278. req = iap2_request_new(dev->ep_out, iAP2_BULK_BUFFER_SIZE);
  279. if (!req)
  280. goto fail;
  281. req->complete = iap2_complete_out;
  282. req->context = (void *)dev;
  283. iap2_req_put(dev, &dev->rx_idle, req);
  284. }
  285. for (i = 0; i < TX_REQ_MAX; i++) {
  286. req = iap2_request_new(dev->ep_in, iAP2_BULK_BUFFER_SIZE);
  287. if (!req)
  288. goto fail;
  289. req->complete = iap2_complete_in;
  290. req->context = (void *)dev;
  291. iap2_req_put(dev, &dev->tx_idle, req);
  292. }
  293. return 0;
  294. fail:
  295. printk(KERN_ERR "iap2_bind() could not allocate requests\n");
  296. return -1;
  297. }
  298. static int iap2_rx_submit(struct iap2_dev *dev, struct usb_request *req)
  299. {
  300. int ret, r;
  301. dev->rx_done = 0;
  302. ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
  303. if (ret < 0) {
  304. pr_debug("iap2_read: failed to queue req %p (%d)\n", req, ret);
  305. r = -EIO;
  306. dev->error = 1;
  307. goto done;
  308. } else {
  309. pr_debug("rx %p queue\n", req);
  310. }
  311. done:
  312. pr_debug("iap2_read returning %d\n", r);
  313. return r;
  314. }
  315. static ssize_t iap2_read(struct file *fp, char __user *buf,
  316. size_t count, loff_t *pos)
  317. {
  318. struct iap2_dev *dev = fp->private_data;
  319. struct usb_request *req = NULL;
  320. int r = 0, xfer, cur_actual = 0;
  321. int ret;
  322. //static int cur_pos = 0;
  323. unsigned long flags;
  324. pr_debug("iap2_read(%d) n", count);
  325. if (!dev)
  326. return -ENODEV;
  327. //if (count > iAP2_BULK_BUFFER_SIZE)
  328. // return -EINVAL;
  329. if (count <= 0)
  330. return 0;
  331. if (iap2_lock(&dev->read_excl))
  332. return -EBUSY;
  333. /*while (!(dev->online || dev->error)) {
  334. printk("iap2_read: waiting for online state\n");
  335. ret = wait_event_interruptible(dev->read_wq,
  336. (dev->online || dev->error));
  337. if (ret < 0) {
  338. iap2_unlock(&dev->read_excl);
  339. return ret;
  340. }
  341. }*/
  342. if (dev->error || dev->online == 0) {
  343. r = -EIO;
  344. goto done;
  345. }
  346. req = NULL;
  347. ret = wait_event_interruptible(dev->read_wq, (req = iap2_req_get(dev, &dev->rx_used)) != NULL);
  348. if (ret < 0) {
  349. r = ret;
  350. usb_ep_dequeue(dev->ep_out, req);
  351. goto done;
  352. }
  353. if (!dev->error && req) {
  354. if (req->actual > dev->cur_read_pos) {
  355. cur_actual = req->actual - dev->cur_read_pos;
  356. xfer = (cur_actual <= count) ? cur_actual : count;
  357. r = xfer;
  358. if (copy_to_user(buf, (req->buf + dev->cur_read_pos), xfer))
  359. r = -EFAULT;
  360. }
  361. if (count >= cur_actual) {
  362. iap2_rx_submit(dev, req);//the req is read completely, return to the usb core
  363. dev->cur_read_pos = 0;
  364. } else {
  365. dev->cur_read_pos += count;
  366. 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
  367. list_add(&req->list, &dev->rx_used);
  368. spin_unlock_irqrestore(&dev->lock, flags);
  369. }
  370. } else {// IO error
  371. if (dev->error)
  372. r = -EIO;
  373. if (dev->online == 0)
  374. r = -ENODEV;
  375. if (req) {
  376. spin_lock_irqsave(&dev->lock, flags);
  377. list_add(&req->list, &dev->rx_used);
  378. spin_unlock_irqrestore(&dev->lock, flags);
  379. }
  380. }
  381. done:
  382. iap2_unlock(&dev->read_excl);
  383. pr_debug("iap2_read returning %d\n", r);
  384. return r;
  385. }
  386. static ssize_t iap2_write(struct file *fp, const char __user *buf,
  387. size_t count, loff_t *pos)
  388. {
  389. struct iap2_dev *dev = fp->private_data;
  390. struct usb_request *req = 0;
  391. int r = count, xfer;
  392. int ret;
  393. if (!dev)
  394. return -ENODEV;
  395. pr_debug("iap2_write(%d)\n", count);
  396. if (iap2_lock(&dev->write_excl))
  397. return -EBUSY;
  398. while (count > 0) {
  399. if (dev->error) {
  400. pr_debug("iap2_write dev->error\n");
  401. r = -EIO;
  402. break;
  403. }
  404. if (dev->online == 0) {
  405. pr_debug("iap2_write dev->online == 0\n");
  406. r = -ENODEV;
  407. break;
  408. }
  409. req = 0;
  410. ret = wait_event_interruptible_timeout(dev->write_wq,
  411. ((req = iap2_req_get(dev, &dev->tx_idle)) || (dev->error) || (dev->online == 0)), msecs_to_jiffies(1000));
  412. if (ret <= 0) {
  413. r = ret;
  414. break;
  415. }
  416. if (req != 0) {
  417. if (count > iAP2_BULK_BUFFER_SIZE)
  418. xfer = iAP2_BULK_BUFFER_SIZE;
  419. else
  420. xfer = count;
  421. if (copy_from_user(req->buf, buf, xfer)) {
  422. r = -EFAULT;
  423. break;
  424. }
  425. req->length = xfer;
  426. ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
  427. if (ret < 0) {
  428. pr_debug("iap2_write: xfer error %d\n", ret);
  429. dev->error = 1;
  430. r = -EIO;
  431. break;
  432. }
  433. buf += xfer;
  434. count -= xfer;
  435. req = 0;
  436. }
  437. }
  438. if (req)
  439. iap2_req_put(dev, &dev->tx_idle, req);
  440. //done:
  441. iap2_unlock(&dev->write_excl);
  442. pr_debug("iap2_write returning %d\n", r);
  443. return r;
  444. }
  445. static int iap2_open(struct inode *ip, struct file *fp)
  446. {
  447. if (!_iap2_dev)
  448. return -ENODEV;
  449. if (iap2_lock(&_iap2_dev->open_excl))
  450. return -EBUSY;
  451. fp->private_data = _iap2_dev;
  452. _iap2_dev->error = 0;
  453. _iap2_dev->rx_done = 0;
  454. //printk(KERN_INFO "iap2_open\n", _iap2_dev->online);
  455. return 0;
  456. }
  457. static int iap2_release(struct inode *ip, struct file *fp)
  458. {
  459. //printk(KERN_INFO "iap2_release\n");
  460. iap2_unlock(&_iap2_dev->open_excl);
  461. return 0;
  462. }
  463. static unsigned int iap2_poll(struct file *fp, struct poll_table_struct *wait)
  464. {
  465. struct iap2_dev *dev = fp->private_data;
  466. unsigned int mask = 0;
  467. //printk("dev->online = %d, dev->error = %d dev->rx_done = %d \n", dev->online, dev->error, dev->rx_done);
  468. poll_wait(fp, &dev->write_wq, wait);
  469. poll_wait(fp, &dev->read_wq, wait);
  470. if (fp->f_mode & FMODE_READ && dev->online && (!dev->error) && (!list_empty_careful(&dev->rx_used)))
  471. mask |= POLLIN| POLLRDNORM;
  472. if (fp->f_mode & FMODE_WRITE && dev->online && (!dev->error) && (!list_empty_careful(&dev->tx_idle)))
  473. mask |= POLLOUT | POLLWRNORM;
  474. return mask;
  475. }
  476. #define iAP2_IOCTL_BASE 0xb7
  477. #define iAP2_GET_ONLINE_STATE _IOW(iAP2_IOCTL_BASE, 0, unsigned long)
  478. static long iap2_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  479. {
  480. struct iap2_dev *dev = fp->private_data;
  481. long res = 0;
  482. int online = 0;
  483. switch(cmd) {
  484. case iAP2_GET_ONLINE_STATE:
  485. //printk("dev->online = %d\n", dev->online);
  486. online = dev->online;
  487. if (copy_to_user((void*)arg, &online, sizeof(online))) {
  488. return -EFAULT;
  489. }
  490. break;
  491. }
  492. return res;
  493. }
  494. /* file operations for iap2 device /dev/iap2 */
  495. static struct file_operations iap2_fops = {
  496. .owner = THIS_MODULE,
  497. .read = iap2_read,
  498. .write = iap2_write,
  499. .unlocked_ioctl = iap2_ioctl,
  500. .open = iap2_open,
  501. .poll = iap2_poll,
  502. .release = iap2_release,
  503. };
  504. static struct miscdevice iap2_device = {
  505. .minor = MISC_DYNAMIC_MINOR,
  506. .name = iap2_shortname,
  507. .fops = &iap2_fops,
  508. };
  509. static int
  510. iap2_function_bind(struct usb_configuration *c, struct usb_function *f)
  511. {
  512. struct usb_composite_dev *cdev = c->cdev;
  513. struct iap2_dev *dev = func_to_iap2(f);
  514. int id;
  515. int ret, status;
  516. dev->cdev = cdev;
  517. DBG(cdev, "iap2_function_bind dev: %p\n", dev);
  518. /* allocate interface ID(s) */
  519. id = usb_interface_id(c, f);
  520. if (id < 0)
  521. return id;
  522. iap2_interface_desc.bInterfaceNumber = id;
  523. status = usb_string_id(cdev);
  524. if (status < 0)
  525. return status;
  526. iap_string_defs[0].id = status;
  527. iap2_interface_desc.iInterface = status;
  528. dev->function.fs_descriptors = usb_copy_descriptors(fs_iap2_descs);
  529. /* allocate endpoints */
  530. ret = iap2_create_bulk_endpoints(dev, &iap2_fullspeed_in_desc,
  531. &iap2_fullspeed_out_desc);
  532. if (ret)
  533. return ret;
  534. /* support high speed hardware */
  535. if (gadget_is_dualspeed(c->cdev->gadget)) {
  536. iap2_highspeed_in_desc.bEndpointAddress =
  537. iap2_fullspeed_in_desc.bEndpointAddress;
  538. iap2_highspeed_out_desc.bEndpointAddress =
  539. iap2_fullspeed_out_desc.bEndpointAddress;
  540. dev->function.hs_descriptors = usb_copy_descriptors(hs_iap2_descs);
  541. }
  542. /* support super speed hardware */
  543. if (gadget_is_superspeed(c->cdev->gadget)) {
  544. iap2_superspeed_in_desc.bEndpointAddress =
  545. iap2_fullspeed_in_desc.bEndpointAddress;
  546. iap2_superspeed_out_desc.bEndpointAddress =
  547. iap2_fullspeed_out_desc.bEndpointAddress;
  548. dev->function.ss_descriptors = usb_copy_descriptors(ss_iap2_descs);
  549. }
  550. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  551. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  552. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  553. f->name, dev->ep_in->name, dev->ep_out->name);
  554. return 0;
  555. }
  556. static void
  557. iap2_function_unbind(struct usb_configuration *c, struct usb_function *f)
  558. {
  559. struct iap2_dev *dev = func_to_iap2(f);
  560. struct usb_request *req = NULL;
  561. dev->online = 0;
  562. dev->error = 1;
  563. wake_up(&dev->read_wq);
  564. wake_up(&dev->write_wq);
  565. while ((req = iap2_req_get(dev, &dev->rx_idle)))
  566. iap2_request_free(req, dev->ep_out);
  567. req = NULL;
  568. while ((req = iap2_req_get(dev, &dev->rx_used)))
  569. iap2_request_free(req, dev->ep_out);
  570. req = NULL;
  571. while ((req = iap2_req_get(dev, &dev->tx_idle)))
  572. iap2_request_free(req, dev->ep_in);
  573. dev->cur_read_pos = 0;
  574. }
  575. static int iap2_function_set_alt(struct usb_function *f,
  576. unsigned intf, unsigned alt)
  577. {
  578. struct iap2_dev *dev = func_to_iap2(f);
  579. struct usb_composite_dev *cdev = f->config->cdev;
  580. int ret;
  581. struct usb_request *req = NULL;
  582. if (config_ep_by_speed(cdev->gadget, f, dev->ep_in) ||
  583. config_ep_by_speed(cdev->gadget, f, dev->ep_out)) {
  584. dev->ep_in->desc = NULL;
  585. dev->ep_out->desc = NULL;
  586. return -1;
  587. }
  588. DBG(cdev, "iap2_function_set_alt intf: %d alt: %d\n", intf, alt);
  589. ret = usb_ep_enable(dev->ep_in);
  590. if (ret)
  591. return ret;
  592. ret = usb_ep_enable(dev->ep_out);
  593. if (ret) {
  594. usb_ep_disable(dev->ep_in);
  595. return ret;
  596. }
  597. dev->online = 1;
  598. usb_dev_ready_notify(dev, 1);
  599. dev->error = 0;
  600. while ((req = iap2_req_get(dev, &dev->rx_used))) {
  601. iap2_req_put(dev, &dev->rx_idle, req);
  602. }
  603. while ((req = iap2_req_get(dev, &dev->rx_idle))) {
  604. req->length = iAP2_BULK_BUFFER_SIZE;
  605. iap2_rx_submit(dev, req);
  606. }
  607. /* readers may be blocked waiting for us to go online */
  608. wake_up(&dev->read_wq);
  609. wake_up(&dev->write_wq);
  610. return 0;
  611. }
  612. static void iap2_function_disable(struct usb_function *f)
  613. {
  614. struct iap2_dev *dev = func_to_iap2(f);
  615. struct usb_composite_dev *cdev = dev->cdev;
  616. printk("iap2_function_disable cdev %p %d\n", cdev, __LINE__);
  617. usb_dev_ready_notify(dev, 0);
  618. dev->online = 0;
  619. dev->error = 1;
  620. usb_ep_disable(dev->ep_in);
  621. usb_ep_disable(dev->ep_out);
  622. /* readers may be blocked waiting for us to go online */
  623. wake_up(&dev->read_wq);
  624. wake_up(&dev->write_wq);
  625. VDBG(cdev, "%s disabled\n", dev->function.name);
  626. }
  627. static void iap2_function_suspend(struct usb_function *f)
  628. {
  629. struct iap2_dev *dev = func_to_iap2(f);
  630. struct usb_composite_dev *cdev = dev->cdev;
  631. printk( "iap2_function_suspend cdev %p\n", cdev);//DBG(cdev,
  632. usb_dev_ready_notify(dev, 0);
  633. dev->online = 0;
  634. dev->error = 0;
  635. usb_ep_disable(dev->ep_in);
  636. usb_ep_disable(dev->ep_out);
  637. /* readers may be blocked waiting for us to go online */
  638. wake_up(&dev->read_wq);
  639. wake_up(&dev->write_wq);
  640. VDBG(cdev, "%s suspend\n", dev->function.name);
  641. }
  642. #if 0
  643. static int iap2_bind_config(struct usb_configuration *c)
  644. {
  645. struct iap2_dev *dev = _iap2_dev;
  646. int status;
  647. if (iap_string_defs[0].id == 0) {
  648. /* control interface label */
  649. status = usb_string_id(c->cdev);
  650. if (status < 0)
  651. return status;
  652. iap_string_defs[STRING_CTRL_IDX].id = status;
  653. iap2_interface_desc.iInterface = status;
  654. }
  655. dev->cdev = c->cdev;
  656. dev->function.name = "iap2";
  657. dev->function.strings = iap_strings;
  658. dev->function.descriptors = fs_iap2_descs;
  659. dev->function.hs_descriptors = hs_iap2_descs;
  660. dev->function.ss_descriptors = ss_iap2_descs;
  661. dev->function.bind = iap2_function_bind;
  662. dev->function.unbind = iap2_function_unbind;
  663. dev->function.set_alt = iap2_function_set_alt;
  664. dev->function.disable = iap2_function_disable;
  665. dev->function.suspend = iap2_function_suspend;
  666. return usb_add_function(c, &dev->function);
  667. }
  668. #endif
  669. static struct netlink_kernel_cfg cfg = {
  670. .groups = 1,
  671. .flags = NL_CFG_F_NONROOT_RECV,
  672. };
  673. static int iap2_setup(struct iap2_dev *dev)
  674. {
  675. int ret = -1;
  676. //dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  677. if (!dev)
  678. return -ENOMEM;
  679. spin_lock_init(&dev->lock);
  680. init_waitqueue_head(&dev->read_wq);
  681. init_waitqueue_head(&dev->write_wq);
  682. atomic_set(&dev->open_excl, 0);
  683. atomic_set(&dev->read_excl, 0);
  684. atomic_set(&dev->write_excl, 0);
  685. INIT_LIST_HEAD(&dev->tx_idle);
  686. INIT_LIST_HEAD(&dev->rx_idle);
  687. INIT_LIST_HEAD(&dev->rx_used);
  688. dev->netlink_handle = netlink_kernel_create(&init_net, NETLINK_USB_DEV_READY, &cfg);
  689. if(!dev->netlink_handle) {
  690. printk(KERN_ERR "can not create a usb dev ready netlink socket!\n");
  691. goto err;
  692. }
  693. ret = misc_register(&iap2_device);
  694. if (ret)
  695. goto err;
  696. _iap2_dev = dev;
  697. return ret;
  698. err:
  699. kfree(dev);
  700. printk(KERN_ERR "iap2 gadget driver failed to initialize\n");
  701. return ret;
  702. }
  703. static void iap2_cleanup(struct iap2_dev *dev)
  704. {
  705. misc_deregister(&iap2_device);
  706. if (NULL == _iap2_dev)
  707. return;
  708. if(_iap2_dev->netlink_handle) {
  709. netlink_kernel_release(_iap2_dev->netlink_handle);
  710. }
  711. kfree(_iap2_dev);
  712. _iap2_dev = NULL;
  713. }
  714. static void iap2_free(struct usb_function *f)
  715. {
  716. (void)f;
  717. }
  718. struct f_iap2_opts {
  719. struct usb_function_instance func_inst;
  720. struct iap2_dev *dev;
  721. char *name;
  722. int refcnt;
  723. struct mutex lock;
  724. };
  725. static struct f_iap2_opts *to_iap2_opts(struct config_item *item)
  726. {
  727. return container_of(to_config_group(item), struct f_iap2_opts,
  728. func_inst.group);
  729. }
  730. static void iap2_attr_release(struct config_item *item)
  731. {
  732. struct f_iap2_opts *opts = to_iap2_opts(item);
  733. usb_put_function_instance(&opts->func_inst);
  734. }
  735. static struct configfs_item_operations iap2_item_ops = {
  736. .release = iap2_attr_release,
  737. };
  738. static struct config_item_type iap2_func_type = {
  739. .ct_item_ops = &iap2_item_ops,
  740. .ct_owner = THIS_MODULE,
  741. };
  742. static void iap_free_inst(struct usb_function_instance *fi)
  743. {
  744. struct f_iap2_opts *opts;
  745. opts = container_of(fi, struct f_iap2_opts, func_inst);
  746. if (NULL != opts->name)
  747. kfree(opts->name);
  748. iap2_cleanup(opts->dev);
  749. kfree(opts);
  750. }
  751. static struct usb_function_instance *iap_alloc_inst(void)
  752. {
  753. struct f_iap2_opts *opts = NULL;
  754. opts = kzalloc(sizeof(struct f_iap2_opts), GFP_KERNEL);
  755. if (!opts)
  756. return ERR_PTR(-ENOMEM);
  757. //fi_iap->func_inst.set_inst_name = iap_set_inst_name;
  758. opts->func_inst.free_func_inst = iap_free_inst;
  759. mutex_init(&opts->lock);
  760. config_group_init_type_name(&opts->func_inst.group,
  761. "", &iap2_func_type);
  762. return &opts->func_inst;
  763. }
  764. struct usb_function *iap_alloc(struct usb_function_instance *fi)
  765. {
  766. struct f_iap2_opts *opts = container_of(fi, struct f_iap2_opts, func_inst);
  767. struct iap2_dev *dev;
  768. int ret = -1;
  769. dev = kzalloc(sizeof *dev, GFP_KERNEL);
  770. if (!dev)
  771. return ERR_PTR(-ENOMEM);
  772. ret = iap2_setup(dev);
  773. if (ret) {
  774. pr_err("Error setting IAP\n");
  775. return ERR_PTR(ret);
  776. }
  777. opts = container_of(fi, struct f_iap2_opts, func_inst);
  778. mutex_lock(&opts->lock);
  779. opts->dev = dev;
  780. opts->refcnt++;
  781. mutex_unlock(&opts->lock);
  782. dev = opts->dev;
  783. dev->function.name = DRIVER_NAME;
  784. dev->function.strings = iap_strings;
  785. dev->function.bind = iap2_function_bind;
  786. dev->function.unbind = iap2_function_unbind;
  787. dev->function.set_alt = iap2_function_set_alt;
  788. dev->function.disable = iap2_function_disable;
  789. dev->function.suspend = iap2_function_suspend;
  790. dev->function.free_func = iap2_free;
  791. return &dev->function;
  792. }
  793. DECLARE_USB_FUNCTION_INIT(iap, iap_alloc_inst, iap_alloc);
  794. MODULE_LICENSE("GPL");