nokia.c 11 KB

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