zaurus.c 12 KB

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