option_ms.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Driver for Option High Speed Mobile Devices.
  3. *
  4. * (c) 2008 Dan Williams <dcbw@redhat.com>
  5. *
  6. * Inspiration taken from sierra_ms.c by Kevin Lloyd <klloyd@sierrawireless.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/usb.h>
  23. #include "usb.h"
  24. #include "transport.h"
  25. #include "option_ms.h"
  26. #include "debug.h"
  27. #define ZCD_FORCE_MODEM 0x01
  28. #define ZCD_ALLOW_MS 0x02
  29. static unsigned int option_zero_cd = ZCD_FORCE_MODEM;
  30. module_param(option_zero_cd, uint, S_IRUGO | S_IWUSR);
  31. MODULE_PARM_DESC(option_zero_cd, "ZeroCD mode (1=Force Modem (default),"
  32. " 2=Allow CD-Rom");
  33. #define RESPONSE_LEN 1024
  34. static int option_rezero(struct us_data *us, int ep_in, int ep_out)
  35. {
  36. const unsigned char rezero_msg[] = {
  37. 0x55, 0x53, 0x42, 0x43, 0x78, 0x56, 0x34, 0x12,
  38. 0x01, 0x00, 0x00, 0x00, 0x80, 0x00, 0x06, 0x01,
  39. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  41. };
  42. char *buffer;
  43. int result;
  44. US_DEBUGP("Option MS: %s", "DEVICE MODE SWITCH\n");
  45. buffer = kzalloc(RESPONSE_LEN, GFP_KERNEL);
  46. if (buffer == NULL)
  47. return USB_STOR_TRANSPORT_ERROR;
  48. memcpy(buffer, rezero_msg, sizeof (rezero_msg));
  49. result = usb_stor_bulk_transfer_buf(us,
  50. usb_sndbulkpipe(us->pusb_dev, ep_out),
  51. buffer, sizeof (rezero_msg), NULL);
  52. if (result != USB_STOR_XFER_GOOD) {
  53. result = USB_STOR_XFER_ERROR;
  54. goto out;
  55. }
  56. /* Some of the devices need to be asked for a response, but we don't
  57. * care what that response is.
  58. */
  59. result = usb_stor_bulk_transfer_buf(us,
  60. usb_sndbulkpipe(us->pusb_dev, ep_out),
  61. buffer, RESPONSE_LEN, NULL);
  62. result = USB_STOR_XFER_GOOD;
  63. out:
  64. kfree(buffer);
  65. return result;
  66. }
  67. int option_ms_init(struct us_data *us)
  68. {
  69. struct usb_device *udev;
  70. struct usb_interface *intf;
  71. struct usb_host_interface *iface_desc;
  72. struct usb_endpoint_descriptor *endpoint = NULL;
  73. u8 ep_in = 0, ep_out = 0;
  74. int ep_in_size = 0, ep_out_size = 0;
  75. int i, result;
  76. udev = us->pusb_dev;
  77. intf = us->pusb_intf;
  78. /* Ensure it's really a ZeroCD device; devices that are already
  79. * in modem mode return 0xFF for class, subclass, and protocol.
  80. */
  81. if (udev->descriptor.bDeviceClass != 0 ||
  82. udev->descriptor.bDeviceSubClass != 0 ||
  83. udev->descriptor.bDeviceProtocol != 0)
  84. return USB_STOR_TRANSPORT_GOOD;
  85. US_DEBUGP("Option MS: option_ms_init called\n");
  86. /* Find the right mass storage interface */
  87. iface_desc = intf->cur_altsetting;
  88. if (iface_desc->desc.bInterfaceClass != 0x8 ||
  89. iface_desc->desc.bInterfaceSubClass != 0x6 ||
  90. iface_desc->desc.bInterfaceProtocol != 0x50) {
  91. US_DEBUGP("Option MS: mass storage interface not found, no action "
  92. "required\n");
  93. return USB_STOR_TRANSPORT_GOOD;
  94. }
  95. /* Find the mass storage bulk endpoints */
  96. for (i = 0; i < iface_desc->desc.bNumEndpoints && (!ep_in_size || !ep_out_size); ++i) {
  97. endpoint = &iface_desc->endpoint[i].desc;
  98. if (usb_endpoint_is_bulk_in(endpoint)) {
  99. ep_in = usb_endpoint_num(endpoint);
  100. ep_in_size = le16_to_cpu(endpoint->wMaxPacketSize);
  101. } else if (usb_endpoint_is_bulk_out(endpoint)) {
  102. ep_out = usb_endpoint_num(endpoint);
  103. ep_out_size = le16_to_cpu(endpoint->wMaxPacketSize);
  104. }
  105. }
  106. /* Can't find the mass storage endpoints */
  107. if (!ep_in_size || !ep_out_size) {
  108. US_DEBUGP("Option MS: mass storage endpoints not found, no action "
  109. "required\n");
  110. return USB_STOR_TRANSPORT_GOOD;
  111. }
  112. /* Force Modem mode */
  113. if (option_zero_cd == ZCD_FORCE_MODEM) {
  114. US_DEBUGP("Option MS: %s", "Forcing Modem Mode\n");
  115. result = option_rezero(us, ep_in, ep_out);
  116. if (result != USB_STOR_XFER_GOOD)
  117. US_DEBUGP("Option MS: Failed to switch to modem mode.\n");
  118. return -EIO;
  119. } else if (option_zero_cd == ZCD_ALLOW_MS) {
  120. /* Allow Mass Storage mode (keep CD-Rom) */
  121. US_DEBUGP("Option MS: %s", "Allowing Mass Storage Mode if device"
  122. " requests it\n");
  123. }
  124. return USB_STOR_TRANSPORT_GOOD;
  125. }