nokia.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/utsname.h>
  19. #include <linux/device.h>
  20. #include "u_serial.h"
  21. #include "u_ether.h"
  22. #include "u_phonet.h"
  23. #include "gadget_chips.h"
  24. /* Defines */
  25. #define NOKIA_VERSION_NUM 0x0211
  26. #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
  27. /*-------------------------------------------------------------------------*/
  28. /*
  29. * Kbuild is not very cooperative with respect to linking separately
  30. * compiled library objects into one module. So for now we won't use
  31. * separate compilation ... ensuring init/exit sections work to shrink
  32. * the runtime footprint, and giving us at least some parts of what
  33. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  34. */
  35. #include "composite.c"
  36. #include "usbstring.c"
  37. #include "config.c"
  38. #include "epautoconf.c"
  39. #include "u_serial.c"
  40. #include "f_acm.c"
  41. #include "f_ecm.c"
  42. #include "f_obex.c"
  43. #include "f_serial.c"
  44. #include "f_phonet.c"
  45. #include "u_ether.c"
  46. /*-------------------------------------------------------------------------*/
  47. #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
  48. #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
  49. /* string IDs are assigned dynamically */
  50. #define STRING_MANUFACTURER_IDX 0
  51. #define STRING_PRODUCT_IDX 1
  52. #define STRING_DESCRIPTION_IDX 2
  53. static char manufacturer_nokia[] = "Nokia";
  54. static const char product_nokia[] = NOKIA_LONG_NAME;
  55. static const char description_nokia[] = "PC-Suite Configuration";
  56. static struct usb_string strings_dev[] = {
  57. [STRING_MANUFACTURER_IDX].s = manufacturer_nokia,
  58. [STRING_PRODUCT_IDX].s = NOKIA_LONG_NAME,
  59. [STRING_DESCRIPTION_IDX].s = description_nokia,
  60. { } /* end of list */
  61. };
  62. static struct usb_gadget_strings stringtab_dev = {
  63. .language = 0x0409, /* en-us */
  64. .strings = strings_dev,
  65. };
  66. static struct usb_gadget_strings *dev_strings[] = {
  67. &stringtab_dev,
  68. NULL,
  69. };
  70. static struct usb_device_descriptor device_desc = {
  71. .bLength = USB_DT_DEVICE_SIZE,
  72. .bDescriptorType = USB_DT_DEVICE,
  73. .bcdUSB = __constant_cpu_to_le16(0x0200),
  74. .bDeviceClass = USB_CLASS_COMM,
  75. .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
  76. .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
  77. /* .iManufacturer = DYNAMIC */
  78. /* .iProduct = DYNAMIC */
  79. .bNumConfigurations = 1,
  80. };
  81. /*-------------------------------------------------------------------------*/
  82. /* Module */
  83. MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
  84. MODULE_AUTHOR("Felipe Balbi");
  85. MODULE_LICENSE("GPL");
  86. /*-------------------------------------------------------------------------*/
  87. static u8 hostaddr[ETH_ALEN];
  88. static int __init nokia_bind_config(struct usb_configuration *c)
  89. {
  90. int status = 0;
  91. status = phonet_bind_config(c);
  92. if (status)
  93. printk(KERN_DEBUG "could not bind phonet config\n");
  94. status = obex_bind_config(c, 0);
  95. if (status)
  96. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  97. status = obex_bind_config(c, 1);
  98. if (status)
  99. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  100. status = acm_bind_config(c, 2);
  101. if (status)
  102. printk(KERN_DEBUG "could not bind acm config\n");
  103. status = ecm_bind_config(c, hostaddr);
  104. if (status)
  105. printk(KERN_DEBUG "could not bind ecm config\n");
  106. return status;
  107. }
  108. static struct usb_configuration nokia_config_500ma_driver = {
  109. .label = "Bus Powered",
  110. .bConfigurationValue = 1,
  111. /* .iConfiguration = DYNAMIC */
  112. .bmAttributes = USB_CONFIG_ATT_ONE,
  113. .bMaxPower = 250, /* 500mA */
  114. };
  115. static struct usb_configuration nokia_config_100ma_driver = {
  116. .label = "Self Powered",
  117. .bConfigurationValue = 2,
  118. /* .iConfiguration = DYNAMIC */
  119. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  120. .bMaxPower = 50, /* 100 mA */
  121. };
  122. static int __init nokia_bind(struct usb_composite_dev *cdev)
  123. {
  124. int gcnum;
  125. struct usb_gadget *gadget = cdev->gadget;
  126. int status;
  127. status = gphonet_setup(cdev->gadget);
  128. if (status < 0)
  129. goto err_phonet;
  130. status = gserial_setup(cdev->gadget, 3);
  131. if (status < 0)
  132. goto err_serial;
  133. status = gether_setup(cdev->gadget, hostaddr);
  134. if (status < 0)
  135. goto err_ether;
  136. status = usb_string_id(cdev);
  137. if (status < 0)
  138. goto err_usb;
  139. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  140. device_desc.iManufacturer = status;
  141. status = usb_string_id(cdev);
  142. if (status < 0)
  143. goto err_usb;
  144. strings_dev[STRING_PRODUCT_IDX].id = status;
  145. device_desc.iProduct = status;
  146. /* config description */
  147. status = usb_string_id(cdev);
  148. if (status < 0)
  149. goto err_usb;
  150. strings_dev[STRING_DESCRIPTION_IDX].id = status;
  151. nokia_config_500ma_driver.iConfiguration = status;
  152. nokia_config_100ma_driver.iConfiguration = status;
  153. /* set up other descriptors */
  154. gcnum = usb_gadget_controller_number(gadget);
  155. if (gcnum >= 0)
  156. device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM);
  157. else {
  158. /* this should only work with hw that supports altsettings
  159. * and several endpoints, anything else, panic.
  160. */
  161. pr_err("nokia_bind: controller '%s' not recognized\n",
  162. gadget->name);
  163. goto err_usb;
  164. }
  165. /* finally register the configuration */
  166. status = usb_add_config(cdev, &nokia_config_500ma_driver,
  167. nokia_bind_config);
  168. if (status < 0)
  169. goto err_usb;
  170. status = usb_add_config(cdev, &nokia_config_100ma_driver,
  171. nokia_bind_config);
  172. if (status < 0)
  173. goto err_usb;
  174. dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
  175. return 0;
  176. err_usb:
  177. gether_cleanup();
  178. err_ether:
  179. gserial_cleanup();
  180. err_serial:
  181. gphonet_cleanup();
  182. err_phonet:
  183. return status;
  184. }
  185. static int __exit nokia_unbind(struct usb_composite_dev *cdev)
  186. {
  187. gphonet_cleanup();
  188. gserial_cleanup();
  189. gether_cleanup();
  190. return 0;
  191. }
  192. static struct usb_composite_driver nokia_driver = {
  193. .name = "g_nokia",
  194. .dev = &device_desc,
  195. .strings = dev_strings,
  196. .unbind = __exit_p(nokia_unbind),
  197. };
  198. static int __init nokia_init(void)
  199. {
  200. return usb_composite_probe(&nokia_driver, nokia_bind);
  201. }
  202. module_init(nokia_init);
  203. static void __exit nokia_cleanup(void)
  204. {
  205. usb_composite_unregister(&nokia_driver);
  206. }
  207. module_exit(nokia_cleanup);