zaurus.c 12 KB

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