zaurus.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2002 Pavel Machek <[email protected]>
  4. * Copyright (C) 2002-2005 by David Brownell
  5. */
  6. // #define DEBUG // error path messages, extra info
  7. // #define VERBOSE // more; success messages
  8. #include <linux/module.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/ethtool.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/mii.h>
  13. #include <linux/crc32.h>
  14. #include <linux/usb.h>
  15. #include <linux/usb/cdc.h>
  16. #include <linux/usb/usbnet.h>
  17. /*
  18. * All known Zaurii lie about their standards conformance. At least
  19. * the earliest SA-1100 models lie by saying they support CDC Ethernet.
  20. * Some later models (especially PXA-25x and PXA-27x based ones) lie
  21. * and say they support CDC MDLM (for access to cell phone modems).
  22. *
  23. * There are non-Zaurus products that use these same protocols too.
  24. *
  25. * The annoying thing is that at the same time Sharp was developing
  26. * that annoying standards-breaking software, the Linux community had
  27. * a simple "CDC Subset" working reliably on the same SA-1100 hardware.
  28. * That is, the same functionality but not violating standards.
  29. *
  30. * The CDC Ethernet nonconformance points are troublesome to hosts
  31. * with a true CDC Ethernet implementation:
  32. * - Framing appends a CRC, which the spec says drivers "must not" do;
  33. * - Transfers data in altsetting zero, instead of altsetting 1;
  34. * - All these peripherals use the same ethernet address.
  35. *
  36. * The CDC MDLM nonconformance is less immediately troublesome, since all
  37. * MDLM implementations are quasi-proprietary anyway.
  38. */
  39. static struct sk_buff *
  40. zaurus_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  41. {
  42. int padlen;
  43. struct sk_buff *skb2;
  44. padlen = 2;
  45. if (!skb_cloned(skb)) {
  46. int tailroom = skb_tailroom(skb);
  47. if ((padlen + 4) <= tailroom)
  48. goto done;
  49. }
  50. skb2 = skb_copy_expand(skb, 0, 4 + padlen, flags);
  51. dev_kfree_skb_any(skb);
  52. skb = skb2;
  53. if (skb) {
  54. u32 fcs;
  55. done:
  56. fcs = crc32_le(~0, skb->data, skb->len);
  57. fcs = ~fcs;
  58. skb_put_u8(skb, fcs & 0xff);
  59. skb_put_u8(skb, (fcs >> 8) & 0xff);
  60. skb_put_u8(skb, (fcs >> 16) & 0xff);
  61. skb_put_u8(skb, (fcs >> 24) & 0xff);
  62. }
  63. return skb;
  64. }
  65. static int zaurus_bind(struct usbnet *dev, struct usb_interface *intf)
  66. {
  67. /* Belcarra's funky framing has other options; mostly
  68. * TRAILERS (!) with 4 bytes CRC, and maybe 2 pad bytes.
  69. */
  70. dev->net->hard_header_len += 6;
  71. dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu;
  72. return usbnet_generic_cdc_bind(dev, intf);
  73. }
  74. /* PDA style devices are always connected if present */
  75. static int always_connected (struct usbnet *dev)
  76. {
  77. return 0;
  78. }
  79. static const struct driver_info zaurus_sl5x00_info = {
  80. .description = "Sharp Zaurus SL-5x00",
  81. .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_Z,
  82. .check_connect = always_connected,
  83. .bind = zaurus_bind,
  84. .unbind = usbnet_cdc_unbind,
  85. .tx_fixup = zaurus_tx_fixup,
  86. };
  87. #define ZAURUS_STRONGARM_INFO ((unsigned long)&zaurus_sl5x00_info)
  88. static const struct driver_info zaurus_pxa_info = {
  89. .description = "Sharp Zaurus, PXA-2xx based",
  90. .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_Z,
  91. .check_connect = always_connected,
  92. .bind = zaurus_bind,
  93. .unbind = usbnet_cdc_unbind,
  94. .tx_fixup = zaurus_tx_fixup,
  95. };
  96. #define ZAURUS_PXA_INFO ((unsigned long)&zaurus_pxa_info)
  97. static const struct driver_info olympus_mxl_info = {
  98. .description = "Olympus R1000",
  99. .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_Z,
  100. .check_connect = always_connected,
  101. .bind = zaurus_bind,
  102. .unbind = usbnet_cdc_unbind,
  103. .tx_fixup = zaurus_tx_fixup,
  104. };
  105. #define OLYMPUS_MXL_INFO ((unsigned long)&olympus_mxl_info)
  106. /* Some more recent products using Lineo/Belcarra code will wrongly claim
  107. * CDC MDLM conformance. They aren't conformant: data endpoints live
  108. * in the control interface, there's no data interface, and it's not used
  109. * to talk to a cell phone radio. But at least we can detect these two
  110. * pseudo-classes, rather than growing this product list with entries for
  111. * each new nonconformant product (sigh).
  112. */
  113. static const u8 safe_guid[16] = {
  114. 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
  115. 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
  116. };
  117. static const u8 blan_guid[16] = {
  118. 0x74, 0xf0, 0x3d, 0xbd, 0x1e, 0xc1, 0x44, 0x70,
  119. 0xa3, 0x67, 0x71, 0x34, 0xc9, 0xf5, 0x54, 0x37,
  120. };
  121. static int blan_mdlm_bind(struct usbnet *dev, struct usb_interface *intf)
  122. {
  123. u8 *buf = intf->cur_altsetting->extra;
  124. int len = intf->cur_altsetting->extralen;
  125. struct usb_cdc_mdlm_desc *desc = NULL;
  126. struct usb_cdc_mdlm_detail_desc *detail = NULL;
  127. while (len > 3) {
  128. if (buf [1] != USB_DT_CS_INTERFACE)
  129. goto next_desc;
  130. /* use bDescriptorSubType, and just verify that we get a
  131. * "BLAN" (or "SAFE") descriptor.
  132. */
  133. switch (buf [2]) {
  134. case USB_CDC_MDLM_TYPE:
  135. if (desc) {
  136. dev_dbg(&intf->dev, "extra MDLM\n");
  137. goto bad_desc;
  138. }
  139. desc = (void *) buf;
  140. if (desc->bLength != sizeof *desc) {
  141. dev_dbg(&intf->dev, "MDLM len %u\n",
  142. desc->bLength);
  143. goto bad_desc;
  144. }
  145. /* expect bcdVersion 1.0, ignore */
  146. if (memcmp(&desc->bGUID, blan_guid, 16) &&
  147. memcmp(&desc->bGUID, safe_guid, 16)) {
  148. /* hey, this one might _really_ be MDLM! */
  149. dev_dbg(&intf->dev, "MDLM guid\n");
  150. goto bad_desc;
  151. }
  152. break;
  153. case USB_CDC_MDLM_DETAIL_TYPE:
  154. if (detail) {
  155. dev_dbg(&intf->dev, "extra MDLM detail\n");
  156. goto bad_desc;
  157. }
  158. detail = (void *) buf;
  159. switch (detail->bGuidDescriptorType) {
  160. case 0: /* "SAFE" */
  161. if (detail->bLength != (sizeof *detail + 2))
  162. goto bad_detail;
  163. break;
  164. case 1: /* "BLAN" */
  165. if (detail->bLength != (sizeof *detail + 3))
  166. goto bad_detail;
  167. break;
  168. default:
  169. goto bad_detail;
  170. }
  171. /* assuming we either noticed BLAN already, or will
  172. * find it soon, there are some data bytes here:
  173. * - bmNetworkCapabilities (unused)
  174. * - bmDataCapabilities (bits, see below)
  175. * - bPad (ignored, for PADAFTER -- BLAN-only)
  176. * bits are:
  177. * - 0x01 -- Zaurus framing (add CRC)
  178. * - 0x02 -- PADBEFORE (CRC includes some padding)
  179. * - 0x04 -- PADAFTER (some padding after CRC)
  180. * - 0x08 -- "fermat" packet mangling (for hw bugs)
  181. * the PADBEFORE appears not to matter; we interop
  182. * with devices that use it and those that don't.
  183. */
  184. if ((detail->bDetailData[1] & ~0x02) != 0x01) {
  185. /* bmDataCapabilities == 0 would be fine too,
  186. * but framing is minidriver-coupled for now.
  187. */
  188. bad_detail:
  189. dev_dbg(&intf->dev,
  190. "bad MDLM detail, %d %d %d\n",
  191. detail->bLength,
  192. detail->bDetailData[0],
  193. detail->bDetailData[2]);
  194. goto bad_desc;
  195. }
  196. /* same extra framing as for non-BLAN mode */
  197. dev->net->hard_header_len += 6;
  198. dev->rx_urb_size = dev->net->hard_header_len
  199. + dev->net->mtu;
  200. break;
  201. }
  202. next_desc:
  203. len -= buf [0]; /* bLength */
  204. buf += buf [0];
  205. }
  206. if (!desc || !detail) {
  207. dev_dbg(&intf->dev, "missing cdc mdlm %s%sdescriptor\n",
  208. desc ? "" : "func ",
  209. detail ? "" : "detail ");
  210. goto bad_desc;
  211. }
  212. /* There's probably a CDC Ethernet descriptor there, but we can't
  213. * rely on the Ethernet address it provides since not all vendors
  214. * bother to make it unique. Likewise there's no point in tracking
  215. * of the CDC event notifications.
  216. */
  217. return usbnet_get_endpoints(dev, intf);
  218. bad_desc:
  219. dev_info(&dev->udev->dev, "unsupported MDLM descriptors\n");
  220. return -ENODEV;
  221. }
  222. static const struct driver_info bogus_mdlm_info = {
  223. .description = "pseudo-MDLM (BLAN) device",
  224. .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_Z,
  225. .check_connect = always_connected,
  226. .tx_fixup = zaurus_tx_fixup,
  227. .bind = blan_mdlm_bind,
  228. };
  229. static const struct usb_device_id products [] = {
  230. #define ZAURUS_MASTER_INTERFACE \
  231. .bInterfaceClass = USB_CLASS_COMM, \
  232. .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
  233. .bInterfaceProtocol = USB_CDC_PROTO_NONE
  234. #define ZAURUS_FAKE_INTERFACE \
  235. .bInterfaceClass = USB_CLASS_COMM, \
  236. .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, \
  237. .bInterfaceProtocol = USB_CDC_PROTO_NONE
  238. /* SA-1100 based Sharp Zaurus ("collie"), or compatible. */
  239. {
  240. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  241. | USB_DEVICE_ID_MATCH_DEVICE,
  242. .idVendor = 0x04DD,
  243. .idProduct = 0x8004,
  244. ZAURUS_MASTER_INTERFACE,
  245. .driver_info = ZAURUS_STRONGARM_INFO,
  246. },
  247. /* PXA-2xx based models are also lying-about-cdc. If you add any
  248. * more devices that claim to be CDC Ethernet, make sure they get
  249. * added to the blacklist in cdc_ether too.
  250. *
  251. * NOTE: OpenZaurus versions with 2.6 kernels won't use these entries,
  252. * unlike the older ones with 2.4 "embedix" kernels.
  253. */
  254. {
  255. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  256. | USB_DEVICE_ID_MATCH_DEVICE,
  257. .idVendor = 0x04DD,
  258. .idProduct = 0x8005, /* A-300 */
  259. ZAURUS_MASTER_INTERFACE,
  260. .driver_info = ZAURUS_PXA_INFO,
  261. }, {
  262. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  263. | USB_DEVICE_ID_MATCH_DEVICE,
  264. .idVendor = 0x04DD,
  265. .idProduct = 0x8005, /* A-300 */
  266. ZAURUS_FAKE_INTERFACE,
  267. .driver_info = (unsigned long)&bogus_mdlm_info,
  268. }, {
  269. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  270. | USB_DEVICE_ID_MATCH_DEVICE,
  271. .idVendor = 0x04DD,
  272. .idProduct = 0x8006, /* B-500/SL-5600 */
  273. ZAURUS_MASTER_INTERFACE,
  274. .driver_info = ZAURUS_PXA_INFO,
  275. }, {
  276. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  277. | USB_DEVICE_ID_MATCH_DEVICE,
  278. .idVendor = 0x04DD,
  279. .idProduct = 0x8006, /* B-500/SL-5600 */
  280. ZAURUS_FAKE_INTERFACE,
  281. .driver_info = (unsigned long)&bogus_mdlm_info,
  282. }, {
  283. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  284. | USB_DEVICE_ID_MATCH_DEVICE,
  285. .idVendor = 0x04DD,
  286. .idProduct = 0x8007, /* C-700 */
  287. ZAURUS_MASTER_INTERFACE,
  288. .driver_info = ZAURUS_PXA_INFO,
  289. }, {
  290. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  291. | USB_DEVICE_ID_MATCH_DEVICE,
  292. .idVendor = 0x04DD,
  293. .idProduct = 0x8007, /* C-700 */
  294. ZAURUS_FAKE_INTERFACE,
  295. .driver_info = (unsigned long)&bogus_mdlm_info,
  296. }, {
  297. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  298. | USB_DEVICE_ID_MATCH_DEVICE,
  299. .idVendor = 0x04DD,
  300. .idProduct = 0x9031, /* C-750 C-760 */
  301. ZAURUS_MASTER_INTERFACE,
  302. .driver_info = ZAURUS_PXA_INFO,
  303. }, {
  304. /* C-750/C-760/C-860/SL-C3000 PDA in MDLM mode */
  305. USB_DEVICE_AND_INTERFACE_INFO(0x04DD, 0x9031, USB_CLASS_COMM,
  306. USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
  307. .driver_info = (unsigned long) &bogus_mdlm_info,
  308. }, {
  309. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  310. | USB_DEVICE_ID_MATCH_DEVICE,
  311. .idVendor = 0x04DD,
  312. .idProduct = 0x9032, /* SL-6000 */
  313. ZAURUS_MASTER_INTERFACE,
  314. .driver_info = ZAURUS_PXA_INFO,
  315. }, {
  316. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  317. | USB_DEVICE_ID_MATCH_DEVICE,
  318. .idVendor = 0x04DD,
  319. .idProduct = 0x9032, /* SL-6000 */
  320. ZAURUS_FAKE_INTERFACE,
  321. .driver_info = (unsigned long)&bogus_mdlm_info,
  322. }, {
  323. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  324. | USB_DEVICE_ID_MATCH_DEVICE,
  325. .idVendor = 0x04DD,
  326. /* reported with some C860 units */
  327. .idProduct = 0x9050, /* C-860 */
  328. ZAURUS_MASTER_INTERFACE,
  329. .driver_info = ZAURUS_PXA_INFO,
  330. },
  331. {
  332. /* Motorola Rokr E6 */
  333. USB_DEVICE_AND_INTERFACE_INFO(0x22b8, 0x6027, USB_CLASS_COMM,
  334. USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
  335. .driver_info = (unsigned long) &bogus_mdlm_info,
  336. }, {
  337. /* Motorola MOTOMAGX phones */
  338. USB_DEVICE_AND_INTERFACE_INFO(0x22b8, 0x6425, USB_CLASS_COMM,
  339. USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
  340. .driver_info = (unsigned long) &bogus_mdlm_info,
  341. },
  342. /* Olympus has some models with a Zaurus-compatible option.
  343. * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
  344. */
  345. {
  346. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  347. | USB_DEVICE_ID_MATCH_DEVICE,
  348. .idVendor = 0x07B4,
  349. .idProduct = 0x0F02, /* R-1000 */
  350. ZAURUS_MASTER_INTERFACE,
  351. .driver_info = OLYMPUS_MXL_INFO,
  352. },
  353. /* Logitech Harmony 900 - uses the pseudo-MDLM (BLAN) driver */
  354. {
  355. USB_DEVICE_AND_INTERFACE_INFO(0x046d, 0xc11f, USB_CLASS_COMM,
  356. USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
  357. .driver_info = (unsigned long) &bogus_mdlm_info,
  358. },
  359. { }, // END
  360. };
  361. MODULE_DEVICE_TABLE(usb, products);
  362. static struct usb_driver zaurus_driver = {
  363. .name = "zaurus",
  364. .id_table = products,
  365. .probe = usbnet_probe,
  366. .disconnect = usbnet_disconnect,
  367. .suspend = usbnet_suspend,
  368. .resume = usbnet_resume,
  369. .disable_hub_initiated_lpm = 1,
  370. };
  371. module_usb_driver(zaurus_driver);
  372. MODULE_AUTHOR("Pavel Machek, David Brownell");
  373. MODULE_DESCRIPTION("Sharp Zaurus PDA, and compatible products");
  374. MODULE_LICENSE("GPL");