f_gps.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/device.h>
  16. #include <linux/spinlock.h>
  17. #include <mach/usb_gadget_xport.h>
  18. #include "u_rmnet.h"
  19. #include "gadget_chips.h"
  20. #define GPS_NOTIFY_INTERVAL 5
  21. #define GPS_MAX_NOTIFY_SIZE 64
  22. #define ACM_CTRL_DTR (1 << 0)
  23. /* TODO: use separate structures for data and
  24. * control paths
  25. */
  26. struct f_gps {
  27. struct grmnet port;
  28. u8 port_num;
  29. int ifc_id;
  30. atomic_t online;
  31. atomic_t ctrl_online;
  32. struct usb_composite_dev *cdev;
  33. spinlock_t lock;
  34. /* usb eps */
  35. struct usb_ep *notify;
  36. struct usb_request *notify_req;
  37. /* control info */
  38. struct list_head cpkt_resp_q;
  39. atomic_t notify_count;
  40. unsigned long cpkts_len;
  41. };
  42. static struct gps_ports {
  43. enum transport_type ctrl_xport;
  44. struct f_gps *port;
  45. } gps_port;
  46. static struct usb_interface_descriptor gps_interface_desc = {
  47. .bLength = USB_DT_INTERFACE_SIZE,
  48. .bDescriptorType = USB_DT_INTERFACE,
  49. .bNumEndpoints = 1,
  50. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  51. .bInterfaceSubClass = USB_CLASS_VENDOR_SPEC,
  52. .bInterfaceProtocol = USB_CLASS_VENDOR_SPEC,
  53. /* .iInterface = DYNAMIC */
  54. };
  55. /* Full speed support */
  56. static struct usb_endpoint_descriptor gps_fs_notify_desc = {
  57. .bLength = USB_DT_ENDPOINT_SIZE,
  58. .bDescriptorType = USB_DT_ENDPOINT,
  59. .bEndpointAddress = USB_DIR_IN,
  60. .bmAttributes = USB_ENDPOINT_XFER_INT,
  61. .wMaxPacketSize = __constant_cpu_to_le16(GPS_MAX_NOTIFY_SIZE),
  62. .bInterval = 1 << GPS_NOTIFY_INTERVAL,
  63. };
  64. static struct usb_descriptor_header *gps_fs_function[] = {
  65. (struct usb_descriptor_header *) &gps_interface_desc,
  66. (struct usb_descriptor_header *) &gps_fs_notify_desc,
  67. NULL,
  68. };
  69. /* High speed support */
  70. static struct usb_endpoint_descriptor gps_hs_notify_desc = {
  71. .bLength = USB_DT_ENDPOINT_SIZE,
  72. .bDescriptorType = USB_DT_ENDPOINT,
  73. .bEndpointAddress = USB_DIR_IN,
  74. .bmAttributes = USB_ENDPOINT_XFER_INT,
  75. .wMaxPacketSize = __constant_cpu_to_le16(GPS_MAX_NOTIFY_SIZE),
  76. .bInterval = GPS_NOTIFY_INTERVAL + 4,
  77. };
  78. static struct usb_descriptor_header *gps_hs_function[] = {
  79. (struct usb_descriptor_header *) &gps_interface_desc,
  80. (struct usb_descriptor_header *) &gps_hs_notify_desc,
  81. NULL,
  82. };
  83. /* Super speed support */
  84. static struct usb_endpoint_descriptor gps_ss_notify_desc = {
  85. .bLength = USB_DT_ENDPOINT_SIZE,
  86. .bDescriptorType = USB_DT_ENDPOINT,
  87. .bEndpointAddress = USB_DIR_IN,
  88. .bmAttributes = USB_ENDPOINT_XFER_INT,
  89. .wMaxPacketSize = __constant_cpu_to_le16(GPS_MAX_NOTIFY_SIZE),
  90. .bInterval = GPS_NOTIFY_INTERVAL + 4,
  91. };
  92. static struct usb_ss_ep_comp_descriptor gps_ss_notify_comp_desc = {
  93. .bLength = sizeof gps_ss_notify_comp_desc,
  94. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  95. /* the following 3 values can be tweaked if necessary */
  96. /* .bMaxBurst = 0, */
  97. /* .bmAttributes = 0, */
  98. .wBytesPerInterval = cpu_to_le16(GPS_MAX_NOTIFY_SIZE),
  99. };
  100. static struct usb_descriptor_header *gps_ss_function[] = {
  101. (struct usb_descriptor_header *) &gps_interface_desc,
  102. (struct usb_descriptor_header *) &gps_ss_notify_desc,
  103. (struct usb_descriptor_header *) &gps_ss_notify_comp_desc,
  104. NULL,
  105. };
  106. /* String descriptors */
  107. static struct usb_string gps_string_defs[] = {
  108. [0].s = "GPS",
  109. { } /* end of list */
  110. };
  111. static struct usb_gadget_strings gps_string_table = {
  112. .language = 0x0409, /* en-us */
  113. .strings = gps_string_defs,
  114. };
  115. static struct usb_gadget_strings *gps_strings[] = {
  116. &gps_string_table,
  117. NULL,
  118. };
  119. static void gps_ctrl_response_available(struct f_gps *dev);
  120. /* ------- misc functions --------------------*/
  121. static inline struct f_gps *func_to_gps(struct usb_function *f)
  122. {
  123. return container_of(f, struct f_gps, port.func);
  124. }
  125. static inline struct f_gps *port_to_gps(struct grmnet *r)
  126. {
  127. return container_of(r, struct f_gps, port);
  128. }
  129. static struct usb_request *
  130. gps_alloc_req(struct usb_ep *ep, unsigned len, gfp_t flags)
  131. {
  132. struct usb_request *req;
  133. req = usb_ep_alloc_request(ep, flags);
  134. if (!req)
  135. return ERR_PTR(-ENOMEM);
  136. req->buf = kmalloc(len, flags);
  137. if (!req->buf) {
  138. usb_ep_free_request(ep, req);
  139. return ERR_PTR(-ENOMEM);
  140. }
  141. req->length = len;
  142. return req;
  143. }
  144. void gps_free_req(struct usb_ep *ep, struct usb_request *req)
  145. {
  146. kfree(req->buf);
  147. usb_ep_free_request(ep, req);
  148. }
  149. static struct rmnet_ctrl_pkt *gps_alloc_ctrl_pkt(unsigned len, gfp_t flags)
  150. {
  151. struct rmnet_ctrl_pkt *pkt;
  152. pkt = kzalloc(sizeof(struct rmnet_ctrl_pkt), flags);
  153. if (!pkt)
  154. return ERR_PTR(-ENOMEM);
  155. pkt->buf = kmalloc(len, flags);
  156. if (!pkt->buf) {
  157. kfree(pkt);
  158. return ERR_PTR(-ENOMEM);
  159. }
  160. pkt->len = len;
  161. return pkt;
  162. }
  163. static void gps_free_ctrl_pkt(struct rmnet_ctrl_pkt *pkt)
  164. {
  165. kfree(pkt->buf);
  166. kfree(pkt);
  167. }
  168. /* -------------------------------------------*/
  169. static int gps_gport_setup(void)
  170. {
  171. u8 base;
  172. int res;
  173. res = gsmd_ctrl_setup(GPS_CTRL_CLIENT, 1, &base);
  174. gps_port.port->port_num += base;
  175. return res;
  176. }
  177. static int gport_ctrl_connect(struct f_gps *dev)
  178. {
  179. return gsmd_ctrl_connect(&dev->port, dev->port_num);
  180. }
  181. static int gport_gps_disconnect(struct f_gps *dev)
  182. {
  183. gsmd_ctrl_disconnect(&dev->port, dev->port_num);
  184. return 0;
  185. }
  186. static void gps_unbind(struct usb_configuration *c, struct usb_function *f)
  187. {
  188. struct f_gps *dev = func_to_gps(f);
  189. pr_debug("%s: portno:%d\n", __func__, dev->port_num);
  190. if (gadget_is_superspeed(c->cdev->gadget))
  191. usb_free_descriptors(f->ss_descriptors);
  192. if (gadget_is_dualspeed(c->cdev->gadget))
  193. usb_free_descriptors(f->hs_descriptors);
  194. usb_free_descriptors(f->fs_descriptors);
  195. gps_free_req(dev->notify, dev->notify_req);
  196. kfree(f->name);
  197. }
  198. static void gps_purge_responses(struct f_gps *dev)
  199. {
  200. unsigned long flags;
  201. struct rmnet_ctrl_pkt *cpkt;
  202. pr_debug("%s: port#%d\n", __func__, dev->port_num);
  203. spin_lock_irqsave(&dev->lock, flags);
  204. while (!list_empty(&dev->cpkt_resp_q)) {
  205. cpkt = list_first_entry(&dev->cpkt_resp_q,
  206. struct rmnet_ctrl_pkt, list);
  207. list_del(&cpkt->list);
  208. rmnet_free_ctrl_pkt(cpkt);
  209. }
  210. atomic_set(&dev->notify_count, 0);
  211. spin_unlock_irqrestore(&dev->lock, flags);
  212. }
  213. static void gps_suspend(struct usb_function *f)
  214. {
  215. struct f_gps *dev = func_to_gps(f);
  216. gps_purge_responses(dev);
  217. }
  218. static void gps_disable(struct usb_function *f)
  219. {
  220. struct f_gps *dev = func_to_gps(f);
  221. usb_ep_disable(dev->notify);
  222. dev->notify->driver_data = NULL;
  223. atomic_set(&dev->online, 0);
  224. gps_purge_responses(dev);
  225. gport_gps_disconnect(dev);
  226. }
  227. static int
  228. gps_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  229. {
  230. struct f_gps *dev = func_to_gps(f);
  231. struct usb_composite_dev *cdev = dev->cdev;
  232. int ret;
  233. struct list_head *cpkt;
  234. pr_debug("%s:dev:%pK\n", __func__, dev);
  235. if (dev->notify->driver_data)
  236. usb_ep_disable(dev->notify);
  237. ret = config_ep_by_speed(cdev->gadget, f, dev->notify);
  238. if (ret) {
  239. dev->notify->desc = NULL;
  240. ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
  241. dev->notify->name, ret);
  242. return ret;
  243. }
  244. ret = usb_ep_enable(dev->notify);
  245. if (ret) {
  246. pr_err("%s: usb ep#%s enable failed, err#%d\n",
  247. __func__, dev->notify->name, ret);
  248. return ret;
  249. }
  250. dev->notify->driver_data = dev;
  251. ret = gport_ctrl_connect(dev);
  252. atomic_set(&dev->online, 1);
  253. /* In case notifications were aborted, but there are pending control
  254. packets in the response queue, re-add the notifications */
  255. list_for_each(cpkt, &dev->cpkt_resp_q)
  256. gps_ctrl_response_available(dev);
  257. return ret;
  258. }
  259. static void gps_ctrl_response_available(struct f_gps *dev)
  260. {
  261. struct usb_request *req = dev->notify_req;
  262. struct usb_cdc_notification *event;
  263. unsigned long flags;
  264. int ret;
  265. struct rmnet_ctrl_pkt *cpkt;
  266. pr_debug("%s:dev:%pK\n", __func__, dev);
  267. spin_lock_irqsave(&dev->lock, flags);
  268. if (!atomic_read(&dev->online) || !req || !req->buf) {
  269. spin_unlock_irqrestore(&dev->lock, flags);
  270. return;
  271. }
  272. if (atomic_inc_return(&dev->notify_count) != 1) {
  273. spin_unlock_irqrestore(&dev->lock, flags);
  274. return;
  275. }
  276. event = req->buf;
  277. event->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
  278. | USB_RECIP_INTERFACE;
  279. event->bNotificationType = USB_CDC_NOTIFY_RESPONSE_AVAILABLE;
  280. event->wValue = cpu_to_le16(0);
  281. event->wIndex = cpu_to_le16(dev->ifc_id);
  282. event->wLength = cpu_to_le16(0);
  283. spin_unlock_irqrestore(&dev->lock, flags);
  284. ret = usb_ep_queue(dev->notify, dev->notify_req, GFP_ATOMIC);
  285. if (ret) {
  286. spin_lock_irqsave(&dev->lock, flags);
  287. if (!list_empty(&dev->cpkt_resp_q)) {
  288. atomic_dec(&dev->notify_count);
  289. cpkt = list_first_entry(&dev->cpkt_resp_q,
  290. struct rmnet_ctrl_pkt, list);
  291. list_del(&cpkt->list);
  292. gps_free_ctrl_pkt(cpkt);
  293. }
  294. spin_unlock_irqrestore(&dev->lock, flags);
  295. pr_debug("ep enqueue error %d\n", ret);
  296. }
  297. }
  298. static void gps_connect(struct grmnet *gr)
  299. {
  300. struct f_gps *dev;
  301. if (!gr) {
  302. pr_err("%s: Invalid grmnet:%pK\n", __func__, gr);
  303. return;
  304. }
  305. dev = port_to_gps(gr);
  306. atomic_set(&dev->ctrl_online, 1);
  307. }
  308. static void gps_disconnect(struct grmnet *gr)
  309. {
  310. struct f_gps *dev;
  311. struct usb_cdc_notification *event;
  312. int status;
  313. if (!gr) {
  314. pr_err("%s: Invalid grmnet:%pK\n", __func__, gr);
  315. return;
  316. }
  317. dev = port_to_gps(gr);
  318. atomic_set(&dev->ctrl_online, 0);
  319. if (!atomic_read(&dev->online)) {
  320. pr_debug("%s: nothing to do\n", __func__);
  321. return;
  322. }
  323. usb_ep_fifo_flush(dev->notify);
  324. event = dev->notify_req->buf;
  325. event->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
  326. | USB_RECIP_INTERFACE;
  327. event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
  328. event->wValue = cpu_to_le16(0);
  329. event->wIndex = cpu_to_le16(dev->ifc_id);
  330. event->wLength = cpu_to_le16(0);
  331. status = usb_ep_queue(dev->notify, dev->notify_req, GFP_ATOMIC);
  332. if (status < 0) {
  333. if (!atomic_read(&dev->online))
  334. return;
  335. pr_err("%s: gps notify ep enqueue error %d\n",
  336. __func__, status);
  337. }
  338. gps_purge_responses(dev);
  339. }
  340. static int
  341. gps_send_cpkt_response(void *gr, void *buf, size_t len)
  342. {
  343. struct f_gps *dev;
  344. struct rmnet_ctrl_pkt *cpkt;
  345. unsigned long flags;
  346. if (!gr || !buf) {
  347. pr_err("%s: Invalid grmnet/buf, grmnet:%pK buf:%pK\n",
  348. __func__, gr, buf);
  349. return -ENODEV;
  350. }
  351. cpkt = gps_alloc_ctrl_pkt(len, GFP_ATOMIC);
  352. if (IS_ERR(cpkt)) {
  353. pr_err("%s: Unable to allocate ctrl pkt\n", __func__);
  354. return -ENOMEM;
  355. }
  356. memcpy(cpkt->buf, buf, len);
  357. cpkt->len = len;
  358. dev = port_to_gps(gr);
  359. pr_debug("%s: dev:%pK\n", __func__, dev);
  360. if (!atomic_read(&dev->online) || !atomic_read(&dev->ctrl_online)) {
  361. gps_free_ctrl_pkt(cpkt);
  362. return 0;
  363. }
  364. spin_lock_irqsave(&dev->lock, flags);
  365. list_add_tail(&cpkt->list, &dev->cpkt_resp_q);
  366. spin_unlock_irqrestore(&dev->lock, flags);
  367. gps_ctrl_response_available(dev);
  368. return 0;
  369. }
  370. static void
  371. gps_cmd_complete(struct usb_ep *ep, struct usb_request *req)
  372. {
  373. struct f_gps *dev = req->context;
  374. struct usb_composite_dev *cdev;
  375. if (!dev) {
  376. pr_err("%s: dev is null\n", __func__);
  377. return;
  378. }
  379. pr_debug("%s: dev:%pK\n", __func__, dev);
  380. cdev = dev->cdev;
  381. if (dev->port.send_encap_cmd)
  382. dev->port.send_encap_cmd(dev->port_num, req->buf, req->actual);
  383. }
  384. static void gps_notify_complete(struct usb_ep *ep, struct usb_request *req)
  385. {
  386. struct f_gps *dev = req->context;
  387. int status = req->status;
  388. unsigned long flags;
  389. struct rmnet_ctrl_pkt *cpkt;
  390. pr_debug("%s: dev:%pK port#%d\n", __func__, dev, dev->port_num);
  391. switch (status) {
  392. case -ECONNRESET:
  393. case -ESHUTDOWN:
  394. /* connection gone */
  395. atomic_set(&dev->notify_count, 0);
  396. break;
  397. default:
  398. pr_err("gps notify ep error %d\n", status);
  399. /* FALLTHROUGH */
  400. case 0:
  401. if (!atomic_read(&dev->ctrl_online))
  402. break;
  403. if (atomic_dec_and_test(&dev->notify_count))
  404. break;
  405. status = usb_ep_queue(dev->notify, req, GFP_ATOMIC);
  406. if (status) {
  407. spin_lock_irqsave(&dev->lock, flags);
  408. if (!list_empty(&dev->cpkt_resp_q)) {
  409. atomic_dec(&dev->notify_count);
  410. cpkt = list_first_entry(&dev->cpkt_resp_q,
  411. struct rmnet_ctrl_pkt, list);
  412. list_del(&cpkt->list);
  413. gps_free_ctrl_pkt(cpkt);
  414. }
  415. spin_unlock_irqrestore(&dev->lock, flags);
  416. pr_debug("ep enqueue error %d\n", status);
  417. }
  418. break;
  419. }
  420. }
  421. static int
  422. gps_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  423. {
  424. struct f_gps *dev = func_to_gps(f);
  425. struct usb_composite_dev *cdev = dev->cdev;
  426. struct usb_request *req = cdev->req;
  427. u16 w_index = le16_to_cpu(ctrl->wIndex);
  428. u16 w_value = le16_to_cpu(ctrl->wValue);
  429. u16 w_length = le16_to_cpu(ctrl->wLength);
  430. int ret = -EOPNOTSUPP;
  431. pr_debug("%s:dev:%pK\n", __func__, dev);
  432. if (!atomic_read(&dev->online)) {
  433. pr_debug("%s: usb cable is not connected\n", __func__);
  434. return -ENOTCONN;
  435. }
  436. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  437. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  438. | USB_CDC_SEND_ENCAPSULATED_COMMAND:
  439. ret = w_length;
  440. req->complete = gps_cmd_complete;
  441. req->context = dev;
  442. break;
  443. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  444. | USB_CDC_GET_ENCAPSULATED_RESPONSE:
  445. if (w_value)
  446. goto invalid;
  447. else {
  448. unsigned len;
  449. struct rmnet_ctrl_pkt *cpkt;
  450. spin_lock(&dev->lock);
  451. if (list_empty(&dev->cpkt_resp_q)) {
  452. pr_err("%s: ctrl resp queue empty", __func__);
  453. spin_unlock(&dev->lock);
  454. goto invalid;
  455. }
  456. cpkt = list_first_entry(&dev->cpkt_resp_q,
  457. struct rmnet_ctrl_pkt, list);
  458. list_del(&cpkt->list);
  459. spin_unlock(&dev->lock);
  460. len = min_t(unsigned, w_length, cpkt->len);
  461. memcpy(req->buf, cpkt->buf, len);
  462. ret = len;
  463. gps_free_ctrl_pkt(cpkt);
  464. }
  465. break;
  466. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  467. | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
  468. if (dev->port.notify_modem)
  469. dev->port.notify_modem(&dev->port,
  470. dev->port_num, w_value);
  471. ret = 0;
  472. break;
  473. default:
  474. invalid:
  475. DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  476. ctrl->bRequestType, ctrl->bRequest,
  477. w_value, w_index, w_length);
  478. }
  479. /* respond with data transfer or status phase? */
  480. if (ret >= 0) {
  481. VDBG(cdev, "gps req%02x.%02x v%04x i%04x l%d\n",
  482. ctrl->bRequestType, ctrl->bRequest,
  483. w_value, w_index, w_length);
  484. req->zero = (ret < w_length);
  485. req->length = ret;
  486. ret = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  487. if (ret < 0)
  488. ERROR(cdev, "gps ep0 enqueue err %d\n", ret);
  489. }
  490. return ret;
  491. }
  492. static int gps_bind(struct usb_configuration *c, struct usb_function *f)
  493. {
  494. struct f_gps *dev = func_to_gps(f);
  495. struct usb_ep *ep;
  496. struct usb_composite_dev *cdev = c->cdev;
  497. int ret = -ENODEV;
  498. dev->ifc_id = usb_interface_id(c, f);
  499. if (dev->ifc_id < 0) {
  500. pr_err("%s: unable to allocate ifc id, err:%d",
  501. __func__, dev->ifc_id);
  502. return dev->ifc_id;
  503. }
  504. gps_interface_desc.bInterfaceNumber = dev->ifc_id;
  505. dev->port.in = NULL;
  506. dev->port.out = NULL;
  507. ep = usb_ep_autoconfig(cdev->gadget, &gps_fs_notify_desc);
  508. if (!ep) {
  509. pr_err("%s: usb epnotify autoconfig failed\n", __func__);
  510. ret = -ENODEV;
  511. goto ep_auto_notify_fail;
  512. }
  513. dev->notify = ep;
  514. ep->driver_data = cdev;
  515. dev->notify_req = gps_alloc_req(ep,
  516. sizeof(struct usb_cdc_notification),
  517. GFP_KERNEL);
  518. if (IS_ERR(dev->notify_req)) {
  519. pr_err("%s: unable to allocate memory for notify req\n",
  520. __func__);
  521. ret = -ENOMEM;
  522. goto ep_notify_alloc_fail;
  523. }
  524. dev->notify_req->complete = gps_notify_complete;
  525. dev->notify_req->context = dev;
  526. ret = -ENOMEM;
  527. f->fs_descriptors = usb_copy_descriptors(gps_fs_function);
  528. if (!f->fs_descriptors)
  529. goto fail;
  530. if (gadget_is_dualspeed(cdev->gadget)) {
  531. gps_hs_notify_desc.bEndpointAddress =
  532. gps_fs_notify_desc.bEndpointAddress;
  533. /* copy descriptors, and track endpoint copies */
  534. f->hs_descriptors = usb_copy_descriptors(gps_hs_function);
  535. if (!f->hs_descriptors)
  536. goto fail;
  537. }
  538. if (gadget_is_superspeed(cdev->gadget)) {
  539. gps_ss_notify_desc.bEndpointAddress =
  540. gps_fs_notify_desc.bEndpointAddress;
  541. /* copy descriptors, and track endpoint copies */
  542. f->ss_descriptors = usb_copy_descriptors(gps_ss_function);
  543. if (!f->ss_descriptors)
  544. goto fail;
  545. }
  546. pr_info("%s: GPS(%d) %s Speed\n",
  547. __func__, dev->port_num,
  548. gadget_is_dualspeed(cdev->gadget) ? "dual" : "full");
  549. return 0;
  550. fail:
  551. if (f->ss_descriptors)
  552. usb_free_descriptors(f->ss_descriptors);
  553. if (f->hs_descriptors)
  554. usb_free_descriptors(f->hs_descriptors);
  555. if (f->fs_descriptors)
  556. usb_free_descriptors(f->fs_descriptors);
  557. if (dev->notify_req)
  558. gps_free_req(dev->notify, dev->notify_req);
  559. ep_notify_alloc_fail:
  560. dev->notify->driver_data = NULL;
  561. dev->notify = NULL;
  562. ep_auto_notify_fail:
  563. return ret;
  564. }
  565. static int gps_bind_config(struct usb_configuration *c)
  566. {
  567. int status;
  568. struct f_gps *dev;
  569. struct usb_function *f;
  570. unsigned long flags;
  571. pr_debug("%s: usb config:%pK\n", __func__, c);
  572. if (gps_string_defs[0].id == 0) {
  573. status = usb_string_id(c->cdev);
  574. if (status < 0) {
  575. pr_err("%s: failed to get string id, err:%d\n",
  576. __func__, status);
  577. return status;
  578. }
  579. gps_string_defs[0].id = status;
  580. }
  581. dev = gps_port.port;
  582. spin_lock_irqsave(&dev->lock, flags);
  583. dev->cdev = c->cdev;
  584. f = &dev->port.func;
  585. f->name = kasprintf(GFP_ATOMIC, "gps");
  586. spin_unlock_irqrestore(&dev->lock, flags);
  587. if (!f->name) {
  588. pr_err("%s: cannot allocate memory for name\n", __func__);
  589. return -ENOMEM;
  590. }
  591. f->strings = gps_strings;
  592. f->bind = gps_bind;
  593. f->unbind = gps_unbind;
  594. f->disable = gps_disable;
  595. f->set_alt = gps_set_alt;
  596. f->setup = gps_setup;
  597. f->suspend = gps_suspend;
  598. dev->port.send_cpkt_response = gps_send_cpkt_response;
  599. dev->port.disconnect = gps_disconnect;
  600. dev->port.connect = gps_connect;
  601. status = usb_add_function(c, f);
  602. if (status) {
  603. pr_err("%s: usb add function failed: %d\n",
  604. __func__, status);
  605. kfree(f->name);
  606. return status;
  607. }
  608. pr_debug("%s: complete\n", __func__);
  609. return status;
  610. }
  611. static void gps_cleanup(void)
  612. {
  613. kfree(gps_port.port);
  614. }
  615. static int gps_init_port(void)
  616. {
  617. struct f_gps *dev;
  618. dev = kzalloc(sizeof(struct f_gps), GFP_KERNEL);
  619. if (!dev) {
  620. pr_err("%s: Unable to allocate gps device\n", __func__);
  621. return -ENOMEM;
  622. }
  623. spin_lock_init(&dev->lock);
  624. INIT_LIST_HEAD(&dev->cpkt_resp_q);
  625. dev->port_num = 0;
  626. gps_port.port = dev;
  627. gps_port.ctrl_xport = USB_GADGET_XPORT_SMD;
  628. return 0;
  629. }