donauboe.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  1. /*****************************************************************
  2. *
  3. * Filename: donauboe.c
  4. * Version: 2.17
  5. * Description: Driver for the Toshiba OBOE (or type-O or 701)
  6. * FIR Chipset, also supports the DONAUOBOE (type-DO
  7. * or d01) FIR chipset which as far as I know is
  8. * register compatible.
  9. * Documentation: http://libxg.free.fr/irda/lib-irda.html
  10. * Status: Experimental.
  11. * Author: James McKenzie <james@fishsoup.dhs.org>
  12. * Created at: Sat May 8 12:35:27 1999
  13. * Modified: Paul Bristow <paul.bristow@technologist.com>
  14. * Modified: Mon Nov 11 19:10:05 1999
  15. * Modified: James McKenzie <james@fishsoup.dhs.org>
  16. * Modified: Thu Mar 16 12:49:00 2000 (Substantial rewrite)
  17. * Modified: Sat Apr 29 00:23:03 2000 (Added DONAUOBOE support)
  18. * Modified: Wed May 24 23:45:02 2000 (Fixed chipio_t structure)
  19. * Modified: 2.13 Christian Gennerat <christian.gennerat@polytechnique.org>
  20. * Modified: 2.13 dim jan 07 21:57:39 2001 (tested with kernel 2.4 & irnet/ppp)
  21. * Modified: 2.14 Christian Gennerat <christian.gennerat@polytechnique.org>
  22. * Modified: 2.14 lun fev 05 17:55:59 2001 (adapted to patch-2.4.1-pre8-irda1)
  23. * Modified: 2.15 Martin Lucina <mato@kotelna.sk>
  24. * Modified: 2.15 Fri Jun 21 20:40:59 2002 (sync with 2.4.18, substantial fixes)
  25. * Modified: 2.16 Martin Lucina <mato@kotelna.sk>
  26. * Modified: 2.16 Sat Jun 22 18:54:29 2002 (fix freeregion, default to verbose)
  27. * Modified: 2.17 Christian Gennerat <christian.gennerat@polytechnique.org>
  28. * Modified: 2.17 jeu sep 12 08:50:20 2002 (save_flags();cli(); replaced by spinlocks)
  29. * Modified: 2.18 Christian Gennerat <christian.gennerat@polytechnique.org>
  30. * Modified: 2.18 ven jan 10 03:14:16 2003 Change probe default options
  31. *
  32. * Copyright (c) 1999 James McKenzie, All Rights Reserved.
  33. *
  34. * This program is free software; you can redistribute it and/or
  35. * modify it under the terms of the GNU General Public License as
  36. * published by the Free Software Foundation; either version 2 of
  37. * the License, or (at your option) any later version.
  38. *
  39. * Neither James McKenzie nor Cambridge University admit liability nor
  40. * provide warranty for any of this software. This material is
  41. * provided "AS-IS" and at no charge.
  42. *
  43. * Applicable Models : Libretto 100/110CT and many more.
  44. * Toshiba refers to this chip as the type-O IR port,
  45. * or the type-DO IR port.
  46. *
  47. ********************************************************************/
  48. /* Look at toshoboe.h (currently in include/net/irda) for details of */
  49. /* Where to get documentation on the chip */
  50. /* See below for a description of the logic in this driver */
  51. /* User servicable parts */
  52. /* USE_PROBE Create the code which probes the chip and does a few tests */
  53. /* do_probe module parameter Enable this code */
  54. /* Probe code is very useful for understanding how the hardware works */
  55. /* Use it with various combinations of TT_LEN, RX_LEN */
  56. /* Strongly recommended, disable if the probe fails on your machine */
  57. /* and send me <james@fishsoup.dhs.org> the output of dmesg */
  58. #define USE_PROBE 1
  59. #undef USE_PROBE
  60. /* Trace Transmit ring, interrupts, Receive ring or not ? */
  61. #define PROBE_VERBOSE 1
  62. /* Debug option, examine sent and received raw data */
  63. /* Irdadump is better, but does not see all packets. enable it if you want. */
  64. #undef DUMP_PACKETS
  65. /* MIR mode has not been tested. Some behaviour is different */
  66. /* Seems to work against an Ericsson R520 for me. -Martin */
  67. #define USE_MIR
  68. /* Schedule back to back hardware transmits wherever possible, otherwise */
  69. /* we need an interrupt for every frame, unset if oboe works for a bit and */
  70. /* then hangs */
  71. #define OPTIMIZE_TX
  72. /* Set the number of slots in the rings */
  73. /* If you get rx/tx fifo overflows at high bitrates, you can try increasing */
  74. /* these */
  75. #define RING_SIZE (OBOE_RING_SIZE_RX8 | OBOE_RING_SIZE_TX8)
  76. #define TX_SLOTS 8
  77. #define RX_SLOTS 8
  78. /* Less user servicable parts below here */
  79. /* Test, Transmit and receive buffer sizes, adjust at your peril */
  80. /* remarks: nfs usually needs 1k blocks */
  81. /* remarks: in SIR mode, CRC is received, -> RX_LEN=TX_LEN+2 */
  82. /* remarks: test accepts large blocks. Standard is 0x80 */
  83. /* When TT_LEN > RX_LEN (SIR mode) data is stored in successive slots. */
  84. /* When 3 or more slots are needed for each test packet, */
  85. /* data received in the first slots is overwritten, even */
  86. /* if OBOE_CTL_RX_HW_OWNS is not set, without any error! */
  87. #define TT_LEN 0x80
  88. #define TX_LEN 0xc00
  89. #define RX_LEN 0xc04
  90. /* Real transmitted length (SIR mode) is about 14+(2%*TX_LEN) more */
  91. /* long than user-defined length (see async_wrap_skb) and is less then 4K */
  92. /* Real received length is (max RX_LEN) differs from user-defined */
  93. /* length only b the CRC (2 or 4 bytes) */
  94. #define BUF_SAFETY 0x7a
  95. #define RX_BUF_SZ (RX_LEN)
  96. #define TX_BUF_SZ (TX_LEN+BUF_SAFETY)
  97. /* Logic of the netdev part of this driver */
  98. /* The RX ring is filled with buffers, when a packet arrives */
  99. /* it is DMA'd into the buffer which is marked used and RxDone called */
  100. /* RxDone forms an skb (and checks the CRC if in SIR mode) and ships */
  101. /* the packet off upstairs */
  102. /* The transmitter on the oboe chip can work in one of two modes */
  103. /* for each ring->tx[] the transmitter can either */
  104. /* a) transmit the packet, leave the trasmitter enabled and proceed to */
  105. /* the next ring */
  106. /* OR */
  107. /* b) transmit the packet, switch off the transmitter and issue TxDone */
  108. /* All packets are entered into the ring in mode b), if the ring was */
  109. /* empty the transmitter is started. */
  110. /* If OPTIMIZE_TX is defined then in TxDone if the ring contains */
  111. /* more than one packet, all but the last are set to mode a) [HOWEVER */
  112. /* the hardware may not notice this, this is why we start in mode b) ] */
  113. /* then restart the transmitter */
  114. /* If OPTIMIZE_TX is not defined then we just restart the transmitter */
  115. /* if the ring isn't empty */
  116. /* Speed changes are delayed until the TxRing is empty */
  117. /* mtt is handled by generating packets with bad CRCs, before the data */
  118. /* TODO: */
  119. /* check the mtt works ok */
  120. /* finish the watchdog */
  121. /* No user servicable parts below here */
  122. #include <linux/module.h>
  123. #include <linux/kernel.h>
  124. #include <linux/types.h>
  125. #include <linux/skbuff.h>
  126. #include <linux/netdevice.h>
  127. #include <linux/ioport.h>
  128. #include <linux/delay.h>
  129. #include <linux/slab.h>
  130. #include <linux/init.h>
  131. #include <linux/interrupt.h>
  132. #include <linux/pci.h>
  133. #include <linux/rtnetlink.h>
  134. #include <asm/io.h>
  135. #include <net/irda/wrapper.h>
  136. #include <net/irda/irda.h>
  137. //#include <net/irda/irmod.h>
  138. //#include <net/irda/irlap_frame.h>
  139. #include <net/irda/irda_device.h>
  140. #include <net/irda/crc.h>
  141. #include "donauboe.h"
  142. #define INB(port) inb_p(port)
  143. #define OUTB(val,port) outb_p(val,port)
  144. #define OUTBP(val,port) outb_p(val,port)
  145. #define PROMPT OUTB(OBOE_PROMPT_BIT,OBOE_PROMPT);
  146. #if PROBE_VERBOSE
  147. #define PROBE_DEBUG(args...) (printk (args))
  148. #else
  149. #define PROBE_DEBUG(args...) ;
  150. #endif
  151. /* Set the DMA to be byte at a time */
  152. #define CONFIG0H_DMA_OFF OBOE_CONFIG0H_RCVANY
  153. #define CONFIG0H_DMA_ON_NORX CONFIG0H_DMA_OFF| OBOE_CONFIG0H_ENDMAC
  154. #define CONFIG0H_DMA_ON CONFIG0H_DMA_ON_NORX | OBOE_CONFIG0H_ENRX
  155. static DEFINE_PCI_DEVICE_TABLE(toshoboe_pci_tbl) = {
  156. { PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_FIR701, PCI_ANY_ID, PCI_ANY_ID, },
  157. { PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_FIRD01, PCI_ANY_ID, PCI_ANY_ID, },
  158. { } /* Terminating entry */
  159. };
  160. MODULE_DEVICE_TABLE(pci, toshoboe_pci_tbl);
  161. #define DRIVER_NAME "toshoboe"
  162. static char *driver_name = DRIVER_NAME;
  163. static int max_baud = 4000000;
  164. #ifdef USE_PROBE
  165. static bool do_probe = false;
  166. #endif
  167. /**********************************************************************/
  168. static int
  169. toshoboe_checkfcs (unsigned char *buf, int len)
  170. {
  171. int i;
  172. union
  173. {
  174. __u16 value;
  175. __u8 bytes[2];
  176. }
  177. fcs;
  178. fcs.value = INIT_FCS;
  179. for (i = 0; i < len; ++i)
  180. fcs.value = irda_fcs (fcs.value, *(buf++));
  181. return fcs.value == GOOD_FCS;
  182. }
  183. /***********************************************************************/
  184. /* Generic chip handling code */
  185. #ifdef DUMP_PACKETS
  186. static unsigned char dump[50];
  187. static void
  188. _dumpbufs (unsigned char *data, int len, char tete)
  189. {
  190. int i,j;
  191. char head=tete;
  192. for (i=0;i<len;i+=16) {
  193. for (j=0;j<16 && i+j<len;j++) { sprintf(&dump[3*j],"%02x.",data[i+j]); }
  194. dump [3*j]=0;
  195. IRDA_DEBUG (2, "%c%s\n",head , dump);
  196. head='+';
  197. }
  198. }
  199. #endif
  200. #ifdef USE_PROBE
  201. /* Dump the registers */
  202. static void
  203. toshoboe_dumpregs (struct toshoboe_cb *self)
  204. {
  205. __u32 ringbase;
  206. IRDA_DEBUG (4, "%s()\n", __func__);
  207. ringbase = INB (OBOE_RING_BASE0) << 10;
  208. ringbase |= INB (OBOE_RING_BASE1) << 18;
  209. ringbase |= INB (OBOE_RING_BASE2) << 26;
  210. printk (KERN_ERR DRIVER_NAME ": Register dump:\n");
  211. printk (KERN_ERR "Interrupts: Tx:%d Rx:%d TxUnder:%d RxOver:%d Sip:%d\n",
  212. self->int_tx, self->int_rx, self->int_txunder, self->int_rxover,
  213. self->int_sip);
  214. printk (KERN_ERR "RX %02x TX %02x RingBase %08x\n",
  215. INB (OBOE_RXSLOT), INB (OBOE_TXSLOT), ringbase);
  216. printk (KERN_ERR "RING_SIZE %02x IER %02x ISR %02x\n",
  217. INB (OBOE_RING_SIZE), INB (OBOE_IER), INB (OBOE_ISR));
  218. printk (KERN_ERR "CONFIG1 %02x STATUS %02x\n",
  219. INB (OBOE_CONFIG1), INB (OBOE_STATUS));
  220. printk (KERN_ERR "CONFIG0 %02x%02x ENABLE %02x%02x\n",
  221. INB (OBOE_CONFIG0H), INB (OBOE_CONFIG0L),
  222. INB (OBOE_ENABLEH), INB (OBOE_ENABLEL));
  223. printk (KERN_ERR "NEW_PCONFIG %02x%02x CURR_PCONFIG %02x%02x\n",
  224. INB (OBOE_NEW_PCONFIGH), INB (OBOE_NEW_PCONFIGL),
  225. INB (OBOE_CURR_PCONFIGH), INB (OBOE_CURR_PCONFIGL));
  226. printk (KERN_ERR "MAXLEN %02x%02x RXCOUNT %02x%02x\n",
  227. INB (OBOE_MAXLENH), INB (OBOE_MAXLENL),
  228. INB (OBOE_RXCOUNTL), INB (OBOE_RXCOUNTH));
  229. if (self->ring)
  230. {
  231. int i;
  232. ringbase = virt_to_bus (self->ring);
  233. printk (KERN_ERR "Ring at %08x:\n", ringbase);
  234. printk (KERN_ERR "RX:");
  235. for (i = 0; i < RX_SLOTS; ++i)
  236. printk (" (%d,%02x)",self->ring->rx[i].len,self->ring->rx[i].control);
  237. printk ("\n");
  238. printk (KERN_ERR "TX:");
  239. for (i = 0; i < RX_SLOTS; ++i)
  240. printk (" (%d,%02x)",self->ring->tx[i].len,self->ring->tx[i].control);
  241. printk ("\n");
  242. }
  243. }
  244. #endif
  245. /*Don't let the chip look at memory */
  246. static void
  247. toshoboe_disablebm (struct toshoboe_cb *self)
  248. {
  249. __u8 command;
  250. IRDA_DEBUG (4, "%s()\n", __func__);
  251. pci_read_config_byte (self->pdev, PCI_COMMAND, &command);
  252. command &= ~PCI_COMMAND_MASTER;
  253. pci_write_config_byte (self->pdev, PCI_COMMAND, command);
  254. }
  255. /* Shutdown the chip and point the taskfile reg somewhere else */
  256. static void
  257. toshoboe_stopchip (struct toshoboe_cb *self)
  258. {
  259. IRDA_DEBUG (4, "%s()\n", __func__);
  260. /*Disable interrupts */
  261. OUTB (0x0, OBOE_IER);
  262. /*Disable DMA, Disable Rx, Disable Tx */
  263. OUTB (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);
  264. /*Disable SIR MIR FIR, Tx and Rx */
  265. OUTB (0x00, OBOE_ENABLEH);
  266. /*Point the ring somewhere safe */
  267. OUTB (0x3f, OBOE_RING_BASE2);
  268. OUTB (0xff, OBOE_RING_BASE1);
  269. OUTB (0xff, OBOE_RING_BASE0);
  270. OUTB (RX_LEN >> 8, OBOE_MAXLENH);
  271. OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
  272. /*Acknoledge any pending interrupts */
  273. OUTB (0xff, OBOE_ISR);
  274. /*Why */
  275. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  276. /*switch it off */
  277. OUTB (OBOE_CONFIG1_OFF, OBOE_CONFIG1);
  278. toshoboe_disablebm (self);
  279. }
  280. /* Transmitter initialization */
  281. static void
  282. toshoboe_start_DMA (struct toshoboe_cb *self, int opts)
  283. {
  284. OUTB (0x0, OBOE_ENABLEH);
  285. OUTB (CONFIG0H_DMA_ON | opts, OBOE_CONFIG0H);
  286. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  287. PROMPT;
  288. }
  289. /*Set the baud rate */
  290. static void
  291. toshoboe_setbaud (struct toshoboe_cb *self)
  292. {
  293. __u16 pconfig = 0;
  294. __u8 config0l = 0;
  295. IRDA_DEBUG (2, "%s(%d/%d)\n", __func__, self->speed, self->io.speed);
  296. switch (self->speed)
  297. {
  298. case 2400:
  299. case 4800:
  300. case 9600:
  301. case 19200:
  302. case 38400:
  303. case 57600:
  304. case 115200:
  305. #ifdef USE_MIR
  306. case 1152000:
  307. #endif
  308. case 4000000:
  309. break;
  310. default:
  311. printk (KERN_ERR DRIVER_NAME ": switch to unsupported baudrate %d\n",
  312. self->speed);
  313. return;
  314. }
  315. switch (self->speed)
  316. {
  317. /* For SIR the preamble is done by adding XBOFs */
  318. /* to the packet */
  319. /* set to filtered SIR mode, filter looks for BOF and EOF */
  320. case 2400:
  321. pconfig |= 47 << OBOE_PCONFIG_BAUDSHIFT;
  322. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  323. break;
  324. case 4800:
  325. pconfig |= 23 << OBOE_PCONFIG_BAUDSHIFT;
  326. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  327. break;
  328. case 9600:
  329. pconfig |= 11 << OBOE_PCONFIG_BAUDSHIFT;
  330. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  331. break;
  332. case 19200:
  333. pconfig |= 5 << OBOE_PCONFIG_BAUDSHIFT;
  334. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  335. break;
  336. case 38400:
  337. pconfig |= 2 << OBOE_PCONFIG_BAUDSHIFT;
  338. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  339. break;
  340. case 57600:
  341. pconfig |= 1 << OBOE_PCONFIG_BAUDSHIFT;
  342. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  343. break;
  344. case 115200:
  345. pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
  346. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  347. break;
  348. default:
  349. /*Set to packet based reception */
  350. OUTB (RX_LEN >> 8, OBOE_MAXLENH);
  351. OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
  352. break;
  353. }
  354. switch (self->speed)
  355. {
  356. case 2400:
  357. case 4800:
  358. case 9600:
  359. case 19200:
  360. case 38400:
  361. case 57600:
  362. case 115200:
  363. config0l = OBOE_CONFIG0L_ENSIR;
  364. if (self->async)
  365. {
  366. /*Set to character based reception */
  367. /*System will lock if MAXLEN=0 */
  368. /*so have to be careful */
  369. OUTB (0x01, OBOE_MAXLENH);
  370. OUTB (0x01, OBOE_MAXLENL);
  371. OUTB (0x00, OBOE_MAXLENH);
  372. }
  373. else
  374. {
  375. /*Set to packet based reception */
  376. config0l |= OBOE_CONFIG0L_ENSIRF;
  377. OUTB (RX_LEN >> 8, OBOE_MAXLENH);
  378. OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
  379. }
  380. break;
  381. #ifdef USE_MIR
  382. /* MIR mode */
  383. /* Set for 16 bit CRC and enable MIR */
  384. /* Preamble now handled by the chip */
  385. case 1152000:
  386. pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
  387. pconfig |= 8 << OBOE_PCONFIG_WIDTHSHIFT;
  388. pconfig |= 1 << OBOE_PCONFIG_PREAMBLESHIFT;
  389. config0l = OBOE_CONFIG0L_CRC16 | OBOE_CONFIG0L_ENMIR;
  390. break;
  391. #endif
  392. /* FIR mode */
  393. /* Set for 32 bit CRC and enable FIR */
  394. /* Preamble handled by the chip */
  395. case 4000000:
  396. pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
  397. /* Documentation says 14, but toshiba use 15 in their drivers */
  398. pconfig |= 15 << OBOE_PCONFIG_PREAMBLESHIFT;
  399. config0l = OBOE_CONFIG0L_ENFIR;
  400. break;
  401. }
  402. /* Copy into new PHY config buffer */
  403. OUTBP (pconfig >> 8, OBOE_NEW_PCONFIGH);
  404. OUTB (pconfig & 0xff, OBOE_NEW_PCONFIGL);
  405. OUTB (config0l, OBOE_CONFIG0L);
  406. /* Now make OBOE copy from new PHY to current PHY */
  407. OUTB (0x0, OBOE_ENABLEH);
  408. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  409. PROMPT;
  410. /* speed change executed */
  411. self->new_speed = 0;
  412. self->io.speed = self->speed;
  413. }
  414. /*Let the chip look at memory */
  415. static void
  416. toshoboe_enablebm (struct toshoboe_cb *self)
  417. {
  418. IRDA_DEBUG (4, "%s()\n", __func__);
  419. pci_set_master (self->pdev);
  420. }
  421. /*setup the ring */
  422. static void
  423. toshoboe_initring (struct toshoboe_cb *self)
  424. {
  425. int i;
  426. IRDA_DEBUG (4, "%s()\n", __func__);
  427. for (i = 0; i < TX_SLOTS; ++i)
  428. {
  429. self->ring->tx[i].len = 0;
  430. self->ring->tx[i].control = 0x00;
  431. self->ring->tx[i].address = virt_to_bus (self->tx_bufs[i]);
  432. }
  433. for (i = 0; i < RX_SLOTS; ++i)
  434. {
  435. self->ring->rx[i].len = RX_LEN;
  436. self->ring->rx[i].len = 0;
  437. self->ring->rx[i].address = virt_to_bus (self->rx_bufs[i]);
  438. self->ring->rx[i].control = OBOE_CTL_RX_HW_OWNS;
  439. }
  440. }
  441. static void
  442. toshoboe_resetptrs (struct toshoboe_cb *self)
  443. {
  444. /* Can reset pointers by twidling DMA */
  445. OUTB (0x0, OBOE_ENABLEH);
  446. OUTBP (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);
  447. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  448. self->rxs = inb_p (OBOE_RXSLOT) & OBOE_SLOT_MASK;
  449. self->txs = inb_p (OBOE_TXSLOT) & OBOE_SLOT_MASK;
  450. }
  451. /* Called in locked state */
  452. static void
  453. toshoboe_initptrs (struct toshoboe_cb *self)
  454. {
  455. /* spin_lock_irqsave(self->spinlock, flags); */
  456. /* save_flags (flags); */
  457. /* Can reset pointers by twidling DMA */
  458. toshoboe_resetptrs (self);
  459. OUTB (0x0, OBOE_ENABLEH);
  460. OUTB (CONFIG0H_DMA_ON, OBOE_CONFIG0H);
  461. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  462. self->txpending = 0;
  463. /* spin_unlock_irqrestore(self->spinlock, flags); */
  464. /* restore_flags (flags); */
  465. }
  466. /* Wake the chip up and get it looking at the rings */
  467. /* Called in locked state */
  468. static void
  469. toshoboe_startchip (struct toshoboe_cb *self)
  470. {
  471. __u32 physaddr;
  472. IRDA_DEBUG (4, "%s()\n", __func__);
  473. toshoboe_initring (self);
  474. toshoboe_enablebm (self);
  475. OUTBP (OBOE_CONFIG1_RESET, OBOE_CONFIG1);
  476. OUTBP (OBOE_CONFIG1_ON, OBOE_CONFIG1);
  477. /* Stop the clocks */
  478. OUTB (0, OBOE_ENABLEH);
  479. /*Set size of rings */
  480. OUTB (RING_SIZE, OBOE_RING_SIZE);
  481. /*Acknoledge any pending interrupts */
  482. OUTB (0xff, OBOE_ISR);
  483. /*Enable ints */
  484. OUTB (OBOE_INT_TXDONE | OBOE_INT_RXDONE |
  485. OBOE_INT_TXUNDER | OBOE_INT_RXOVER | OBOE_INT_SIP , OBOE_IER);
  486. /*Acknoledge any pending interrupts */
  487. OUTB (0xff, OBOE_ISR);
  488. /*Set the maximum packet length to 0xfff (4095) */
  489. OUTB (RX_LEN >> 8, OBOE_MAXLENH);
  490. OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
  491. /*Shutdown DMA */
  492. OUTB (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);
  493. /*Find out where the rings live */
  494. physaddr = virt_to_bus (self->ring);
  495. IRDA_ASSERT ((physaddr & 0x3ff) == 0,
  496. printk (KERN_ERR DRIVER_NAME "ring not correctly aligned\n");
  497. return;);
  498. OUTB ((physaddr >> 10) & 0xff, OBOE_RING_BASE0);
  499. OUTB ((physaddr >> 18) & 0xff, OBOE_RING_BASE1);
  500. OUTB ((physaddr >> 26) & 0x3f, OBOE_RING_BASE2);
  501. /*Enable DMA controller in byte mode and RX */
  502. OUTB (CONFIG0H_DMA_ON, OBOE_CONFIG0H);
  503. /* Start up the clocks */
  504. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  505. /*set to sensible speed */
  506. self->speed = 9600;
  507. toshoboe_setbaud (self);
  508. toshoboe_initptrs (self);
  509. }
  510. static void
  511. toshoboe_isntstuck (struct toshoboe_cb *self)
  512. {
  513. }
  514. static void
  515. toshoboe_checkstuck (struct toshoboe_cb *self)
  516. {
  517. unsigned long flags;
  518. if (0)
  519. {
  520. spin_lock_irqsave(&self->spinlock, flags);
  521. /* This will reset the chip completely */
  522. printk (KERN_ERR DRIVER_NAME ": Resetting chip\n");
  523. toshoboe_stopchip (self);
  524. toshoboe_startchip (self);
  525. spin_unlock_irqrestore(&self->spinlock, flags);
  526. }
  527. }
  528. /*Generate packet of about mtt us long */
  529. static int
  530. toshoboe_makemttpacket (struct toshoboe_cb *self, void *buf, int mtt)
  531. {
  532. int xbofs;
  533. xbofs = ((int) (mtt/100)) * (int) (self->speed);
  534. xbofs=xbofs/80000; /*Eight bits per byte, and mtt is in us*/
  535. xbofs++;
  536. IRDA_DEBUG (2, DRIVER_NAME
  537. ": generated mtt of %d bytes for %d us at %d baud\n"
  538. , xbofs,mtt,self->speed);
  539. if (xbofs > TX_LEN)
  540. {
  541. printk (KERN_ERR DRIVER_NAME ": wanted %d bytes MTT but TX_LEN is %d\n",
  542. xbofs, TX_LEN);
  543. xbofs = TX_LEN;
  544. }
  545. /*xbofs will do for SIR, MIR and FIR,SIR mode doesn't generate a checksum anyway */
  546. memset (buf, XBOF, xbofs);
  547. return xbofs;
  548. }
  549. #ifdef USE_PROBE
  550. /***********************************************************************/
  551. /* Probe code */
  552. static void
  553. toshoboe_dumptx (struct toshoboe_cb *self)
  554. {
  555. int i;
  556. PROBE_DEBUG(KERN_WARNING "TX:");
  557. for (i = 0; i < RX_SLOTS; ++i)
  558. PROBE_DEBUG(" (%d,%02x)",self->ring->tx[i].len,self->ring->tx[i].control);
  559. PROBE_DEBUG(" [%d]\n",self->speed);
  560. }
  561. static void
  562. toshoboe_dumprx (struct toshoboe_cb *self, int score)
  563. {
  564. int i;
  565. PROBE_DEBUG(" %d\nRX:",score);
  566. for (i = 0; i < RX_SLOTS; ++i)
  567. PROBE_DEBUG(" (%d,%02x)",self->ring->rx[i].len,self->ring->rx[i].control);
  568. PROBE_DEBUG("\n");
  569. }
  570. static inline int
  571. stuff_byte (__u8 byte, __u8 * buf)
  572. {
  573. switch (byte)
  574. {
  575. case BOF: /* FALLTHROUGH */
  576. case EOF: /* FALLTHROUGH */
  577. case CE:
  578. /* Insert transparently coded */
  579. buf[0] = CE; /* Send link escape */
  580. buf[1] = byte ^ IRDA_TRANS; /* Complement bit 5 */
  581. return 2;
  582. /* break; */
  583. default:
  584. /* Non-special value, no transparency required */
  585. buf[0] = byte;
  586. return 1;
  587. /* break; */
  588. }
  589. }
  590. static irqreturn_t
  591. toshoboe_probeinterrupt (int irq, void *dev_id)
  592. {
  593. struct toshoboe_cb *self = dev_id;
  594. __u8 irqstat;
  595. irqstat = INB (OBOE_ISR);
  596. /* was it us */
  597. if (!(irqstat & OBOE_INT_MASK))
  598. return IRQ_NONE;
  599. /* Ack all the interrupts */
  600. OUTB (irqstat, OBOE_ISR);
  601. if (irqstat & OBOE_INT_TXDONE)
  602. {
  603. int txp;
  604. self->int_tx++;
  605. PROBE_DEBUG("T");
  606. txp = INB (OBOE_TXSLOT) & OBOE_SLOT_MASK;
  607. if (self->ring->tx[txp].control & OBOE_CTL_TX_HW_OWNS)
  608. {
  609. self->int_tx+=100;
  610. PROBE_DEBUG("S");
  611. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
  612. }
  613. }
  614. if (irqstat & OBOE_INT_RXDONE) {
  615. self->int_rx++;
  616. PROBE_DEBUG("R"); }
  617. if (irqstat & OBOE_INT_TXUNDER) {
  618. self->int_txunder++;
  619. PROBE_DEBUG("U"); }
  620. if (irqstat & OBOE_INT_RXOVER) {
  621. self->int_rxover++;
  622. PROBE_DEBUG("O"); }
  623. if (irqstat & OBOE_INT_SIP) {
  624. self->int_sip++;
  625. PROBE_DEBUG("I"); }
  626. return IRQ_HANDLED;
  627. }
  628. static int
  629. toshoboe_maketestpacket (unsigned char *buf, int badcrc, int fir)
  630. {
  631. int i;
  632. int len = 0;
  633. union
  634. {
  635. __u16 value;
  636. __u8 bytes[2];
  637. }
  638. fcs;
  639. if (fir)
  640. {
  641. memset (buf, 0, TT_LEN);
  642. return TT_LEN;
  643. }
  644. fcs.value = INIT_FCS;
  645. memset (buf, XBOF, 10);
  646. len += 10;
  647. buf[len++] = BOF;
  648. for (i = 0; i < TT_LEN; ++i)
  649. {
  650. len += stuff_byte (i, buf + len);
  651. fcs.value = irda_fcs (fcs.value, i);
  652. }
  653. len += stuff_byte (fcs.bytes[0] ^ badcrc, buf + len);
  654. len += stuff_byte (fcs.bytes[1] ^ badcrc, buf + len);
  655. buf[len++] = EOF;
  656. len++;
  657. return len;
  658. }
  659. static int
  660. toshoboe_probefail (struct toshoboe_cb *self, char *msg)
  661. {
  662. printk (KERN_ERR DRIVER_NAME "probe(%d) failed %s\n",self-> speed, msg);
  663. toshoboe_dumpregs (self);
  664. toshoboe_stopchip (self);
  665. free_irq (self->io.irq, (void *) self);
  666. return 0;
  667. }
  668. static int
  669. toshoboe_numvalidrcvs (struct toshoboe_cb *self)
  670. {
  671. int i, ret = 0;
  672. for (i = 0; i < RX_SLOTS; ++i)
  673. if ((self->ring->rx[i].control & 0xe0) == 0)
  674. ret++;
  675. return ret;
  676. }
  677. static int
  678. toshoboe_numrcvs (struct toshoboe_cb *self)
  679. {
  680. int i, ret = 0;
  681. for (i = 0; i < RX_SLOTS; ++i)
  682. if (!(self->ring->rx[i].control & OBOE_CTL_RX_HW_OWNS))
  683. ret++;
  684. return ret;
  685. }
  686. static int
  687. toshoboe_probe (struct toshoboe_cb *self)
  688. {
  689. int i, j, n;
  690. #ifdef USE_MIR
  691. static const int bauds[] = { 9600, 115200, 4000000, 1152000 };
  692. #else
  693. static const int bauds[] = { 9600, 115200, 4000000 };
  694. #endif
  695. unsigned long flags;
  696. IRDA_DEBUG (4, "%s()\n", __func__);
  697. if (request_irq (self->io.irq, toshoboe_probeinterrupt,
  698. self->io.irqflags, "toshoboe", (void *) self))
  699. {
  700. printk (KERN_ERR DRIVER_NAME ": probe failed to allocate irq %d\n",
  701. self->io.irq);
  702. return 0;
  703. }
  704. /* test 1: SIR filter and back to back */
  705. for (j = 0; j < ARRAY_SIZE(bauds); ++j)
  706. {
  707. int fir = (j > 1);
  708. toshoboe_stopchip (self);
  709. spin_lock_irqsave(&self->spinlock, flags);
  710. /*Address is already setup */
  711. toshoboe_startchip (self);
  712. self->int_rx = self->int_tx = 0;
  713. self->speed = bauds[j];
  714. toshoboe_setbaud (self);
  715. toshoboe_initptrs (self);
  716. spin_unlock_irqrestore(&self->spinlock, flags);
  717. self->ring->tx[self->txs].control =
  718. /* (FIR only) OBOE_CTL_TX_SIP needed for switching to next slot */
  719. /* MIR: all received data is stored in one slot */
  720. (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
  721. : OBOE_CTL_TX_HW_OWNS ;
  722. self->ring->tx[self->txs].len =
  723. toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
  724. self->txs++;
  725. self->txs %= TX_SLOTS;
  726. self->ring->tx[self->txs].control =
  727. (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_SIP
  728. : OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX ;
  729. self->ring->tx[self->txs].len =
  730. toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
  731. self->txs++;
  732. self->txs %= TX_SLOTS;
  733. self->ring->tx[self->txs].control =
  734. (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
  735. : OBOE_CTL_TX_HW_OWNS ;
  736. self->ring->tx[self->txs].len =
  737. toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
  738. self->txs++;
  739. self->txs %= TX_SLOTS;
  740. self->ring->tx[self->txs].control =
  741. (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
  742. | OBOE_CTL_TX_SIP | OBOE_CTL_TX_BAD_CRC
  743. : OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX ;
  744. self->ring->tx[self->txs].len =
  745. toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
  746. self->txs++;
  747. self->txs %= TX_SLOTS;
  748. toshoboe_dumptx (self);
  749. /* Turn on TX and RX and loopback */
  750. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
  751. i = 0;
  752. n = fir ? 1 : 4;
  753. while (toshoboe_numvalidrcvs (self) != n)
  754. {
  755. if (i > 4800)
  756. return toshoboe_probefail (self, "filter test");
  757. udelay ((9600*(TT_LEN+16))/self->speed);
  758. i++;
  759. }
  760. n = fir ? 203 : 102;
  761. while ((toshoboe_numrcvs(self) != self->int_rx) || (self->int_tx != n))
  762. {
  763. if (i > 4800)
  764. return toshoboe_probefail (self, "interrupt test");
  765. udelay ((9600*(TT_LEN+16))/self->speed);
  766. i++;
  767. }
  768. toshoboe_dumprx (self,i);
  769. }
  770. /* test 2: SIR in char at a time */
  771. toshoboe_stopchip (self);
  772. self->int_rx = self->int_tx = 0;
  773. spin_lock_irqsave(&self->spinlock, flags);
  774. toshoboe_startchip (self);
  775. spin_unlock_irqrestore(&self->spinlock, flags);
  776. self->async = 1;
  777. self->speed = 115200;
  778. toshoboe_setbaud (self);
  779. self->ring->tx[self->txs].control =
  780. OBOE_CTL_TX_RTCENTX | OBOE_CTL_TX_HW_OWNS;
  781. self->ring->tx[self->txs].len = 4;
  782. ((unsigned char *) self->tx_bufs[self->txs])[0] = 'f';
  783. ((unsigned char *) self->tx_bufs[self->txs])[1] = 'i';
  784. ((unsigned char *) self->tx_bufs[self->txs])[2] = 's';
  785. ((unsigned char *) self->tx_bufs[self->txs])[3] = 'h';
  786. toshoboe_dumptx (self);
  787. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
  788. i = 0;
  789. while (toshoboe_numvalidrcvs (self) != 4)
  790. {
  791. if (i > 100)
  792. return toshoboe_probefail (self, "Async test");
  793. udelay (100);
  794. i++;
  795. }
  796. while ((toshoboe_numrcvs (self) != self->int_rx) || (self->int_tx != 1))
  797. {
  798. if (i > 100)
  799. return toshoboe_probefail (self, "Async interrupt test");
  800. udelay (100);
  801. i++;
  802. }
  803. toshoboe_dumprx (self,i);
  804. self->async = 0;
  805. self->speed = 9600;
  806. toshoboe_setbaud (self);
  807. toshoboe_stopchip (self);
  808. free_irq (self->io.irq, (void *) self);
  809. printk (KERN_WARNING DRIVER_NAME ": Self test passed ok\n");
  810. return 1;
  811. }
  812. #endif
  813. /******************************************************************/
  814. /* Netdev style code */
  815. /* Transmit something */
  816. static netdev_tx_t
  817. toshoboe_hard_xmit (struct sk_buff *skb, struct net_device *dev)
  818. {
  819. struct toshoboe_cb *self;
  820. __s32 speed;
  821. int mtt, len, ctl;
  822. unsigned long flags;
  823. struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
  824. self = netdev_priv(dev);
  825. IRDA_ASSERT (self != NULL, return NETDEV_TX_OK; );
  826. IRDA_DEBUG (1, "%s.tx:%x(%x)%x\n", __func__
  827. ,skb->len,self->txpending,INB (OBOE_ENABLEH));
  828. if (!cb->magic) {
  829. IRDA_DEBUG (2, "%s.Not IrLAP:%x\n", __func__, cb->magic);
  830. #ifdef DUMP_PACKETS
  831. _dumpbufs(skb->data,skb->len,'>');
  832. #endif
  833. }
  834. /* change speed pending, wait for its execution */
  835. if (self->new_speed)
  836. return NETDEV_TX_BUSY;
  837. /* device stopped (apm) wait for restart */
  838. if (self->stopped)
  839. return NETDEV_TX_BUSY;
  840. toshoboe_checkstuck (self);
  841. /* Check if we need to change the speed */
  842. /* But not now. Wait after transmission if mtt not required */
  843. speed=irda_get_next_speed(skb);
  844. if ((speed != self->io.speed) && (speed != -1))
  845. {
  846. spin_lock_irqsave(&self->spinlock, flags);
  847. if (self->txpending || skb->len)
  848. {
  849. self->new_speed = speed;
  850. IRDA_DEBUG (1, "%s: Queued TxDone scheduled speed change %d\n" ,
  851. __func__, speed);
  852. /* if no data, that's all! */
  853. if (!skb->len)
  854. {
  855. spin_unlock_irqrestore(&self->spinlock, flags);
  856. dev_kfree_skb (skb);
  857. return NETDEV_TX_OK;
  858. }
  859. /* True packet, go on, but */
  860. /* do not accept anything before change speed execution */
  861. netif_stop_queue(dev);
  862. /* ready to process TxDone interrupt */
  863. spin_unlock_irqrestore(&self->spinlock, flags);
  864. }
  865. else
  866. {
  867. /* idle and no data, change speed now */
  868. self->speed = speed;
  869. toshoboe_setbaud (self);
  870. spin_unlock_irqrestore(&self->spinlock, flags);
  871. dev_kfree_skb (skb);
  872. return NETDEV_TX_OK;
  873. }
  874. }
  875. if ((mtt = irda_get_mtt(skb)))
  876. {
  877. /* This is fair since the queue should be empty anyway */
  878. spin_lock_irqsave(&self->spinlock, flags);
  879. if (self->txpending)
  880. {
  881. spin_unlock_irqrestore(&self->spinlock, flags);
  882. return NETDEV_TX_BUSY;
  883. }
  884. /* If in SIR mode we need to generate a string of XBOFs */
  885. /* In MIR and FIR we need to generate a string of data */
  886. /* which we will add a wrong checksum to */
  887. mtt = toshoboe_makemttpacket (self, self->tx_bufs[self->txs], mtt);
  888. IRDA_DEBUG (1, "%s.mtt:%x(%x)%d\n", __func__
  889. ,skb->len,mtt,self->txpending);
  890. if (mtt)
  891. {
  892. self->ring->tx[self->txs].len = mtt & 0xfff;
  893. ctl = OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX;
  894. if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_FIRON)
  895. {
  896. ctl |= OBOE_CTL_TX_BAD_CRC | OBOE_CTL_TX_SIP ;
  897. }
  898. #ifdef USE_MIR
  899. else if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_MIRON)
  900. {
  901. ctl |= OBOE_CTL_TX_BAD_CRC;
  902. }
  903. #endif
  904. self->ring->tx[self->txs].control = ctl;
  905. OUTB (0x0, OBOE_ENABLEH);
  906. /* It is only a timer. Do not send mtt packet outside! */
  907. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
  908. self->txpending++;
  909. self->txs++;
  910. self->txs %= TX_SLOTS;
  911. }
  912. else
  913. {
  914. printk(KERN_ERR DRIVER_NAME ": problem with mtt packet - ignored\n");
  915. }
  916. spin_unlock_irqrestore(&self->spinlock, flags);
  917. }
  918. #ifdef DUMP_PACKETS
  919. dumpbufs(skb->data,skb->len,'>');
  920. #endif
  921. spin_lock_irqsave(&self->spinlock, flags);
  922. if (self->ring->tx[self->txs].control & OBOE_CTL_TX_HW_OWNS)
  923. {
  924. IRDA_DEBUG (0, "%s.ful:%x(%x)%x\n", __func__
  925. ,skb->len, self->ring->tx[self->txs].control, self->txpending);
  926. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);
  927. spin_unlock_irqrestore(&self->spinlock, flags);
  928. return NETDEV_TX_BUSY;
  929. }
  930. if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_SIRON)
  931. {
  932. len = async_wrap_skb (skb, self->tx_bufs[self->txs], TX_BUF_SZ);
  933. }
  934. else
  935. {
  936. len = skb->len;
  937. skb_copy_from_linear_data(skb, self->tx_bufs[self->txs], len);
  938. }
  939. self->ring->tx[self->txs].len = len & 0x0fff;
  940. /*Sometimes the HW doesn't see us assert RTCENTX in the interrupt code */
  941. /*later this plays safe, we garuntee the last packet to be transmitted */
  942. /*has RTCENTX set */
  943. ctl = OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX;
  944. if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_FIRON)
  945. {
  946. ctl |= OBOE_CTL_TX_SIP ;
  947. }
  948. self->ring->tx[self->txs].control = ctl;
  949. /* If transmitter is idle start in one-shot mode */
  950. if (!self->txpending)
  951. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);
  952. self->txpending++;
  953. self->txs++;
  954. self->txs %= TX_SLOTS;
  955. spin_unlock_irqrestore(&self->spinlock, flags);
  956. dev_kfree_skb (skb);
  957. return NETDEV_TX_OK;
  958. }
  959. /*interrupt handler */
  960. static irqreturn_t
  961. toshoboe_interrupt (int irq, void *dev_id)
  962. {
  963. struct toshoboe_cb *self = dev_id;
  964. __u8 irqstat;
  965. struct sk_buff *skb = NULL;
  966. irqstat = INB (OBOE_ISR);
  967. /* was it us */
  968. if (!(irqstat & OBOE_INT_MASK))
  969. return IRQ_NONE;
  970. /* Ack all the interrupts */
  971. OUTB (irqstat, OBOE_ISR);
  972. toshoboe_isntstuck (self);
  973. /* Txdone */
  974. if (irqstat & OBOE_INT_TXDONE)
  975. {
  976. int txp, txpc;
  977. int i;
  978. txp = self->txpending;
  979. self->txpending = 0;
  980. for (i = 0; i < TX_SLOTS; ++i)
  981. {
  982. if (self->ring->tx[i].control & OBOE_CTL_TX_HW_OWNS)
  983. self->txpending++;
  984. }
  985. IRDA_DEBUG (1, "%s.txd(%x)%x/%x\n", __func__
  986. ,irqstat,txp,self->txpending);
  987. txp = INB (OBOE_TXSLOT) & OBOE_SLOT_MASK;
  988. /* Got anything queued ? start it together */
  989. if (self->ring->tx[txp].control & OBOE_CTL_TX_HW_OWNS)
  990. {
  991. txpc = txp;
  992. #ifdef OPTIMIZE_TX
  993. while (self->ring->tx[txpc].control & OBOE_CTL_TX_HW_OWNS)
  994. {
  995. txp = txpc;
  996. txpc++;
  997. txpc %= TX_SLOTS;
  998. self->netdev->stats.tx_packets++;
  999. if (self->ring->tx[txpc].control & OBOE_CTL_TX_HW_OWNS)
  1000. self->ring->tx[txp].control &= ~OBOE_CTL_TX_RTCENTX;
  1001. }
  1002. self->netdev->stats.tx_packets--;
  1003. #else
  1004. self->netdev->stats.tx_packets++;
  1005. #endif
  1006. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);
  1007. }
  1008. if ((!self->txpending) && (self->new_speed))
  1009. {
  1010. self->speed = self->new_speed;
  1011. IRDA_DEBUG (1, "%s: Executed TxDone scheduled speed change %d\n",
  1012. __func__, self->speed);
  1013. toshoboe_setbaud (self);
  1014. }
  1015. /* Tell network layer that we want more frames */
  1016. if (!self->new_speed)
  1017. netif_wake_queue(self->netdev);
  1018. }
  1019. if (irqstat & OBOE_INT_RXDONE)
  1020. {
  1021. while (!(self->ring->rx[self->rxs].control & OBOE_CTL_RX_HW_OWNS))
  1022. {
  1023. int len = self->ring->rx[self->rxs].len;
  1024. skb = NULL;
  1025. IRDA_DEBUG (3, "%s.rcv:%x(%x)\n", __func__
  1026. ,len,self->ring->rx[self->rxs].control);
  1027. #ifdef DUMP_PACKETS
  1028. dumpbufs(self->rx_bufs[self->rxs],len,'<');
  1029. #endif
  1030. if (self->ring->rx[self->rxs].control == 0)
  1031. {
  1032. __u8 enable = INB (OBOE_ENABLEH);
  1033. /* In SIR mode we need to check the CRC as this */
  1034. /* hasn't been done by the hardware */
  1035. if (enable & OBOE_ENABLEH_SIRON)
  1036. {
  1037. if (!toshoboe_checkfcs (self->rx_bufs[self->rxs], len))
  1038. len = 0;
  1039. /*Trim off the CRC */
  1040. if (len > 1)
  1041. len -= 2;
  1042. else
  1043. len = 0;
  1044. IRDA_DEBUG (1, "%s.SIR:%x(%x)\n", __func__, len,enable);
  1045. }
  1046. #ifdef USE_MIR
  1047. else if (enable & OBOE_ENABLEH_MIRON)
  1048. {
  1049. if (len > 1)
  1050. len -= 2;
  1051. else
  1052. len = 0;
  1053. IRDA_DEBUG (2, "%s.MIR:%x(%x)\n", __func__, len,enable);
  1054. }
  1055. #endif
  1056. else if (enable & OBOE_ENABLEH_FIRON)
  1057. {
  1058. if (len > 3)
  1059. len -= 4; /*FIXME: check this */
  1060. else
  1061. len = 0;
  1062. IRDA_DEBUG (1, "%s.FIR:%x(%x)\n", __func__, len,enable);
  1063. }
  1064. else
  1065. IRDA_DEBUG (0, "%s.?IR:%x(%x)\n", __func__, len,enable);
  1066. if (len)
  1067. {
  1068. skb = dev_alloc_skb (len + 1);
  1069. if (skb)
  1070. {
  1071. skb_reserve (skb, 1);
  1072. skb_put (skb, len);
  1073. skb_copy_to_linear_data(skb, self->rx_bufs[self->rxs],
  1074. len);
  1075. self->netdev->stats.rx_packets++;
  1076. skb->dev = self->netdev;
  1077. skb_reset_mac_header(skb);
  1078. skb->protocol = htons (ETH_P_IRDA);
  1079. }
  1080. else
  1081. {
  1082. printk (KERN_INFO
  1083. "%s(), memory squeeze, dropping frame.\n",
  1084. __func__);
  1085. }
  1086. }
  1087. }
  1088. else
  1089. {
  1090. /* TODO: =========================================== */
  1091. /* if OBOE_CTL_RX_LENGTH, our buffers are too small */
  1092. /* (MIR or FIR) data is lost. */
  1093. /* (SIR) data is splitted in several slots. */
  1094. /* we have to join all the received buffers received */
  1095. /*in a large buffer before checking CRC. */
  1096. IRDA_DEBUG (0, "%s.err:%x(%x)\n", __func__
  1097. ,len,self->ring->rx[self->rxs].control);
  1098. }
  1099. self->ring->rx[self->rxs].len = 0x0;
  1100. self->ring->rx[self->rxs].control = OBOE_CTL_RX_HW_OWNS;
  1101. self->rxs++;
  1102. self->rxs %= RX_SLOTS;
  1103. if (skb)
  1104. netif_rx (skb);
  1105. }
  1106. }
  1107. if (irqstat & OBOE_INT_TXUNDER)
  1108. {
  1109. printk (KERN_WARNING DRIVER_NAME ": tx fifo underflow\n");
  1110. }
  1111. if (irqstat & OBOE_INT_RXOVER)
  1112. {
  1113. printk (KERN_WARNING DRIVER_NAME ": rx fifo overflow\n");
  1114. }
  1115. /* This must be useful for something... */
  1116. if (irqstat & OBOE_INT_SIP)
  1117. {
  1118. self->int_sip++;
  1119. IRDA_DEBUG (1, "%s.sip:%x(%x)%x\n", __func__
  1120. ,self->int_sip,irqstat,self->txpending);
  1121. }
  1122. return IRQ_HANDLED;
  1123. }
  1124. static int
  1125. toshoboe_net_open (struct net_device *dev)
  1126. {
  1127. struct toshoboe_cb *self;
  1128. unsigned long flags;
  1129. int rc;
  1130. IRDA_DEBUG (4, "%s()\n", __func__);
  1131. self = netdev_priv(dev);
  1132. if (self->async)
  1133. return -EBUSY;
  1134. if (self->stopped)
  1135. return 0;
  1136. rc = request_irq (self->io.irq, toshoboe_interrupt,
  1137. IRQF_SHARED | IRQF_DISABLED, dev->name, self);
  1138. if (rc)
  1139. return rc;
  1140. spin_lock_irqsave(&self->spinlock, flags);
  1141. toshoboe_startchip (self);
  1142. spin_unlock_irqrestore(&self->spinlock, flags);
  1143. /* Ready to play! */
  1144. netif_start_queue(dev);
  1145. /*
  1146. * Open new IrLAP layer instance, now that everything should be
  1147. * initialized properly
  1148. */
  1149. self->irlap = irlap_open (dev, &self->qos, driver_name);
  1150. self->irdad = 1;
  1151. return 0;
  1152. }
  1153. static int
  1154. toshoboe_net_close (struct net_device *dev)
  1155. {
  1156. struct toshoboe_cb *self;
  1157. IRDA_DEBUG (4, "%s()\n", __func__);
  1158. IRDA_ASSERT (dev != NULL, return -1; );
  1159. self = netdev_priv(dev);
  1160. /* Stop device */
  1161. netif_stop_queue(dev);
  1162. /* Stop and remove instance of IrLAP */
  1163. if (self->irlap)
  1164. irlap_close (self->irlap);
  1165. self->irlap = NULL;
  1166. self->irdad = 0;
  1167. free_irq (self->io.irq, (void *) self);
  1168. if (!self->stopped)
  1169. {
  1170. toshoboe_stopchip (self);
  1171. }
  1172. return 0;
  1173. }
  1174. /*
  1175. * Function toshoboe_net_ioctl (dev, rq, cmd)
  1176. *
  1177. * Process IOCTL commands for this device
  1178. *
  1179. */
  1180. static int
  1181. toshoboe_net_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
  1182. {
  1183. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1184. struct toshoboe_cb *self;
  1185. unsigned long flags;
  1186. int ret = 0;
  1187. IRDA_ASSERT (dev != NULL, return -1; );
  1188. self = netdev_priv(dev);
  1189. IRDA_ASSERT (self != NULL, return -1; );
  1190. IRDA_DEBUG (5, "%s(), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
  1191. /* Disable interrupts & save flags */
  1192. spin_lock_irqsave(&self->spinlock, flags);
  1193. switch (cmd)
  1194. {
  1195. case SIOCSBANDWIDTH: /* Set bandwidth */
  1196. /* This function will also be used by IrLAP to change the
  1197. * speed, so we still must allow for speed change within
  1198. * interrupt context.
  1199. */
  1200. IRDA_DEBUG (1, "%s(BANDWIDTH), %s, (%X/%ld\n", __func__
  1201. ,dev->name, INB (OBOE_STATUS), irq->ifr_baudrate );
  1202. if (!in_interrupt () && !capable (CAP_NET_ADMIN)) {
  1203. ret = -EPERM;
  1204. goto out;
  1205. }
  1206. /* self->speed=irq->ifr_baudrate; */
  1207. /* toshoboe_setbaud(self); */
  1208. /* Just change speed once - inserted by Paul Bristow */
  1209. self->new_speed = irq->ifr_baudrate;
  1210. break;
  1211. case SIOCSMEDIABUSY: /* Set media busy */
  1212. IRDA_DEBUG (1, "%s(MEDIABUSY), %s, (%X/%x)\n", __func__
  1213. ,dev->name, INB (OBOE_STATUS), capable (CAP_NET_ADMIN) );
  1214. if (!capable (CAP_NET_ADMIN)) {
  1215. ret = -EPERM;
  1216. goto out;
  1217. }
  1218. irda_device_set_media_busy (self->netdev, TRUE);
  1219. break;
  1220. case SIOCGRECEIVING: /* Check if we are receiving right now */
  1221. irq->ifr_receiving = (INB (OBOE_STATUS) & OBOE_STATUS_RXBUSY) ? 1 : 0;
  1222. IRDA_DEBUG (3, "%s(RECEIVING), %s, (%X/%x)\n", __func__
  1223. ,dev->name, INB (OBOE_STATUS), irq->ifr_receiving );
  1224. break;
  1225. default:
  1226. IRDA_DEBUG (1, "%s(?), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
  1227. ret = -EOPNOTSUPP;
  1228. }
  1229. out:
  1230. spin_unlock_irqrestore(&self->spinlock, flags);
  1231. return ret;
  1232. }
  1233. MODULE_DESCRIPTION("Toshiba OBOE IrDA Device Driver");
  1234. MODULE_AUTHOR("James McKenzie <james@fishsoup.dhs.org>");
  1235. MODULE_LICENSE("GPL");
  1236. module_param (max_baud, int, 0);
  1237. MODULE_PARM_DESC(max_baud, "Maximum baud rate");
  1238. #ifdef USE_PROBE
  1239. module_param (do_probe, bool, 0);
  1240. MODULE_PARM_DESC(do_probe, "Enable/disable chip probing and self-test");
  1241. #endif
  1242. static void
  1243. toshoboe_close (struct pci_dev *pci_dev)
  1244. {
  1245. int i;
  1246. struct toshoboe_cb *self = (struct toshoboe_cb*)pci_get_drvdata(pci_dev);
  1247. IRDA_DEBUG (4, "%s()\n", __func__);
  1248. IRDA_ASSERT (self != NULL, return; );
  1249. if (!self->stopped)
  1250. {
  1251. toshoboe_stopchip (self);
  1252. }
  1253. release_region (self->io.fir_base, self->io.fir_ext);
  1254. for (i = 0; i < TX_SLOTS; ++i)
  1255. {
  1256. kfree (self->tx_bufs[i]);
  1257. self->tx_bufs[i] = NULL;
  1258. }
  1259. for (i = 0; i < RX_SLOTS; ++i)
  1260. {
  1261. kfree (self->rx_bufs[i]);
  1262. self->rx_bufs[i] = NULL;
  1263. }
  1264. unregister_netdev(self->netdev);
  1265. kfree (self->ringbuf);
  1266. self->ringbuf = NULL;
  1267. self->ring = NULL;
  1268. free_netdev(self->netdev);
  1269. }
  1270. static const struct net_device_ops toshoboe_netdev_ops = {
  1271. .ndo_open = toshoboe_net_open,
  1272. .ndo_stop = toshoboe_net_close,
  1273. .ndo_start_xmit = toshoboe_hard_xmit,
  1274. .ndo_do_ioctl = toshoboe_net_ioctl,
  1275. };
  1276. static int
  1277. toshoboe_open (struct pci_dev *pci_dev, const struct pci_device_id *pdid)
  1278. {
  1279. struct toshoboe_cb *self;
  1280. struct net_device *dev;
  1281. int i = 0;
  1282. int ok = 0;
  1283. int err;
  1284. IRDA_DEBUG (4, "%s()\n", __func__);
  1285. if ((err=pci_enable_device(pci_dev)))
  1286. return err;
  1287. dev = alloc_irdadev(sizeof (struct toshoboe_cb));
  1288. if (dev == NULL)
  1289. {
  1290. printk (KERN_ERR DRIVER_NAME ": can't allocate memory for "
  1291. "IrDA control block\n");
  1292. return -ENOMEM;
  1293. }
  1294. self = netdev_priv(dev);
  1295. self->netdev = dev;
  1296. self->pdev = pci_dev;
  1297. self->base = pci_resource_start(pci_dev,0);
  1298. self->io.fir_base = self->base;
  1299. self->io.fir_ext = OBOE_IO_EXTENT;
  1300. self->io.irq = pci_dev->irq;
  1301. self->io.irqflags = IRQF_SHARED | IRQF_DISABLED;
  1302. self->speed = self->io.speed = 9600;
  1303. self->async = 0;
  1304. /* Lock the port that we need */
  1305. if (NULL==request_region (self->io.fir_base, self->io.fir_ext, driver_name))
  1306. {
  1307. printk (KERN_ERR DRIVER_NAME ": can't get iobase of 0x%03x\n"
  1308. ,self->io.fir_base);
  1309. err = -EBUSY;
  1310. goto freeself;
  1311. }
  1312. spin_lock_init(&self->spinlock);
  1313. irda_init_max_qos_capabilies (&self->qos);
  1314. self->qos.baud_rate.bits = 0;
  1315. if (max_baud >= 2400)
  1316. self->qos.baud_rate.bits |= IR_2400;
  1317. /*if (max_baud>=4800) idev->qos.baud_rate.bits|=IR_4800; */
  1318. if (max_baud >= 9600)
  1319. self->qos.baud_rate.bits |= IR_9600;
  1320. if (max_baud >= 19200)
  1321. self->qos.baud_rate.bits |= IR_19200;
  1322. if (max_baud >= 115200)
  1323. self->qos.baud_rate.bits |= IR_115200;
  1324. #ifdef USE_MIR
  1325. if (max_baud >= 1152000)
  1326. {
  1327. self->qos.baud_rate.bits |= IR_1152000;
  1328. }
  1329. #endif
  1330. if (max_baud >= 4000000)
  1331. {
  1332. self->qos.baud_rate.bits |= (IR_4000000 << 8);
  1333. }
  1334. /*FIXME: work this out... */
  1335. self->qos.min_turn_time.bits = 0xff;
  1336. irda_qos_bits_to_value (&self->qos);
  1337. /* Allocate twice the size to guarantee alignment */
  1338. self->ringbuf = kmalloc(OBOE_RING_LEN << 1, GFP_KERNEL);
  1339. if (!self->ringbuf)
  1340. {
  1341. err = -ENOMEM;
  1342. goto freeregion;
  1343. }
  1344. #if (BITS_PER_LONG == 64)
  1345. #error broken on 64-bit: casts pointer to 32-bit, and then back to pointer.
  1346. #endif
  1347. /*We need to align the taskfile on a taskfile size boundary */
  1348. {
  1349. unsigned long addr;
  1350. addr = (__u32) self->ringbuf;
  1351. addr &= ~(OBOE_RING_LEN - 1);
  1352. addr += OBOE_RING_LEN;
  1353. self->ring = (struct OboeRing *) addr;
  1354. }
  1355. memset (self->ring, 0, OBOE_RING_LEN);
  1356. self->io.mem_base = (__u32) self->ring;
  1357. ok = 1;
  1358. for (i = 0; i < TX_SLOTS; ++i)
  1359. {
  1360. self->tx_bufs[i] = kmalloc (TX_BUF_SZ, GFP_KERNEL);
  1361. if (!self->tx_bufs[i])
  1362. ok = 0;
  1363. }
  1364. for (i = 0; i < RX_SLOTS; ++i)
  1365. {
  1366. self->rx_bufs[i] = kmalloc (RX_BUF_SZ, GFP_KERNEL);
  1367. if (!self->rx_bufs[i])
  1368. ok = 0;
  1369. }
  1370. if (!ok)
  1371. {
  1372. err = -ENOMEM;
  1373. goto freebufs;
  1374. }
  1375. #ifdef USE_PROBE
  1376. if (do_probe)
  1377. if (!toshoboe_probe (self))
  1378. {
  1379. err = -ENODEV;
  1380. goto freebufs;
  1381. }
  1382. #endif
  1383. SET_NETDEV_DEV(dev, &pci_dev->dev);
  1384. dev->netdev_ops = &toshoboe_netdev_ops;
  1385. err = register_netdev(dev);
  1386. if (err)
  1387. {
  1388. printk (KERN_ERR DRIVER_NAME ": register_netdev() failed\n");
  1389. err = -ENOMEM;
  1390. goto freebufs;
  1391. }
  1392. printk (KERN_INFO "IrDA: Registered device %s\n", dev->name);
  1393. pci_set_drvdata(pci_dev,self);
  1394. printk (KERN_INFO DRIVER_NAME ": Using multiple tasks\n");
  1395. return 0;
  1396. freebufs:
  1397. for (i = 0; i < TX_SLOTS; ++i)
  1398. kfree (self->tx_bufs[i]);
  1399. for (i = 0; i < RX_SLOTS; ++i)
  1400. kfree (self->rx_bufs[i]);
  1401. kfree(self->ringbuf);
  1402. freeregion:
  1403. release_region (self->io.fir_base, self->io.fir_ext);
  1404. freeself:
  1405. free_netdev(dev);
  1406. return err;
  1407. }
  1408. static int
  1409. toshoboe_gotosleep (struct pci_dev *pci_dev, pm_message_t crap)
  1410. {
  1411. struct toshoboe_cb *self = (struct toshoboe_cb*)pci_get_drvdata(pci_dev);
  1412. unsigned long flags;
  1413. int i = 10;
  1414. IRDA_DEBUG (4, "%s()\n", __func__);
  1415. if (!self || self->stopped)
  1416. return 0;
  1417. if ((!self->irdad) && (!self->async))
  1418. return 0;
  1419. /* Flush all packets */
  1420. while ((i--) && (self->txpending))
  1421. udelay (10000);
  1422. spin_lock_irqsave(&self->spinlock, flags);
  1423. toshoboe_stopchip (self);
  1424. self->stopped = 1;
  1425. self->txpending = 0;
  1426. spin_unlock_irqrestore(&self->spinlock, flags);
  1427. return 0;
  1428. }
  1429. static int
  1430. toshoboe_wakeup (struct pci_dev *pci_dev)
  1431. {
  1432. struct toshoboe_cb *self = (struct toshoboe_cb*)pci_get_drvdata(pci_dev);
  1433. unsigned long flags;
  1434. IRDA_DEBUG (4, "%s()\n", __func__);
  1435. if (!self || !self->stopped)
  1436. return 0;
  1437. if ((!self->irdad) && (!self->async))
  1438. return 0;
  1439. spin_lock_irqsave(&self->spinlock, flags);
  1440. toshoboe_startchip (self);
  1441. self->stopped = 0;
  1442. netif_wake_queue(self->netdev);
  1443. spin_unlock_irqrestore(&self->spinlock, flags);
  1444. return 0;
  1445. }
  1446. static struct pci_driver donauboe_pci_driver = {
  1447. .name = "donauboe",
  1448. .id_table = toshoboe_pci_tbl,
  1449. .probe = toshoboe_open,
  1450. .remove = toshoboe_close,
  1451. .suspend = toshoboe_gotosleep,
  1452. .resume = toshoboe_wakeup
  1453. };
  1454. static int __init
  1455. donauboe_init (void)
  1456. {
  1457. return pci_register_driver(&donauboe_pci_driver);
  1458. }
  1459. static void __exit
  1460. donauboe_cleanup (void)
  1461. {
  1462. pci_unregister_driver(&donauboe_pci_driver);
  1463. }
  1464. module_init(donauboe_init);
  1465. module_exit(donauboe_cleanup);