stir4200.c 26 KB

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