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. .bind = nokia_bind_config,
  111. .bConfigurationValue = 1,
  112. /* .iConfiguration = DYNAMIC */
  113. .bmAttributes = USB_CONFIG_ATT_ONE,
  114. .bMaxPower = 250, /* 500mA */
  115. };
  116. static struct usb_configuration nokia_config_100ma_driver = {
  117. .label = "Self Powered",
  118. .bind = nokia_bind_config,
  119. .bConfigurationValue = 2,
  120. /* .iConfiguration = DYNAMIC */
  121. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  122. .bMaxPower = 50, /* 100 mA */
  123. };
  124. static int __init nokia_bind(struct usb_composite_dev *cdev)
  125. {
  126. int gcnum;
  127. struct usb_gadget *gadget = cdev->gadget;
  128. int status;
  129. status = gphonet_setup(cdev->gadget);
  130. if (status < 0)
  131. goto err_phonet;
  132. status = gserial_setup(cdev->gadget, 3);
  133. if (status < 0)
  134. goto err_serial;
  135. status = gether_setup(cdev->gadget, hostaddr);
  136. if (status < 0)
  137. goto err_ether;
  138. status = usb_string_id(cdev);
  139. if (status < 0)
  140. goto err_usb;
  141. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  142. device_desc.iManufacturer = status;
  143. status = usb_string_id(cdev);
  144. if (status < 0)
  145. goto err_usb;
  146. strings_dev[STRING_PRODUCT_IDX].id = status;
  147. device_desc.iProduct = status;
  148. /* config description */
  149. status = usb_string_id(cdev);
  150. if (status < 0)
  151. goto err_usb;
  152. strings_dev[STRING_DESCRIPTION_IDX].id = status;
  153. nokia_config_500ma_driver.iConfiguration = status;
  154. nokia_config_100ma_driver.iConfiguration = status;
  155. /* set up other descriptors */
  156. gcnum = usb_gadget_controller_number(gadget);
  157. if (gcnum >= 0)
  158. device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM);
  159. else {
  160. /* this should only work with hw that supports altsettings
  161. * and several endpoints, anything else, panic.
  162. */
  163. pr_err("nokia_bind: controller '%s' not recognized\n",
  164. gadget->name);
  165. goto err_usb;
  166. }
  167. /* finaly register the configuration */
  168. status = usb_add_config(cdev, &nokia_config_500ma_driver);
  169. if (status < 0)
  170. goto err_usb;
  171. status = usb_add_config(cdev, &nokia_config_100ma_driver);
  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. .bind = nokia_bind,
  197. .unbind = __exit_p(nokia_unbind),
  198. };
  199. static int __init nokia_init(void)
  200. {
  201. return usb_composite_register(&nokia_driver);
  202. }
  203. module_init(nokia_init);
  204. static void __exit nokia_cleanup(void)
  205. {
  206. usb_composite_unregister(&nokia_driver);
  207. }
  208. module_exit(nokia_cleanup);