nokia.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * nokia.c -- Nokia Composite Gadget Driver
  4. *
  5. * Copyright (C) 2008-2010 Nokia Corporation
  6. * Contact: Felipe Balbi <felipe.balbi@nokia.com>
  7. *
  8. * This gadget driver borrows from serial.c which is:
  9. *
  10. * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
  11. * Copyright (C) 2008 by David Brownell
  12. * Copyright (C) 2008 by Nokia Corporation
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include "u_serial.h"
  18. #include "u_ether.h"
  19. #include "u_phonet.h"
  20. #include "u_ecm.h"
  21. #include "f_mass_storage.h"
  22. /* Defines */
  23. #define NOKIA_VERSION_NUM 0x0211
  24. #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
  25. USB_GADGET_COMPOSITE_OPTIONS();
  26. USB_ETHERNET_MODULE_PARAMETERS();
  27. static struct fsg_module_parameters fsg_mod_data = {
  28. .stall = 0,
  29. .luns = 2,
  30. .removable_count = 2,
  31. .removable = { 1, 1, },
  32. };
  33. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  34. static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
  35. #else
  36. /*
  37. * Number of buffers we will use.
  38. * 2 is usually enough for good buffering pipeline
  39. */
  40. #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
  41. #endif /* CONFIG_USB_DEBUG */
  42. FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
  43. #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
  44. #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
  45. /* string IDs are assigned dynamically */
  46. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  47. static char manufacturer_nokia[] = "Nokia";
  48. static const char product_nokia[] = NOKIA_LONG_NAME;
  49. static const char description_nokia[] = "PC-Suite Configuration";
  50. static struct usb_string strings_dev[] = {
  51. [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
  52. [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
  53. [USB_GADGET_SERIAL_IDX].s = "",
  54. [STRING_DESCRIPTION_IDX].s = description_nokia,
  55. { } /* end of list */
  56. };
  57. static struct usb_gadget_strings stringtab_dev = {
  58. .language = 0x0409, /* en-us */
  59. .strings = strings_dev,
  60. };
  61. static struct usb_gadget_strings *dev_strings[] = {
  62. &stringtab_dev,
  63. NULL,
  64. };
  65. static struct usb_device_descriptor device_desc = {
  66. .bLength = USB_DT_DEVICE_SIZE,
  67. .bDescriptorType = USB_DT_DEVICE,
  68. /* .bcdUSB = DYNAMIC */
  69. .bDeviceClass = USB_CLASS_COMM,
  70. .idVendor = cpu_to_le16(NOKIA_VENDOR_ID),
  71. .idProduct = cpu_to_le16(NOKIA_PRODUCT_ID),
  72. .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
  73. /* .iManufacturer = DYNAMIC */
  74. /* .iProduct = DYNAMIC */
  75. .bNumConfigurations = 1,
  76. };
  77. /*-------------------------------------------------------------------------*/
  78. /* Module */
  79. MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
  80. MODULE_AUTHOR("Felipe Balbi");
  81. MODULE_LICENSE("GPL");
  82. /*-------------------------------------------------------------------------*/
  83. static struct usb_function *f_acm_cfg1;
  84. static struct usb_function *f_acm_cfg2;
  85. static struct usb_function *f_ecm_cfg1;
  86. static struct usb_function *f_ecm_cfg2;
  87. static struct usb_function *f_obex1_cfg1;
  88. static struct usb_function *f_obex2_cfg1;
  89. static struct usb_function *f_obex1_cfg2;
  90. static struct usb_function *f_obex2_cfg2;
  91. static struct usb_function *f_phonet_cfg1;
  92. static struct usb_function *f_phonet_cfg2;
  93. static struct usb_function *f_msg_cfg1;
  94. static struct usb_function *f_msg_cfg2;
  95. static struct usb_configuration nokia_config_500ma_driver = {
  96. .label = "Bus Powered",
  97. .bConfigurationValue = 1,
  98. /* .iConfiguration = DYNAMIC */
  99. .bmAttributes = USB_CONFIG_ATT_ONE,
  100. .MaxPower = 500,
  101. };
  102. static struct usb_configuration nokia_config_100ma_driver = {
  103. .label = "Self Powered",
  104. .bConfigurationValue = 2,
  105. /* .iConfiguration = DYNAMIC */
  106. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  107. .MaxPower = 100,
  108. };
  109. static struct usb_function_instance *fi_acm;
  110. static struct usb_function_instance *fi_ecm;
  111. static struct usb_function_instance *fi_obex1;
  112. static struct usb_function_instance *fi_obex2;
  113. static struct usb_function_instance *fi_phonet;
  114. static struct usb_function_instance *fi_msg;
  115. static int nokia_bind_config(struct usb_configuration *c)
  116. {
  117. struct usb_function *f_acm;
  118. struct usb_function *f_phonet = NULL;
  119. struct usb_function *f_obex1 = NULL;
  120. struct usb_function *f_ecm;
  121. struct usb_function *f_obex2 = NULL;
  122. struct usb_function *f_msg;
  123. int status = 0;
  124. int obex1_stat = -1;
  125. int obex2_stat = -1;
  126. int phonet_stat = -1;
  127. if (!IS_ERR(fi_phonet)) {
  128. f_phonet = usb_get_function(fi_phonet);
  129. if (IS_ERR(f_phonet))
  130. pr_debug("could not get phonet function\n");
  131. }
  132. if (!IS_ERR(fi_obex1)) {
  133. f_obex1 = usb_get_function(fi_obex1);
  134. if (IS_ERR(f_obex1))
  135. pr_debug("could not get obex function 0\n");
  136. }
  137. if (!IS_ERR(fi_obex2)) {
  138. f_obex2 = usb_get_function(fi_obex2);
  139. if (IS_ERR(f_obex2))
  140. pr_debug("could not get obex function 1\n");
  141. }
  142. f_acm = usb_get_function(fi_acm);
  143. if (IS_ERR(f_acm)) {
  144. status = PTR_ERR(f_acm);
  145. goto err_get_acm;
  146. }
  147. f_ecm = usb_get_function(fi_ecm);
  148. if (IS_ERR(f_ecm)) {
  149. status = PTR_ERR(f_ecm);
  150. goto err_get_ecm;
  151. }
  152. f_msg = usb_get_function(fi_msg);
  153. if (IS_ERR(f_msg)) {
  154. status = PTR_ERR(f_msg);
  155. goto err_get_msg;
  156. }
  157. if (!IS_ERR_OR_NULL(f_phonet)) {
  158. phonet_stat = usb_add_function(c, f_phonet);
  159. if (phonet_stat)
  160. pr_debug("could not add phonet function\n");
  161. }
  162. if (!IS_ERR_OR_NULL(f_obex1)) {
  163. obex1_stat = usb_add_function(c, f_obex1);
  164. if (obex1_stat)
  165. pr_debug("could not add obex function 0\n");
  166. }
  167. if (!IS_ERR_OR_NULL(f_obex2)) {
  168. obex2_stat = usb_add_function(c, f_obex2);
  169. if (obex2_stat)
  170. pr_debug("could not add obex function 1\n");
  171. }
  172. status = usb_add_function(c, f_acm);
  173. if (status)
  174. goto err_conf;
  175. status = usb_add_function(c, f_ecm);
  176. if (status) {
  177. pr_debug("could not bind ecm config %d\n", status);
  178. goto err_ecm;
  179. }
  180. status = usb_add_function(c, f_msg);
  181. if (status)
  182. goto err_msg;
  183. if (c == &nokia_config_500ma_driver) {
  184. f_acm_cfg1 = f_acm;
  185. f_ecm_cfg1 = f_ecm;
  186. f_phonet_cfg1 = f_phonet;
  187. f_obex1_cfg1 = f_obex1;
  188. f_obex2_cfg1 = f_obex2;
  189. f_msg_cfg1 = f_msg;
  190. } else {
  191. f_acm_cfg2 = f_acm;
  192. f_ecm_cfg2 = f_ecm;
  193. f_phonet_cfg2 = f_phonet;
  194. f_obex1_cfg2 = f_obex1;
  195. f_obex2_cfg2 = f_obex2;
  196. f_msg_cfg2 = f_msg;
  197. }
  198. return status;
  199. err_msg:
  200. usb_remove_function(c, f_ecm);
  201. err_ecm:
  202. usb_remove_function(c, f_acm);
  203. err_conf:
  204. if (!obex2_stat)
  205. usb_remove_function(c, f_obex2);
  206. if (!obex1_stat)
  207. usb_remove_function(c, f_obex1);
  208. if (!phonet_stat)
  209. usb_remove_function(c, f_phonet);
  210. usb_put_function(f_msg);
  211. err_get_msg:
  212. usb_put_function(f_ecm);
  213. err_get_ecm:
  214. usb_put_function(f_acm);
  215. err_get_acm:
  216. if (!IS_ERR_OR_NULL(f_obex2))
  217. usb_put_function(f_obex2);
  218. if (!IS_ERR_OR_NULL(f_obex1))
  219. usb_put_function(f_obex1);
  220. if (!IS_ERR_OR_NULL(f_phonet))
  221. usb_put_function(f_phonet);
  222. return status;
  223. }
  224. static int nokia_bind(struct usb_composite_dev *cdev)
  225. {
  226. struct usb_gadget *gadget = cdev->gadget;
  227. struct fsg_opts *fsg_opts;
  228. struct fsg_config fsg_config;
  229. int status;
  230. status = usb_string_ids_tab(cdev, strings_dev);
  231. if (status < 0)
  232. goto err_usb;
  233. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  234. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  235. status = strings_dev[STRING_DESCRIPTION_IDX].id;
  236. nokia_config_500ma_driver.iConfiguration = status;
  237. nokia_config_100ma_driver.iConfiguration = status;
  238. if (!gadget_is_altset_supported(gadget)) {
  239. status = -ENODEV;
  240. goto err_usb;
  241. }
  242. fi_phonet = usb_get_function_instance("phonet");
  243. if (IS_ERR(fi_phonet))
  244. pr_debug("could not find phonet function\n");
  245. fi_obex1 = usb_get_function_instance("obex");
  246. if (IS_ERR(fi_obex1))
  247. pr_debug("could not find obex function 1\n");
  248. fi_obex2 = usb_get_function_instance("obex");
  249. if (IS_ERR(fi_obex2))
  250. pr_debug("could not find obex function 2\n");
  251. fi_acm = usb_get_function_instance("acm");
  252. if (IS_ERR(fi_acm)) {
  253. status = PTR_ERR(fi_acm);
  254. goto err_obex2_inst;
  255. }
  256. fi_ecm = usb_get_function_instance("ecm");
  257. if (IS_ERR(fi_ecm)) {
  258. status = PTR_ERR(fi_ecm);
  259. goto err_acm_inst;
  260. }
  261. fi_msg = usb_get_function_instance("mass_storage");
  262. if (IS_ERR(fi_msg)) {
  263. status = PTR_ERR(fi_msg);
  264. goto err_ecm_inst;
  265. }
  266. /* set up mass storage function */
  267. fsg_config_from_params(&fsg_config, &fsg_mod_data, fsg_num_buffers);
  268. fsg_config.vendor_name = "Nokia";
  269. fsg_config.product_name = "N900";
  270. fsg_opts = fsg_opts_from_func_inst(fi_msg);
  271. fsg_opts->no_configfs = true;
  272. status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers);
  273. if (status)
  274. goto err_msg_inst;
  275. status = fsg_common_set_cdev(fsg_opts->common, cdev, fsg_config.can_stall);
  276. if (status)
  277. goto err_msg_buf;
  278. fsg_common_set_sysfs(fsg_opts->common, true);
  279. status = fsg_common_create_luns(fsg_opts->common, &fsg_config);
  280. if (status)
  281. goto err_msg_buf;
  282. fsg_common_set_inquiry_string(fsg_opts->common, fsg_config.vendor_name,
  283. fsg_config.product_name);
  284. /* finally register the configuration */
  285. status = usb_add_config(cdev, &nokia_config_500ma_driver,
  286. nokia_bind_config);
  287. if (status < 0)
  288. goto err_msg_luns;
  289. status = usb_add_config(cdev, &nokia_config_100ma_driver,
  290. nokia_bind_config);
  291. if (status < 0)
  292. goto err_put_cfg1;
  293. usb_composite_overwrite_options(cdev, &coverwrite);
  294. dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
  295. return 0;
  296. err_put_cfg1:
  297. usb_put_function(f_acm_cfg1);
  298. if (!IS_ERR_OR_NULL(f_obex1_cfg1))
  299. usb_put_function(f_obex1_cfg1);
  300. if (!IS_ERR_OR_NULL(f_obex2_cfg1))
  301. usb_put_function(f_obex2_cfg1);
  302. if (!IS_ERR_OR_NULL(f_phonet_cfg1))
  303. usb_put_function(f_phonet_cfg1);
  304. usb_put_function(f_ecm_cfg1);
  305. err_msg_luns:
  306. fsg_common_remove_luns(fsg_opts->common);
  307. err_msg_buf:
  308. fsg_common_free_buffers(fsg_opts->common);
  309. err_msg_inst:
  310. usb_put_function_instance(fi_msg);
  311. err_ecm_inst:
  312. usb_put_function_instance(fi_ecm);
  313. err_acm_inst:
  314. usb_put_function_instance(fi_acm);
  315. err_obex2_inst:
  316. if (!IS_ERR(fi_obex2))
  317. usb_put_function_instance(fi_obex2);
  318. if (!IS_ERR(fi_obex1))
  319. usb_put_function_instance(fi_obex1);
  320. if (!IS_ERR(fi_phonet))
  321. usb_put_function_instance(fi_phonet);
  322. err_usb:
  323. return status;
  324. }
  325. static int nokia_unbind(struct usb_composite_dev *cdev)
  326. {
  327. if (!IS_ERR_OR_NULL(f_obex1_cfg2))
  328. usb_put_function(f_obex1_cfg2);
  329. if (!IS_ERR_OR_NULL(f_obex2_cfg2))
  330. usb_put_function(f_obex2_cfg2);
  331. if (!IS_ERR_OR_NULL(f_obex1_cfg1))
  332. usb_put_function(f_obex1_cfg1);
  333. if (!IS_ERR_OR_NULL(f_obex2_cfg1))
  334. usb_put_function(f_obex2_cfg1);
  335. if (!IS_ERR_OR_NULL(f_phonet_cfg1))
  336. usb_put_function(f_phonet_cfg1);
  337. if (!IS_ERR_OR_NULL(f_phonet_cfg2))
  338. usb_put_function(f_phonet_cfg2);
  339. usb_put_function(f_acm_cfg1);
  340. usb_put_function(f_acm_cfg2);
  341. usb_put_function(f_ecm_cfg1);
  342. usb_put_function(f_ecm_cfg2);
  343. usb_put_function(f_msg_cfg1);
  344. usb_put_function(f_msg_cfg2);
  345. usb_put_function_instance(fi_msg);
  346. usb_put_function_instance(fi_ecm);
  347. if (!IS_ERR(fi_obex2))
  348. usb_put_function_instance(fi_obex2);
  349. if (!IS_ERR(fi_obex1))
  350. usb_put_function_instance(fi_obex1);
  351. if (!IS_ERR(fi_phonet))
  352. usb_put_function_instance(fi_phonet);
  353. usb_put_function_instance(fi_acm);
  354. return 0;
  355. }
  356. static struct usb_composite_driver nokia_driver = {
  357. .name = "g_nokia",
  358. .dev = &device_desc,
  359. .strings = dev_strings,
  360. .max_speed = USB_SPEED_HIGH,
  361. .bind = nokia_bind,
  362. .unbind = nokia_unbind,
  363. };
  364. module_usb_composite_driver(nokia_driver);