nokia.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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/device.h>
  19. #include "u_serial.h"
  20. #include "u_ether.h"
  21. #include "u_phonet.h"
  22. #include "gadget_chips.h"
  23. /* Defines */
  24. #define NOKIA_VERSION_NUM 0x0211
  25. #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
  26. /*-------------------------------------------------------------------------*/
  27. /*
  28. * Kbuild is not very cooperative with respect to linking separately
  29. * compiled library objects into one module. So for now we won't use
  30. * separate compilation ... ensuring init/exit sections work to shrink
  31. * the runtime footprint, and giving us at least some parts of what
  32. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  33. */
  34. #define USBF_OBEX_INCLUDED
  35. #include "f_obex.c"
  36. #include "f_phonet.c"
  37. #include "u_ether.c"
  38. /*-------------------------------------------------------------------------*/
  39. USB_GADGET_COMPOSITE_OPTIONS();
  40. #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
  41. #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
  42. /* string IDs are assigned dynamically */
  43. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  44. static char manufacturer_nokia[] = "Nokia";
  45. static const char product_nokia[] = NOKIA_LONG_NAME;
  46. static const char description_nokia[] = "PC-Suite Configuration";
  47. static struct usb_string strings_dev[] = {
  48. [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
  49. [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
  50. [USB_GADGET_SERIAL_IDX].s = "",
  51. [STRING_DESCRIPTION_IDX].s = description_nokia,
  52. { } /* end of list */
  53. };
  54. static struct usb_gadget_strings stringtab_dev = {
  55. .language = 0x0409, /* en-us */
  56. .strings = strings_dev,
  57. };
  58. static struct usb_gadget_strings *dev_strings[] = {
  59. &stringtab_dev,
  60. NULL,
  61. };
  62. static struct usb_device_descriptor device_desc = {
  63. .bLength = USB_DT_DEVICE_SIZE,
  64. .bDescriptorType = USB_DT_DEVICE,
  65. .bcdUSB = __constant_cpu_to_le16(0x0200),
  66. .bDeviceClass = USB_CLASS_COMM,
  67. .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
  68. .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
  69. .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
  70. /* .iManufacturer = DYNAMIC */
  71. /* .iProduct = DYNAMIC */
  72. .bNumConfigurations = 1,
  73. };
  74. /*-------------------------------------------------------------------------*/
  75. /* Module */
  76. MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
  77. MODULE_AUTHOR("Felipe Balbi");
  78. MODULE_LICENSE("GPL");
  79. /*-------------------------------------------------------------------------*/
  80. static struct usb_function *f_acm_cfg1;
  81. static struct usb_function *f_acm_cfg2;
  82. static u8 hostaddr[ETH_ALEN];
  83. static struct eth_dev *the_dev;
  84. enum {
  85. TTY_PORT_OBEX0,
  86. TTY_PORT_OBEX1,
  87. TTY_PORTS_MAX,
  88. };
  89. static unsigned char tty_lines[TTY_PORTS_MAX];
  90. static struct usb_configuration nokia_config_500ma_driver = {
  91. .label = "Bus Powered",
  92. .bConfigurationValue = 1,
  93. /* .iConfiguration = DYNAMIC */
  94. .bmAttributes = USB_CONFIG_ATT_ONE,
  95. .MaxPower = 500,
  96. };
  97. static struct usb_configuration nokia_config_100ma_driver = {
  98. .label = "Self Powered",
  99. .bConfigurationValue = 2,
  100. /* .iConfiguration = DYNAMIC */
  101. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  102. .MaxPower = 100,
  103. };
  104. static struct usb_function_instance *fi_acm;
  105. static int __init nokia_bind_config(struct usb_configuration *c)
  106. {
  107. struct usb_function *f_acm;
  108. int status = 0;
  109. status = phonet_bind_config(c);
  110. if (status)
  111. printk(KERN_DEBUG "could not bind phonet config\n");
  112. status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX0]);
  113. if (status)
  114. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  115. status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX1]);
  116. if (status)
  117. printk(KERN_DEBUG "could not bind obex config %d\n", 0);
  118. f_acm = usb_get_function(fi_acm);
  119. if (IS_ERR(f_acm))
  120. return PTR_ERR(f_acm);
  121. status = usb_add_function(c, f_acm);
  122. if (status)
  123. goto err_conf;
  124. status = ecm_bind_config(c, hostaddr, the_dev);
  125. if (status) {
  126. pr_debug("could not bind ecm config %d\n", status);
  127. goto err_ecm;
  128. }
  129. if (c == &nokia_config_500ma_driver)
  130. f_acm_cfg1 = f_acm;
  131. else
  132. f_acm_cfg2 = f_acm;
  133. return status;
  134. err_ecm:
  135. usb_remove_function(c, f_acm);
  136. err_conf:
  137. usb_put_function(f_acm);
  138. return status;
  139. }
  140. static int __init nokia_bind(struct usb_composite_dev *cdev)
  141. {
  142. struct usb_gadget *gadget = cdev->gadget;
  143. int status;
  144. int cur_line;
  145. status = gphonet_setup(cdev->gadget);
  146. if (status < 0)
  147. goto err_phonet;
  148. for (cur_line = 0; cur_line < TTY_PORTS_MAX; cur_line++) {
  149. status = gserial_alloc_line(&tty_lines[cur_line]);
  150. if (status)
  151. goto err_ether;
  152. }
  153. the_dev = gether_setup(cdev->gadget, hostaddr);
  154. if (IS_ERR(the_dev)) {
  155. status = PTR_ERR(the_dev);
  156. goto err_ether;
  157. }
  158. status = usb_string_ids_tab(cdev, strings_dev);
  159. if (status < 0)
  160. goto err_usb;
  161. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  162. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  163. status = strings_dev[STRING_DESCRIPTION_IDX].id;
  164. nokia_config_500ma_driver.iConfiguration = status;
  165. nokia_config_100ma_driver.iConfiguration = status;
  166. if (!gadget_supports_altsettings(gadget))
  167. goto err_usb;
  168. fi_acm = usb_get_function_instance("acm");
  169. if (IS_ERR(fi_acm))
  170. goto err_usb;
  171. /* finally register the configuration */
  172. status = usb_add_config(cdev, &nokia_config_500ma_driver,
  173. nokia_bind_config);
  174. if (status < 0)
  175. goto err_acm_inst;
  176. status = usb_add_config(cdev, &nokia_config_100ma_driver,
  177. nokia_bind_config);
  178. if (status < 0)
  179. goto err_put_cfg1;
  180. usb_composite_overwrite_options(cdev, &coverwrite);
  181. dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
  182. return 0;
  183. err_put_cfg1:
  184. usb_put_function(f_acm_cfg1);
  185. err_acm_inst:
  186. usb_put_function_instance(fi_acm);
  187. err_usb:
  188. gether_cleanup(the_dev);
  189. err_ether:
  190. cur_line--;
  191. while (cur_line >= 0)
  192. gserial_free_line(tty_lines[cur_line--]);
  193. gphonet_cleanup();
  194. err_phonet:
  195. return status;
  196. }
  197. static int __exit nokia_unbind(struct usb_composite_dev *cdev)
  198. {
  199. int i;
  200. usb_put_function(f_acm_cfg1);
  201. usb_put_function(f_acm_cfg2);
  202. usb_put_function_instance(fi_acm);
  203. gphonet_cleanup();
  204. for (i = 0; i < TTY_PORTS_MAX; i++)
  205. gserial_free_line(tty_lines[i]);
  206. gether_cleanup(the_dev);
  207. return 0;
  208. }
  209. static __refdata struct usb_composite_driver nokia_driver = {
  210. .name = "g_nokia",
  211. .dev = &device_desc,
  212. .strings = dev_strings,
  213. .max_speed = USB_SPEED_HIGH,
  214. .bind = nokia_bind,
  215. .unbind = __exit_p(nokia_unbind),
  216. };
  217. static int __init nokia_init(void)
  218. {
  219. return usb_composite_probe(&nokia_driver);
  220. }
  221. module_init(nokia_init);
  222. static void __exit nokia_cleanup(void)
  223. {
  224. usb_composite_unregister(&nokia_driver);
  225. }
  226. module_exit(nokia_cleanup);