donauboe.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  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 const struct pci_device_id 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. pr_debug("%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. ringbase = INB (OBOE_RING_BASE0) << 10;
  207. ringbase |= INB (OBOE_RING_BASE1) << 18;
  208. ringbase |= INB (OBOE_RING_BASE2) << 26;
  209. printk (KERN_ERR DRIVER_NAME ": Register dump:\n");
  210. printk (KERN_ERR "Interrupts: Tx:%d Rx:%d TxUnder:%d RxOver:%d Sip:%d\n",
  211. self->int_tx, self->int_rx, self->int_txunder, self->int_rxover,
  212. self->int_sip);
  213. printk (KERN_ERR "RX %02x TX %02x RingBase %08x\n",
  214. INB (OBOE_RXSLOT), INB (OBOE_TXSLOT), ringbase);
  215. printk (KERN_ERR "RING_SIZE %02x IER %02x ISR %02x\n",
  216. INB (OBOE_RING_SIZE), INB (OBOE_IER), INB (OBOE_ISR));
  217. printk (KERN_ERR "CONFIG1 %02x STATUS %02x\n",
  218. INB (OBOE_CONFIG1), INB (OBOE_STATUS));
  219. printk (KERN_ERR "CONFIG0 %02x%02x ENABLE %02x%02x\n",
  220. INB (OBOE_CONFIG0H), INB (OBOE_CONFIG0L),
  221. INB (OBOE_ENABLEH), INB (OBOE_ENABLEL));
  222. printk (KERN_ERR "NEW_PCONFIG %02x%02x CURR_PCONFIG %02x%02x\n",
  223. INB (OBOE_NEW_PCONFIGH), INB (OBOE_NEW_PCONFIGL),
  224. INB (OBOE_CURR_PCONFIGH), INB (OBOE_CURR_PCONFIGL));
  225. printk (KERN_ERR "MAXLEN %02x%02x RXCOUNT %02x%02x\n",
  226. INB (OBOE_MAXLENH), INB (OBOE_MAXLENL),
  227. INB (OBOE_RXCOUNTL), INB (OBOE_RXCOUNTH));
  228. if (self->ring)
  229. {
  230. int i;
  231. ringbase = virt_to_bus (self->ring);
  232. printk (KERN_ERR "Ring at %08x:\n", ringbase);
  233. printk (KERN_ERR "RX:");
  234. for (i = 0; i < RX_SLOTS; ++i)
  235. printk (" (%d,%02x)",self->ring->rx[i].len,self->ring->rx[i].control);
  236. printk ("\n");
  237. printk (KERN_ERR "TX:");
  238. for (i = 0; i < RX_SLOTS; ++i)
  239. printk (" (%d,%02x)",self->ring->tx[i].len,self->ring->tx[i].control);
  240. printk ("\n");
  241. }
  242. }
  243. #endif
  244. /*Don't let the chip look at memory */
  245. static void
  246. toshoboe_disablebm (struct toshoboe_cb *self)
  247. {
  248. __u8 command;
  249. pci_read_config_byte (self->pdev, PCI_COMMAND, &command);
  250. command &= ~PCI_COMMAND_MASTER;
  251. pci_write_config_byte (self->pdev, PCI_COMMAND, command);
  252. }
  253. /* Shutdown the chip and point the taskfile reg somewhere else */
  254. static void
  255. toshoboe_stopchip (struct toshoboe_cb *self)
  256. {
  257. /*Disable interrupts */
  258. OUTB (0x0, OBOE_IER);
  259. /*Disable DMA, Disable Rx, Disable Tx */
  260. OUTB (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);
  261. /*Disable SIR MIR FIR, Tx and Rx */
  262. OUTB (0x00, OBOE_ENABLEH);
  263. /*Point the ring somewhere safe */
  264. OUTB (0x3f, OBOE_RING_BASE2);
  265. OUTB (0xff, OBOE_RING_BASE1);
  266. OUTB (0xff, OBOE_RING_BASE0);
  267. OUTB (RX_LEN >> 8, OBOE_MAXLENH);
  268. OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
  269. /*Acknoledge any pending interrupts */
  270. OUTB (0xff, OBOE_ISR);
  271. /*Why */
  272. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  273. /*switch it off */
  274. OUTB (OBOE_CONFIG1_OFF, OBOE_CONFIG1);
  275. toshoboe_disablebm (self);
  276. }
  277. /* Transmitter initialization */
  278. static void
  279. toshoboe_start_DMA (struct toshoboe_cb *self, int opts)
  280. {
  281. OUTB (0x0, OBOE_ENABLEH);
  282. OUTB (CONFIG0H_DMA_ON | opts, OBOE_CONFIG0H);
  283. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  284. PROMPT;
  285. }
  286. /*Set the baud rate */
  287. static void
  288. toshoboe_setbaud (struct toshoboe_cb *self)
  289. {
  290. __u16 pconfig = 0;
  291. __u8 config0l = 0;
  292. pr_debug("%s(%d/%d)\n", __func__, self->speed, self->io.speed);
  293. switch (self->speed)
  294. {
  295. case 2400:
  296. case 4800:
  297. case 9600:
  298. case 19200:
  299. case 38400:
  300. case 57600:
  301. case 115200:
  302. #ifdef USE_MIR
  303. case 1152000:
  304. #endif
  305. case 4000000:
  306. break;
  307. default:
  308. printk (KERN_ERR DRIVER_NAME ": switch to unsupported baudrate %d\n",
  309. self->speed);
  310. return;
  311. }
  312. switch (self->speed)
  313. {
  314. /* For SIR the preamble is done by adding XBOFs */
  315. /* to the packet */
  316. /* set to filtered SIR mode, filter looks for BOF and EOF */
  317. case 2400:
  318. pconfig |= 47 << OBOE_PCONFIG_BAUDSHIFT;
  319. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  320. break;
  321. case 4800:
  322. pconfig |= 23 << OBOE_PCONFIG_BAUDSHIFT;
  323. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  324. break;
  325. case 9600:
  326. pconfig |= 11 << OBOE_PCONFIG_BAUDSHIFT;
  327. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  328. break;
  329. case 19200:
  330. pconfig |= 5 << OBOE_PCONFIG_BAUDSHIFT;
  331. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  332. break;
  333. case 38400:
  334. pconfig |= 2 << OBOE_PCONFIG_BAUDSHIFT;
  335. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  336. break;
  337. case 57600:
  338. pconfig |= 1 << OBOE_PCONFIG_BAUDSHIFT;
  339. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  340. break;
  341. case 115200:
  342. pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
  343. pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
  344. break;
  345. default:
  346. /*Set to packet based reception */
  347. OUTB (RX_LEN >> 8, OBOE_MAXLENH);
  348. OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
  349. break;
  350. }
  351. switch (self->speed)
  352. {
  353. case 2400:
  354. case 4800:
  355. case 9600:
  356. case 19200:
  357. case 38400:
  358. case 57600:
  359. case 115200:
  360. config0l = OBOE_CONFIG0L_ENSIR;
  361. if (self->async)
  362. {
  363. /*Set to character based reception */
  364. /*System will lock if MAXLEN=0 */
  365. /*so have to be careful */
  366. OUTB (0x01, OBOE_MAXLENH);
  367. OUTB (0x01, OBOE_MAXLENL);
  368. OUTB (0x00, OBOE_MAXLENH);
  369. }
  370. else
  371. {
  372. /*Set to packet based reception */
  373. config0l |= OBOE_CONFIG0L_ENSIRF;
  374. OUTB (RX_LEN >> 8, OBOE_MAXLENH);
  375. OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
  376. }
  377. break;
  378. #ifdef USE_MIR
  379. /* MIR mode */
  380. /* Set for 16 bit CRC and enable MIR */
  381. /* Preamble now handled by the chip */
  382. case 1152000:
  383. pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
  384. pconfig |= 8 << OBOE_PCONFIG_WIDTHSHIFT;
  385. pconfig |= 1 << OBOE_PCONFIG_PREAMBLESHIFT;
  386. config0l = OBOE_CONFIG0L_CRC16 | OBOE_CONFIG0L_ENMIR;
  387. break;
  388. #endif
  389. /* FIR mode */
  390. /* Set for 32 bit CRC and enable FIR */
  391. /* Preamble handled by the chip */
  392. case 4000000:
  393. pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
  394. /* Documentation says 14, but toshiba use 15 in their drivers */
  395. pconfig |= 15 << OBOE_PCONFIG_PREAMBLESHIFT;
  396. config0l = OBOE_CONFIG0L_ENFIR;
  397. break;
  398. }
  399. /* Copy into new PHY config buffer */
  400. OUTBP (pconfig >> 8, OBOE_NEW_PCONFIGH);
  401. OUTB (pconfig & 0xff, OBOE_NEW_PCONFIGL);
  402. OUTB (config0l, OBOE_CONFIG0L);
  403. /* Now make OBOE copy from new PHY to current PHY */
  404. OUTB (0x0, OBOE_ENABLEH);
  405. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  406. PROMPT;
  407. /* speed change executed */
  408. self->new_speed = 0;
  409. self->io.speed = self->speed;
  410. }
  411. /*Let the chip look at memory */
  412. static void
  413. toshoboe_enablebm (struct toshoboe_cb *self)
  414. {
  415. pci_set_master (self->pdev);
  416. }
  417. /*setup the ring */
  418. static void
  419. toshoboe_initring (struct toshoboe_cb *self)
  420. {
  421. int i;
  422. for (i = 0; i < TX_SLOTS; ++i)
  423. {
  424. self->ring->tx[i].len = 0;
  425. self->ring->tx[i].control = 0x00;
  426. self->ring->tx[i].address = virt_to_bus (self->tx_bufs[i]);
  427. }
  428. for (i = 0; i < RX_SLOTS; ++i)
  429. {
  430. self->ring->rx[i].len = RX_LEN;
  431. self->ring->rx[i].len = 0;
  432. self->ring->rx[i].address = virt_to_bus (self->rx_bufs[i]);
  433. self->ring->rx[i].control = OBOE_CTL_RX_HW_OWNS;
  434. }
  435. }
  436. static void
  437. toshoboe_resetptrs (struct toshoboe_cb *self)
  438. {
  439. /* Can reset pointers by twidling DMA */
  440. OUTB (0x0, OBOE_ENABLEH);
  441. OUTBP (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);
  442. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  443. self->rxs = inb_p (OBOE_RXSLOT) & OBOE_SLOT_MASK;
  444. self->txs = inb_p (OBOE_TXSLOT) & OBOE_SLOT_MASK;
  445. }
  446. /* Called in locked state */
  447. static void
  448. toshoboe_initptrs (struct toshoboe_cb *self)
  449. {
  450. /* spin_lock_irqsave(self->spinlock, flags); */
  451. /* save_flags (flags); */
  452. /* Can reset pointers by twidling DMA */
  453. toshoboe_resetptrs (self);
  454. OUTB (0x0, OBOE_ENABLEH);
  455. OUTB (CONFIG0H_DMA_ON, OBOE_CONFIG0H);
  456. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  457. self->txpending = 0;
  458. /* spin_unlock_irqrestore(self->spinlock, flags); */
  459. /* restore_flags (flags); */
  460. }
  461. /* Wake the chip up and get it looking at the rings */
  462. /* Called in locked state */
  463. static void
  464. toshoboe_startchip (struct toshoboe_cb *self)
  465. {
  466. __u32 physaddr;
  467. toshoboe_initring (self);
  468. toshoboe_enablebm (self);
  469. OUTBP (OBOE_CONFIG1_RESET, OBOE_CONFIG1);
  470. OUTBP (OBOE_CONFIG1_ON, OBOE_CONFIG1);
  471. /* Stop the clocks */
  472. OUTB (0, OBOE_ENABLEH);
  473. /*Set size of rings */
  474. OUTB (RING_SIZE, OBOE_RING_SIZE);
  475. /*Acknoledge any pending interrupts */
  476. OUTB (0xff, OBOE_ISR);
  477. /*Enable ints */
  478. OUTB (OBOE_INT_TXDONE | OBOE_INT_RXDONE |
  479. OBOE_INT_TXUNDER | OBOE_INT_RXOVER | OBOE_INT_SIP , OBOE_IER);
  480. /*Acknoledge any pending interrupts */
  481. OUTB (0xff, OBOE_ISR);
  482. /*Set the maximum packet length to 0xfff (4095) */
  483. OUTB (RX_LEN >> 8, OBOE_MAXLENH);
  484. OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
  485. /*Shutdown DMA */
  486. OUTB (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);
  487. /*Find out where the rings live */
  488. physaddr = virt_to_bus (self->ring);
  489. IRDA_ASSERT ((physaddr & 0x3ff) == 0,
  490. printk (KERN_ERR DRIVER_NAME "ring not correctly aligned\n");
  491. return;);
  492. OUTB ((physaddr >> 10) & 0xff, OBOE_RING_BASE0);
  493. OUTB ((physaddr >> 18) & 0xff, OBOE_RING_BASE1);
  494. OUTB ((physaddr >> 26) & 0x3f, OBOE_RING_BASE2);
  495. /*Enable DMA controller in byte mode and RX */
  496. OUTB (CONFIG0H_DMA_ON, OBOE_CONFIG0H);
  497. /* Start up the clocks */
  498. OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  499. /*set to sensible speed */
  500. self->speed = 9600;
  501. toshoboe_setbaud (self);
  502. toshoboe_initptrs (self);
  503. }
  504. static void
  505. toshoboe_isntstuck (struct toshoboe_cb *self)
  506. {
  507. }
  508. static void
  509. toshoboe_checkstuck (struct toshoboe_cb *self)
  510. {
  511. unsigned long flags;
  512. if (0)
  513. {
  514. spin_lock_irqsave(&self->spinlock, flags);
  515. /* This will reset the chip completely */
  516. printk (KERN_ERR DRIVER_NAME ": Resetting chip\n");
  517. toshoboe_stopchip (self);
  518. toshoboe_startchip (self);
  519. spin_unlock_irqrestore(&self->spinlock, flags);
  520. }
  521. }
  522. /*Generate packet of about mtt us long */
  523. static int
  524. toshoboe_makemttpacket (struct toshoboe_cb *self, void *buf, int mtt)
  525. {
  526. int xbofs;
  527. xbofs = ((int) (mtt/100)) * (int) (self->speed);
  528. xbofs=xbofs/80000; /*Eight bits per byte, and mtt is in us*/
  529. xbofs++;
  530. pr_debug(DRIVER_NAME ": generated mtt of %d bytes for %d us at %d baud\n",
  531. xbofs, mtt, self->speed);
  532. if (xbofs > TX_LEN)
  533. {
  534. printk (KERN_ERR DRIVER_NAME ": wanted %d bytes MTT but TX_LEN is %d\n",
  535. xbofs, TX_LEN);
  536. xbofs = TX_LEN;
  537. }
  538. /*xbofs will do for SIR, MIR and FIR,SIR mode doesn't generate a checksum anyway */
  539. memset (buf, XBOF, xbofs);
  540. return xbofs;
  541. }
  542. #ifdef USE_PROBE
  543. /***********************************************************************/
  544. /* Probe code */
  545. static void
  546. toshoboe_dumptx (struct toshoboe_cb *self)
  547. {
  548. int i;
  549. PROBE_DEBUG(KERN_WARNING "TX:");
  550. for (i = 0; i < RX_SLOTS; ++i)
  551. PROBE_DEBUG(" (%d,%02x)",self->ring->tx[i].len,self->ring->tx[i].control);
  552. PROBE_DEBUG(" [%d]\n",self->speed);
  553. }
  554. static void
  555. toshoboe_dumprx (struct toshoboe_cb *self, int score)
  556. {
  557. int i;
  558. PROBE_DEBUG(" %d\nRX:",score);
  559. for (i = 0; i < RX_SLOTS; ++i)
  560. PROBE_DEBUG(" (%d,%02x)",self->ring->rx[i].len,self->ring->rx[i].control);
  561. PROBE_DEBUG("\n");
  562. }
  563. static inline int
  564. stuff_byte (__u8 byte, __u8 * buf)
  565. {
  566. switch (byte)
  567. {
  568. case BOF: /* FALLTHROUGH */
  569. case EOF: /* FALLTHROUGH */
  570. case CE:
  571. /* Insert transparently coded */
  572. buf[0] = CE; /* Send link escape */
  573. buf[1] = byte ^ IRDA_TRANS; /* Complement bit 5 */
  574. return 2;
  575. /* break; */
  576. default:
  577. /* Non-special value, no transparency required */
  578. buf[0] = byte;
  579. return 1;
  580. /* break; */
  581. }
  582. }
  583. static irqreturn_t
  584. toshoboe_probeinterrupt (int irq, void *dev_id)
  585. {
  586. struct toshoboe_cb *self = dev_id;
  587. __u8 irqstat;
  588. irqstat = INB (OBOE_ISR);
  589. /* was it us */
  590. if (!(irqstat & OBOE_INT_MASK))
  591. return IRQ_NONE;
  592. /* Ack all the interrupts */
  593. OUTB (irqstat, OBOE_ISR);
  594. if (irqstat & OBOE_INT_TXDONE)
  595. {
  596. int txp;
  597. self->int_tx++;
  598. PROBE_DEBUG("T");
  599. txp = INB (OBOE_TXSLOT) & OBOE_SLOT_MASK;
  600. if (self->ring->tx[txp].control & OBOE_CTL_TX_HW_OWNS)
  601. {
  602. self->int_tx+=100;
  603. PROBE_DEBUG("S");
  604. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
  605. }
  606. }
  607. if (irqstat & OBOE_INT_RXDONE) {
  608. self->int_rx++;
  609. PROBE_DEBUG("R"); }
  610. if (irqstat & OBOE_INT_TXUNDER) {
  611. self->int_txunder++;
  612. PROBE_DEBUG("U"); }
  613. if (irqstat & OBOE_INT_RXOVER) {
  614. self->int_rxover++;
  615. PROBE_DEBUG("O"); }
  616. if (irqstat & OBOE_INT_SIP) {
  617. self->int_sip++;
  618. PROBE_DEBUG("I"); }
  619. return IRQ_HANDLED;
  620. }
  621. static int
  622. toshoboe_maketestpacket (unsigned char *buf, int badcrc, int fir)
  623. {
  624. int i;
  625. int len = 0;
  626. union
  627. {
  628. __u16 value;
  629. __u8 bytes[2];
  630. }
  631. fcs;
  632. if (fir)
  633. {
  634. memset (buf, 0, TT_LEN);
  635. return TT_LEN;
  636. }
  637. fcs.value = INIT_FCS;
  638. memset (buf, XBOF, 10);
  639. len += 10;
  640. buf[len++] = BOF;
  641. for (i = 0; i < TT_LEN; ++i)
  642. {
  643. len += stuff_byte (i, buf + len);
  644. fcs.value = irda_fcs (fcs.value, i);
  645. }
  646. len += stuff_byte (fcs.bytes[0] ^ badcrc, buf + len);
  647. len += stuff_byte (fcs.bytes[1] ^ badcrc, buf + len);
  648. buf[len++] = EOF;
  649. len++;
  650. return len;
  651. }
  652. static int
  653. toshoboe_probefail (struct toshoboe_cb *self, char *msg)
  654. {
  655. printk (KERN_ERR DRIVER_NAME "probe(%d) failed %s\n",self-> speed, msg);
  656. toshoboe_dumpregs (self);
  657. toshoboe_stopchip (self);
  658. free_irq (self->io.irq, (void *) self);
  659. return 0;
  660. }
  661. static int
  662. toshoboe_numvalidrcvs (struct toshoboe_cb *self)
  663. {
  664. int i, ret = 0;
  665. for (i = 0; i < RX_SLOTS; ++i)
  666. if ((self->ring->rx[i].control & 0xe0) == 0)
  667. ret++;
  668. return ret;
  669. }
  670. static int
  671. toshoboe_numrcvs (struct toshoboe_cb *self)
  672. {
  673. int i, ret = 0;
  674. for (i = 0; i < RX_SLOTS; ++i)
  675. if (!(self->ring->rx[i].control & OBOE_CTL_RX_HW_OWNS))
  676. ret++;
  677. return ret;
  678. }
  679. static int
  680. toshoboe_probe (struct toshoboe_cb *self)
  681. {
  682. int i, j, n;
  683. #ifdef USE_MIR
  684. static const int bauds[] = { 9600, 115200, 4000000, 1152000 };
  685. #else
  686. static const int bauds[] = { 9600, 115200, 4000000 };
  687. #endif
  688. unsigned long flags;
  689. if (request_irq (self->io.irq, toshoboe_probeinterrupt,
  690. self->io.irqflags, "toshoboe", (void *) self))
  691. {
  692. printk (KERN_ERR DRIVER_NAME ": probe failed to allocate irq %d\n",
  693. self->io.irq);
  694. return 0;
  695. }
  696. /* test 1: SIR filter and back to back */
  697. for (j = 0; j < ARRAY_SIZE(bauds); ++j)
  698. {
  699. int fir = (j > 1);
  700. toshoboe_stopchip (self);
  701. spin_lock_irqsave(&self->spinlock, flags);
  702. /*Address is already setup */
  703. toshoboe_startchip (self);
  704. self->int_rx = self->int_tx = 0;
  705. self->speed = bauds[j];
  706. toshoboe_setbaud (self);
  707. toshoboe_initptrs (self);
  708. spin_unlock_irqrestore(&self->spinlock, flags);
  709. self->ring->tx[self->txs].control =
  710. /* (FIR only) OBOE_CTL_TX_SIP needed for switching to next slot */
  711. /* MIR: all received data is stored in one slot */
  712. (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
  713. : OBOE_CTL_TX_HW_OWNS ;
  714. self->ring->tx[self->txs].len =
  715. toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
  716. self->txs++;
  717. self->txs %= TX_SLOTS;
  718. self->ring->tx[self->txs].control =
  719. (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_SIP
  720. : OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX ;
  721. self->ring->tx[self->txs].len =
  722. toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
  723. self->txs++;
  724. self->txs %= TX_SLOTS;
  725. self->ring->tx[self->txs].control =
  726. (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
  727. : OBOE_CTL_TX_HW_OWNS ;
  728. self->ring->tx[self->txs].len =
  729. toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
  730. self->txs++;
  731. self->txs %= TX_SLOTS;
  732. self->ring->tx[self->txs].control =
  733. (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
  734. | OBOE_CTL_TX_SIP | OBOE_CTL_TX_BAD_CRC
  735. : OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX ;
  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. toshoboe_dumptx (self);
  741. /* Turn on TX and RX and loopback */
  742. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
  743. i = 0;
  744. n = fir ? 1 : 4;
  745. while (toshoboe_numvalidrcvs (self) != n)
  746. {
  747. if (i > 4800)
  748. return toshoboe_probefail (self, "filter test");
  749. udelay ((9600*(TT_LEN+16))/self->speed);
  750. i++;
  751. }
  752. n = fir ? 203 : 102;
  753. while ((toshoboe_numrcvs(self) != self->int_rx) || (self->int_tx != n))
  754. {
  755. if (i > 4800)
  756. return toshoboe_probefail (self, "interrupt test");
  757. udelay ((9600*(TT_LEN+16))/self->speed);
  758. i++;
  759. }
  760. toshoboe_dumprx (self,i);
  761. }
  762. /* test 2: SIR in char at a time */
  763. toshoboe_stopchip (self);
  764. self->int_rx = self->int_tx = 0;
  765. spin_lock_irqsave(&self->spinlock, flags);
  766. toshoboe_startchip (self);
  767. spin_unlock_irqrestore(&self->spinlock, flags);
  768. self->async = 1;
  769. self->speed = 115200;
  770. toshoboe_setbaud (self);
  771. self->ring->tx[self->txs].control =
  772. OBOE_CTL_TX_RTCENTX | OBOE_CTL_TX_HW_OWNS;
  773. self->ring->tx[self->txs].len = 4;
  774. ((unsigned char *) self->tx_bufs[self->txs])[0] = 'f';
  775. ((unsigned char *) self->tx_bufs[self->txs])[1] = 'i';
  776. ((unsigned char *) self->tx_bufs[self->txs])[2] = 's';
  777. ((unsigned char *) self->tx_bufs[self->txs])[3] = 'h';
  778. toshoboe_dumptx (self);
  779. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
  780. i = 0;
  781. while (toshoboe_numvalidrcvs (self) != 4)
  782. {
  783. if (i > 100)
  784. return toshoboe_probefail (self, "Async test");
  785. udelay (100);
  786. i++;
  787. }
  788. while ((toshoboe_numrcvs (self) != self->int_rx) || (self->int_tx != 1))
  789. {
  790. if (i > 100)
  791. return toshoboe_probefail (self, "Async interrupt test");
  792. udelay (100);
  793. i++;
  794. }
  795. toshoboe_dumprx (self,i);
  796. self->async = 0;
  797. self->speed = 9600;
  798. toshoboe_setbaud (self);
  799. toshoboe_stopchip (self);
  800. free_irq (self->io.irq, (void *) self);
  801. printk (KERN_WARNING DRIVER_NAME ": Self test passed ok\n");
  802. return 1;
  803. }
  804. #endif
  805. /******************************************************************/
  806. /* Netdev style code */
  807. /* Transmit something */
  808. static netdev_tx_t
  809. toshoboe_hard_xmit (struct sk_buff *skb, struct net_device *dev)
  810. {
  811. struct toshoboe_cb *self;
  812. __s32 speed;
  813. int mtt, len, ctl;
  814. unsigned long flags;
  815. struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
  816. self = netdev_priv(dev);
  817. IRDA_ASSERT (self != NULL, return NETDEV_TX_OK; );
  818. pr_debug("%s.tx:%x(%x)%x\n",
  819. __func__, skb->len, self->txpending, INB(OBOE_ENABLEH));
  820. if (!cb->magic) {
  821. pr_debug("%s.Not IrLAP:%x\n", __func__, cb->magic);
  822. #ifdef DUMP_PACKETS
  823. _dumpbufs(skb->data,skb->len,'>');
  824. #endif
  825. }
  826. /* change speed pending, wait for its execution */
  827. if (self->new_speed)
  828. return NETDEV_TX_BUSY;
  829. /* device stopped (apm) wait for restart */
  830. if (self->stopped)
  831. return NETDEV_TX_BUSY;
  832. toshoboe_checkstuck (self);
  833. /* Check if we need to change the speed */
  834. /* But not now. Wait after transmission if mtt not required */
  835. speed=irda_get_next_speed(skb);
  836. if ((speed != self->io.speed) && (speed != -1))
  837. {
  838. spin_lock_irqsave(&self->spinlock, flags);
  839. if (self->txpending || skb->len)
  840. {
  841. self->new_speed = speed;
  842. pr_debug("%s: Queued TxDone scheduled speed change %d\n" ,
  843. __func__, speed);
  844. /* if no data, that's all! */
  845. if (!skb->len)
  846. {
  847. spin_unlock_irqrestore(&self->spinlock, flags);
  848. dev_kfree_skb (skb);
  849. return NETDEV_TX_OK;
  850. }
  851. /* True packet, go on, but */
  852. /* do not accept anything before change speed execution */
  853. netif_stop_queue(dev);
  854. /* ready to process TxDone interrupt */
  855. spin_unlock_irqrestore(&self->spinlock, flags);
  856. }
  857. else
  858. {
  859. /* idle and no data, change speed now */
  860. self->speed = speed;
  861. toshoboe_setbaud (self);
  862. spin_unlock_irqrestore(&self->spinlock, flags);
  863. dev_kfree_skb (skb);
  864. return NETDEV_TX_OK;
  865. }
  866. }
  867. if ((mtt = irda_get_mtt(skb)))
  868. {
  869. /* This is fair since the queue should be empty anyway */
  870. spin_lock_irqsave(&self->spinlock, flags);
  871. if (self->txpending)
  872. {
  873. spin_unlock_irqrestore(&self->spinlock, flags);
  874. return NETDEV_TX_BUSY;
  875. }
  876. /* If in SIR mode we need to generate a string of XBOFs */
  877. /* In MIR and FIR we need to generate a string of data */
  878. /* which we will add a wrong checksum to */
  879. mtt = toshoboe_makemttpacket (self, self->tx_bufs[self->txs], mtt);
  880. pr_debug("%s.mtt:%x(%x)%d\n", __func__, skb->len, mtt, self->txpending);
  881. if (mtt)
  882. {
  883. self->ring->tx[self->txs].len = mtt & 0xfff;
  884. ctl = OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX;
  885. if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_FIRON)
  886. {
  887. ctl |= OBOE_CTL_TX_BAD_CRC | OBOE_CTL_TX_SIP ;
  888. }
  889. #ifdef USE_MIR
  890. else if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_MIRON)
  891. {
  892. ctl |= OBOE_CTL_TX_BAD_CRC;
  893. }
  894. #endif
  895. self->ring->tx[self->txs].control = ctl;
  896. OUTB (0x0, OBOE_ENABLEH);
  897. /* It is only a timer. Do not send mtt packet outside! */
  898. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
  899. self->txpending++;
  900. self->txs++;
  901. self->txs %= TX_SLOTS;
  902. }
  903. else
  904. {
  905. printk(KERN_ERR DRIVER_NAME ": problem with mtt packet - ignored\n");
  906. }
  907. spin_unlock_irqrestore(&self->spinlock, flags);
  908. }
  909. #ifdef DUMP_PACKETS
  910. dumpbufs(skb->data,skb->len,'>');
  911. #endif
  912. spin_lock_irqsave(&self->spinlock, flags);
  913. if (self->ring->tx[self->txs].control & OBOE_CTL_TX_HW_OWNS)
  914. {
  915. pr_debug("%s.ful:%x(%x)%x\n",
  916. __func__, skb->len, self->ring->tx[self->txs].control,
  917. self->txpending);
  918. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);
  919. spin_unlock_irqrestore(&self->spinlock, flags);
  920. return NETDEV_TX_BUSY;
  921. }
  922. if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_SIRON)
  923. {
  924. len = async_wrap_skb (skb, self->tx_bufs[self->txs], TX_BUF_SZ);
  925. }
  926. else
  927. {
  928. len = skb->len;
  929. skb_copy_from_linear_data(skb, self->tx_bufs[self->txs], len);
  930. }
  931. self->ring->tx[self->txs].len = len & 0x0fff;
  932. /*Sometimes the HW doesn't see us assert RTCENTX in the interrupt code */
  933. /*later this plays safe, we garuntee the last packet to be transmitted */
  934. /*has RTCENTX set */
  935. ctl = OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX;
  936. if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_FIRON)
  937. {
  938. ctl |= OBOE_CTL_TX_SIP ;
  939. }
  940. self->ring->tx[self->txs].control = ctl;
  941. /* If transmitter is idle start in one-shot mode */
  942. if (!self->txpending)
  943. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);
  944. self->txpending++;
  945. self->txs++;
  946. self->txs %= TX_SLOTS;
  947. spin_unlock_irqrestore(&self->spinlock, flags);
  948. dev_kfree_skb (skb);
  949. return NETDEV_TX_OK;
  950. }
  951. /*interrupt handler */
  952. static irqreturn_t
  953. toshoboe_interrupt (int irq, void *dev_id)
  954. {
  955. struct toshoboe_cb *self = dev_id;
  956. __u8 irqstat;
  957. struct sk_buff *skb = NULL;
  958. irqstat = INB (OBOE_ISR);
  959. /* was it us */
  960. if (!(irqstat & OBOE_INT_MASK))
  961. return IRQ_NONE;
  962. /* Ack all the interrupts */
  963. OUTB (irqstat, OBOE_ISR);
  964. toshoboe_isntstuck (self);
  965. /* Txdone */
  966. if (irqstat & OBOE_INT_TXDONE)
  967. {
  968. int txp, txpc;
  969. int i;
  970. txp = self->txpending;
  971. self->txpending = 0;
  972. for (i = 0; i < TX_SLOTS; ++i)
  973. {
  974. if (self->ring->tx[i].control & OBOE_CTL_TX_HW_OWNS)
  975. self->txpending++;
  976. }
  977. pr_debug("%s.txd(%x)%x/%x\n", __func__, irqstat, txp, self->txpending);
  978. txp = INB (OBOE_TXSLOT) & OBOE_SLOT_MASK;
  979. /* Got anything queued ? start it together */
  980. if (self->ring->tx[txp].control & OBOE_CTL_TX_HW_OWNS)
  981. {
  982. txpc = txp;
  983. #ifdef OPTIMIZE_TX
  984. while (self->ring->tx[txpc].control & OBOE_CTL_TX_HW_OWNS)
  985. {
  986. txp = txpc;
  987. txpc++;
  988. txpc %= TX_SLOTS;
  989. self->netdev->stats.tx_packets++;
  990. if (self->ring->tx[txpc].control & OBOE_CTL_TX_HW_OWNS)
  991. self->ring->tx[txp].control &= ~OBOE_CTL_TX_RTCENTX;
  992. }
  993. self->netdev->stats.tx_packets--;
  994. #else
  995. self->netdev->stats.tx_packets++;
  996. #endif
  997. toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);
  998. }
  999. if ((!self->txpending) && (self->new_speed))
  1000. {
  1001. self->speed = self->new_speed;
  1002. pr_debug("%s: Executed TxDone scheduled speed change %d\n",
  1003. __func__, self->speed);
  1004. toshoboe_setbaud (self);
  1005. }
  1006. /* Tell network layer that we want more frames */
  1007. if (!self->new_speed)
  1008. netif_wake_queue(self->netdev);
  1009. }
  1010. if (irqstat & OBOE_INT_RXDONE)
  1011. {
  1012. while (!(self->ring->rx[self->rxs].control & OBOE_CTL_RX_HW_OWNS))
  1013. {
  1014. int len = self->ring->rx[self->rxs].len;
  1015. skb = NULL;
  1016. pr_debug("%s.rcv:%x(%x)\n", __func__
  1017. , len, self->ring->rx[self->rxs].control);
  1018. #ifdef DUMP_PACKETS
  1019. dumpbufs(self->rx_bufs[self->rxs],len,'<');
  1020. #endif
  1021. if (self->ring->rx[self->rxs].control == 0)
  1022. {
  1023. __u8 enable = INB (OBOE_ENABLEH);
  1024. /* In SIR mode we need to check the CRC as this */
  1025. /* hasn't been done by the hardware */
  1026. if (enable & OBOE_ENABLEH_SIRON)
  1027. {
  1028. if (!toshoboe_checkfcs (self->rx_bufs[self->rxs], len))
  1029. len = 0;
  1030. /*Trim off the CRC */
  1031. if (len > 1)
  1032. len -= 2;
  1033. else
  1034. len = 0;
  1035. pr_debug("%s.SIR:%x(%x)\n", __func__, len, enable);
  1036. }
  1037. #ifdef USE_MIR
  1038. else if (enable & OBOE_ENABLEH_MIRON)
  1039. {
  1040. if (len > 1)
  1041. len -= 2;
  1042. else
  1043. len = 0;
  1044. pr_debug("%s.MIR:%x(%x)\n", __func__, len, enable);
  1045. }
  1046. #endif
  1047. else if (enable & OBOE_ENABLEH_FIRON)
  1048. {
  1049. if (len > 3)
  1050. len -= 4; /*FIXME: check this */
  1051. else
  1052. len = 0;
  1053. pr_debug("%s.FIR:%x(%x)\n", __func__, len, enable);
  1054. }
  1055. else
  1056. pr_debug("%s.?IR:%x(%x)\n", __func__, len, enable);
  1057. if (len)
  1058. {
  1059. skb = dev_alloc_skb (len + 1);
  1060. if (skb)
  1061. {
  1062. skb_reserve (skb, 1);
  1063. skb_put (skb, len);
  1064. skb_copy_to_linear_data(skb, self->rx_bufs[self->rxs],
  1065. len);
  1066. self->netdev->stats.rx_packets++;
  1067. skb->dev = self->netdev;
  1068. skb_reset_mac_header(skb);
  1069. skb->protocol = htons (ETH_P_IRDA);
  1070. }
  1071. else
  1072. {
  1073. printk (KERN_INFO
  1074. "%s(), memory squeeze, dropping frame.\n",
  1075. __func__);
  1076. }
  1077. }
  1078. }
  1079. else
  1080. {
  1081. /* TODO: =========================================== */
  1082. /* if OBOE_CTL_RX_LENGTH, our buffers are too small */
  1083. /* (MIR or FIR) data is lost. */
  1084. /* (SIR) data is splitted in several slots. */
  1085. /* we have to join all the received buffers received */
  1086. /*in a large buffer before checking CRC. */
  1087. pr_debug("%s.err:%x(%x)\n", __func__
  1088. , len, self->ring->rx[self->rxs].control);
  1089. }
  1090. self->ring->rx[self->rxs].len = 0x0;
  1091. self->ring->rx[self->rxs].control = OBOE_CTL_RX_HW_OWNS;
  1092. self->rxs++;
  1093. self->rxs %= RX_SLOTS;
  1094. if (skb)
  1095. netif_rx (skb);
  1096. }
  1097. }
  1098. if (irqstat & OBOE_INT_TXUNDER)
  1099. {
  1100. printk (KERN_WARNING DRIVER_NAME ": tx fifo underflow\n");
  1101. }
  1102. if (irqstat & OBOE_INT_RXOVER)
  1103. {
  1104. printk (KERN_WARNING DRIVER_NAME ": rx fifo overflow\n");
  1105. }
  1106. /* This must be useful for something... */
  1107. if (irqstat & OBOE_INT_SIP)
  1108. {
  1109. self->int_sip++;
  1110. pr_debug("%s.sip:%x(%x)%x\n",
  1111. __func__, self->int_sip, irqstat, self->txpending);
  1112. }
  1113. return IRQ_HANDLED;
  1114. }
  1115. static int
  1116. toshoboe_net_open (struct net_device *dev)
  1117. {
  1118. struct toshoboe_cb *self;
  1119. unsigned long flags;
  1120. int rc;
  1121. self = netdev_priv(dev);
  1122. if (self->async)
  1123. return -EBUSY;
  1124. if (self->stopped)
  1125. return 0;
  1126. rc = request_irq (self->io.irq, toshoboe_interrupt,
  1127. IRQF_SHARED, dev->name, self);
  1128. if (rc)
  1129. return rc;
  1130. spin_lock_irqsave(&self->spinlock, flags);
  1131. toshoboe_startchip (self);
  1132. spin_unlock_irqrestore(&self->spinlock, flags);
  1133. /* Ready to play! */
  1134. netif_start_queue(dev);
  1135. /*
  1136. * Open new IrLAP layer instance, now that everything should be
  1137. * initialized properly
  1138. */
  1139. self->irlap = irlap_open (dev, &self->qos, driver_name);
  1140. self->irdad = 1;
  1141. return 0;
  1142. }
  1143. static int
  1144. toshoboe_net_close (struct net_device *dev)
  1145. {
  1146. struct toshoboe_cb *self;
  1147. IRDA_ASSERT (dev != NULL, return -1; );
  1148. self = netdev_priv(dev);
  1149. /* Stop device */
  1150. netif_stop_queue(dev);
  1151. /* Stop and remove instance of IrLAP */
  1152. if (self->irlap)
  1153. irlap_close (self->irlap);
  1154. self->irlap = NULL;
  1155. self->irdad = 0;
  1156. free_irq (self->io.irq, (void *) self);
  1157. if (!self->stopped)
  1158. {
  1159. toshoboe_stopchip (self);
  1160. }
  1161. return 0;
  1162. }
  1163. /*
  1164. * Function toshoboe_net_ioctl (dev, rq, cmd)
  1165. *
  1166. * Process IOCTL commands for this device
  1167. *
  1168. */
  1169. static int
  1170. toshoboe_net_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
  1171. {
  1172. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1173. struct toshoboe_cb *self;
  1174. unsigned long flags;
  1175. int ret = 0;
  1176. IRDA_ASSERT (dev != NULL, return -1; );
  1177. self = netdev_priv(dev);
  1178. IRDA_ASSERT (self != NULL, return -1; );
  1179. pr_debug("%s(), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
  1180. /* Disable interrupts & save flags */
  1181. spin_lock_irqsave(&self->spinlock, flags);
  1182. switch (cmd)
  1183. {
  1184. case SIOCSBANDWIDTH: /* Set bandwidth */
  1185. /* This function will also be used by IrLAP to change the
  1186. * speed, so we still must allow for speed change within
  1187. * interrupt context.
  1188. */
  1189. pr_debug("%s(BANDWIDTH), %s, (%X/%ld\n",
  1190. __func__, dev->name, INB(OBOE_STATUS), irq->ifr_baudrate);
  1191. if (!in_interrupt () && !capable (CAP_NET_ADMIN)) {
  1192. ret = -EPERM;
  1193. goto out;
  1194. }
  1195. /* self->speed=irq->ifr_baudrate; */
  1196. /* toshoboe_setbaud(self); */
  1197. /* Just change speed once - inserted by Paul Bristow */
  1198. self->new_speed = irq->ifr_baudrate;
  1199. break;
  1200. case SIOCSMEDIABUSY: /* Set media busy */
  1201. pr_debug("%s(MEDIABUSY), %s, (%X/%x)\n",
  1202. __func__, dev->name,
  1203. INB(OBOE_STATUS), capable(CAP_NET_ADMIN));
  1204. if (!capable (CAP_NET_ADMIN)) {
  1205. ret = -EPERM;
  1206. goto out;
  1207. }
  1208. irda_device_set_media_busy (self->netdev, TRUE);
  1209. break;
  1210. case SIOCGRECEIVING: /* Check if we are receiving right now */
  1211. irq->ifr_receiving = (INB (OBOE_STATUS) & OBOE_STATUS_RXBUSY) ? 1 : 0;
  1212. pr_debug("%s(RECEIVING), %s, (%X/%x)\n",
  1213. __func__, dev->name, INB(OBOE_STATUS), irq->ifr_receiving);
  1214. break;
  1215. default:
  1216. pr_debug("%s(?), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
  1217. ret = -EOPNOTSUPP;
  1218. }
  1219. out:
  1220. spin_unlock_irqrestore(&self->spinlock, flags);
  1221. return ret;
  1222. }
  1223. MODULE_DESCRIPTION("Toshiba OBOE IrDA Device Driver");
  1224. MODULE_AUTHOR("James McKenzie <james@fishsoup.dhs.org>");
  1225. MODULE_LICENSE("GPL");
  1226. module_param (max_baud, int, 0);
  1227. MODULE_PARM_DESC(max_baud, "Maximum baud rate");
  1228. #ifdef USE_PROBE
  1229. module_param (do_probe, bool, 0);
  1230. MODULE_PARM_DESC(do_probe, "Enable/disable chip probing and self-test");
  1231. #endif
  1232. static void
  1233. toshoboe_close (struct pci_dev *pci_dev)
  1234. {
  1235. int i;
  1236. struct toshoboe_cb *self = pci_get_drvdata(pci_dev);
  1237. IRDA_ASSERT (self != NULL, return; );
  1238. if (!self->stopped)
  1239. {
  1240. toshoboe_stopchip (self);
  1241. }
  1242. release_region (self->io.fir_base, self->io.fir_ext);
  1243. for (i = 0; i < TX_SLOTS; ++i)
  1244. {
  1245. kfree (self->tx_bufs[i]);
  1246. self->tx_bufs[i] = NULL;
  1247. }
  1248. for (i = 0; i < RX_SLOTS; ++i)
  1249. {
  1250. kfree (self->rx_bufs[i]);
  1251. self->rx_bufs[i] = NULL;
  1252. }
  1253. unregister_netdev(self->netdev);
  1254. kfree (self->ringbuf);
  1255. self->ringbuf = NULL;
  1256. self->ring = NULL;
  1257. free_netdev(self->netdev);
  1258. }
  1259. static const struct net_device_ops toshoboe_netdev_ops = {
  1260. .ndo_open = toshoboe_net_open,
  1261. .ndo_stop = toshoboe_net_close,
  1262. .ndo_start_xmit = toshoboe_hard_xmit,
  1263. .ndo_do_ioctl = toshoboe_net_ioctl,
  1264. };
  1265. static int
  1266. toshoboe_open (struct pci_dev *pci_dev, const struct pci_device_id *pdid)
  1267. {
  1268. struct toshoboe_cb *self;
  1269. struct net_device *dev;
  1270. int i = 0;
  1271. int ok = 0;
  1272. int err;
  1273. if ((err=pci_enable_device(pci_dev)))
  1274. return err;
  1275. dev = alloc_irdadev(sizeof (struct toshoboe_cb));
  1276. if (dev == NULL)
  1277. {
  1278. printk (KERN_ERR DRIVER_NAME ": can't allocate memory for "
  1279. "IrDA control block\n");
  1280. return -ENOMEM;
  1281. }
  1282. self = netdev_priv(dev);
  1283. self->netdev = dev;
  1284. self->pdev = pci_dev;
  1285. self->base = pci_resource_start(pci_dev,0);
  1286. self->io.fir_base = self->base;
  1287. self->io.fir_ext = OBOE_IO_EXTENT;
  1288. self->io.irq = pci_dev->irq;
  1289. self->io.irqflags = IRQF_SHARED;
  1290. self->speed = self->io.speed = 9600;
  1291. self->async = 0;
  1292. /* Lock the port that we need */
  1293. if (NULL==request_region (self->io.fir_base, self->io.fir_ext, driver_name))
  1294. {
  1295. printk (KERN_ERR DRIVER_NAME ": can't get iobase of 0x%03x\n"
  1296. ,self->io.fir_base);
  1297. err = -EBUSY;
  1298. goto freeself;
  1299. }
  1300. spin_lock_init(&self->spinlock);
  1301. irda_init_max_qos_capabilies (&self->qos);
  1302. self->qos.baud_rate.bits = 0;
  1303. if (max_baud >= 2400)
  1304. self->qos.baud_rate.bits |= IR_2400;
  1305. /*if (max_baud>=4800) idev->qos.baud_rate.bits|=IR_4800; */
  1306. if (max_baud >= 9600)
  1307. self->qos.baud_rate.bits |= IR_9600;
  1308. if (max_baud >= 19200)
  1309. self->qos.baud_rate.bits |= IR_19200;
  1310. if (max_baud >= 115200)
  1311. self->qos.baud_rate.bits |= IR_115200;
  1312. #ifdef USE_MIR
  1313. if (max_baud >= 1152000)
  1314. {
  1315. self->qos.baud_rate.bits |= IR_1152000;
  1316. }
  1317. #endif
  1318. if (max_baud >= 4000000)
  1319. {
  1320. self->qos.baud_rate.bits |= (IR_4000000 << 8);
  1321. }
  1322. /*FIXME: work this out... */
  1323. self->qos.min_turn_time.bits = 0xff;
  1324. irda_qos_bits_to_value (&self->qos);
  1325. /* Allocate twice the size to guarantee alignment */
  1326. self->ringbuf = kmalloc(OBOE_RING_LEN << 1, GFP_KERNEL);
  1327. if (!self->ringbuf)
  1328. {
  1329. err = -ENOMEM;
  1330. goto freeregion;
  1331. }
  1332. #if (BITS_PER_LONG == 64)
  1333. #error broken on 64-bit: casts pointer to 32-bit, and then back to pointer.
  1334. #endif
  1335. /*We need to align the taskfile on a taskfile size boundary */
  1336. {
  1337. unsigned long addr;
  1338. addr = (__u32) self->ringbuf;
  1339. addr &= ~(OBOE_RING_LEN - 1);
  1340. addr += OBOE_RING_LEN;
  1341. self->ring = (struct OboeRing *) addr;
  1342. }
  1343. memset (self->ring, 0, OBOE_RING_LEN);
  1344. self->io.mem_base = (__u32) self->ring;
  1345. ok = 1;
  1346. for (i = 0; i < TX_SLOTS; ++i)
  1347. {
  1348. self->tx_bufs[i] = kmalloc (TX_BUF_SZ, GFP_KERNEL);
  1349. if (!self->tx_bufs[i])
  1350. ok = 0;
  1351. }
  1352. for (i = 0; i < RX_SLOTS; ++i)
  1353. {
  1354. self->rx_bufs[i] = kmalloc (RX_BUF_SZ, GFP_KERNEL);
  1355. if (!self->rx_bufs[i])
  1356. ok = 0;
  1357. }
  1358. if (!ok)
  1359. {
  1360. err = -ENOMEM;
  1361. goto freebufs;
  1362. }
  1363. #ifdef USE_PROBE
  1364. if (do_probe)
  1365. if (!toshoboe_probe (self))
  1366. {
  1367. err = -ENODEV;
  1368. goto freebufs;
  1369. }
  1370. #endif
  1371. SET_NETDEV_DEV(dev, &pci_dev->dev);
  1372. dev->netdev_ops = &toshoboe_netdev_ops;
  1373. err = register_netdev(dev);
  1374. if (err)
  1375. {
  1376. printk (KERN_ERR DRIVER_NAME ": register_netdev() failed\n");
  1377. err = -ENOMEM;
  1378. goto freebufs;
  1379. }
  1380. printk (KERN_INFO "IrDA: Registered device %s\n", dev->name);
  1381. pci_set_drvdata(pci_dev,self);
  1382. printk (KERN_INFO DRIVER_NAME ": Using multiple tasks\n");
  1383. return 0;
  1384. freebufs:
  1385. for (i = 0; i < TX_SLOTS; ++i)
  1386. kfree (self->tx_bufs[i]);
  1387. for (i = 0; i < RX_SLOTS; ++i)
  1388. kfree (self->rx_bufs[i]);
  1389. kfree(self->ringbuf);
  1390. freeregion:
  1391. release_region (self->io.fir_base, self->io.fir_ext);
  1392. freeself:
  1393. free_netdev(dev);
  1394. return err;
  1395. }
  1396. static int
  1397. toshoboe_gotosleep (struct pci_dev *pci_dev, pm_message_t crap)
  1398. {
  1399. struct toshoboe_cb *self = pci_get_drvdata(pci_dev);
  1400. unsigned long flags;
  1401. int i = 10;
  1402. if (!self || self->stopped)
  1403. return 0;
  1404. if ((!self->irdad) && (!self->async))
  1405. return 0;
  1406. /* Flush all packets */
  1407. while ((i--) && (self->txpending))
  1408. msleep(10);
  1409. spin_lock_irqsave(&self->spinlock, flags);
  1410. toshoboe_stopchip (self);
  1411. self->stopped = 1;
  1412. self->txpending = 0;
  1413. spin_unlock_irqrestore(&self->spinlock, flags);
  1414. return 0;
  1415. }
  1416. static int
  1417. toshoboe_wakeup (struct pci_dev *pci_dev)
  1418. {
  1419. struct toshoboe_cb *self = pci_get_drvdata(pci_dev);
  1420. unsigned long flags;
  1421. if (!self || !self->stopped)
  1422. return 0;
  1423. if ((!self->irdad) && (!self->async))
  1424. return 0;
  1425. spin_lock_irqsave(&self->spinlock, flags);
  1426. toshoboe_startchip (self);
  1427. self->stopped = 0;
  1428. netif_wake_queue(self->netdev);
  1429. spin_unlock_irqrestore(&self->spinlock, flags);
  1430. return 0;
  1431. }
  1432. static struct pci_driver donauboe_pci_driver = {
  1433. .name = "donauboe",
  1434. .id_table = toshoboe_pci_tbl,
  1435. .probe = toshoboe_open,
  1436. .remove = toshoboe_close,
  1437. .suspend = toshoboe_gotosleep,
  1438. .resume = toshoboe_wakeup
  1439. };
  1440. module_pci_driver(donauboe_pci_driver);