stir4200.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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_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. struct timeval 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 int stir_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
  480. {
  481. struct stir_cb *stir = netdev_priv(netdev);
  482. netif_stop_queue(netdev);
  483. /* the IRDA wrapping routines don't deal with non linear skb */
  484. SKB_LINEAR_ASSERT(skb);
  485. skb = xchg(&stir->tx_pending, skb);
  486. wake_up_process(stir->thread);
  487. /* this should never happen unless stop/wakeup problem */
  488. if (unlikely(skb)) {
  489. WARN_ON(1);
  490. dev_kfree_skb(skb);
  491. }
  492. return 0;
  493. }
  494. /*
  495. * Wait for the transmit FIFO to have space for next data
  496. *
  497. * If space < 0 then wait till FIFO completely drains.
  498. * FYI: can take up to 13 seconds at 2400baud.
  499. */
  500. static int fifo_txwait(struct stir_cb *stir, int space)
  501. {
  502. int err;
  503. unsigned long count, status;
  504. unsigned long prev_count = 0x1fff;
  505. /* Read FIFO status and count */
  506. for (;; prev_count = count) {
  507. err = read_reg(stir, REG_FIFOCTL, stir->fifo_status,
  508. FIFO_REGS_SIZE);
  509. if (unlikely(err != FIFO_REGS_SIZE)) {
  510. dev_warn(&stir->netdev->dev,
  511. "FIFO register read error: %d\n", err);
  512. return err;
  513. }
  514. status = stir->fifo_status[0];
  515. count = (unsigned)(stir->fifo_status[2] & 0x1f) << 8
  516. | stir->fifo_status[1];
  517. pr_debug("fifo status 0x%lx count %lu\n", status, count);
  518. /* is fifo receiving already, or empty */
  519. if (!(status & FIFOCTL_DIR)
  520. || (status & FIFOCTL_EMPTY))
  521. return 0;
  522. if (signal_pending(current))
  523. return -EINTR;
  524. /* shutting down? */
  525. if (!netif_running(stir->netdev)
  526. || !netif_device_present(stir->netdev))
  527. return -ESHUTDOWN;
  528. /* only waiting for some space */
  529. if (space >= 0 && STIR_FIFO_SIZE - 4 > space + count)
  530. return 0;
  531. /* queue confused */
  532. if (prev_count < count)
  533. break;
  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->netdev->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->netdev->stats.tx_packets++;
  606. stir->netdev->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->netdev->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. dev_info(&dev->dev,
  660. "%s: receive usb submit failed\n",
  661. stir->netdev->name);
  662. stir->receiving = 0;
  663. msleep(10);
  664. continue;
  665. }
  666. }
  667. /* sleep if nothing to send */
  668. set_current_state(TASK_INTERRUPTIBLE);
  669. schedule();
  670. }
  671. return 0;
  672. }
  673. /*
  674. * USB bulk receive completion callback.
  675. * Wakes up every ms (usb round trip) with wrapped
  676. * data.
  677. */
  678. static void stir_rcv_irq(struct urb *urb)
  679. {
  680. struct stir_cb *stir = urb->context;
  681. int err;
  682. /* in process of stopping, just drop data */
  683. if (!netif_running(stir->netdev))
  684. return;
  685. /* unlink, shutdown, unplug, other nasties */
  686. if (urb->status != 0)
  687. return;
  688. if (urb->actual_length > 0) {
  689. pr_debug("receive %d\n", urb->actual_length);
  690. unwrap_chars(stir, urb->transfer_buffer,
  691. urb->actual_length);
  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. dev_warn(&stir->netdev->dev, "usb receive submit error: %d\n",
  702. 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. static const struct net_device_ops stir_netdev_ops = {
  841. .ndo_open = stir_net_open,
  842. .ndo_stop = stir_net_close,
  843. .ndo_start_xmit = stir_hard_xmit,
  844. .ndo_do_ioctl = stir_net_ioctl,
  845. };
  846. /*
  847. * This routine is called by the USB subsystem for each new device
  848. * in the system. We need to check if the device is ours, and in
  849. * this case start handling it.
  850. * Note : it might be worth protecting this function by a global
  851. * spinlock... Or not, because maybe USB already deal with that...
  852. */
  853. static int stir_probe(struct usb_interface *intf,
  854. const struct usb_device_id *id)
  855. {
  856. struct usb_device *dev = interface_to_usbdev(intf);
  857. struct stir_cb *stir = NULL;
  858. struct net_device *net;
  859. int ret = -ENOMEM;
  860. /* Allocate network device container. */
  861. net = alloc_irdadev(sizeof(*stir));
  862. if(!net)
  863. goto err_out1;
  864. SET_NETDEV_DEV(net, &intf->dev);
  865. stir = netdev_priv(net);
  866. stir->netdev = net;
  867. stir->usbdev = dev;
  868. ret = usb_reset_configuration(dev);
  869. if (ret != 0) {
  870. err("stir4200: usb reset configuration failed");
  871. goto err_out2;
  872. }
  873. printk(KERN_INFO "SigmaTel STIr4200 IRDA/USB found at address %d, "
  874. "Vendor: %x, Product: %x\n",
  875. dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
  876. le16_to_cpu(dev->descriptor.idProduct));
  877. /* Initialize QoS for this device */
  878. irda_init_max_qos_capabilies(&stir->qos);
  879. /* That's the Rx capability. */
  880. stir->qos.baud_rate.bits &= IR_2400 | IR_9600 | IR_19200 |
  881. IR_38400 | IR_57600 | IR_115200 |
  882. (IR_4000000 << 8);
  883. stir->qos.min_turn_time.bits &= qos_mtt_bits;
  884. irda_qos_bits_to_value(&stir->qos);
  885. /* Override the network functions we need to use */
  886. net->netdev_ops = &stir_netdev_ops;
  887. ret = register_netdev(net);
  888. if (ret != 0)
  889. goto err_out2;
  890. dev_info(&intf->dev, "IrDA: Registered SigmaTel device %s\n",
  891. net->name);
  892. usb_set_intfdata(intf, stir);
  893. return 0;
  894. err_out2:
  895. free_netdev(net);
  896. err_out1:
  897. return ret;
  898. }
  899. /*
  900. * The current device is removed, the USB layer tell us to shut it down...
  901. */
  902. static void stir_disconnect(struct usb_interface *intf)
  903. {
  904. struct stir_cb *stir = usb_get_intfdata(intf);
  905. if (!stir)
  906. return;
  907. unregister_netdev(stir->netdev);
  908. free_netdev(stir->netdev);
  909. usb_set_intfdata(intf, NULL);
  910. }
  911. #ifdef CONFIG_PM
  912. /* USB suspend, so power off the transmitter/receiver */
  913. static int stir_suspend(struct usb_interface *intf, pm_message_t message)
  914. {
  915. struct stir_cb *stir = usb_get_intfdata(intf);
  916. netif_device_detach(stir->netdev);
  917. return 0;
  918. }
  919. /* Coming out of suspend, so reset hardware */
  920. static int stir_resume(struct usb_interface *intf)
  921. {
  922. struct stir_cb *stir = usb_get_intfdata(intf);
  923. netif_device_attach(stir->netdev);
  924. /* receiver restarted when send thread wakes up */
  925. return 0;
  926. }
  927. #endif
  928. /*
  929. * USB device callbacks
  930. */
  931. static struct usb_driver irda_driver = {
  932. .name = "stir4200",
  933. .probe = stir_probe,
  934. .disconnect = stir_disconnect,
  935. .id_table = dongles,
  936. #ifdef CONFIG_PM
  937. .suspend = stir_suspend,
  938. .resume = stir_resume,
  939. #endif
  940. };
  941. /*
  942. * Module insertion
  943. */
  944. static int __init stir_init(void)
  945. {
  946. return usb_register(&irda_driver);
  947. }
  948. module_init(stir_init);
  949. /*
  950. * Module removal
  951. */
  952. static void __exit stir_cleanup(void)
  953. {
  954. /* Deregister the driver and remove all pending instances */
  955. usb_deregister(&irda_driver);
  956. }
  957. module_exit(stir_cleanup);