f_eap.c 24 KB

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