sierra_net.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * USB-to-WWAN Driver for Sierra Wireless modems
  3. *
  4. * Copyright (C) 2008, 2009, 2010 Paxton Smith, Matthew Safar, Rory Filer
  5. * <linux@sierrawireless.com>
  6. *
  7. * Portions of this based on the cdc_ether driver by David Brownell (2003-2005)
  8. * and Ole Andre Vadla Ravnas (ActiveSync) (2006).
  9. *
  10. * IMPORTANT DISCLAIMER: This driver is not commercially supported by
  11. * Sierra Wireless. Use at your own risk.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #define DRIVER_VERSION "v.2.0"
  28. #define DRIVER_AUTHOR "Paxton Smith, Matthew Safar, Rory Filer"
  29. #define DRIVER_DESC "USB-to-WWAN Driver for Sierra Wireless modems"
  30. static const char driver_name[] = "sierra_net";
  31. /* if defined debug messages enabled */
  32. /*#define DEBUG*/
  33. #include <linux/module.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/ethtool.h>
  36. #include <linux/mii.h>
  37. #include <linux/sched.h>
  38. #include <linux/timer.h>
  39. #include <linux/usb.h>
  40. #include <linux/usb/cdc.h>
  41. #include <net/ip.h>
  42. #include <net/udp.h>
  43. #include <asm/unaligned.h>
  44. #include <linux/usb/usbnet.h>
  45. #define SWI_USB_REQUEST_GET_FW_ATTR 0x06
  46. #define SWI_GET_FW_ATTR_MASK 0x08
  47. /* atomic counter partially included in MAC address to make sure 2 devices
  48. * do not end up with the same MAC - concept breaks in case of > 255 ifaces
  49. */
  50. static atomic_t iface_counter = ATOMIC_INIT(0);
  51. /*
  52. * SYNC Timer Delay definition used to set the expiry time
  53. */
  54. #define SIERRA_NET_SYNCDELAY (2*HZ)
  55. /* Max. MTU supported. The modem buffers are limited to 1500 */
  56. #define SIERRA_NET_MAX_SUPPORTED_MTU 1500
  57. /* The SIERRA_NET_USBCTL_BUF_LEN defines a buffer size allocated for control
  58. * message reception ... and thus the max. received packet.
  59. * (May be the cause for parse_hip returning -EINVAL)
  60. */
  61. #define SIERRA_NET_USBCTL_BUF_LEN 1024
  62. /* list of interface numbers - used for constructing interface lists */
  63. struct sierra_net_iface_info {
  64. const u32 infolen; /* number of interface numbers on list */
  65. const u8 *ifaceinfo; /* pointer to the array holding the numbers */
  66. };
  67. struct sierra_net_info_data {
  68. u16 rx_urb_size;
  69. struct sierra_net_iface_info whitelist;
  70. };
  71. /* Private data structure */
  72. struct sierra_net_data {
  73. u8 ethr_hdr_tmpl[ETH_HLEN]; /* ethernet header template for rx'd pkts */
  74. u16 link_up; /* air link up or down */
  75. u8 tx_hdr_template[4]; /* part of HIP hdr for tx'd packets */
  76. u8 sync_msg[4]; /* SYNC message */
  77. u8 shdwn_msg[4]; /* Shutdown message */
  78. /* Backpointer to the container */
  79. struct usbnet *usbnet;
  80. u8 ifnum; /* interface number */
  81. /* Bit masks, must be a power of 2 */
  82. #define SIERRA_NET_EVENT_RESP_AVAIL 0x01
  83. #define SIERRA_NET_TIMER_EXPIRY 0x02
  84. unsigned long kevent_flags;
  85. struct work_struct sierra_net_kevent;
  86. struct timer_list sync_timer; /* For retrying SYNC sequence */
  87. };
  88. struct param {
  89. int is_present;
  90. union {
  91. void *ptr;
  92. u32 dword;
  93. u16 word;
  94. u8 byte;
  95. };
  96. };
  97. /* HIP message type */
  98. #define SIERRA_NET_HIP_EXTENDEDID 0x7F
  99. #define SIERRA_NET_HIP_HSYNC_ID 0x60 /* Modem -> host */
  100. #define SIERRA_NET_HIP_RESTART_ID 0x62 /* Modem -> host */
  101. #define SIERRA_NET_HIP_MSYNC_ID 0x20 /* Host -> modem */
  102. #define SIERRA_NET_HIP_SHUTD_ID 0x26 /* Host -> modem */
  103. #define SIERRA_NET_HIP_EXT_IP_IN_ID 0x0202
  104. #define SIERRA_NET_HIP_EXT_IP_OUT_ID 0x0002
  105. /* 3G UMTS Link Sense Indication definitions */
  106. #define SIERRA_NET_HIP_LSI_UMTSID 0x78
  107. /* Reverse Channel Grant Indication HIP message */
  108. #define SIERRA_NET_HIP_RCGI 0x64
  109. /* LSI Protocol types */
  110. #define SIERRA_NET_PROTOCOL_UMTS 0x01
  111. /* LSI Coverage */
  112. #define SIERRA_NET_COVERAGE_NONE 0x00
  113. #define SIERRA_NET_COVERAGE_NOPACKET 0x01
  114. /* LSI Session */
  115. #define SIERRA_NET_SESSION_IDLE 0x00
  116. /* LSI Link types */
  117. #define SIERRA_NET_AS_LINK_TYPE_IPv4 0x00
  118. struct lsi_umts {
  119. u8 protocol;
  120. u8 unused1;
  121. __be16 length;
  122. /* eventually use a union for the rest - assume umts for now */
  123. u8 coverage;
  124. u8 unused2[41];
  125. u8 session_state;
  126. u8 unused3[33];
  127. u8 link_type;
  128. u8 pdp_addr_len; /* NW-supplied PDP address len */
  129. u8 pdp_addr[16]; /* NW-supplied PDP address (bigendian)) */
  130. u8 unused4[23];
  131. u8 dns1_addr_len; /* NW-supplied 1st DNS address len (bigendian) */
  132. u8 dns1_addr[16]; /* NW-supplied 1st DNS address */
  133. u8 dns2_addr_len; /* NW-supplied 2nd DNS address len */
  134. u8 dns2_addr[16]; /* NW-supplied 2nd DNS address (bigendian)*/
  135. u8 wins1_addr_len; /* NW-supplied 1st Wins address len */
  136. u8 wins1_addr[16]; /* NW-supplied 1st Wins address (bigendian)*/
  137. u8 wins2_addr_len; /* NW-supplied 2nd Wins address len */
  138. u8 wins2_addr[16]; /* NW-supplied 2nd Wins address (bigendian) */
  139. u8 unused5[4];
  140. u8 gw_addr_len; /* NW-supplied GW address len */
  141. u8 gw_addr[16]; /* NW-supplied GW address (bigendian) */
  142. u8 reserved[8];
  143. } __packed;
  144. #define SIERRA_NET_LSI_COMMON_LEN 4
  145. #define SIERRA_NET_LSI_UMTS_LEN (sizeof(struct lsi_umts))
  146. #define SIERRA_NET_LSI_UMTS_STATUS_LEN \
  147. (SIERRA_NET_LSI_UMTS_LEN - SIERRA_NET_LSI_COMMON_LEN)
  148. /* Forward definitions */
  149. static void sierra_sync_timer(unsigned long syncdata);
  150. static int sierra_net_change_mtu(struct net_device *net, int new_mtu);
  151. /* Our own net device operations structure */
  152. static const struct net_device_ops sierra_net_device_ops = {
  153. .ndo_open = usbnet_open,
  154. .ndo_stop = usbnet_stop,
  155. .ndo_start_xmit = usbnet_start_xmit,
  156. .ndo_tx_timeout = usbnet_tx_timeout,
  157. .ndo_change_mtu = sierra_net_change_mtu,
  158. .ndo_set_mac_address = eth_mac_addr,
  159. .ndo_validate_addr = eth_validate_addr,
  160. };
  161. /* get private data associated with passed in usbnet device */
  162. static inline struct sierra_net_data *sierra_net_get_private(struct usbnet *dev)
  163. {
  164. return (struct sierra_net_data *)dev->data[0];
  165. }
  166. /* set private data associated with passed in usbnet device */
  167. static inline void sierra_net_set_private(struct usbnet *dev,
  168. struct sierra_net_data *priv)
  169. {
  170. dev->data[0] = (unsigned long)priv;
  171. }
  172. /* is packet IPv4 */
  173. static inline int is_ip(struct sk_buff *skb)
  174. {
  175. return skb->protocol == cpu_to_be16(ETH_P_IP);
  176. }
  177. /*
  178. * check passed in packet and make sure that:
  179. * - it is linear (no scatter/gather)
  180. * - it is ethernet (mac_header properly set)
  181. */
  182. static int check_ethip_packet(struct sk_buff *skb, struct usbnet *dev)
  183. {
  184. skb_reset_mac_header(skb); /* ethernet header */
  185. if (skb_is_nonlinear(skb)) {
  186. netdev_err(dev->net, "Non linear buffer-dropping\n");
  187. return 0;
  188. }
  189. if (!pskb_may_pull(skb, ETH_HLEN))
  190. return 0;
  191. skb->protocol = eth_hdr(skb)->h_proto;
  192. return 1;
  193. }
  194. static const u8 *save16bit(struct param *p, const u8 *datap)
  195. {
  196. p->is_present = 1;
  197. p->word = get_unaligned_be16(datap);
  198. return datap + sizeof(p->word);
  199. }
  200. static const u8 *save8bit(struct param *p, const u8 *datap)
  201. {
  202. p->is_present = 1;
  203. p->byte = *datap;
  204. return datap + sizeof(p->byte);
  205. }
  206. /*----------------------------------------------------------------------------*
  207. * BEGIN HIP *
  208. *----------------------------------------------------------------------------*/
  209. /* HIP header */
  210. #define SIERRA_NET_HIP_HDR_LEN 4
  211. /* Extended HIP header */
  212. #define SIERRA_NET_HIP_EXT_HDR_LEN 6
  213. struct hip_hdr {
  214. int hdrlen;
  215. struct param payload_len;
  216. struct param msgid;
  217. struct param msgspecific;
  218. struct param extmsgid;
  219. };
  220. static int parse_hip(const u8 *buf, const u32 buflen, struct hip_hdr *hh)
  221. {
  222. const u8 *curp = buf;
  223. int padded;
  224. if (buflen < SIERRA_NET_HIP_HDR_LEN)
  225. return -EPROTO;
  226. curp = save16bit(&hh->payload_len, curp);
  227. curp = save8bit(&hh->msgid, curp);
  228. curp = save8bit(&hh->msgspecific, curp);
  229. padded = hh->msgid.byte & 0x80;
  230. hh->msgid.byte &= 0x7F; /* 7 bits */
  231. hh->extmsgid.is_present = (hh->msgid.byte == SIERRA_NET_HIP_EXTENDEDID);
  232. if (hh->extmsgid.is_present) {
  233. if (buflen < SIERRA_NET_HIP_EXT_HDR_LEN)
  234. return -EPROTO;
  235. hh->payload_len.word &= 0x3FFF; /* 14 bits */
  236. curp = save16bit(&hh->extmsgid, curp);
  237. hh->extmsgid.word &= 0x03FF; /* 10 bits */
  238. hh->hdrlen = SIERRA_NET_HIP_EXT_HDR_LEN;
  239. } else {
  240. hh->payload_len.word &= 0x07FF; /* 11 bits */
  241. hh->hdrlen = SIERRA_NET_HIP_HDR_LEN;
  242. }
  243. if (padded) {
  244. hh->hdrlen++;
  245. hh->payload_len.word--;
  246. }
  247. /* if real packet shorter than the claimed length */
  248. if (buflen < (hh->hdrlen + hh->payload_len.word))
  249. return -EINVAL;
  250. return 0;
  251. }
  252. static void build_hip(u8 *buf, const u16 payloadlen,
  253. struct sierra_net_data *priv)
  254. {
  255. /* the following doesn't have the full functionality. We
  256. * currently build only one kind of header, so it is faster this way
  257. */
  258. put_unaligned_be16(payloadlen, buf);
  259. memcpy(buf+2, priv->tx_hdr_template, sizeof(priv->tx_hdr_template));
  260. }
  261. /*----------------------------------------------------------------------------*
  262. * END HIP *
  263. *----------------------------------------------------------------------------*/
  264. static int sierra_net_send_cmd(struct usbnet *dev,
  265. u8 *cmd, int cmdlen, const char * cmd_name)
  266. {
  267. struct sierra_net_data *priv = sierra_net_get_private(dev);
  268. int status;
  269. status = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  270. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  271. USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE, 0,
  272. priv->ifnum, cmd, cmdlen, USB_CTRL_SET_TIMEOUT);
  273. if (status != cmdlen && status != -ENODEV)
  274. netdev_err(dev->net, "Submit %s failed %d\n", cmd_name, status);
  275. return status;
  276. }
  277. static int sierra_net_send_sync(struct usbnet *dev)
  278. {
  279. int status;
  280. struct sierra_net_data *priv = sierra_net_get_private(dev);
  281. dev_dbg(&dev->udev->dev, "%s", __func__);
  282. status = sierra_net_send_cmd(dev, priv->sync_msg,
  283. sizeof(priv->sync_msg), "SYNC");
  284. return status;
  285. }
  286. static void sierra_net_set_ctx_index(struct sierra_net_data *priv, u8 ctx_ix)
  287. {
  288. dev_dbg(&(priv->usbnet->udev->dev), "%s %d", __func__, ctx_ix);
  289. priv->tx_hdr_template[0] = 0x3F;
  290. priv->tx_hdr_template[1] = ctx_ix;
  291. *((u16 *)&priv->tx_hdr_template[2]) =
  292. cpu_to_be16(SIERRA_NET_HIP_EXT_IP_OUT_ID);
  293. }
  294. static inline int sierra_net_is_valid_addrlen(u8 len)
  295. {
  296. return len == sizeof(struct in_addr);
  297. }
  298. static int sierra_net_parse_lsi(struct usbnet *dev, char *data, int datalen)
  299. {
  300. struct lsi_umts *lsi = (struct lsi_umts *)data;
  301. if (datalen < sizeof(struct lsi_umts)) {
  302. netdev_err(dev->net, "%s: Data length %d, exp %Zu\n",
  303. __func__, datalen,
  304. sizeof(struct lsi_umts));
  305. return -1;
  306. }
  307. if (lsi->length != cpu_to_be16(SIERRA_NET_LSI_UMTS_STATUS_LEN)) {
  308. netdev_err(dev->net, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n",
  309. __func__, be16_to_cpu(lsi->length),
  310. (u32)SIERRA_NET_LSI_UMTS_STATUS_LEN);
  311. return -1;
  312. }
  313. /* Validate the protocol - only support UMTS for now */
  314. if (lsi->protocol != SIERRA_NET_PROTOCOL_UMTS) {
  315. netdev_err(dev->net, "Protocol unsupported, 0x%02x\n",
  316. lsi->protocol);
  317. return -1;
  318. }
  319. /* Validate the link type */
  320. if (lsi->link_type != SIERRA_NET_AS_LINK_TYPE_IPv4) {
  321. netdev_err(dev->net, "Link type unsupported: 0x%02x\n",
  322. lsi->link_type);
  323. return -1;
  324. }
  325. /* Validate the coverage */
  326. if (lsi->coverage == SIERRA_NET_COVERAGE_NONE
  327. || lsi->coverage == SIERRA_NET_COVERAGE_NOPACKET) {
  328. netdev_err(dev->net, "No coverage, 0x%02x\n", lsi->coverage);
  329. return 0;
  330. }
  331. /* Validate the session state */
  332. if (lsi->session_state == SIERRA_NET_SESSION_IDLE) {
  333. netdev_err(dev->net, "Session idle, 0x%02x\n",
  334. lsi->session_state);
  335. return 0;
  336. }
  337. /* Set link_sense true */
  338. return 1;
  339. }
  340. static void sierra_net_handle_lsi(struct usbnet *dev, char *data,
  341. struct hip_hdr *hh)
  342. {
  343. struct sierra_net_data *priv = sierra_net_get_private(dev);
  344. int link_up;
  345. link_up = sierra_net_parse_lsi(dev, data + hh->hdrlen,
  346. hh->payload_len.word);
  347. if (link_up < 0) {
  348. netdev_err(dev->net, "Invalid LSI\n");
  349. return;
  350. }
  351. if (link_up) {
  352. sierra_net_set_ctx_index(priv, hh->msgspecific.byte);
  353. priv->link_up = 1;
  354. netif_carrier_on(dev->net);
  355. } else {
  356. priv->link_up = 0;
  357. netif_carrier_off(dev->net);
  358. }
  359. }
  360. static void sierra_net_dosync(struct usbnet *dev)
  361. {
  362. int status;
  363. struct sierra_net_data *priv = sierra_net_get_private(dev);
  364. dev_dbg(&dev->udev->dev, "%s", __func__);
  365. /* tell modem we are ready */
  366. status = sierra_net_send_sync(dev);
  367. if (status < 0)
  368. netdev_err(dev->net,
  369. "Send SYNC failed, status %d\n", status);
  370. status = sierra_net_send_sync(dev);
  371. if (status < 0)
  372. netdev_err(dev->net,
  373. "Send SYNC failed, status %d\n", status);
  374. /* Now, start a timer and make sure we get the Restart Indication */
  375. priv->sync_timer.function = sierra_sync_timer;
  376. priv->sync_timer.data = (unsigned long) dev;
  377. priv->sync_timer.expires = jiffies + SIERRA_NET_SYNCDELAY;
  378. add_timer(&priv->sync_timer);
  379. }
  380. static void sierra_net_kevent(struct work_struct *work)
  381. {
  382. struct sierra_net_data *priv =
  383. container_of(work, struct sierra_net_data, sierra_net_kevent);
  384. struct usbnet *dev = priv->usbnet;
  385. int len;
  386. int err;
  387. u8 *buf;
  388. u8 ifnum;
  389. if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags)) {
  390. clear_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags);
  391. /* Query the modem for the LSI message */
  392. buf = kzalloc(SIERRA_NET_USBCTL_BUF_LEN, GFP_KERNEL);
  393. if (!buf) {
  394. netdev_err(dev->net,
  395. "failed to allocate buf for LS msg\n");
  396. return;
  397. }
  398. ifnum = priv->ifnum;
  399. len = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  400. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  401. USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
  402. 0, ifnum, buf, SIERRA_NET_USBCTL_BUF_LEN,
  403. USB_CTRL_SET_TIMEOUT);
  404. if (len < 0) {
  405. netdev_err(dev->net,
  406. "usb_control_msg failed, status %d\n", len);
  407. } else {
  408. struct hip_hdr hh;
  409. dev_dbg(&dev->udev->dev, "%s: Received status message,"
  410. " %04x bytes", __func__, len);
  411. err = parse_hip(buf, len, &hh);
  412. if (err) {
  413. netdev_err(dev->net, "%s: Bad packet,"
  414. " parse result %d\n", __func__, err);
  415. kfree(buf);
  416. return;
  417. }
  418. /* Validate packet length */
  419. if (len != hh.hdrlen + hh.payload_len.word) {
  420. netdev_err(dev->net, "%s: Bad packet, received"
  421. " %d, expected %d\n", __func__, len,
  422. hh.hdrlen + hh.payload_len.word);
  423. kfree(buf);
  424. return;
  425. }
  426. /* Switch on received message types */
  427. switch (hh.msgid.byte) {
  428. case SIERRA_NET_HIP_LSI_UMTSID:
  429. dev_dbg(&dev->udev->dev, "LSI for ctx:%d",
  430. hh.msgspecific.byte);
  431. sierra_net_handle_lsi(dev, buf, &hh);
  432. break;
  433. case SIERRA_NET_HIP_RESTART_ID:
  434. dev_dbg(&dev->udev->dev, "Restart reported: %d,"
  435. " stopping sync timer",
  436. hh.msgspecific.byte);
  437. /* Got sync resp - stop timer & clear mask */
  438. del_timer_sync(&priv->sync_timer);
  439. clear_bit(SIERRA_NET_TIMER_EXPIRY,
  440. &priv->kevent_flags);
  441. break;
  442. case SIERRA_NET_HIP_HSYNC_ID:
  443. dev_dbg(&dev->udev->dev, "SYNC received");
  444. err = sierra_net_send_sync(dev);
  445. if (err < 0)
  446. netdev_err(dev->net,
  447. "Send SYNC failed %d\n", err);
  448. break;
  449. case SIERRA_NET_HIP_EXTENDEDID:
  450. netdev_err(dev->net, "Unrecognized HIP msg, "
  451. "extmsgid 0x%04x\n", hh.extmsgid.word);
  452. break;
  453. case SIERRA_NET_HIP_RCGI:
  454. /* Ignored */
  455. break;
  456. default:
  457. netdev_err(dev->net, "Unrecognized HIP msg, "
  458. "msgid 0x%02x\n", hh.msgid.byte);
  459. break;
  460. }
  461. }
  462. kfree(buf);
  463. }
  464. /* The sync timer bit might be set */
  465. if (test_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags)) {
  466. clear_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags);
  467. dev_dbg(&dev->udev->dev, "Deferred sync timer expiry");
  468. sierra_net_dosync(priv->usbnet);
  469. }
  470. if (priv->kevent_flags)
  471. dev_dbg(&dev->udev->dev, "sierra_net_kevent done, "
  472. "kevent_flags = 0x%lx", priv->kevent_flags);
  473. }
  474. static void sierra_net_defer_kevent(struct usbnet *dev, int work)
  475. {
  476. struct sierra_net_data *priv = sierra_net_get_private(dev);
  477. set_bit(work, &priv->kevent_flags);
  478. schedule_work(&priv->sierra_net_kevent);
  479. }
  480. /*
  481. * Sync Retransmit Timer Handler. On expiry, kick the work queue
  482. */
  483. void sierra_sync_timer(unsigned long syncdata)
  484. {
  485. struct usbnet *dev = (struct usbnet *)syncdata;
  486. dev_dbg(&dev->udev->dev, "%s", __func__);
  487. /* Kick the tasklet */
  488. sierra_net_defer_kevent(dev, SIERRA_NET_TIMER_EXPIRY);
  489. }
  490. static void sierra_net_status(struct usbnet *dev, struct urb *urb)
  491. {
  492. struct usb_cdc_notification *event;
  493. dev_dbg(&dev->udev->dev, "%s", __func__);
  494. if (urb->actual_length < sizeof *event)
  495. return;
  496. /* Add cases to handle other standard notifications. */
  497. event = urb->transfer_buffer;
  498. switch (event->bNotificationType) {
  499. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  500. case USB_CDC_NOTIFY_SPEED_CHANGE:
  501. /* USB 305 sends those */
  502. break;
  503. case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
  504. sierra_net_defer_kevent(dev, SIERRA_NET_EVENT_RESP_AVAIL);
  505. break;
  506. default:
  507. netdev_err(dev->net, ": unexpected notification %02x!\n",
  508. event->bNotificationType);
  509. break;
  510. }
  511. }
  512. static void sierra_net_get_drvinfo(struct net_device *net,
  513. struct ethtool_drvinfo *info)
  514. {
  515. /* Inherit standard device info */
  516. usbnet_get_drvinfo(net, info);
  517. strncpy(info->driver, driver_name, sizeof info->driver);
  518. strncpy(info->version, DRIVER_VERSION, sizeof info->version);
  519. }
  520. static u32 sierra_net_get_link(struct net_device *net)
  521. {
  522. struct usbnet *dev = netdev_priv(net);
  523. /* Report link is down whenever the interface is down */
  524. return sierra_net_get_private(dev)->link_up && netif_running(net);
  525. }
  526. static struct ethtool_ops sierra_net_ethtool_ops = {
  527. .get_drvinfo = sierra_net_get_drvinfo,
  528. .get_link = sierra_net_get_link,
  529. .get_msglevel = usbnet_get_msglevel,
  530. .set_msglevel = usbnet_set_msglevel,
  531. .get_settings = usbnet_get_settings,
  532. .set_settings = usbnet_set_settings,
  533. .nway_reset = usbnet_nway_reset,
  534. };
  535. /* MTU can not be more than 1500 bytes, enforce it. */
  536. static int sierra_net_change_mtu(struct net_device *net, int new_mtu)
  537. {
  538. if (new_mtu > SIERRA_NET_MAX_SUPPORTED_MTU)
  539. return -EINVAL;
  540. return usbnet_change_mtu(net, new_mtu);
  541. }
  542. static int is_whitelisted(const u8 ifnum,
  543. const struct sierra_net_iface_info *whitelist)
  544. {
  545. if (whitelist) {
  546. const u8 *list = whitelist->ifaceinfo;
  547. int i;
  548. for (i = 0; i < whitelist->infolen; i++) {
  549. if (list[i] == ifnum)
  550. return 1;
  551. }
  552. }
  553. return 0;
  554. }
  555. static int sierra_net_get_fw_attr(struct usbnet *dev, u16 *datap)
  556. {
  557. int result = 0;
  558. u16 *attrdata;
  559. attrdata = kmalloc(sizeof(*attrdata), GFP_KERNEL);
  560. if (!attrdata)
  561. return -ENOMEM;
  562. result = usb_control_msg(
  563. dev->udev,
  564. usb_rcvctrlpipe(dev->udev, 0),
  565. /* _u8 vendor specific request */
  566. SWI_USB_REQUEST_GET_FW_ATTR,
  567. USB_DIR_IN | USB_TYPE_VENDOR, /* __u8 request type */
  568. 0x0000, /* __u16 value not used */
  569. 0x0000, /* __u16 index not used */
  570. attrdata, /* char *data */
  571. sizeof(*attrdata), /* __u16 size */
  572. USB_CTRL_SET_TIMEOUT); /* int timeout */
  573. if (result < 0) {
  574. kfree(attrdata);
  575. return -EIO;
  576. }
  577. *datap = *attrdata;
  578. kfree(attrdata);
  579. return result;
  580. }
  581. /*
  582. * collects the bulk endpoints, the status endpoint.
  583. */
  584. static int sierra_net_bind(struct usbnet *dev, struct usb_interface *intf)
  585. {
  586. u8 ifacenum;
  587. u8 numendpoints;
  588. u16 fwattr = 0;
  589. int status;
  590. struct ethhdr *eth;
  591. struct sierra_net_data *priv;
  592. static const u8 sync_tmplate[sizeof(priv->sync_msg)] = {
  593. 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID, 0x00};
  594. static const u8 shdwn_tmplate[sizeof(priv->shdwn_msg)] = {
  595. 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID, 0x00};
  596. struct sierra_net_info_data *data =
  597. (struct sierra_net_info_data *)dev->driver_info->data;
  598. dev_dbg(&dev->udev->dev, "%s", __func__);
  599. ifacenum = intf->cur_altsetting->desc.bInterfaceNumber;
  600. /* We only accept certain interfaces */
  601. if (!is_whitelisted(ifacenum, &data->whitelist)) {
  602. dev_dbg(&dev->udev->dev, "Ignoring interface: %d", ifacenum);
  603. return -ENODEV;
  604. }
  605. numendpoints = intf->cur_altsetting->desc.bNumEndpoints;
  606. /* We have three endpoints, bulk in and out, and a status */
  607. if (numendpoints != 3) {
  608. dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d",
  609. numendpoints);
  610. return -ENODEV;
  611. }
  612. /* Status endpoint set in usbnet_get_endpoints() */
  613. dev->status = NULL;
  614. status = usbnet_get_endpoints(dev, intf);
  615. if (status < 0) {
  616. dev_err(&dev->udev->dev, "Error in usbnet_get_endpoints (%d)",
  617. status);
  618. return -ENODEV;
  619. }
  620. /* Initialize sierra private data */
  621. priv = kzalloc(sizeof *priv, GFP_KERNEL);
  622. if (!priv) {
  623. dev_err(&dev->udev->dev, "No memory");
  624. return -ENOMEM;
  625. }
  626. priv->usbnet = dev;
  627. priv->ifnum = ifacenum;
  628. dev->net->netdev_ops = &sierra_net_device_ops;
  629. /* change MAC addr to include, ifacenum, and to be unique */
  630. dev->net->dev_addr[ETH_ALEN-2] = atomic_inc_return(&iface_counter);
  631. dev->net->dev_addr[ETH_ALEN-1] = ifacenum;
  632. /* we will have to manufacture ethernet headers, prepare template */
  633. eth = (struct ethhdr *)priv->ethr_hdr_tmpl;
  634. memcpy(&eth->h_dest, dev->net->dev_addr, ETH_ALEN);
  635. eth->h_proto = cpu_to_be16(ETH_P_IP);
  636. /* prepare shutdown message template */
  637. memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg));
  638. /* set context index initially to 0 - prepares tx hdr template */
  639. sierra_net_set_ctx_index(priv, 0);
  640. /* decrease the rx_urb_size and max_tx_size to 4k on USB 1.1 */
  641. dev->rx_urb_size = data->rx_urb_size;
  642. if (dev->udev->speed != USB_SPEED_HIGH)
  643. dev->rx_urb_size = min_t(size_t, 4096, data->rx_urb_size);
  644. dev->net->hard_header_len += SIERRA_NET_HIP_EXT_HDR_LEN;
  645. dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
  646. /* Set up the netdev */
  647. dev->net->flags |= IFF_NOARP;
  648. dev->net->ethtool_ops = &sierra_net_ethtool_ops;
  649. netif_carrier_off(dev->net);
  650. sierra_net_set_private(dev, priv);
  651. priv->kevent_flags = 0;
  652. /* Use the shared workqueue */
  653. INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent);
  654. /* Only need to do this once */
  655. init_timer(&priv->sync_timer);
  656. /* verify fw attributes */
  657. status = sierra_net_get_fw_attr(dev, &fwattr);
  658. dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr);
  659. /* test whether firmware supports DHCP */
  660. if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) {
  661. /* found incompatible firmware version */
  662. dev_err(&dev->udev->dev, "Incompatible driver and firmware"
  663. " versions\n");
  664. kfree(priv);
  665. return -ENODEV;
  666. }
  667. /* prepare sync message from template */
  668. memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg));
  669. /* initiate the sync sequence */
  670. sierra_net_dosync(dev);
  671. return 0;
  672. }
  673. static void sierra_net_unbind(struct usbnet *dev, struct usb_interface *intf)
  674. {
  675. int status;
  676. struct sierra_net_data *priv = sierra_net_get_private(dev);
  677. dev_dbg(&dev->udev->dev, "%s", __func__);
  678. /* kill the timer and work */
  679. del_timer_sync(&priv->sync_timer);
  680. cancel_work_sync(&priv->sierra_net_kevent);
  681. /* tell modem we are going away */
  682. status = sierra_net_send_cmd(dev, priv->shdwn_msg,
  683. sizeof(priv->shdwn_msg), "Shutdown");
  684. if (status < 0)
  685. netdev_err(dev->net,
  686. "usb_control_msg failed, status %d\n", status);
  687. sierra_net_set_private(dev, NULL);
  688. kfree(priv);
  689. }
  690. static struct sk_buff *sierra_net_skb_clone(struct usbnet *dev,
  691. struct sk_buff *skb, int len)
  692. {
  693. struct sk_buff *new_skb;
  694. /* clone skb */
  695. new_skb = skb_clone(skb, GFP_ATOMIC);
  696. /* remove len bytes from original */
  697. skb_pull(skb, len);
  698. /* trim next packet to it's length */
  699. if (new_skb) {
  700. skb_trim(new_skb, len);
  701. } else {
  702. if (netif_msg_rx_err(dev))
  703. netdev_err(dev->net, "failed to get skb\n");
  704. dev->net->stats.rx_dropped++;
  705. }
  706. return new_skb;
  707. }
  708. /* ---------------------------- Receive data path ----------------------*/
  709. static int sierra_net_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  710. {
  711. int err;
  712. struct hip_hdr hh;
  713. struct sk_buff *new_skb;
  714. dev_dbg(&dev->udev->dev, "%s", __func__);
  715. /* could contain multiple packets */
  716. while (likely(skb->len)) {
  717. err = parse_hip(skb->data, skb->len, &hh);
  718. if (err) {
  719. if (netif_msg_rx_err(dev))
  720. netdev_err(dev->net, "Invalid HIP header %d\n",
  721. err);
  722. /* dev->net->stats.rx_errors incremented by caller */
  723. dev->net->stats.rx_length_errors++;
  724. return 0;
  725. }
  726. /* Validate Extended HIP header */
  727. if (!hh.extmsgid.is_present
  728. || hh.extmsgid.word != SIERRA_NET_HIP_EXT_IP_IN_ID) {
  729. if (netif_msg_rx_err(dev))
  730. netdev_err(dev->net, "HIP/ETH: Invalid pkt\n");
  731. dev->net->stats.rx_frame_errors++;
  732. /* dev->net->stats.rx_errors incremented by caller */;
  733. return 0;
  734. }
  735. skb_pull(skb, hh.hdrlen);
  736. /* We are going to accept this packet, prepare it */
  737. memcpy(skb->data, sierra_net_get_private(dev)->ethr_hdr_tmpl,
  738. ETH_HLEN);
  739. /* Last packet in batch handled by usbnet */
  740. if (hh.payload_len.word == skb->len)
  741. return 1;
  742. new_skb = sierra_net_skb_clone(dev, skb, hh.payload_len.word);
  743. if (new_skb)
  744. usbnet_skb_return(dev, new_skb);
  745. } /* while */
  746. return 0;
  747. }
  748. /* ---------------------------- Transmit data path ----------------------*/
  749. struct sk_buff *sierra_net_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  750. gfp_t flags)
  751. {
  752. struct sierra_net_data *priv = sierra_net_get_private(dev);
  753. u16 len;
  754. bool need_tail;
  755. dev_dbg(&dev->udev->dev, "%s", __func__);
  756. if (priv->link_up && check_ethip_packet(skb, dev) && is_ip(skb)) {
  757. /* enough head room as is? */
  758. if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) {
  759. /* Save the Eth/IP length and set up HIP hdr */
  760. len = skb->len;
  761. skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN);
  762. /* Handle ZLP issue */
  763. need_tail = ((len + SIERRA_NET_HIP_EXT_HDR_LEN)
  764. % dev->maxpacket == 0);
  765. if (need_tail) {
  766. if (unlikely(skb_tailroom(skb) == 0)) {
  767. netdev_err(dev->net, "tx_fixup:"
  768. "no room for packet\n");
  769. dev_kfree_skb_any(skb);
  770. return NULL;
  771. } else {
  772. skb->data[skb->len] = 0;
  773. __skb_put(skb, 1);
  774. len = len + 1;
  775. }
  776. }
  777. build_hip(skb->data, len, priv);
  778. return skb;
  779. } else {
  780. /*
  781. * compensate in the future if necessary
  782. */
  783. netdev_err(dev->net, "tx_fixup: no room for HIP\n");
  784. } /* headroom */
  785. }
  786. if (!priv->link_up)
  787. dev->net->stats.tx_carrier_errors++;
  788. /* tx_dropped incremented by usbnet */
  789. /* filter the packet out, release it */
  790. dev_kfree_skb_any(skb);
  791. return NULL;
  792. }
  793. static const u8 sierra_net_ifnum_list[] = { 7, 10, 11 };
  794. static const struct sierra_net_info_data sierra_net_info_data_68A3 = {
  795. .rx_urb_size = 8 * 1024,
  796. .whitelist = {
  797. .infolen = ARRAY_SIZE(sierra_net_ifnum_list),
  798. .ifaceinfo = sierra_net_ifnum_list
  799. }
  800. };
  801. static const struct driver_info sierra_net_info_68A3 = {
  802. .description = "Sierra Wireless USB-to-WWAN Modem",
  803. .flags = FLAG_WWAN | FLAG_SEND_ZLP,
  804. .bind = sierra_net_bind,
  805. .unbind = sierra_net_unbind,
  806. .status = sierra_net_status,
  807. .rx_fixup = sierra_net_rx_fixup,
  808. .tx_fixup = sierra_net_tx_fixup,
  809. .data = (unsigned long)&sierra_net_info_data_68A3,
  810. };
  811. static const struct usb_device_id products[] = {
  812. {USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless USB-to-WWAN modem */
  813. .driver_info = (unsigned long) &sierra_net_info_68A3},
  814. {}, /* last item */
  815. };
  816. MODULE_DEVICE_TABLE(usb, products);
  817. /* We are based on usbnet, so let it handle the USB driver specifics */
  818. static struct usb_driver sierra_net_driver = {
  819. .name = "sierra_net",
  820. .id_table = products,
  821. .probe = usbnet_probe,
  822. .disconnect = usbnet_disconnect,
  823. .suspend = usbnet_suspend,
  824. .resume = usbnet_resume,
  825. .no_dynamic_id = 1,
  826. };
  827. static int __init sierra_net_init(void)
  828. {
  829. BUILD_BUG_ON(FIELD_SIZEOF(struct usbnet, data)
  830. < sizeof(struct cdc_state));
  831. return usb_register(&sierra_net_driver);
  832. }
  833. static void __exit sierra_net_exit(void)
  834. {
  835. usb_deregister(&sierra_net_driver);
  836. }
  837. module_exit(sierra_net_exit);
  838. module_init(sierra_net_init);
  839. MODULE_AUTHOR(DRIVER_AUTHOR);
  840. MODULE_DESCRIPTION(DRIVER_DESC);
  841. MODULE_VERSION(DRIVER_VERSION);
  842. MODULE_LICENSE("GPL");