nokia.c 11 KB

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