nokia.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "epautoconf.c"
  37. #include "u_serial.c"
  38. #include "f_acm.c"
  39. #include "f_ecm.c"
  40. #include "f_obex.c"
  41. #include "f_serial.c"
  42. #include "f_phonet.c"
  43. #include "u_ether.c"
  44. /*-------------------------------------------------------------------------*/
  45. #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
  46. #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
  47. /* string IDs are assigned dynamically */
  48. #define STRING_MANUFACTURER_IDX 0
  49. #define STRING_PRODUCT_IDX 1
  50. #define STRING_DESCRIPTION_IDX 2
  51. static char manufacturer_nokia[] = "Nokia";
  52. static const char product_nokia[] = NOKIA_LONG_NAME;
  53. static const char description_nokia[] = "PC-Suite Configuration";
  54. static struct usb_string strings_dev[] = {
  55. [STRING_MANUFACTURER_IDX].s = manufacturer_nokia,
  56. [STRING_PRODUCT_IDX].s = NOKIA_LONG_NAME,
  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 = __constant_cpu_to_le16(0x0200),
  72. .bDeviceClass = USB_CLASS_COMM,
  73. .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
  74. .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
  75. /* .iManufacturer = DYNAMIC */
  76. /* .iProduct = DYNAMIC */
  77. .bNumConfigurations = 1,
  78. };
  79. /*-------------------------------------------------------------------------*/
  80. /* Module */
  81. MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
  82. MODULE_AUTHOR("Felipe Balbi");
  83. MODULE_LICENSE("GPL");
  84. /*-------------------------------------------------------------------------*/
  85. static u8 hostaddr[ETH_ALEN];
  86. static int __init nokia_bind_config(struct usb_configuration *c)
  87. {
  88. int status = 0;
  89. status = phonet_bind_config(c);
  90. if (status)
  91. printk(KERN_DEBUG "could not bind phonet config\n");
  92. status = obex_bind_config(c, 0);
  93. if (status)
  94. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  95. status = obex_bind_config(c, 1);
  96. if (status)
  97. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  98. status = acm_bind_config(c, 2);
  99. if (status)
  100. printk(KERN_DEBUG "could not bind acm config\n");
  101. status = ecm_bind_config(c, hostaddr);
  102. if (status)
  103. printk(KERN_DEBUG "could not bind ecm config\n");
  104. return status;
  105. }
  106. static struct usb_configuration nokia_config_500ma_driver = {
  107. .label = "Bus Powered",
  108. .bConfigurationValue = 1,
  109. /* .iConfiguration = DYNAMIC */
  110. .bmAttributes = USB_CONFIG_ATT_ONE,
  111. .bMaxPower = 250, /* 500mA */
  112. };
  113. static struct usb_configuration nokia_config_100ma_driver = {
  114. .label = "Self Powered",
  115. .bConfigurationValue = 2,
  116. /* .iConfiguration = DYNAMIC */
  117. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  118. .bMaxPower = 50, /* 100 mA */
  119. };
  120. static int __init nokia_bind(struct usb_composite_dev *cdev)
  121. {
  122. int gcnum;
  123. struct usb_gadget *gadget = cdev->gadget;
  124. int status;
  125. status = gphonet_setup(cdev->gadget);
  126. if (status < 0)
  127. goto err_phonet;
  128. status = gserial_setup(cdev->gadget, 3);
  129. if (status < 0)
  130. goto err_serial;
  131. status = gether_setup(cdev->gadget, hostaddr);
  132. if (status < 0)
  133. goto err_ether;
  134. status = usb_string_id(cdev);
  135. if (status < 0)
  136. goto err_usb;
  137. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  138. device_desc.iManufacturer = status;
  139. status = usb_string_id(cdev);
  140. if (status < 0)
  141. goto err_usb;
  142. strings_dev[STRING_PRODUCT_IDX].id = status;
  143. device_desc.iProduct = status;
  144. /* config description */
  145. status = usb_string_id(cdev);
  146. if (status < 0)
  147. goto err_usb;
  148. strings_dev[STRING_DESCRIPTION_IDX].id = status;
  149. nokia_config_500ma_driver.iConfiguration = status;
  150. nokia_config_100ma_driver.iConfiguration = status;
  151. /* set up other descriptors */
  152. gcnum = usb_gadget_controller_number(gadget);
  153. if (gcnum >= 0)
  154. device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM);
  155. else {
  156. /* this should only work with hw that supports altsettings
  157. * and several endpoints, anything else, panic.
  158. */
  159. pr_err("nokia_bind: controller '%s' not recognized\n",
  160. gadget->name);
  161. goto err_usb;
  162. }
  163. /* finally register the configuration */
  164. status = usb_add_config(cdev, &nokia_config_500ma_driver,
  165. nokia_bind_config);
  166. if (status < 0)
  167. goto err_usb;
  168. status = usb_add_config(cdev, &nokia_config_100ma_driver,
  169. nokia_bind_config);
  170. if (status < 0)
  171. goto err_usb;
  172. dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
  173. return 0;
  174. err_usb:
  175. gether_cleanup();
  176. err_ether:
  177. gserial_cleanup();
  178. err_serial:
  179. gphonet_cleanup();
  180. err_phonet:
  181. return status;
  182. }
  183. static int __exit nokia_unbind(struct usb_composite_dev *cdev)
  184. {
  185. gphonet_cleanup();
  186. gserial_cleanup();
  187. gether_cleanup();
  188. return 0;
  189. }
  190. static __refdata struct usb_composite_driver nokia_driver = {
  191. .name = "g_nokia",
  192. .dev = &device_desc,
  193. .strings = dev_strings,
  194. .max_speed = USB_SPEED_HIGH,
  195. .bind = nokia_bind,
  196. .unbind = __exit_p(nokia_unbind),
  197. };
  198. static int __init nokia_init(void)
  199. {
  200. return usb_composite_probe(&nokia_driver);
  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);