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