nokia.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 <[email protected]>
  7. *
  8. * This gadget driver borrows from serial.c which is:
  9. *
  10. * Copyright (C) 2003 Al Borchers ([email protected])
  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 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 = DYNAMIC */
  68. .bDeviceClass = USB_CLASS_COMM,
  69. .idVendor = cpu_to_le16(NOKIA_VENDOR_ID),
  70. .idProduct = 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 struct usb_function *f_ecm_cfg1;
  85. static struct usb_function *f_ecm_cfg2;
  86. static struct usb_function *f_obex1_cfg1;
  87. static struct usb_function *f_obex2_cfg1;
  88. static struct usb_function *f_obex1_cfg2;
  89. static struct usb_function *f_obex2_cfg2;
  90. static struct usb_function *f_phonet_cfg1;
  91. static struct usb_function *f_phonet_cfg2;
  92. static struct usb_function *f_msg_cfg1;
  93. static struct usb_function *f_msg_cfg2;
  94. static struct usb_configuration nokia_config_500ma_driver = {
  95. .label = "Bus Powered",
  96. .bConfigurationValue = 1,
  97. /* .iConfiguration = DYNAMIC */
  98. .bmAttributes = USB_CONFIG_ATT_ONE,
  99. .MaxPower = 500,
  100. };
  101. static struct usb_configuration nokia_config_100ma_driver = {
  102. .label = "Self Powered",
  103. .bConfigurationValue = 2,
  104. /* .iConfiguration = DYNAMIC */
  105. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  106. .MaxPower = 100,
  107. };
  108. static struct usb_function_instance *fi_acm;
  109. static struct usb_function_instance *fi_ecm;
  110. static struct usb_function_instance *fi_obex1;
  111. static struct usb_function_instance *fi_obex2;
  112. static struct usb_function_instance *fi_phonet;
  113. static struct usb_function_instance *fi_msg;
  114. static int nokia_bind_config(struct usb_configuration *c)
  115. {
  116. struct usb_function *f_acm;
  117. struct usb_function *f_phonet = NULL;
  118. struct usb_function *f_obex1 = NULL;
  119. struct usb_function *f_ecm;
  120. struct usb_function *f_obex2 = NULL;
  121. struct usb_function *f_msg;
  122. int status = 0;
  123. int obex1_stat = -1;
  124. int obex2_stat = -1;
  125. int phonet_stat = -1;
  126. if (!IS_ERR(fi_phonet)) {
  127. f_phonet = usb_get_function(fi_phonet);
  128. if (IS_ERR(f_phonet))
  129. pr_debug("could not get phonet function\n");
  130. }
  131. if (!IS_ERR(fi_obex1)) {
  132. f_obex1 = usb_get_function(fi_obex1);
  133. if (IS_ERR(f_obex1))
  134. pr_debug("could not get obex function 0\n");
  135. }
  136. if (!IS_ERR(fi_obex2)) {
  137. f_obex2 = usb_get_function(fi_obex2);
  138. if (IS_ERR(f_obex2))
  139. pr_debug("could not get obex function 1\n");
  140. }
  141. f_acm = usb_get_function(fi_acm);
  142. if (IS_ERR(f_acm)) {
  143. status = PTR_ERR(f_acm);
  144. goto err_get_acm;
  145. }
  146. f_ecm = usb_get_function(fi_ecm);
  147. if (IS_ERR(f_ecm)) {
  148. status = PTR_ERR(f_ecm);
  149. goto err_get_ecm;
  150. }
  151. f_msg = usb_get_function(fi_msg);
  152. if (IS_ERR(f_msg)) {
  153. status = PTR_ERR(f_msg);
  154. goto err_get_msg;
  155. }
  156. if (!IS_ERR_OR_NULL(f_phonet)) {
  157. phonet_stat = usb_add_function(c, f_phonet);
  158. if (phonet_stat)
  159. pr_debug("could not add phonet function\n");
  160. }
  161. if (!IS_ERR_OR_NULL(f_obex1)) {
  162. obex1_stat = usb_add_function(c, f_obex1);
  163. if (obex1_stat)
  164. pr_debug("could not add obex function 0\n");
  165. }
  166. if (!IS_ERR_OR_NULL(f_obex2)) {
  167. obex2_stat = usb_add_function(c, f_obex2);
  168. if (obex2_stat)
  169. pr_debug("could not add obex function 1\n");
  170. }
  171. status = usb_add_function(c, f_acm);
  172. if (status)
  173. goto err_conf;
  174. status = usb_add_function(c, f_ecm);
  175. if (status) {
  176. pr_debug("could not bind ecm config %d\n", status);
  177. goto err_ecm;
  178. }
  179. status = usb_add_function(c, f_msg);
  180. if (status)
  181. goto err_msg;
  182. if (c == &nokia_config_500ma_driver) {
  183. f_acm_cfg1 = f_acm;
  184. f_ecm_cfg1 = f_ecm;
  185. f_phonet_cfg1 = f_phonet;
  186. f_obex1_cfg1 = f_obex1;
  187. f_obex2_cfg1 = f_obex2;
  188. f_msg_cfg1 = f_msg;
  189. } else {
  190. f_acm_cfg2 = f_acm;
  191. f_ecm_cfg2 = f_ecm;
  192. f_phonet_cfg2 = f_phonet;
  193. f_obex1_cfg2 = f_obex1;
  194. f_obex2_cfg2 = f_obex2;
  195. f_msg_cfg2 = f_msg;
  196. }
  197. return status;
  198. err_msg:
  199. usb_remove_function(c, f_ecm);
  200. err_ecm:
  201. usb_remove_function(c, f_acm);
  202. err_conf:
  203. if (!obex2_stat)
  204. usb_remove_function(c, f_obex2);
  205. if (!obex1_stat)
  206. usb_remove_function(c, f_obex1);
  207. if (!phonet_stat)
  208. usb_remove_function(c, f_phonet);
  209. usb_put_function(f_msg);
  210. err_get_msg:
  211. usb_put_function(f_ecm);
  212. err_get_ecm:
  213. usb_put_function(f_acm);
  214. err_get_acm:
  215. if (!IS_ERR_OR_NULL(f_obex2))
  216. usb_put_function(f_obex2);
  217. if (!IS_ERR_OR_NULL(f_obex1))
  218. usb_put_function(f_obex1);
  219. if (!IS_ERR_OR_NULL(f_phonet))
  220. usb_put_function(f_phonet);
  221. return status;
  222. }
  223. static int nokia_bind(struct usb_composite_dev *cdev)
  224. {
  225. struct usb_gadget *gadget = cdev->gadget;
  226. struct fsg_opts *fsg_opts;
  227. struct fsg_config fsg_config;
  228. int status;
  229. status = usb_string_ids_tab(cdev, strings_dev);
  230. if (status < 0)
  231. goto err_usb;
  232. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  233. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  234. status = strings_dev[STRING_DESCRIPTION_IDX].id;
  235. nokia_config_500ma_driver.iConfiguration = status;
  236. nokia_config_100ma_driver.iConfiguration = status;
  237. if (!gadget_is_altset_supported(gadget)) {
  238. status = -ENODEV;
  239. goto err_usb;
  240. }
  241. fi_phonet = usb_get_function_instance("phonet");
  242. if (IS_ERR(fi_phonet))
  243. pr_debug("could not find phonet function\n");
  244. fi_obex1 = usb_get_function_instance("obex");
  245. if (IS_ERR(fi_obex1))
  246. pr_debug("could not find obex function 1\n");
  247. fi_obex2 = usb_get_function_instance("obex");
  248. if (IS_ERR(fi_obex2))
  249. pr_debug("could not find obex function 2\n");
  250. fi_acm = usb_get_function_instance("acm");
  251. if (IS_ERR(fi_acm)) {
  252. status = PTR_ERR(fi_acm);
  253. goto err_obex2_inst;
  254. }
  255. fi_ecm = usb_get_function_instance("ecm");
  256. if (IS_ERR(fi_ecm)) {
  257. status = PTR_ERR(fi_ecm);
  258. goto err_acm_inst;
  259. }
  260. fi_msg = usb_get_function_instance("mass_storage");
  261. if (IS_ERR(fi_msg)) {
  262. status = PTR_ERR(fi_msg);
  263. goto err_ecm_inst;
  264. }
  265. /* set up mass storage function */
  266. fsg_config_from_params(&fsg_config, &fsg_mod_data, fsg_num_buffers);
  267. fsg_config.vendor_name = "Nokia";
  268. fsg_config.product_name = "N900";
  269. fsg_opts = fsg_opts_from_func_inst(fi_msg);
  270. fsg_opts->no_configfs = true;
  271. status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers);
  272. if (status)
  273. goto err_msg_inst;
  274. status = fsg_common_set_cdev(fsg_opts->common, cdev, fsg_config.can_stall);
  275. if (status)
  276. goto err_msg_buf;
  277. fsg_common_set_sysfs(fsg_opts->common, true);
  278. status = fsg_common_create_luns(fsg_opts->common, &fsg_config);
  279. if (status)
  280. goto err_msg_buf;
  281. fsg_common_set_inquiry_string(fsg_opts->common, fsg_config.vendor_name,
  282. fsg_config.product_name);
  283. /* finally register the configuration */
  284. status = usb_add_config(cdev, &nokia_config_500ma_driver,
  285. nokia_bind_config);
  286. if (status < 0)
  287. goto err_msg_luns;
  288. status = usb_add_config(cdev, &nokia_config_100ma_driver,
  289. nokia_bind_config);
  290. if (status < 0)
  291. goto err_put_cfg1;
  292. usb_composite_overwrite_options(cdev, &coverwrite);
  293. dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
  294. return 0;
  295. err_put_cfg1:
  296. usb_put_function(f_acm_cfg1);
  297. if (!IS_ERR_OR_NULL(f_obex1_cfg1))
  298. usb_put_function(f_obex1_cfg1);
  299. if (!IS_ERR_OR_NULL(f_obex2_cfg1))
  300. usb_put_function(f_obex2_cfg1);
  301. if (!IS_ERR_OR_NULL(f_phonet_cfg1))
  302. usb_put_function(f_phonet_cfg1);
  303. usb_put_function(f_ecm_cfg1);
  304. err_msg_luns:
  305. fsg_common_remove_luns(fsg_opts->common);
  306. err_msg_buf:
  307. fsg_common_free_buffers(fsg_opts->common);
  308. err_msg_inst:
  309. usb_put_function_instance(fi_msg);
  310. err_ecm_inst:
  311. usb_put_function_instance(fi_ecm);
  312. err_acm_inst:
  313. usb_put_function_instance(fi_acm);
  314. err_obex2_inst:
  315. if (!IS_ERR(fi_obex2))
  316. usb_put_function_instance(fi_obex2);
  317. if (!IS_ERR(fi_obex1))
  318. usb_put_function_instance(fi_obex1);
  319. if (!IS_ERR(fi_phonet))
  320. usb_put_function_instance(fi_phonet);
  321. err_usb:
  322. return status;
  323. }
  324. static int nokia_unbind(struct usb_composite_dev *cdev)
  325. {
  326. if (!IS_ERR_OR_NULL(f_obex1_cfg2))
  327. usb_put_function(f_obex1_cfg2);
  328. if (!IS_ERR_OR_NULL(f_obex2_cfg2))
  329. usb_put_function(f_obex2_cfg2);
  330. if (!IS_ERR_OR_NULL(f_obex1_cfg1))
  331. usb_put_function(f_obex1_cfg1);
  332. if (!IS_ERR_OR_NULL(f_obex2_cfg1))
  333. usb_put_function(f_obex2_cfg1);
  334. if (!IS_ERR_OR_NULL(f_phonet_cfg1))
  335. usb_put_function(f_phonet_cfg1);
  336. if (!IS_ERR_OR_NULL(f_phonet_cfg2))
  337. usb_put_function(f_phonet_cfg2);
  338. usb_put_function(f_acm_cfg1);
  339. usb_put_function(f_acm_cfg2);
  340. usb_put_function(f_ecm_cfg1);
  341. usb_put_function(f_ecm_cfg2);
  342. usb_put_function(f_msg_cfg1);
  343. usb_put_function(f_msg_cfg2);
  344. usb_put_function_instance(fi_msg);
  345. usb_put_function_instance(fi_ecm);
  346. if (!IS_ERR(fi_obex2))
  347. usb_put_function_instance(fi_obex2);
  348. if (!IS_ERR(fi_obex1))
  349. usb_put_function_instance(fi_obex1);
  350. if (!IS_ERR(fi_phonet))
  351. usb_put_function_instance(fi_phonet);
  352. usb_put_function_instance(fi_acm);
  353. return 0;
  354. }
  355. static struct usb_composite_driver nokia_driver = {
  356. .name = "g_nokia",
  357. .dev = &device_desc,
  358. .strings = dev_strings,
  359. .max_speed = USB_SPEED_HIGH,
  360. .bind = nokia_bind,
  361. .unbind = nokia_unbind,
  362. };
  363. module_usb_composite_driver(nokia_driver);