f_apple_ptp_sim.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * f_apple_ptp_sim.c - USB peripheral apple_ptp_sim configuration driver
  3. *
  4. * Copyright (C) 2020 Arkmicro Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. /* #define VERBOSE_DEBUG */
  12. #include <linux/slab.h>
  13. #include <linux/kernel.h>
  14. #include <linux/device.h>
  15. #include <linux/module.h>
  16. #include <net/sock.h>
  17. #include <linux/netlink.h>
  18. #include <linux/usb/composite.h>
  19. #include <linux/usb/functionfs.h>
  20. #include <net/sock.h>
  21. #include <linux/netlink.h>
  22. #define NETLINK_USB_DEV_READY 26
  23. static struct sock *netlink_handle;
  24. int usb_dev_ready_notify(int is_ready)
  25. {
  26. struct sk_buff *nl_skb;
  27. struct nlmsghdr *nlh;
  28. char *buf_on = "connected";
  29. char *buf_off = "disconnected";
  30. char *buf_hid_ready = "hidready";
  31. char *buf = NULL;
  32. int len = 0;
  33. int ret;
  34. if (!netlink_handle) {
  35. }
  36. if (is_ready == 1) {
  37. buf = buf_on;printk("@%s:%d %s\n", __func__, __LINE__, buf_on);
  38. } else if (is_ready == 2) {
  39. buf = buf_hid_ready;printk("%s:%d %s\n", __func__, __LINE__, buf);
  40. } else {
  41. buf = buf_off;printk("%s:%d %s\n", __func__, __LINE__, buf_off);
  42. }
  43. len = strlen(buf) + 1;
  44. nl_skb = nlmsg_new(len, GFP_ATOMIC);
  45. if(!nl_skb) {
  46. printk("netlink_alloc_skb error\n");
  47. return -1;
  48. }
  49. nlh = nlmsg_put(nl_skb, 0, 0, NETLINK_USB_DEV_READY, len, 0);
  50. if(nlh == NULL) {
  51. printk("nlmsg_put() error\n");
  52. nlmsg_free(nl_skb);
  53. return -1;
  54. }
  55. memcpy(nlmsg_data(nlh), buf, len);
  56. ret = netlink_unicast(netlink_handle, nl_skb, 53, MSG_DONTWAIT);
  57. //exit:
  58. return ret;
  59. }
  60. EXPORT_SYMBOL(usb_dev_ready_notify);
  61. struct f_apple_ptp_sim {
  62. struct usb_function function;
  63. };
  64. struct f_apple_ptp_sim_opts {
  65. struct usb_function_instance func_inst;
  66. struct mutex lock;
  67. int refcnt;
  68. unsigned iso_qlen;
  69. };
  70. static inline struct f_apple_ptp_sim *func_to_apple_ptp_sim(struct usb_function *f)
  71. {
  72. return container_of(f, struct f_apple_ptp_sim, function);
  73. }
  74. static struct usb_interface_descriptor apple_ptp_sim_intf = {
  75. .bLength = sizeof apple_ptp_sim_intf,
  76. .bDescriptorType = USB_DT_INTERFACE,
  77. .bNumEndpoints = 3,
  78. .bInterfaceClass = USB_CLASS_STILL_IMAGE,
  79. .bInterfaceSubClass = 1,
  80. .bInterfaceProtocol = 1,
  81. /* .iInterface = DYNAMIC */
  82. };
  83. /* full speed support: */
  84. static struct usb_endpoint_descriptor fs_apple_ptp_sim_source_desc = {
  85. .bLength = USB_DT_ENDPOINT_SIZE,
  86. .bDescriptorType = USB_DT_ENDPOINT,
  87. .bEndpointAddress = USB_DIR_IN,
  88. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  89. };
  90. static struct usb_endpoint_descriptor fs_apple_ptp_sim_sink_desc = {
  91. .bLength = USB_DT_ENDPOINT_SIZE,
  92. .bDescriptorType = USB_DT_ENDPOINT,
  93. .bEndpointAddress = USB_DIR_OUT,
  94. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  95. };
  96. static struct usb_endpoint_descriptor fs_apple_ptp_sim_intr_desc = {
  97. .bLength = USB_DT_ENDPOINT_SIZE,
  98. .bDescriptorType = USB_DT_ENDPOINT,
  99. .bEndpointAddress = USB_DIR_IN,
  100. .bmAttributes = USB_ENDPOINT_XFER_INT,
  101. .bInterval = 10,
  102. };
  103. static struct usb_descriptor_header *fs_apple_ptp_sim_descs[] = {
  104. (struct usb_descriptor_header *) &apple_ptp_sim_intf,
  105. (struct usb_descriptor_header *) &fs_apple_ptp_sim_sink_desc,
  106. (struct usb_descriptor_header *) &fs_apple_ptp_sim_source_desc,
  107. (struct usb_descriptor_header *) &fs_apple_ptp_sim_intr_desc,
  108. NULL,
  109. };
  110. /* high speed support: */
  111. static struct usb_endpoint_descriptor hs_apple_ptp_sim_source_desc = {
  112. .bLength = USB_DT_ENDPOINT_SIZE,
  113. .bDescriptorType = USB_DT_ENDPOINT,
  114. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  115. .wMaxPacketSize = cpu_to_le16(512),
  116. };
  117. static struct usb_endpoint_descriptor hs_apple_ptp_sim_sink_desc = {
  118. .bLength = USB_DT_ENDPOINT_SIZE,
  119. .bDescriptorType = USB_DT_ENDPOINT,
  120. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  121. .wMaxPacketSize = cpu_to_le16(512),
  122. };
  123. static struct usb_endpoint_descriptor hs_apple_ptp_sim_intr_desc = {
  124. .bLength = USB_DT_ENDPOINT_SIZE,
  125. .bDescriptorType = USB_DT_ENDPOINT,
  126. .bmAttributes = USB_ENDPOINT_XFER_INT,
  127. .wMaxPacketSize = cpu_to_le16(64),
  128. .bInterval = 10,
  129. };
  130. static struct usb_descriptor_header *hs_apple_ptp_sim_descs[] = {
  131. (struct usb_descriptor_header *) &apple_ptp_sim_intf,
  132. (struct usb_descriptor_header *) &hs_apple_ptp_sim_sink_desc,
  133. (struct usb_descriptor_header *) &hs_apple_ptp_sim_source_desc,
  134. (struct usb_descriptor_header *) &hs_apple_ptp_sim_intr_desc,
  135. NULL,
  136. };
  137. /* super speed support: */
  138. static struct usb_endpoint_descriptor ss_apple_ptp_sim_source_desc = {
  139. .bLength = USB_DT_ENDPOINT_SIZE,
  140. .bDescriptorType = USB_DT_ENDPOINT,
  141. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  142. .wMaxPacketSize = cpu_to_le16(1024),
  143. };
  144. static struct usb_ss_ep_comp_descriptor ss_apple_ptp_sim_source_comp_desc = {
  145. .bLength = USB_DT_SS_EP_COMP_SIZE,
  146. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  147. .bMaxBurst = 0,
  148. .bmAttributes = 0,
  149. .wBytesPerInterval = 0,
  150. };
  151. static struct usb_endpoint_descriptor ss_apple_ptp_sim_sink_desc = {
  152. .bLength = USB_DT_ENDPOINT_SIZE,
  153. .bDescriptorType = USB_DT_ENDPOINT,
  154. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  155. .wMaxPacketSize = cpu_to_le16(1024),
  156. };
  157. static struct usb_ss_ep_comp_descriptor ss_apple_ptp_sim_sink_comp_desc = {
  158. .bLength = USB_DT_SS_EP_COMP_SIZE,
  159. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  160. .bMaxBurst = 0,
  161. .bmAttributes = 0,
  162. .wBytesPerInterval = 0,
  163. };
  164. static struct usb_descriptor_header *ss_apple_ptp_sim_descs[] = {
  165. (struct usb_descriptor_header *) &apple_ptp_sim_intf,
  166. (struct usb_descriptor_header *) &ss_apple_ptp_sim_sink_desc,
  167. (struct usb_descriptor_header *) &ss_apple_ptp_sim_sink_comp_desc,
  168. (struct usb_descriptor_header *) &ss_apple_ptp_sim_source_desc,
  169. (struct usb_descriptor_header *) &ss_apple_ptp_sim_source_comp_desc,
  170. NULL,
  171. };
  172. #define FUNC_PTP_IDX 0
  173. static struct usb_string apple_ptp_sim_string_defs[] = {
  174. [FUNC_PTP_IDX].s = "PTP",
  175. {},
  176. };
  177. static struct usb_gadget_strings apple_ptp_sim_string_table = {
  178. .language = 0x0409, /* en-US */
  179. .strings = apple_ptp_sim_string_defs,
  180. };
  181. static struct usb_gadget_strings *apple_ptp_sim_strings[] = {
  182. &apple_ptp_sim_string_table,
  183. NULL,
  184. };
  185. static int apple_ptp_sim_bind(struct usb_configuration *c, struct usb_function *f)
  186. {
  187. int id;
  188. struct usb_string *us;
  189. printk("%s:%d\n", __func__, __LINE__);
  190. /* maybe allocate device-global string IDs, and patch descriptors */
  191. us = usb_gstrings_attach(c->cdev, apple_ptp_sim_strings,
  192. ARRAY_SIZE(apple_ptp_sim_string_defs));
  193. if (IS_ERR(us))
  194. return PTR_ERR(us);
  195. apple_ptp_sim_intf.iInterface = us[FUNC_PTP_IDX].id;
  196. /* allocate interface ID(s) */
  197. id = usb_interface_id(c, f);
  198. if (id < 0)
  199. return id;
  200. apple_ptp_sim_intf.bInterfaceNumber = id;
  201. /* support high speed hardware */
  202. if (gadget_is_dualspeed(c->cdev->gadget)) {
  203. hs_apple_ptp_sim_source_desc.bEndpointAddress = 0x81;
  204. hs_apple_ptp_sim_sink_desc.bEndpointAddress = 0x02;
  205. hs_apple_ptp_sim_intr_desc.bEndpointAddress = 0x83;
  206. f->hs_descriptors = hs_apple_ptp_sim_descs;
  207. }
  208. /* support super speed hardware */
  209. if (gadget_is_superspeed(c->cdev->gadget)) {
  210. ss_apple_ptp_sim_source_desc.bEndpointAddress =
  211. fs_apple_ptp_sim_source_desc.bEndpointAddress;
  212. ss_apple_ptp_sim_sink_desc.bEndpointAddress =
  213. fs_apple_ptp_sim_sink_desc.bEndpointAddress;
  214. f->ss_descriptors = ss_apple_ptp_sim_descs;
  215. }
  216. return 0;
  217. }
  218. static void apple_ptp_sim_unbind(struct usb_configuration *c, struct usb_function *f)
  219. {printk("%s:%d\n", __func__, __LINE__);
  220. (void)f;
  221. (void)c;
  222. }
  223. static int apple_ptp_sim_set_alt(struct usb_function *f,
  224. unsigned intf, unsigned alt)
  225. {printk("%s:%d\n", __func__, __LINE__);
  226. (void)f;
  227. (void)intf;
  228. (void)alt;
  229. return 0;
  230. }
  231. static void apple_ptp_sim_disable(struct usb_function *f)
  232. {printk("%s:%d\n", __func__, __LINE__);
  233. (void)f;
  234. }
  235. static bool apple_ptp_sim_req_match(struct usb_function *f,
  236. const struct usb_ctrlrequest *ctrl,
  237. bool config0)
  238. {
  239. #if 0
  240. u16 w_index = le16_to_cpu(ctrl->wIndex);
  241. u16 w_value = le16_to_cpu(ctrl->wValue);
  242. u16 w_length = le16_to_cpu(ctrl->wLength);
  243. printk("@%s:%d\n", __func__, __LINE__);
  244. if ((ctrl->bRequestType & USB_RECIP_MASK) != USB_RECIP_INTERFACE ||
  245. (ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
  246. return false;
  247. printk("%s:%d\n", __func__, __LINE__);
  248. switch (ctrl->bRequest) {
  249. case 0x40:
  250. if (!w_value && w_length == 1 &&
  251. (USB_DIR_IN & ctrl->bRequestType))
  252. break;
  253. return false;
  254. case 0xc0:
  255. if (!w_value && !w_length &&
  256. !(USB_DIR_IN & ctrl->bRequestType))
  257. break;
  258. /* fall through */
  259. default:
  260. return false;
  261. }
  262. #else
  263. (void)f;
  264. (void)ctrl;
  265. (void)config0;
  266. #endif
  267. return true;
  268. }
  269. static int apple_ptp_sim_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  270. {
  271. struct usb_composite_dev *cdev = f->config->cdev;
  272. struct usb_request *req = cdev->req;
  273. int value = -EOPNOTSUPP;
  274. u16 w_index = le16_to_cpu(ctrl->wIndex);
  275. u16 w_value = le16_to_cpu(ctrl->wValue);
  276. u16 w_length = le16_to_cpu(ctrl->wLength);
  277. char result[4] = {0x01, 0x00, 0x00, 0x00};
  278. printk("%s-->bRequestType=%02x bRequest=%02x\n", __func__, ctrl->bRequestType, ctrl->bRequest);
  279. switch (ctrl->bRequestType) {
  280. case 0x40 :
  281. printk("%s %x-->receive switch ctrl\n", __func__, ctrl->bRequest);
  282. if (ctrl->bRequest != 0x51)
  283. break;
  284. value = 0;
  285. usb_dev_ready_notify(1);
  286. break;
  287. case 0xc0 :
  288. printk("%s %x-->receive query ctrl\n", __func__, ctrl->bRequest);
  289. if (ctrl->bRequest != 0x53)
  290. break;
  291. value = 4;
  292. memcpy(req->buf, result, value);
  293. break;
  294. default:
  295. value = 0;
  296. DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  297. ctrl->bRequestType, ctrl->bRequest,
  298. w_value, w_index, w_length);
  299. }
  300. if (value >= 0) {
  301. req->zero = 0;
  302. req->length = value;
  303. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  304. if (value < 0)
  305. ERROR(cdev, "ncm req %02x.%02x response err %d\n",
  306. ctrl->bRequestType, ctrl->bRequest,
  307. value);
  308. }
  309. return value;
  310. }
  311. static void apple_ptp_sim_free_func(struct usb_function *f)
  312. {
  313. struct f_apple_ptp_sim_opts *opts;
  314. opts = container_of(f->fi, struct f_apple_ptp_sim_opts, func_inst);
  315. mutex_lock(&opts->lock);
  316. opts->refcnt--;
  317. mutex_unlock(&opts->lock);
  318. printk("%s:%d\n", __func__, __LINE__);
  319. kfree(func_to_apple_ptp_sim(f));
  320. }
  321. static inline struct f_apple_ptp_sim_opts *to_f_apple_ptp_sim_opts(struct config_item *item)
  322. {
  323. return container_of(to_config_group(item), struct f_apple_ptp_sim_opts,
  324. func_inst.group);
  325. }
  326. static ssize_t f_apple_ptp_sim_opts_iso_qlen_show(struct config_item *item, char *page)
  327. {
  328. struct f_apple_ptp_sim_opts *opts = to_f_apple_ptp_sim_opts(item);
  329. int result;
  330. mutex_lock(&opts->lock);
  331. result = sprintf(page, "%u\n", opts->iso_qlen);
  332. mutex_unlock(&opts->lock);
  333. return result;
  334. }
  335. static ssize_t f_apple_ptp_sim_opts_iso_qlen_store(struct config_item *item,
  336. const char *page, size_t len)
  337. {
  338. struct f_apple_ptp_sim_opts *opts = to_f_apple_ptp_sim_opts(item);
  339. int ret;
  340. u32 num;
  341. mutex_lock(&opts->lock);
  342. if (opts->refcnt) {
  343. ret = -EBUSY;
  344. goto end;
  345. }
  346. ret = kstrtou32(page, 0, &num);
  347. if (ret)
  348. goto end;
  349. opts->iso_qlen = num;
  350. ret = len;
  351. end:
  352. mutex_unlock(&opts->lock);
  353. return ret;
  354. }
  355. static void apple_ptp_sim_attr_release(struct config_item *item)
  356. {
  357. struct f_apple_ptp_sim_opts *ss_opts = to_f_apple_ptp_sim_opts(item);
  358. usb_put_function_instance(&ss_opts->func_inst);
  359. }
  360. static struct configfs_item_operations apple_ptp_sim_item_ops = {
  361. .release = apple_ptp_sim_attr_release,
  362. };
  363. CONFIGFS_ATTR(f_apple_ptp_sim_opts_, iso_qlen);
  364. static struct configfs_attribute *apple_ptp_sim_attrs[] = {
  365. &f_apple_ptp_sim_opts_attr_iso_qlen,
  366. NULL,
  367. };
  368. static struct config_item_type apple_ptp_sim_func_type = {
  369. .ct_item_ops = &apple_ptp_sim_item_ops,
  370. .ct_attrs = apple_ptp_sim_attrs,
  371. .ct_owner = THIS_MODULE,
  372. };
  373. static void apple_ptp_sim_free_inst(struct usb_function_instance *f)
  374. {
  375. struct f_apple_ptp_sim_opts *opts;
  376. printk("%s:%d\n", __func__, __LINE__);
  377. opts = container_of(f, struct f_apple_ptp_sim_opts, func_inst);
  378. kfree(opts);
  379. }
  380. static struct usb_function_instance *apple_ptp_sim_alloc_inst(void)
  381. {
  382. struct f_apple_ptp_sim_opts *opts = NULL;
  383. printk("%s:%d\n", __func__, __LINE__);
  384. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  385. if (!opts)
  386. return ERR_PTR(-ENOMEM);
  387. mutex_init(&opts->lock);
  388. opts->func_inst.free_func_inst = apple_ptp_sim_free_inst;
  389. config_group_init_type_name(&opts->func_inst.group, "", &apple_ptp_sim_func_type);
  390. printk("%s:%d\n", __func__, __LINE__);
  391. return &opts->func_inst;
  392. }
  393. static struct netlink_kernel_cfg cfg = {
  394. .groups = 1,
  395. .flags = NL_CFG_F_NONROOT_RECV,
  396. };
  397. static struct usb_function *apple_ptp_sim_alloc(struct usb_function_instance *fi)
  398. {
  399. struct f_apple_ptp_sim *pctx = NULL;
  400. struct f_apple_ptp_sim_opts *opts = NULL;
  401. pctx = kzalloc(sizeof *pctx, GFP_KERNEL);
  402. if (!pctx)
  403. return NULL;
  404. printk("%s:%d\n", __func__, __LINE__);
  405. opts = container_of(fi, struct f_apple_ptp_sim_opts, func_inst);
  406. mutex_lock(&opts->lock);
  407. opts->refcnt++;
  408. mutex_unlock(&opts->lock);
  409. pctx->function.name = "ptp";
  410. pctx->function.fs_descriptors = fs_apple_ptp_sim_descs;
  411. pctx->function.bind = apple_ptp_sim_bind;
  412. pctx->function.unbind = apple_ptp_sim_unbind;
  413. pctx->function.set_alt = apple_ptp_sim_set_alt;
  414. pctx->function.disable = apple_ptp_sim_disable;
  415. pctx->function.setup = apple_ptp_sim_setup;
  416. pctx->function.req_match = apple_ptp_sim_req_match;
  417. pctx->function.free_func = apple_ptp_sim_free_func;
  418. if (NULL == netlink_handle) {
  419. printk("##alloc usb netlink sock\n");
  420. netlink_handle = netlink_kernel_create(&init_net, NETLINK_USB_DEV_READY, &cfg);
  421. if(!netlink_handle) {
  422. printk(KERN_ERR "can not create a usb dev ready netlink socket!\n");
  423. kfree(pctx);
  424. return NULL;
  425. }
  426. }
  427. printk("%s:%d\n", __func__, __LINE__);
  428. return &pctx->function;
  429. }
  430. DECLARE_USB_FUNCTION_INIT(ptp, apple_ptp_sim_alloc_inst, apple_ptp_sim_alloc);
  431. MODULE_LICENSE("GPL");
  432. MODULE_AUTHOR("art");