f_iap.c 24 KB

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