nokia.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * nokia.c -- Nokia Composite Gadget Driver
  3. *
  4. * Copyright (C) 2008-2010 Nokia Corporation
  5. * Contact: Felipe Balbi <felipe.balbi@nokia.com>
  6. *
  7. * This gadget driver borrows from serial.c which is:
  8. *
  9. * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
  10. * Copyright (C) 2008 by David Brownell
  11. * Copyright (C) 2008 by Nokia Corporation
  12. *
  13. * This software is distributed under the terms of the GNU General
  14. * Public License ("GPL") as published by the Free Software Foundation,
  15. * version 2 of that License.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include "u_serial.h"
  21. #include "u_ether.h"
  22. #include "u_phonet.h"
  23. #include "gadget_chips.h"
  24. /* Defines */
  25. #define NOKIA_VERSION_NUM 0x0211
  26. #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
  27. /*-------------------------------------------------------------------------*/
  28. /*
  29. * Kbuild is not very cooperative with respect to linking separately
  30. * compiled library objects into one module. So for now we won't use
  31. * separate compilation ... ensuring init/exit sections work to shrink
  32. * the runtime footprint, and giving us at least some parts of what
  33. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  34. */
  35. #define USBF_ECM_INCLUDED
  36. #include "f_ecm.c"
  37. #include "f_phonet.c"
  38. #include "u_ether.h"
  39. /*-------------------------------------------------------------------------*/
  40. USB_GADGET_COMPOSITE_OPTIONS();
  41. USB_ETHERNET_MODULE_PARAMETERS();
  42. #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
  43. #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
  44. /* string IDs are assigned dynamically */
  45. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  46. static char manufacturer_nokia[] = "Nokia";
  47. static const char product_nokia[] = NOKIA_LONG_NAME;
  48. static const char description_nokia[] = "PC-Suite Configuration";
  49. static struct usb_string strings_dev[] = {
  50. [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
  51. [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
  52. [USB_GADGET_SERIAL_IDX].s = "",
  53. [STRING_DESCRIPTION_IDX].s = description_nokia,
  54. { } /* end of list */
  55. };
  56. static struct usb_gadget_strings stringtab_dev = {
  57. .language = 0x0409, /* en-us */
  58. .strings = strings_dev,
  59. };
  60. static struct usb_gadget_strings *dev_strings[] = {
  61. &stringtab_dev,
  62. NULL,
  63. };
  64. static struct usb_device_descriptor device_desc = {
  65. .bLength = USB_DT_DEVICE_SIZE,
  66. .bDescriptorType = USB_DT_DEVICE,
  67. .bcdUSB = __constant_cpu_to_le16(0x0200),
  68. .bDeviceClass = USB_CLASS_COMM,
  69. .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
  70. .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
  71. .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
  72. /* .iManufacturer = DYNAMIC */
  73. /* .iProduct = DYNAMIC */
  74. .bNumConfigurations = 1,
  75. };
  76. /*-------------------------------------------------------------------------*/
  77. /* Module */
  78. MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
  79. MODULE_AUTHOR("Felipe Balbi");
  80. MODULE_LICENSE("GPL");
  81. /*-------------------------------------------------------------------------*/
  82. static struct usb_function *f_acm_cfg1;
  83. static struct usb_function *f_acm_cfg2;
  84. static u8 host_mac[ETH_ALEN];
  85. static struct usb_function *f_obex1_cfg1;
  86. static struct usb_function *f_obex2_cfg1;
  87. static struct usb_function *f_obex1_cfg2;
  88. static struct usb_function *f_obex2_cfg2;
  89. static struct eth_dev *the_dev;
  90. static struct net_device *phonet_dev;
  91. static struct usb_configuration nokia_config_500ma_driver = {
  92. .label = "Bus Powered",
  93. .bConfigurationValue = 1,
  94. /* .iConfiguration = DYNAMIC */
  95. .bmAttributes = USB_CONFIG_ATT_ONE,
  96. .MaxPower = 500,
  97. };
  98. static struct usb_configuration nokia_config_100ma_driver = {
  99. .label = "Self Powered",
  100. .bConfigurationValue = 2,
  101. /* .iConfiguration = DYNAMIC */
  102. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  103. .MaxPower = 100,
  104. };
  105. static struct usb_function_instance *fi_acm;
  106. static struct usb_function_instance *fi_obex1;
  107. static struct usb_function_instance *fi_obex2;
  108. static int __init nokia_bind_config(struct usb_configuration *c)
  109. {
  110. struct usb_function *f_acm;
  111. struct usb_function *f_obex1 = NULL;
  112. struct usb_function *f_obex2 = NULL;
  113. int status = 0;
  114. int obex1_stat = 0;
  115. int obex2_stat = 0;
  116. status = phonet_bind_config(c, phonet_dev);
  117. if (status)
  118. pr_debug("could not bind phonet config\n");
  119. if (!IS_ERR(fi_obex1)) {
  120. f_obex1 = usb_get_function(fi_obex1);
  121. if (IS_ERR(f_obex1))
  122. pr_debug("could not get obex function 0\n");
  123. }
  124. if (!IS_ERR(fi_obex2)) {
  125. f_obex2 = usb_get_function(fi_obex2);
  126. if (IS_ERR(f_obex2))
  127. pr_debug("could not get obex function 1\n");
  128. }
  129. f_acm = usb_get_function(fi_acm);
  130. if (IS_ERR(f_acm)) {
  131. status = PTR_ERR(f_acm);
  132. goto err_get_acm;
  133. }
  134. if (!IS_ERR_OR_NULL(f_obex1)) {
  135. obex1_stat = usb_add_function(c, f_obex1);
  136. if (obex1_stat)
  137. pr_debug("could not add obex function 0\n");
  138. }
  139. if (!IS_ERR_OR_NULL(f_obex2)) {
  140. obex2_stat = usb_add_function(c, f_obex2);
  141. if (obex2_stat)
  142. pr_debug("could not add obex function 1\n");
  143. }
  144. status = usb_add_function(c, f_acm);
  145. if (status)
  146. goto err_conf;
  147. status = ecm_bind_config(c, host_mac, the_dev);
  148. if (status) {
  149. pr_debug("could not bind ecm config %d\n", status);
  150. goto err_ecm;
  151. }
  152. if (c == &nokia_config_500ma_driver) {
  153. f_acm_cfg1 = f_acm;
  154. f_obex1_cfg1 = f_obex1;
  155. f_obex2_cfg1 = f_obex2;
  156. } else {
  157. f_acm_cfg2 = f_acm;
  158. f_obex1_cfg2 = f_obex1;
  159. f_obex2_cfg2 = f_obex2;
  160. }
  161. return status;
  162. err_ecm:
  163. usb_remove_function(c, f_acm);
  164. err_conf:
  165. if (!obex2_stat)
  166. usb_remove_function(c, f_obex2);
  167. if (!obex1_stat)
  168. usb_remove_function(c, f_obex1);
  169. usb_put_function(f_acm);
  170. err_get_acm:
  171. if (!IS_ERR_OR_NULL(f_obex2))
  172. usb_put_function(f_obex2);
  173. if (!IS_ERR_OR_NULL(f_obex1))
  174. usb_put_function(f_obex1);
  175. return status;
  176. }
  177. static int __init nokia_bind(struct usb_composite_dev *cdev)
  178. {
  179. struct usb_gadget *gadget = cdev->gadget;
  180. int status;
  181. phonet_dev = gphonet_setup(cdev->gadget);
  182. if (IS_ERR(phonet_dev)) {
  183. status = PTR_ERR(phonet_dev);
  184. goto err_phonet;
  185. }
  186. the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac,
  187. qmult);
  188. if (IS_ERR(the_dev)) {
  189. status = PTR_ERR(the_dev);
  190. goto err_ether;
  191. }
  192. status = usb_string_ids_tab(cdev, strings_dev);
  193. if (status < 0)
  194. goto err_usb;
  195. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  196. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  197. status = strings_dev[STRING_DESCRIPTION_IDX].id;
  198. nokia_config_500ma_driver.iConfiguration = status;
  199. nokia_config_100ma_driver.iConfiguration = status;
  200. if (!gadget_supports_altsettings(gadget))
  201. goto err_usb;
  202. fi_obex1 = usb_get_function_instance("obex");
  203. if (IS_ERR(fi_obex1))
  204. pr_debug("could not find obex function 1\n");
  205. fi_obex2 = usb_get_function_instance("obex");
  206. if (IS_ERR(fi_obex2))
  207. pr_debug("could not find obex function 2\n");
  208. fi_acm = usb_get_function_instance("acm");
  209. if (IS_ERR(fi_acm))
  210. goto err_obex2_inst;
  211. /* finally register the configuration */
  212. status = usb_add_config(cdev, &nokia_config_500ma_driver,
  213. nokia_bind_config);
  214. if (status < 0)
  215. goto err_acm_inst;
  216. status = usb_add_config(cdev, &nokia_config_100ma_driver,
  217. nokia_bind_config);
  218. if (status < 0)
  219. goto err_put_cfg1;
  220. usb_composite_overwrite_options(cdev, &coverwrite);
  221. dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
  222. return 0;
  223. err_put_cfg1:
  224. usb_put_function(f_acm_cfg1);
  225. if (!IS_ERR_OR_NULL(f_obex1_cfg1))
  226. usb_put_function(f_obex1_cfg1);
  227. if (!IS_ERR_OR_NULL(f_obex2_cfg1))
  228. usb_put_function(f_obex2_cfg1);
  229. err_acm_inst:
  230. usb_put_function_instance(fi_acm);
  231. err_obex2_inst:
  232. if (!IS_ERR(fi_obex2))
  233. usb_put_function_instance(fi_obex2);
  234. if (!IS_ERR(fi_obex1))
  235. usb_put_function_instance(fi_obex1);
  236. err_usb:
  237. gether_cleanup(the_dev);
  238. err_ether:
  239. gphonet_cleanup(phonet_dev);
  240. err_phonet:
  241. return status;
  242. }
  243. static int __exit nokia_unbind(struct usb_composite_dev *cdev)
  244. {
  245. if (!IS_ERR_OR_NULL(f_obex1_cfg2))
  246. usb_put_function(f_obex1_cfg2);
  247. if (!IS_ERR_OR_NULL(f_obex2_cfg2))
  248. usb_put_function(f_obex2_cfg2);
  249. if (!IS_ERR_OR_NULL(f_obex1_cfg1))
  250. usb_put_function(f_obex1_cfg1);
  251. if (!IS_ERR_OR_NULL(f_obex2_cfg1))
  252. usb_put_function(f_obex2_cfg1);
  253. usb_put_function(f_acm_cfg1);
  254. usb_put_function(f_acm_cfg2);
  255. if (!IS_ERR(fi_obex1))
  256. usb_put_function_instance(fi_obex1);
  257. if (!IS_ERR(fi_obex2))
  258. usb_put_function_instance(fi_obex2);
  259. usb_put_function_instance(fi_acm);
  260. gphonet_cleanup(phonet_dev);
  261. gether_cleanup(the_dev);
  262. return 0;
  263. }
  264. static __refdata struct usb_composite_driver nokia_driver = {
  265. .name = "g_nokia",
  266. .dev = &device_desc,
  267. .strings = dev_strings,
  268. .max_speed = USB_SPEED_HIGH,
  269. .bind = nokia_bind,
  270. .unbind = __exit_p(nokia_unbind),
  271. };
  272. static int __init nokia_init(void)
  273. {
  274. return usb_composite_probe(&nokia_driver);
  275. }
  276. module_init(nokia_init);
  277. static void __exit nokia_cleanup(void)
  278. {
  279. usb_composite_unregister(&nokia_driver);
  280. }
  281. module_exit(nokia_cleanup);