nokia.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 "u_ecm.h"
  24. #include "gadget_chips.h"
  25. /* Defines */
  26. #define NOKIA_VERSION_NUM 0x0211
  27. #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
  28. USB_GADGET_COMPOSITE_OPTIONS();
  29. USB_ETHERNET_MODULE_PARAMETERS();
  30. #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
  31. #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
  32. /* string IDs are assigned dynamically */
  33. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  34. static char manufacturer_nokia[] = "Nokia";
  35. static const char product_nokia[] = NOKIA_LONG_NAME;
  36. static const char description_nokia[] = "PC-Suite Configuration";
  37. static struct usb_string strings_dev[] = {
  38. [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
  39. [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
  40. [USB_GADGET_SERIAL_IDX].s = "",
  41. [STRING_DESCRIPTION_IDX].s = description_nokia,
  42. { } /* end of list */
  43. };
  44. static struct usb_gadget_strings stringtab_dev = {
  45. .language = 0x0409, /* en-us */
  46. .strings = strings_dev,
  47. };
  48. static struct usb_gadget_strings *dev_strings[] = {
  49. &stringtab_dev,
  50. NULL,
  51. };
  52. static struct usb_device_descriptor device_desc = {
  53. .bLength = USB_DT_DEVICE_SIZE,
  54. .bDescriptorType = USB_DT_DEVICE,
  55. .bcdUSB = __constant_cpu_to_le16(0x0200),
  56. .bDeviceClass = USB_CLASS_COMM,
  57. .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
  58. .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
  59. .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
  60. /* .iManufacturer = DYNAMIC */
  61. /* .iProduct = DYNAMIC */
  62. .bNumConfigurations = 1,
  63. };
  64. /*-------------------------------------------------------------------------*/
  65. /* Module */
  66. MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
  67. MODULE_AUTHOR("Felipe Balbi");
  68. MODULE_LICENSE("GPL");
  69. /*-------------------------------------------------------------------------*/
  70. static struct usb_function *f_acm_cfg1;
  71. static struct usb_function *f_acm_cfg2;
  72. static struct usb_function *f_ecm_cfg1;
  73. static struct usb_function *f_ecm_cfg2;
  74. static struct usb_function *f_obex1_cfg1;
  75. static struct usb_function *f_obex2_cfg1;
  76. static struct usb_function *f_obex1_cfg2;
  77. static struct usb_function *f_obex2_cfg2;
  78. static struct usb_function *f_phonet_cfg1;
  79. static struct usb_function *f_phonet_cfg2;
  80. static struct usb_configuration nokia_config_500ma_driver = {
  81. .label = "Bus Powered",
  82. .bConfigurationValue = 1,
  83. /* .iConfiguration = DYNAMIC */
  84. .bmAttributes = USB_CONFIG_ATT_ONE,
  85. .MaxPower = 500,
  86. };
  87. static struct usb_configuration nokia_config_100ma_driver = {
  88. .label = "Self Powered",
  89. .bConfigurationValue = 2,
  90. /* .iConfiguration = DYNAMIC */
  91. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  92. .MaxPower = 100,
  93. };
  94. static struct usb_function_instance *fi_acm;
  95. static struct usb_function_instance *fi_ecm;
  96. static struct usb_function_instance *fi_obex1;
  97. static struct usb_function_instance *fi_obex2;
  98. static struct usb_function_instance *fi_phonet;
  99. static int __init nokia_bind_config(struct usb_configuration *c)
  100. {
  101. struct usb_function *f_acm;
  102. struct usb_function *f_phonet = NULL;
  103. struct usb_function *f_obex1 = NULL;
  104. struct usb_function *f_ecm;
  105. struct usb_function *f_obex2 = NULL;
  106. int status = 0;
  107. int obex1_stat = 0;
  108. int obex2_stat = 0;
  109. int phonet_stat = 0;
  110. if (!IS_ERR(fi_phonet)) {
  111. f_phonet = usb_get_function(fi_phonet);
  112. if (IS_ERR(f_phonet))
  113. pr_debug("could not get phonet function\n");
  114. }
  115. if (!IS_ERR(fi_obex1)) {
  116. f_obex1 = usb_get_function(fi_obex1);
  117. if (IS_ERR(f_obex1))
  118. pr_debug("could not get obex function 0\n");
  119. }
  120. if (!IS_ERR(fi_obex2)) {
  121. f_obex2 = usb_get_function(fi_obex2);
  122. if (IS_ERR(f_obex2))
  123. pr_debug("could not get obex function 1\n");
  124. }
  125. f_acm = usb_get_function(fi_acm);
  126. if (IS_ERR(f_acm)) {
  127. status = PTR_ERR(f_acm);
  128. goto err_get_acm;
  129. }
  130. f_ecm = usb_get_function(fi_ecm);
  131. if (IS_ERR(f_ecm)) {
  132. status = PTR_ERR(f_ecm);
  133. goto err_get_ecm;
  134. }
  135. if (!IS_ERR_OR_NULL(f_phonet)) {
  136. phonet_stat = usb_add_function(c, f_phonet);
  137. if (phonet_stat)
  138. pr_debug("could not add phonet function\n");
  139. }
  140. if (!IS_ERR_OR_NULL(f_obex1)) {
  141. obex1_stat = usb_add_function(c, f_obex1);
  142. if (obex1_stat)
  143. pr_debug("could not add obex function 0\n");
  144. }
  145. if (!IS_ERR_OR_NULL(f_obex2)) {
  146. obex2_stat = usb_add_function(c, f_obex2);
  147. if (obex2_stat)
  148. pr_debug("could not add obex function 1\n");
  149. }
  150. status = usb_add_function(c, f_acm);
  151. if (status)
  152. goto err_conf;
  153. status = usb_add_function(c, f_ecm);
  154. if (status) {
  155. pr_debug("could not bind ecm config %d\n", status);
  156. goto err_ecm;
  157. }
  158. if (c == &nokia_config_500ma_driver) {
  159. f_acm_cfg1 = f_acm;
  160. f_ecm_cfg1 = f_ecm;
  161. f_phonet_cfg1 = f_phonet;
  162. f_obex1_cfg1 = f_obex1;
  163. f_obex2_cfg1 = f_obex2;
  164. } else {
  165. f_acm_cfg2 = f_acm;
  166. f_ecm_cfg2 = f_ecm;
  167. f_phonet_cfg2 = f_phonet;
  168. f_obex1_cfg2 = f_obex1;
  169. f_obex2_cfg2 = f_obex2;
  170. }
  171. return status;
  172. err_ecm:
  173. usb_remove_function(c, f_acm);
  174. err_conf:
  175. if (!obex2_stat)
  176. usb_remove_function(c, f_obex2);
  177. if (!obex1_stat)
  178. usb_remove_function(c, f_obex1);
  179. if (!phonet_stat)
  180. usb_remove_function(c, f_phonet);
  181. usb_put_function(f_ecm);
  182. err_get_ecm:
  183. usb_put_function(f_acm);
  184. err_get_acm:
  185. if (!IS_ERR_OR_NULL(f_obex2))
  186. usb_put_function(f_obex2);
  187. if (!IS_ERR_OR_NULL(f_obex1))
  188. usb_put_function(f_obex1);
  189. if (!IS_ERR_OR_NULL(f_phonet))
  190. usb_put_function(f_phonet);
  191. return status;
  192. }
  193. static int __init nokia_bind(struct usb_composite_dev *cdev)
  194. {
  195. struct usb_gadget *gadget = cdev->gadget;
  196. int status;
  197. status = usb_string_ids_tab(cdev, strings_dev);
  198. if (status < 0)
  199. goto err_usb;
  200. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  201. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  202. status = strings_dev[STRING_DESCRIPTION_IDX].id;
  203. nokia_config_500ma_driver.iConfiguration = status;
  204. nokia_config_100ma_driver.iConfiguration = status;
  205. if (!gadget_supports_altsettings(gadget)) {
  206. status = -ENODEV;
  207. goto err_usb;
  208. }
  209. fi_phonet = usb_get_function_instance("phonet");
  210. if (IS_ERR(fi_phonet))
  211. pr_debug("could not find phonet function\n");
  212. fi_obex1 = usb_get_function_instance("obex");
  213. if (IS_ERR(fi_obex1))
  214. pr_debug("could not find obex function 1\n");
  215. fi_obex2 = usb_get_function_instance("obex");
  216. if (IS_ERR(fi_obex2))
  217. pr_debug("could not find obex function 2\n");
  218. fi_acm = usb_get_function_instance("acm");
  219. if (IS_ERR(fi_acm)) {
  220. status = PTR_ERR(fi_acm);
  221. goto err_obex2_inst;
  222. }
  223. fi_ecm = usb_get_function_instance("ecm");
  224. if (IS_ERR(fi_ecm)) {
  225. status = PTR_ERR(fi_ecm);
  226. goto err_acm_inst;
  227. }
  228. /* finally register the configuration */
  229. status = usb_add_config(cdev, &nokia_config_500ma_driver,
  230. nokia_bind_config);
  231. if (status < 0)
  232. goto err_ecm_inst;
  233. status = usb_add_config(cdev, &nokia_config_100ma_driver,
  234. nokia_bind_config);
  235. if (status < 0)
  236. goto err_put_cfg1;
  237. usb_composite_overwrite_options(cdev, &coverwrite);
  238. dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
  239. return 0;
  240. err_put_cfg1:
  241. usb_put_function(f_acm_cfg1);
  242. if (!IS_ERR_OR_NULL(f_obex1_cfg1))
  243. usb_put_function(f_obex1_cfg1);
  244. if (!IS_ERR_OR_NULL(f_obex2_cfg1))
  245. usb_put_function(f_obex2_cfg1);
  246. if (!IS_ERR_OR_NULL(f_phonet_cfg1))
  247. usb_put_function(f_phonet_cfg1);
  248. usb_put_function(f_ecm_cfg1);
  249. err_ecm_inst:
  250. usb_put_function_instance(fi_ecm);
  251. err_acm_inst:
  252. usb_put_function_instance(fi_acm);
  253. err_obex2_inst:
  254. if (!IS_ERR(fi_obex2))
  255. usb_put_function_instance(fi_obex2);
  256. if (!IS_ERR(fi_obex1))
  257. usb_put_function_instance(fi_obex1);
  258. if (!IS_ERR(fi_phonet))
  259. usb_put_function_instance(fi_phonet);
  260. err_usb:
  261. return status;
  262. }
  263. static int __exit nokia_unbind(struct usb_composite_dev *cdev)
  264. {
  265. if (!IS_ERR_OR_NULL(f_obex1_cfg2))
  266. usb_put_function(f_obex1_cfg2);
  267. if (!IS_ERR_OR_NULL(f_obex2_cfg2))
  268. usb_put_function(f_obex2_cfg2);
  269. if (!IS_ERR_OR_NULL(f_obex1_cfg1))
  270. usb_put_function(f_obex1_cfg1);
  271. if (!IS_ERR_OR_NULL(f_obex2_cfg1))
  272. usb_put_function(f_obex2_cfg1);
  273. if (!IS_ERR_OR_NULL(f_phonet_cfg1))
  274. usb_put_function(f_phonet_cfg1);
  275. if (!IS_ERR_OR_NULL(f_phonet_cfg2))
  276. usb_put_function(f_phonet_cfg2);
  277. usb_put_function(f_acm_cfg1);
  278. usb_put_function(f_acm_cfg2);
  279. usb_put_function(f_ecm_cfg1);
  280. usb_put_function(f_ecm_cfg2);
  281. usb_put_function_instance(fi_ecm);
  282. if (!IS_ERR(fi_obex2))
  283. usb_put_function_instance(fi_obex2);
  284. if (!IS_ERR(fi_obex1))
  285. usb_put_function_instance(fi_obex1);
  286. if (!IS_ERR(fi_phonet))
  287. usb_put_function_instance(fi_phonet);
  288. usb_put_function_instance(fi_acm);
  289. return 0;
  290. }
  291. static __refdata struct usb_composite_driver nokia_driver = {
  292. .name = "g_nokia",
  293. .dev = &device_desc,
  294. .strings = dev_strings,
  295. .max_speed = USB_SPEED_HIGH,
  296. .bind = nokia_bind,
  297. .unbind = __exit_p(nokia_unbind),
  298. };
  299. static int __init nokia_init(void)
  300. {
  301. return usb_composite_probe(&nokia_driver);
  302. }
  303. module_init(nokia_init);
  304. static void __exit nokia_cleanup(void)
  305. {
  306. usb_composite_unregister(&nokia_driver);
  307. }
  308. module_exit(nokia_cleanup);