u_ncm.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * File Name : u_ncm.c
  3. *
  4. * ncm utilities for composite USB gadgets.
  5. * This utilitie can support to connect head unit for mirror link
  6. *
  7. * Copyright (C) 2011 Samsung Electronics
  8. * Author: SoonYong, Cho <soonyong.cho@samsung.com>
  9. *
  10. * This software is licensed under the terms of the GNU General Public
  11. * License version 2, as published by the Free Software Foundation, and
  12. * may be copied, distributed, and modified under those terms.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include "f_ncm.c"
  21. #include <linux/miscdevice.h>
  22. #include <linux/module.h>
  23. /* Support dynamic tethering mode.
  24. * if ncm_connect is true, device is received vendor specific request
  25. * from head unit.
  26. */
  27. struct ncm_dev {
  28. struct work_struct work;
  29. };
  30. static const char mirrorlink_shortname[] = "usb_ncm";
  31. /* Create misc driver for Mirror Link cmd */
  32. static struct miscdevice mirrorlink_device = {
  33. .minor = MISC_DYNAMIC_MINOR,
  34. .name = mirrorlink_shortname,
  35. //.fops = &mirrorlink_fops,
  36. };
  37. static bool ncm_connect;
  38. /* terminal version using vendor specific request */
  39. u16 terminal_mode_version;
  40. u16 terminal_mode_vendor_id;
  41. struct ncm_function_config {
  42. u8 ethaddr[ETH_ALEN];
  43. };
  44. static struct ncm_dev *_ncm_dev;
  45. static void ncm_work(struct work_struct *data)
  46. {
  47. char *ncm_start[2] = { "NCM_DEVICE=START", NULL };
  48. char *ncm_release[2] = { "NCM_DEVICE=RELEASE", NULL };
  49. char **uevent_envp = NULL;
  50. printk(KERN_DEBUG "usb: %s ncm_connect=%d\n", __func__, ncm_connect);
  51. if ( ncm_connect==true )
  52. uevent_envp = ncm_start;
  53. else
  54. uevent_envp = ncm_release;
  55. kobject_uevent_env(&mirrorlink_device.this_device->kobj, KOBJ_CHANGE, uevent_envp);
  56. }
  57. static int ncm_function_init(struct android_usb_function *f,
  58. struct usb_composite_dev *cdev)
  59. {
  60. struct ncm_dev *dev;
  61. int ret=0;
  62. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  63. if (!dev)
  64. return -ENOMEM;
  65. f->config = kzalloc(sizeof(struct ncm_function_config), GFP_KERNEL);
  66. if (!f->config)
  67. {
  68. kfree(dev);
  69. return -ENOMEM;
  70. }
  71. INIT_WORK(&dev->work, ncm_work);
  72. _ncm_dev = dev;
  73. ret = misc_register(&mirrorlink_device);
  74. if (ret)
  75. printk("usb: %s - usb_ncm misc driver fail \n",__func__);
  76. return 0;
  77. }
  78. static void ncm_function_cleanup(struct android_usb_function *f)
  79. {
  80. misc_deregister(&mirrorlink_device);
  81. kfree(_ncm_dev);
  82. kfree(f->config);
  83. f->config = NULL;
  84. _ncm_dev = NULL;
  85. }
  86. static int ncm_function_bind_config(struct android_usb_function *f,
  87. struct usb_configuration *c)
  88. {
  89. int ret;
  90. int i;
  91. char *src;
  92. struct ncm_function_config *ncm = f->config;
  93. if (!ncm) {
  94. pr_err("%s: ncm_pdata\n", __func__);
  95. return -1;
  96. }
  97. ncm = f->config;
  98. if (!f->config)
  99. return -ENOMEM;
  100. for (i = 0; i < ETH_ALEN; i++)
  101. ncm->ethaddr[i] = 0;
  102. /* create a fake MAC address from our serial number.
  103. * first byte is 0x02 to signify locally administered.
  104. */
  105. ncm->ethaddr[0] = 0x02;
  106. src = serial_string;
  107. for (i = 0; (i < 256) && *src; i++) {
  108. /* XOR the USB serial across the remaining bytes */
  109. ncm->ethaddr[i % (ETH_ALEN - 1) + 1] ^= *src++;
  110. }
  111. printk(KERN_DEBUG "usb: %s MAC:%02X:%02X:%02X:%02X:%02X:%02X\n",
  112. __func__, ncm->ethaddr[0], ncm->ethaddr[1],
  113. ncm->ethaddr[2], ncm->ethaddr[3], ncm->ethaddr[4],
  114. ncm->ethaddr[5]);
  115. printk(KERN_DEBUG "usb: %s before MAC:%02X:%02X:%02X:%02X:%02X:%02X\n",
  116. __func__, ncm->ethaddr[0], ncm->ethaddr[1],
  117. ncm->ethaddr[2], ncm->ethaddr[3], ncm->ethaddr[4],
  118. ncm->ethaddr[5]);
  119. /* we have to use trick.
  120. * rndis name will be used for ethernet interface name.
  121. */
  122. ret = gether_setup_name(c->cdev->gadget, ncm->ethaddr, "ncm");
  123. printk(KERN_DEBUG "usb: %s after MAC:%02X:%02X:%02X:%02X:%02X:%02X\n",
  124. __func__, ncm->ethaddr[0], ncm->ethaddr[1],
  125. ncm->ethaddr[2], ncm->ethaddr[3], ncm->ethaddr[4],
  126. ncm->ethaddr[5]);
  127. if (ret) {
  128. pr_err("%s: gether_setup failed\n", __func__);
  129. return ret;
  130. }
  131. return ncm_bind_config(c, ncm->ethaddr);
  132. }
  133. static void ncm_function_unbind_config(struct android_usb_function *f,
  134. struct usb_configuration *c)
  135. {
  136. gether_cleanup();
  137. }
  138. static struct android_usb_function ncm_function = {
  139. .name = "ncm",
  140. .init = ncm_function_init,
  141. .cleanup = ncm_function_cleanup,
  142. .bind_config = ncm_function_bind_config,
  143. .unbind_config = ncm_function_unbind_config,
  144. };
  145. bool is_ncm_ready(char *name)
  146. {
  147. /* Enable ncm function */
  148. if (!strcmp(name, "rndis") || !strcmp(name, "ncm")) {
  149. if (ncm_connect) {
  150. printk(KERN_DEBUG "usb: %s ncm ready (%s)\n",
  151. __func__, name);
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. void set_ncm_device_descriptor(struct usb_device_descriptor *desc)
  158. {
  159. desc->idProduct = 0x685d;
  160. desc->bDeviceClass = USB_CLASS_COMM;
  161. printk(KERN_DEBUG "usb: %s idProduct=0x%x, DeviceClass=0x%x\n",
  162. __func__, desc->idProduct, desc->bDeviceClass);
  163. }
  164. void set_ncm_ready(bool ready)
  165. {
  166. if (ready != ncm_connect)
  167. {
  168. printk(KERN_DEBUG "usb: %s old status=%d, new status=%d\n",
  169. __func__, ncm_connect, ready);
  170. ncm_connect = ready;
  171. schedule_work(&_ncm_dev->work);
  172. }
  173. else
  174. ncm_connect = ready;
  175. if (ready == false) {
  176. terminal_mode_version = 0;
  177. terminal_mode_vendor_id = 0;
  178. }
  179. }
  180. EXPORT_SYMBOL(set_ncm_ready);
  181. static ssize_t terminal_version_show(struct device *dev,
  182. struct device_attribute *attr, char *buf)
  183. {
  184. int ret;
  185. ret = sprintf(buf, "major %x minor %x vendor %x\n",
  186. terminal_mode_version & 0xff,
  187. (terminal_mode_version >> 8 & 0xff),
  188. terminal_mode_vendor_id);
  189. if(terminal_mode_version)
  190. printk(KERN_DEBUG "usb: %s terminal_mode %s\n", __func__, buf);
  191. return ret;
  192. }
  193. static ssize_t terminal_version_store(struct device *dev,
  194. struct device_attribute *attr, const char *buf, size_t size)
  195. {
  196. int value;
  197. sscanf(buf, "%x", &value);
  198. terminal_mode_version = (u16)value;
  199. printk(KERN_DEBUG "usb: %s buf=%s\n", __func__, buf);
  200. /* only set ncm ready when terminal verision value is not zero */
  201. if(value)
  202. set_ncm_ready(true);
  203. else
  204. set_ncm_ready(false);
  205. return size;
  206. }
  207. static DEVICE_ATTR(terminal_version, S_IRUGO | S_IWUSR,
  208. terminal_version_show, terminal_version_store);
  209. static int create_terminal_attribute(struct device **pdev)
  210. {
  211. int err;
  212. if (IS_ERR(*pdev)) {
  213. printk(KERN_DEBUG "usb: %s error pdev(%pK)\n",
  214. __func__, *pdev);
  215. return PTR_ERR(*pdev);
  216. }
  217. err = device_create_file(*pdev, &dev_attr_terminal_version);
  218. if (err) {
  219. printk(KERN_DEBUG "usb: %s failed to create attr\n",
  220. __func__);
  221. return err;
  222. }
  223. return 0;
  224. }
  225. static int terminal_ctrl_request(struct usb_composite_dev *cdev,
  226. const struct usb_ctrlrequest *ctrl)
  227. {
  228. int value = -EOPNOTSUPP;
  229. u16 w_index = le16_to_cpu(ctrl->wIndex);
  230. u16 w_value = le16_to_cpu(ctrl->wValue);
  231. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
  232. /* Handle Terminal mode request */
  233. if (ctrl->bRequest == 0xf0) {
  234. terminal_mode_version = w_value;
  235. terminal_mode_vendor_id = w_index;
  236. set_ncm_ready(true);
  237. printk(KERN_DEBUG "usb: %s ver=0x%x vendor_id=0x%x\n",
  238. __func__, terminal_mode_version,
  239. terminal_mode_vendor_id);
  240. value = 0;
  241. }
  242. }
  243. /* respond ZLP */
  244. if (value >= 0) {
  245. int rc;
  246. cdev->req->zero = 0;
  247. cdev->req->length = value;
  248. rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
  249. if (rc < 0)
  250. printk(KERN_DEBUG "usb: %s failed usb_ep_queue\n",
  251. __func__);
  252. }
  253. return value;
  254. }