option_ms.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * (c) 2008 Dan Williams <dcbw@redhat.com>
  6. *
  7. * Inspiration taken from sierra_ms.c by Kevin Lloyd <klloyd@sierrawireless.com>
  8. */
  9. #include <linux/usb.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include "usb.h"
  13. #include "transport.h"
  14. #include "option_ms.h"
  15. #include "debug.h"
  16. #define ZCD_FORCE_MODEM 0x01
  17. #define ZCD_ALLOW_MS 0x02
  18. static unsigned int option_zero_cd = ZCD_FORCE_MODEM;
  19. module_param(option_zero_cd, uint, S_IRUGO | S_IWUSR);
  20. MODULE_PARM_DESC(option_zero_cd, "ZeroCD mode (1=Force Modem (default),"
  21. " 2=Allow CD-Rom");
  22. #define RESPONSE_LEN 1024
  23. static int option_rezero(struct us_data *us)
  24. {
  25. static const unsigned char rezero_msg[] = {
  26. 0x55, 0x53, 0x42, 0x43, 0x78, 0x56, 0x34, 0x12,
  27. 0x01, 0x00, 0x00, 0x00, 0x80, 0x00, 0x06, 0x01,
  28. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  30. };
  31. char *buffer;
  32. int result;
  33. usb_stor_dbg(us, "Option MS: %s\n", "DEVICE MODE SWITCH");
  34. buffer = kzalloc(RESPONSE_LEN, GFP_KERNEL);
  35. if (buffer == NULL)
  36. return USB_STOR_TRANSPORT_ERROR;
  37. memcpy(buffer, rezero_msg, sizeof(rezero_msg));
  38. result = usb_stor_bulk_transfer_buf(us,
  39. us->send_bulk_pipe,
  40. buffer, sizeof(rezero_msg), NULL);
  41. if (result != USB_STOR_XFER_GOOD) {
  42. result = USB_STOR_XFER_ERROR;
  43. goto out;
  44. }
  45. /*
  46. * Some of the devices need to be asked for a response, but we don't
  47. * care what that response is.
  48. */
  49. usb_stor_bulk_transfer_buf(us,
  50. us->recv_bulk_pipe,
  51. buffer, RESPONSE_LEN, NULL);
  52. /* Read the CSW */
  53. usb_stor_bulk_transfer_buf(us,
  54. us->recv_bulk_pipe,
  55. buffer, 13, NULL);
  56. result = USB_STOR_XFER_GOOD;
  57. out:
  58. kfree(buffer);
  59. return result;
  60. }
  61. static int option_inquiry(struct us_data *us)
  62. {
  63. static const unsigned char inquiry_msg[] = {
  64. 0x55, 0x53, 0x42, 0x43, 0x12, 0x34, 0x56, 0x78,
  65. 0x24, 0x00, 0x00, 0x00, 0x80, 0x00, 0x06, 0x12,
  66. 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  68. };
  69. char *buffer;
  70. int result;
  71. usb_stor_dbg(us, "Option MS: %s\n", "device inquiry for vendor name");
  72. buffer = kzalloc(0x24, GFP_KERNEL);
  73. if (buffer == NULL)
  74. return USB_STOR_TRANSPORT_ERROR;
  75. memcpy(buffer, inquiry_msg, sizeof(inquiry_msg));
  76. result = usb_stor_bulk_transfer_buf(us,
  77. us->send_bulk_pipe,
  78. buffer, sizeof(inquiry_msg), NULL);
  79. if (result != USB_STOR_XFER_GOOD) {
  80. result = USB_STOR_XFER_ERROR;
  81. goto out;
  82. }
  83. result = usb_stor_bulk_transfer_buf(us,
  84. us->recv_bulk_pipe,
  85. buffer, 0x24, NULL);
  86. if (result != USB_STOR_XFER_GOOD) {
  87. result = USB_STOR_XFER_ERROR;
  88. goto out;
  89. }
  90. result = memcmp(buffer+8, "Option", 6);
  91. if (result != 0)
  92. result = memcmp(buffer+8, "ZCOPTION", 8);
  93. /* Read the CSW */
  94. usb_stor_bulk_transfer_buf(us,
  95. us->recv_bulk_pipe,
  96. buffer, 13, NULL);
  97. out:
  98. kfree(buffer);
  99. return result;
  100. }
  101. int option_ms_init(struct us_data *us)
  102. {
  103. int result;
  104. usb_stor_dbg(us, "Option MS: %s\n", "option_ms_init called");
  105. /*
  106. * Additional test for vendor information via INQUIRY,
  107. * because some vendor/product IDs are ambiguous
  108. */
  109. result = option_inquiry(us);
  110. if (result != 0) {
  111. usb_stor_dbg(us, "Option MS: %s\n",
  112. "vendor is not Option or not determinable, no action taken");
  113. return 0;
  114. } else
  115. usb_stor_dbg(us, "Option MS: %s\n",
  116. "this is a genuine Option device, proceeding");
  117. /* Force Modem mode */
  118. if (option_zero_cd == ZCD_FORCE_MODEM) {
  119. usb_stor_dbg(us, "Option MS: %s\n", "Forcing Modem Mode");
  120. result = option_rezero(us);
  121. if (result != USB_STOR_XFER_GOOD)
  122. usb_stor_dbg(us, "Option MS: %s\n",
  123. "Failed to switch to modem mode");
  124. return -EIO;
  125. } else if (option_zero_cd == ZCD_ALLOW_MS) {
  126. /* Allow Mass Storage mode (keep CD-Rom) */
  127. usb_stor_dbg(us, "Option MS: %s\n",
  128. "Allowing Mass Storage Mode if device requests it");
  129. }
  130. return 0;
  131. }