stir4200.c 27 KB

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