stir4200.c 26 KB

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