stir4200.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*****************************************************************************
  2. *
  3. * Filename: stir4200.c
  4. * Version: 0.4
  5. * Description: Irda SigmaTel USB Dongle
  6. * Status: Experimental
  7. * Author: Stephen Hemminger <shemminger@osdl.org>
  8. *
  9. * Based on earlier driver by Paul Stewart <stewart@parc.com>
  10. *
  11. * Copyright (C) 2000, Roman Weissgaerber <weissg@vienna.at>
  12. * Copyright (C) 2001, Dag Brattli <dag@brattli.net>
  13. * Copyright (C) 2001, Jean Tourrilhes <jt@hpl.hp.com>
  14. * Copyright (C) 2004, Stephen Hemminger <shemminger@osdl.org>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. *****************************************************************************/
  30. /*
  31. * This dongle does no framing, and requires polling to receive the
  32. * data. The STIr4200 has bulk in and out endpoints just like
  33. * usr-irda devices, but the data it sends and receives is raw; like
  34. * irtty, it needs to call the wrap and unwrap functions to add and
  35. * remove SOF/BOF and escape characters to/from the frame.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/kernel.h>
  40. #include <linux/types.h>
  41. #include <linux/init.h>
  42. #include <linux/time.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/netdevice.h>
  45. #include <linux/slab.h>
  46. #include <linux/delay.h>
  47. #include <linux/usb.h>
  48. #include <linux/crc32.h>
  49. #include <linux/kthread.h>
  50. #include <linux/freezer.h>
  51. #include <net/irda/irda.h>
  52. #include <net/irda/irda_device.h>
  53. #include <net/irda/wrapper.h>
  54. #include <net/irda/crc.h>
  55. #include <asm/byteorder.h>
  56. #include <asm/unaligned.h>
  57. MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
  58. MODULE_DESCRIPTION("IrDA-USB Dongle Driver for SigmaTel STIr4200");
  59. MODULE_LICENSE("GPL");
  60. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  61. module_param(qos_mtt_bits, int, 0);
  62. MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
  63. static int rx_sensitivity = 1; /* FIR 0..4, SIR 0..6 */
  64. module_param(rx_sensitivity, int, 0);
  65. MODULE_PARM_DESC(rx_sensitivity, "Set Receiver sensitivity (0-6, 0 is most sensitive)");
  66. static int tx_power = 0; /* 0 = highest ... 3 = lowest */
  67. module_param(tx_power, int, 0);
  68. MODULE_PARM_DESC(tx_power, "Set Transmitter power (0-3, 0 is highest power)");
  69. #define STIR_IRDA_HEADER 4
  70. #define CTRL_TIMEOUT 100 /* milliseconds */
  71. #define TRANSMIT_TIMEOUT 200 /* milliseconds */
  72. #define STIR_FIFO_SIZE 4096
  73. #define FIFO_REGS_SIZE 3
  74. enum FirChars {
  75. FIR_CE = 0x7d,
  76. FIR_XBOF = 0x7f,
  77. FIR_EOF = 0x7e,
  78. };
  79. enum StirRequests {
  80. REQ_WRITE_REG = 0x00,
  81. REQ_READ_REG = 0x01,
  82. REQ_READ_ROM = 0x02,
  83. REQ_WRITE_SINGLE = 0x03,
  84. };
  85. /* Register offsets */
  86. enum StirRegs {
  87. REG_RSVD=0,
  88. REG_MODE,
  89. REG_PDCLK,
  90. REG_CTRL1,
  91. REG_CTRL2,
  92. REG_FIFOCTL,
  93. REG_FIFOLSB,
  94. REG_FIFOMSB,
  95. REG_DPLL,
  96. REG_IRDIG,
  97. REG_TEST=15,
  98. };
  99. enum StirModeMask {
  100. MODE_FIR = 0x80,
  101. MODE_SIR = 0x20,
  102. MODE_ASK = 0x10,
  103. MODE_FASTRX = 0x08,
  104. MODE_FFRSTEN = 0x04,
  105. MODE_NRESET = 0x02,
  106. MODE_2400 = 0x01,
  107. };
  108. enum StirPdclkMask {
  109. PDCLK_4000000 = 0x02,
  110. PDCLK_115200 = 0x09,
  111. PDCLK_57600 = 0x13,
  112. PDCLK_38400 = 0x1D,
  113. PDCLK_19200 = 0x3B,
  114. PDCLK_9600 = 0x77,
  115. PDCLK_2400 = 0xDF,
  116. };
  117. enum StirCtrl1Mask {
  118. CTRL1_SDMODE = 0x80,
  119. CTRL1_RXSLOW = 0x40,
  120. CTRL1_TXPWD = 0x10,
  121. CTRL1_RXPWD = 0x08,
  122. CTRL1_SRESET = 0x01,
  123. };
  124. enum StirCtrl2Mask {
  125. CTRL2_SPWIDTH = 0x08,
  126. CTRL2_REVID = 0x03,
  127. };
  128. enum StirFifoCtlMask {
  129. FIFOCTL_EOF = 0x80,
  130. FIFOCTL_UNDER = 0x40,
  131. FIFOCTL_OVER = 0x20,
  132. FIFOCTL_DIR = 0x10,
  133. FIFOCTL_CLR = 0x08,
  134. FIFOCTL_EMPTY = 0x04,
  135. };
  136. enum StirDiagMask {
  137. IRDIG_RXHIGH = 0x80,
  138. IRDIG_RXLOW = 0x40,
  139. };
  140. enum StirTestMask {
  141. TEST_PLLDOWN = 0x80,
  142. TEST_LOOPIR = 0x40,
  143. TEST_LOOPUSB = 0x20,
  144. TEST_TSTENA = 0x10,
  145. TEST_TSTOSC = 0x0F,
  146. };
  147. struct stir_cb {
  148. struct usb_device *usbdev; /* init: probe_irda */
  149. struct net_device *netdev; /* network layer */
  150. struct irlap_cb *irlap; /* The link layer we are binded to */
  151. struct net_device_stats stats; /* network statistics */
  152. struct qos_info qos;
  153. unsigned speed; /* Current speed */
  154. struct task_struct *thread; /* transmit thread */
  155. struct sk_buff *tx_pending;
  156. void *io_buf; /* transmit/receive buffer */
  157. __u8 *fifo_status;
  158. iobuff_t rx_buff; /* receive unwrap state machine */
  159. struct timeval rx_time;
  160. int receiving;
  161. struct urb *rx_urb;
  162. };
  163. /* These are the currently known USB ids */
  164. static struct usb_device_id dongles[] = {
  165. /* SigmaTel, Inc, STIr4200 IrDA/USB Bridge */
  166. { USB_DEVICE(0x066f, 0x4200) },
  167. { }
  168. };
  169. MODULE_DEVICE_TABLE(usb, dongles);
  170. /* Send control message to set dongle register */
  171. static int write_reg(struct stir_cb *stir, __u16 reg, __u8 value)
  172. {
  173. struct usb_device *dev = stir->usbdev;
  174. pr_debug("%s: write reg %d = 0x%x\n",
  175. stir->netdev->name, reg, value);
  176. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  177. REQ_WRITE_SINGLE,
  178. USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE,
  179. value, reg, NULL, 0,
  180. CTRL_TIMEOUT);
  181. }
  182. /* Send control message to read multiple registers */
  183. static inline int read_reg(struct stir_cb *stir, __u16 reg,
  184. __u8 *data, __u16 count)
  185. {
  186. struct usb_device *dev = stir->usbdev;
  187. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  188. REQ_READ_REG,
  189. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  190. 0, reg, data, count,
  191. CTRL_TIMEOUT);
  192. }
  193. static inline int isfir(u32 speed)
  194. {
  195. return (speed == 4000000);
  196. }
  197. /*
  198. * Prepare a FIR IrDA frame for transmission to the USB dongle. The
  199. * FIR transmit frame is documented in the datasheet. It consists of
  200. * a two byte 0x55 0xAA sequence, two little-endian length bytes, a
  201. * sequence of exactly 16 XBOF bytes of 0x7E, two BOF bytes of 0x7E,
  202. * then the data escaped as follows:
  203. *
  204. * 0x7D -> 0x7D 0x5D
  205. * 0x7E -> 0x7D 0x5E
  206. * 0x7F -> 0x7D 0x5F
  207. *
  208. * Then, 4 bytes of little endian (stuffed) FCS follow, then two
  209. * trailing EOF bytes of 0x7E.
  210. */
  211. static inline __u8 *stuff_fir(__u8 *p, __u8 c)
  212. {
  213. switch(c) {
  214. case 0x7d:
  215. case 0x7e:
  216. case 0x7f:
  217. *p++ = 0x7d;
  218. c ^= IRDA_TRANS;
  219. /* fall through */
  220. default:
  221. *p++ = c;
  222. }
  223. return p;
  224. }
  225. /* Take raw data in skb and put it wrapped into buf */
  226. static unsigned wrap_fir_skb(const struct sk_buff *skb, __u8 *buf)
  227. {
  228. __u8 *ptr = buf;
  229. __u32 fcs = ~(crc32_le(~0, skb->data, skb->len));
  230. __u16 wraplen;
  231. int i;
  232. /* Header */
  233. buf[0] = 0x55;
  234. buf[1] = 0xAA;
  235. ptr = buf + STIR_IRDA_HEADER;
  236. memset(ptr, 0x7f, 16);
  237. ptr += 16;
  238. /* BOF */
  239. *ptr++ = 0x7e;
  240. *ptr++ = 0x7e;
  241. /* Address / Control / Information */
  242. for (i = 0; i < skb->len; i++)
  243. ptr = stuff_fir(ptr, skb->data[i]);
  244. /* FCS */
  245. ptr = stuff_fir(ptr, fcs & 0xff);
  246. ptr = stuff_fir(ptr, (fcs >> 8) & 0xff);
  247. ptr = stuff_fir(ptr, (fcs >> 16) & 0xff);
  248. ptr = stuff_fir(ptr, (fcs >> 24) & 0xff);
  249. /* EOFs */
  250. *ptr++ = 0x7e;
  251. *ptr++ = 0x7e;
  252. /* Total length, minus the header */
  253. wraplen = (ptr - buf) - STIR_IRDA_HEADER;
  254. buf[2] = wraplen & 0xff;
  255. buf[3] = (wraplen >> 8) & 0xff;
  256. return wraplen + STIR_IRDA_HEADER;
  257. }
  258. static unsigned wrap_sir_skb(struct sk_buff *skb, __u8 *buf)
  259. {
  260. __u16 wraplen;
  261. wraplen = async_wrap_skb(skb, buf + STIR_IRDA_HEADER,
  262. STIR_FIFO_SIZE - STIR_IRDA_HEADER);
  263. buf[0] = 0x55;
  264. buf[1] = 0xAA;
  265. buf[2] = wraplen & 0xff;
  266. buf[3] = (wraplen >> 8) & 0xff;
  267. return wraplen + STIR_IRDA_HEADER;
  268. }
  269. /*
  270. * Frame is fully formed in the rx_buff so check crc
  271. * and pass up to irlap
  272. * setup for next receive
  273. */
  274. static void fir_eof(struct stir_cb *stir)
  275. {
  276. iobuff_t *rx_buff = &stir->rx_buff;
  277. int len = rx_buff->len - 4;
  278. struct sk_buff *skb, *nskb;
  279. __u32 fcs;
  280. if (unlikely(len <= 0)) {
  281. pr_debug("%s: short frame len %d\n",
  282. stir->netdev->name, len);
  283. ++stir->stats.rx_errors;
  284. ++stir->stats.rx_length_errors;
  285. return;
  286. }
  287. fcs = ~(crc32_le(~0, rx_buff->data, len));
  288. if (fcs != le32_to_cpu(get_unaligned((__le32 *)(rx_buff->data+len)))) {
  289. pr_debug("crc error calc 0x%x len %d\n", fcs, len);
  290. stir->stats.rx_errors++;
  291. stir->stats.rx_crc_errors++;
  292. return;
  293. }
  294. /* if frame is short then just copy it */
  295. if (len < IRDA_RX_COPY_THRESHOLD) {
  296. nskb = dev_alloc_skb(len + 1);
  297. if (unlikely(!nskb)) {
  298. ++stir->stats.rx_dropped;
  299. return;
  300. }
  301. skb_reserve(nskb, 1);
  302. skb = nskb;
  303. skb_copy_to_linear_data(nskb, rx_buff->data, len);
  304. } else {
  305. nskb = dev_alloc_skb(rx_buff->truesize);
  306. if (unlikely(!nskb)) {
  307. ++stir->stats.rx_dropped;
  308. return;
  309. }
  310. skb_reserve(nskb, 1);
  311. skb = rx_buff->skb;
  312. rx_buff->skb = nskb;
  313. rx_buff->head = nskb->data;
  314. }
  315. skb_put(skb, len);
  316. skb_reset_mac_header(skb);
  317. skb->protocol = htons(ETH_P_IRDA);
  318. skb->dev = stir->netdev;
  319. netif_rx(skb);
  320. stir->stats.rx_packets++;
  321. stir->stats.rx_bytes += len;
  322. rx_buff->data = rx_buff->head;
  323. rx_buff->len = 0;
  324. }
  325. /* Unwrap FIR stuffed data and bump it to IrLAP */
  326. static void stir_fir_chars(struct stir_cb *stir,
  327. const __u8 *bytes, int len)
  328. {
  329. iobuff_t *rx_buff = &stir->rx_buff;
  330. int i;
  331. for (i = 0; i < len; i++) {
  332. __u8 byte = bytes[i];
  333. switch(rx_buff->state) {
  334. case OUTSIDE_FRAME:
  335. /* ignore garbage till start of frame */
  336. if (unlikely(byte != FIR_EOF))
  337. continue;
  338. /* Now receiving frame */
  339. rx_buff->state = BEGIN_FRAME;
  340. /* Time to initialize receive buffer */
  341. rx_buff->data = rx_buff->head;
  342. rx_buff->len = 0;
  343. continue;
  344. case LINK_ESCAPE:
  345. if (byte == FIR_EOF) {
  346. pr_debug("%s: got EOF after escape\n",
  347. stir->netdev->name);
  348. goto frame_error;
  349. }
  350. rx_buff->state = INSIDE_FRAME;
  351. byte ^= IRDA_TRANS;
  352. break;
  353. case BEGIN_FRAME:
  354. /* ignore multiple BOF/EOF */
  355. if (byte == FIR_EOF)
  356. continue;
  357. rx_buff->state = INSIDE_FRAME;
  358. rx_buff->in_frame = TRUE;
  359. /* fall through */
  360. case INSIDE_FRAME:
  361. switch(byte) {
  362. case FIR_CE:
  363. rx_buff->state = LINK_ESCAPE;
  364. continue;
  365. case FIR_XBOF:
  366. /* 0x7f is not used in this framing */
  367. pr_debug("%s: got XBOF without escape\n",
  368. stir->netdev->name);
  369. goto frame_error;
  370. case FIR_EOF:
  371. rx_buff->state = OUTSIDE_FRAME;
  372. rx_buff->in_frame = FALSE;
  373. fir_eof(stir);
  374. continue;
  375. }
  376. break;
  377. }
  378. /* add byte to rx buffer */
  379. if (unlikely(rx_buff->len >= rx_buff->truesize)) {
  380. pr_debug("%s: fir frame exceeds %d\n",
  381. stir->netdev->name, rx_buff->truesize);
  382. ++stir->stats.rx_over_errors;
  383. goto error_recovery;
  384. }
  385. rx_buff->data[rx_buff->len++] = byte;
  386. continue;
  387. frame_error:
  388. ++stir->stats.rx_frame_errors;
  389. error_recovery:
  390. ++stir->stats.rx_errors;
  391. rx_buff->state = OUTSIDE_FRAME;
  392. rx_buff->in_frame = FALSE;
  393. }
  394. }
  395. /* Unwrap SIR stuffed data and bump it up to IrLAP */
  396. static void stir_sir_chars(struct stir_cb *stir,
  397. const __u8 *bytes, int len)
  398. {
  399. int i;
  400. for (i = 0; i < len; i++)
  401. async_unwrap_char(stir->netdev, &stir->stats,
  402. &stir->rx_buff, bytes[i]);
  403. }
  404. static inline void unwrap_chars(struct stir_cb *stir,
  405. const __u8 *bytes, int length)
  406. {
  407. if (isfir(stir->speed))
  408. stir_fir_chars(stir, bytes, length);
  409. else
  410. stir_sir_chars(stir, bytes, length);
  411. }
  412. /* Mode parameters for each speed */
  413. static const struct {
  414. unsigned speed;
  415. __u8 pdclk;
  416. } stir_modes[] = {
  417. { 2400, PDCLK_2400 },
  418. { 9600, PDCLK_9600 },
  419. { 19200, PDCLK_19200 },
  420. { 38400, PDCLK_38400 },
  421. { 57600, PDCLK_57600 },
  422. { 115200, PDCLK_115200 },
  423. { 4000000, PDCLK_4000000 },
  424. };
  425. /*
  426. * Setup chip for speed.
  427. * Called at startup to initialize the chip
  428. * and on speed changes.
  429. *
  430. * Note: Write multiple registers doesn't appear to work
  431. */
  432. static int change_speed(struct stir_cb *stir, unsigned speed)
  433. {
  434. int i, err;
  435. __u8 mode;
  436. for (i = 0; i < ARRAY_SIZE(stir_modes); ++i) {
  437. if (speed == stir_modes[i].speed)
  438. goto found;
  439. }
  440. warn("%s: invalid speed %d", stir->netdev->name, speed);
  441. return -EINVAL;
  442. found:
  443. pr_debug("speed change from %d to %d\n", stir->speed, speed);
  444. /* Reset modulator */
  445. err = write_reg(stir, REG_CTRL1, CTRL1_SRESET);
  446. if (err)
  447. goto out;
  448. /* Undocumented magic to tweak the DPLL */
  449. err = write_reg(stir, REG_DPLL, 0x15);
  450. if (err)
  451. goto out;
  452. /* Set clock */
  453. err = write_reg(stir, REG_PDCLK, stir_modes[i].pdclk);
  454. if (err)
  455. goto out;
  456. mode = MODE_NRESET | MODE_FASTRX;
  457. if (isfir(speed))
  458. mode |= MODE_FIR | MODE_FFRSTEN;
  459. else
  460. mode |= MODE_SIR;
  461. if (speed == 2400)
  462. mode |= MODE_2400;
  463. err = write_reg(stir, REG_MODE, mode);
  464. if (err)
  465. goto out;
  466. /* This resets TEMIC style transceiver if any. */
  467. err = write_reg(stir, REG_CTRL1,
  468. CTRL1_SDMODE | (tx_power & 3) << 1);
  469. if (err)
  470. goto out;
  471. err = write_reg(stir, REG_CTRL1, (tx_power & 3) << 1);
  472. if (err)
  473. goto out;
  474. /* Reset sensitivity */
  475. err = write_reg(stir, REG_CTRL2, (rx_sensitivity & 7) << 5);
  476. out:
  477. stir->speed = speed;
  478. return err;
  479. }
  480. /*
  481. * Called from net/core when new frame is available.
  482. */
  483. static int stir_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
  484. {
  485. struct stir_cb *stir = netdev_priv(netdev);
  486. netif_stop_queue(netdev);
  487. /* the IRDA wrapping routines don't deal with non linear skb */
  488. SKB_LINEAR_ASSERT(skb);
  489. skb = xchg(&stir->tx_pending, skb);
  490. wake_up_process(stir->thread);
  491. /* this should never happen unless stop/wakeup problem */
  492. if (unlikely(skb)) {
  493. WARN_ON(1);
  494. dev_kfree_skb(skb);
  495. }
  496. return 0;
  497. }
  498. /*
  499. * Wait for the transmit FIFO to have space for next data
  500. *
  501. * If space < 0 then wait till FIFO completely drains.
  502. * FYI: can take up to 13 seconds at 2400baud.
  503. */
  504. static int fifo_txwait(struct stir_cb *stir, int space)
  505. {
  506. int err;
  507. unsigned long count, status;
  508. /* Read FIFO status and count */
  509. for(;;) {
  510. err = read_reg(stir, REG_FIFOCTL, stir->fifo_status,
  511. FIFO_REGS_SIZE);
  512. if (unlikely(err != FIFO_REGS_SIZE)) {
  513. warn("%s: FIFO register read error: %d",
  514. stir->netdev->name, err);
  515. return err;
  516. }
  517. status = stir->fifo_status[0];
  518. count = (unsigned)(stir->fifo_status[2] & 0x1f) << 8
  519. | stir->fifo_status[1];
  520. pr_debug("fifo status 0x%lx count %lu\n", status, count);
  521. /* is fifo receiving already, or empty */
  522. if (!(status & FIFOCTL_DIR)
  523. || (status & FIFOCTL_EMPTY))
  524. return 0;
  525. if (signal_pending(current))
  526. return -EINTR;
  527. /* shutting down? */
  528. if (!netif_running(stir->netdev)
  529. || !netif_device_present(stir->netdev))
  530. return -ESHUTDOWN;
  531. /* only waiting for some space */
  532. if (space >= 0 && STIR_FIFO_SIZE - 4 > space + count)
  533. return 0;
  534. /* estimate transfer time for remaining chars */
  535. msleep((count * 8000) / stir->speed);
  536. }
  537. err = write_reg(stir, REG_FIFOCTL, FIFOCTL_CLR);
  538. if (err)
  539. return err;
  540. err = write_reg(stir, REG_FIFOCTL, 0);
  541. if (err)
  542. return err;
  543. return 0;
  544. }
  545. /* Wait for turnaround delay before starting transmit. */
  546. static void turnaround_delay(const struct stir_cb *stir, long us)
  547. {
  548. long ticks;
  549. struct timeval now;
  550. if (us <= 0)
  551. return;
  552. do_gettimeofday(&now);
  553. if (now.tv_sec - stir->rx_time.tv_sec > 0)
  554. us -= USEC_PER_SEC;
  555. us -= now.tv_usec - stir->rx_time.tv_usec;
  556. if (us < 10)
  557. return;
  558. ticks = us / (1000000 / HZ);
  559. if (ticks > 0)
  560. schedule_timeout_interruptible(1 + ticks);
  561. else
  562. udelay(us);
  563. }
  564. /*
  565. * Start receiver by submitting a request to the receive pipe.
  566. * If nothing is available it will return after rx_interval.
  567. */
  568. static int receive_start(struct stir_cb *stir)
  569. {
  570. /* reset state */
  571. stir->receiving = 1;
  572. stir->rx_buff.in_frame = FALSE;
  573. stir->rx_buff.state = OUTSIDE_FRAME;
  574. stir->rx_urb->status = 0;
  575. return usb_submit_urb(stir->rx_urb, GFP_KERNEL);
  576. }
  577. /* Stop all pending receive Urb's */
  578. static void receive_stop(struct stir_cb *stir)
  579. {
  580. stir->receiving = 0;
  581. usb_kill_urb(stir->rx_urb);
  582. if (stir->rx_buff.in_frame)
  583. stir->stats.collisions++;
  584. }
  585. /*
  586. * Wrap data in socket buffer and send it.
  587. */
  588. static void stir_send(struct stir_cb *stir, struct sk_buff *skb)
  589. {
  590. unsigned wraplen;
  591. int first_frame = 0;
  592. /* if receiving, need to turnaround */
  593. if (stir->receiving) {
  594. receive_stop(stir);
  595. turnaround_delay(stir, irda_get_mtt(skb));
  596. first_frame = 1;
  597. }
  598. if (isfir(stir->speed))
  599. wraplen = wrap_fir_skb(skb, stir->io_buf);
  600. else
  601. wraplen = wrap_sir_skb(skb, stir->io_buf);
  602. /* check for space available in fifo */
  603. if (!first_frame)
  604. fifo_txwait(stir, wraplen);
  605. stir->stats.tx_packets++;
  606. stir->stats.tx_bytes += skb->len;
  607. stir->netdev->trans_start = jiffies;
  608. pr_debug("send %d (%d)\n", skb->len, wraplen);
  609. if (usb_bulk_msg(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1),
  610. stir->io_buf, wraplen,
  611. NULL, TRANSMIT_TIMEOUT))
  612. stir->stats.tx_errors++;
  613. }
  614. /*
  615. * Transmit state machine thread
  616. */
  617. static int stir_transmit_thread(void *arg)
  618. {
  619. struct stir_cb *stir = arg;
  620. struct net_device *dev = stir->netdev;
  621. struct sk_buff *skb;
  622. while (!kthread_should_stop()) {
  623. #ifdef CONFIG_PM
  624. /* if suspending, then power off and wait */
  625. if (unlikely(freezing(current))) {
  626. if (stir->receiving)
  627. receive_stop(stir);
  628. else
  629. fifo_txwait(stir, -1);
  630. write_reg(stir, REG_CTRL1, CTRL1_TXPWD|CTRL1_RXPWD);
  631. refrigerator();
  632. if (change_speed(stir, stir->speed))
  633. break;
  634. }
  635. #endif
  636. /* if something to send? */
  637. skb = xchg(&stir->tx_pending, NULL);
  638. if (skb) {
  639. unsigned new_speed = irda_get_next_speed(skb);
  640. netif_wake_queue(dev);
  641. if (skb->len > 0)
  642. stir_send(stir, skb);
  643. dev_kfree_skb(skb);
  644. if ((new_speed != -1) && (stir->speed != new_speed)) {
  645. if (fifo_txwait(stir, -1) ||
  646. change_speed(stir, new_speed))
  647. break;
  648. }
  649. continue;
  650. }
  651. /* nothing to send? start receiving */
  652. if (!stir->receiving
  653. && irda_device_txqueue_empty(dev)) {
  654. /* Wait otherwise chip gets confused. */
  655. if (fifo_txwait(stir, -1))
  656. break;
  657. if (unlikely(receive_start(stir))) {
  658. if (net_ratelimit())
  659. info("%s: receive usb submit failed",
  660. stir->netdev->name);
  661. stir->receiving = 0;
  662. msleep(10);
  663. continue;
  664. }
  665. }
  666. /* sleep if nothing to send */
  667. set_current_state(TASK_INTERRUPTIBLE);
  668. schedule();
  669. }
  670. return 0;
  671. }
  672. /*
  673. * USB bulk receive completion callback.
  674. * Wakes up every ms (usb round trip) with wrapped
  675. * data.
  676. */
  677. static void stir_rcv_irq(struct urb *urb)
  678. {
  679. struct stir_cb *stir = urb->context;
  680. int err;
  681. /* in process of stopping, just drop data */
  682. if (!netif_running(stir->netdev))
  683. return;
  684. /* unlink, shutdown, unplug, other nasties */
  685. if (urb->status != 0)
  686. return;
  687. if (urb->actual_length > 0) {
  688. pr_debug("receive %d\n", urb->actual_length);
  689. unwrap_chars(stir, urb->transfer_buffer,
  690. urb->actual_length);
  691. stir->netdev->last_rx = jiffies;
  692. do_gettimeofday(&stir->rx_time);
  693. }
  694. /* kernel thread is stopping receiver don't resubmit */
  695. if (!stir->receiving)
  696. return;
  697. /* resubmit existing urb */
  698. err = usb_submit_urb(urb, GFP_ATOMIC);
  699. /* in case of error, the kernel thread will restart us */
  700. if (err) {
  701. warn("%s: usb receive submit error: %d",
  702. stir->netdev->name, err);
  703. stir->receiving = 0;
  704. wake_up_process(stir->thread);
  705. }
  706. }
  707. /*
  708. * Function stir_net_open (dev)
  709. *
  710. * Network device is taken up. Usually this is done by "ifconfig irda0 up"
  711. */
  712. static int stir_net_open(struct net_device *netdev)
  713. {
  714. struct stir_cb *stir = netdev_priv(netdev);
  715. int err;
  716. char hwname[16];
  717. err = usb_clear_halt(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1));
  718. if (err)
  719. goto err_out1;
  720. err = usb_clear_halt(stir->usbdev, usb_rcvbulkpipe(stir->usbdev, 2));
  721. if (err)
  722. goto err_out1;
  723. err = change_speed(stir, 9600);
  724. if (err)
  725. goto err_out1;
  726. err = -ENOMEM;
  727. /* Initialize for SIR/FIR to copy data directly into skb. */
  728. stir->receiving = 0;
  729. stir->rx_buff.truesize = IRDA_SKB_MAX_MTU;
  730. stir->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
  731. if (!stir->rx_buff.skb)
  732. goto err_out1;
  733. skb_reserve(stir->rx_buff.skb, 1);
  734. stir->rx_buff.head = stir->rx_buff.skb->data;
  735. do_gettimeofday(&stir->rx_time);
  736. stir->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  737. if (!stir->rx_urb)
  738. goto err_out2;
  739. stir->io_buf = kmalloc(STIR_FIFO_SIZE, GFP_KERNEL);
  740. if (!stir->io_buf)
  741. goto err_out3;
  742. usb_fill_bulk_urb(stir->rx_urb, stir->usbdev,
  743. usb_rcvbulkpipe(stir->usbdev, 2),
  744. stir->io_buf, STIR_FIFO_SIZE,
  745. stir_rcv_irq, stir);
  746. stir->fifo_status = kmalloc(FIFO_REGS_SIZE, GFP_KERNEL);
  747. if (!stir->fifo_status)
  748. goto err_out4;
  749. /*
  750. * Now that everything should be initialized properly,
  751. * Open new IrLAP layer instance to take care of us...
  752. * Note : will send immediately a speed change...
  753. */
  754. sprintf(hwname, "usb#%d", stir->usbdev->devnum);
  755. stir->irlap = irlap_open(netdev, &stir->qos, hwname);
  756. if (!stir->irlap) {
  757. err("stir4200: irlap_open failed");
  758. goto err_out5;
  759. }
  760. /** Start kernel thread for transmit. */
  761. stir->thread = kthread_run(stir_transmit_thread, stir,
  762. "%s", stir->netdev->name);
  763. if (IS_ERR(stir->thread)) {
  764. err = PTR_ERR(stir->thread);
  765. err("stir4200: unable to start kernel thread");
  766. goto err_out6;
  767. }
  768. netif_start_queue(netdev);
  769. return 0;
  770. err_out6:
  771. irlap_close(stir->irlap);
  772. err_out5:
  773. kfree(stir->fifo_status);
  774. err_out4:
  775. kfree(stir->io_buf);
  776. err_out3:
  777. usb_free_urb(stir->rx_urb);
  778. err_out2:
  779. kfree_skb(stir->rx_buff.skb);
  780. err_out1:
  781. return err;
  782. }
  783. /*
  784. * Function stir_net_close (stir)
  785. *
  786. * Network device is taken down. Usually this is done by
  787. * "ifconfig irda0 down"
  788. */
  789. static int stir_net_close(struct net_device *netdev)
  790. {
  791. struct stir_cb *stir = netdev_priv(netdev);
  792. /* Stop transmit processing */
  793. netif_stop_queue(netdev);
  794. /* Kill transmit thread */
  795. kthread_stop(stir->thread);
  796. kfree(stir->fifo_status);
  797. /* Mop up receive urb's */
  798. usb_kill_urb(stir->rx_urb);
  799. kfree(stir->io_buf);
  800. usb_free_urb(stir->rx_urb);
  801. kfree_skb(stir->rx_buff.skb);
  802. /* Stop and remove instance of IrLAP */
  803. if (stir->irlap)
  804. irlap_close(stir->irlap);
  805. stir->irlap = NULL;
  806. return 0;
  807. }
  808. /*
  809. * IOCTLs : Extra out-of-band network commands...
  810. */
  811. static int stir_net_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  812. {
  813. struct if_irda_req *irq = (struct if_irda_req *) rq;
  814. struct stir_cb *stir = netdev_priv(netdev);
  815. int ret = 0;
  816. switch (cmd) {
  817. case SIOCSBANDWIDTH: /* Set bandwidth */
  818. if (!capable(CAP_NET_ADMIN))
  819. return -EPERM;
  820. /* Check if the device is still there */
  821. if (netif_device_present(stir->netdev))
  822. ret = change_speed(stir, irq->ifr_baudrate);
  823. break;
  824. case SIOCSMEDIABUSY: /* Set media busy */
  825. if (!capable(CAP_NET_ADMIN))
  826. return -EPERM;
  827. /* Check if the IrDA stack is still there */
  828. if (netif_running(stir->netdev))
  829. irda_device_set_media_busy(stir->netdev, TRUE);
  830. break;
  831. case SIOCGRECEIVING:
  832. /* Only approximately true */
  833. irq->ifr_receiving = stir->receiving;
  834. break;
  835. default:
  836. ret = -EOPNOTSUPP;
  837. }
  838. return ret;
  839. }
  840. /*
  841. * Get device stats (for /proc/net/dev and ifconfig)
  842. */
  843. static struct net_device_stats *stir_net_get_stats(struct net_device *netdev)
  844. {
  845. struct stir_cb *stir = netdev_priv(netdev);
  846. return &stir->stats;
  847. }
  848. /*
  849. * This routine is called by the USB subsystem for each new device
  850. * in the system. We need to check if the device is ours, and in
  851. * this case start handling it.
  852. * Note : it might be worth protecting this function by a global
  853. * spinlock... Or not, because maybe USB already deal with that...
  854. */
  855. static int stir_probe(struct usb_interface *intf,
  856. const struct usb_device_id *id)
  857. {
  858. struct usb_device *dev = interface_to_usbdev(intf);
  859. struct stir_cb *stir = NULL;
  860. struct net_device *net;
  861. int ret = -ENOMEM;
  862. /* Allocate network device container. */
  863. net = alloc_irdadev(sizeof(*stir));
  864. if(!net)
  865. goto err_out1;
  866. SET_NETDEV_DEV(net, &intf->dev);
  867. stir = netdev_priv(net);
  868. stir->netdev = net;
  869. stir->usbdev = dev;
  870. ret = usb_reset_configuration(dev);
  871. if (ret != 0) {
  872. err("stir4200: usb reset configuration failed");
  873. goto err_out2;
  874. }
  875. printk(KERN_INFO "SigmaTel STIr4200 IRDA/USB found at address %d, "
  876. "Vendor: %x, Product: %x\n",
  877. dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
  878. le16_to_cpu(dev->descriptor.idProduct));
  879. /* Initialize QoS for this device */
  880. irda_init_max_qos_capabilies(&stir->qos);
  881. /* That's the Rx capability. */
  882. stir->qos.baud_rate.bits &= IR_2400 | IR_9600 | IR_19200 |
  883. IR_38400 | IR_57600 | IR_115200 |
  884. (IR_4000000 << 8);
  885. stir->qos.min_turn_time.bits &= qos_mtt_bits;
  886. irda_qos_bits_to_value(&stir->qos);
  887. /* Override the network functions we need to use */
  888. net->hard_start_xmit = stir_hard_xmit;
  889. net->open = stir_net_open;
  890. net->stop = stir_net_close;
  891. net->get_stats = stir_net_get_stats;
  892. net->do_ioctl = stir_net_ioctl;
  893. ret = register_netdev(net);
  894. if (ret != 0)
  895. goto err_out2;
  896. info("IrDA: Registered SigmaTel device %s", net->name);
  897. usb_set_intfdata(intf, stir);
  898. return 0;
  899. err_out2:
  900. free_netdev(net);
  901. err_out1:
  902. return ret;
  903. }
  904. /*
  905. * The current device is removed, the USB layer tell us to shut it down...
  906. */
  907. static void stir_disconnect(struct usb_interface *intf)
  908. {
  909. struct stir_cb *stir = usb_get_intfdata(intf);
  910. if (!stir)
  911. return;
  912. unregister_netdev(stir->netdev);
  913. free_netdev(stir->netdev);
  914. usb_set_intfdata(intf, NULL);
  915. }
  916. #ifdef CONFIG_PM
  917. /* USB suspend, so power off the transmitter/receiver */
  918. static int stir_suspend(struct usb_interface *intf, pm_message_t message)
  919. {
  920. struct stir_cb *stir = usb_get_intfdata(intf);
  921. netif_device_detach(stir->netdev);
  922. return 0;
  923. }
  924. /* Coming out of suspend, so reset hardware */
  925. static int stir_resume(struct usb_interface *intf)
  926. {
  927. struct stir_cb *stir = usb_get_intfdata(intf);
  928. netif_device_attach(stir->netdev);
  929. /* receiver restarted when send thread wakes up */
  930. return 0;
  931. }
  932. #endif
  933. /*
  934. * USB device callbacks
  935. */
  936. static struct usb_driver irda_driver = {
  937. .name = "stir4200",
  938. .probe = stir_probe,
  939. .disconnect = stir_disconnect,
  940. .id_table = dongles,
  941. #ifdef CONFIG_PM
  942. .suspend = stir_suspend,
  943. .resume = stir_resume,
  944. #endif
  945. };
  946. /*
  947. * Module insertion
  948. */
  949. static int __init stir_init(void)
  950. {
  951. return usb_register(&irda_driver);
  952. }
  953. module_init(stir_init);
  954. /*
  955. * Module removal
  956. */
  957. static void __exit stir_cleanup(void)
  958. {
  959. /* Deregister the driver and remove all pending instances */
  960. usb_deregister(&irda_driver);
  961. }
  962. module_exit(stir_cleanup);