cm4000_cs.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  1. /*
  2. * A driver for the PCMCIA Smartcard Reader "Omnikey CardMan Mobile 4000"
  3. *
  4. * cm4000_cs.c support.linux@omnikey.com
  5. *
  6. * Tue Oct 23 11:32:43 GMT 2001 herp - cleaned up header files
  7. * Sun Jan 20 10:11:15 MET 2002 herp - added modversion header files
  8. * Thu Nov 14 16:34:11 GMT 2002 mh - added PPS functionality
  9. * Tue Nov 19 16:36:27 GMT 2002 mh - added SUSPEND/RESUME functionailty
  10. * Wed Jul 28 12:55:01 CEST 2004 mh - kernel 2.6 adjustments
  11. *
  12. * current version: 2.4.0gm4
  13. *
  14. * (C) 2000,2001,2002,2003,2004 Omnikey AG
  15. *
  16. * (C) 2005-2006 Harald Welte <laforge@gnumonks.org>
  17. * - Adhere to Kernel CodingStyle
  18. * - Port to 2.6.13 "new" style PCMCIA
  19. * - Check for copy_{from,to}_user return values
  20. * - Use nonseekable_open()
  21. * - add class interface for udev device creation
  22. *
  23. * All rights reserved. Licensed under dual BSD/GPL license.
  24. */
  25. /* #define PCMCIA_DEBUG 6 */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/init.h>
  30. #include <linux/fs.h>
  31. #include <linux/delay.h>
  32. #include <linux/bitrev.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/io.h>
  35. #include <pcmcia/cs_types.h>
  36. #include <pcmcia/cs.h>
  37. #include <pcmcia/cistpl.h>
  38. #include <pcmcia/cisreg.h>
  39. #include <pcmcia/ciscode.h>
  40. #include <pcmcia/ds.h>
  41. #include <linux/cm4000_cs.h>
  42. /* #define ATR_CSUM */
  43. #ifdef PCMCIA_DEBUG
  44. #define reader_to_dev(x) (&handle_to_dev(x->p_dev->handle))
  45. static int pc_debug = PCMCIA_DEBUG;
  46. module_param(pc_debug, int, 0600);
  47. #define DEBUGP(n, rdr, x, args...) do { \
  48. if (pc_debug >= (n)) \
  49. dev_printk(KERN_DEBUG, reader_to_dev(rdr), "%s:" x, \
  50. __FUNCTION__ , ## args); \
  51. } while (0)
  52. #else
  53. #define DEBUGP(n, rdr, x, args...)
  54. #endif
  55. static char *version = "cm4000_cs.c v2.4.0gm6 - All bugs added by Harald Welte";
  56. #define T_1SEC (HZ)
  57. #define T_10MSEC msecs_to_jiffies(10)
  58. #define T_20MSEC msecs_to_jiffies(20)
  59. #define T_40MSEC msecs_to_jiffies(40)
  60. #define T_50MSEC msecs_to_jiffies(50)
  61. #define T_100MSEC msecs_to_jiffies(100)
  62. #define T_500MSEC msecs_to_jiffies(500)
  63. static void cm4000_release(struct pcmcia_device *link);
  64. static int major; /* major number we get from the kernel */
  65. /* note: the first state has to have number 0 always */
  66. #define M_FETCH_ATR 0
  67. #define M_TIMEOUT_WAIT 1
  68. #define M_READ_ATR_LEN 2
  69. #define M_READ_ATR 3
  70. #define M_ATR_PRESENT 4
  71. #define M_BAD_CARD 5
  72. #define M_CARDOFF 6
  73. #define LOCK_IO 0
  74. #define LOCK_MONITOR 1
  75. #define IS_AUTOPPS_ACT 6
  76. #define IS_PROCBYTE_PRESENT 7
  77. #define IS_INVREV 8
  78. #define IS_ANY_T0 9
  79. #define IS_ANY_T1 10
  80. #define IS_ATR_PRESENT 11
  81. #define IS_ATR_VALID 12
  82. #define IS_CMM_ABSENT 13
  83. #define IS_BAD_LENGTH 14
  84. #define IS_BAD_CSUM 15
  85. #define IS_BAD_CARD 16
  86. #define REG_FLAGS0(x) (x + 0)
  87. #define REG_FLAGS1(x) (x + 1)
  88. #define REG_NUM_BYTES(x) (x + 2)
  89. #define REG_BUF_ADDR(x) (x + 3)
  90. #define REG_BUF_DATA(x) (x + 4)
  91. #define REG_NUM_SEND(x) (x + 5)
  92. #define REG_BAUDRATE(x) (x + 6)
  93. #define REG_STOPBITS(x) (x + 7)
  94. struct cm4000_dev {
  95. struct pcmcia_device *p_dev;
  96. dev_node_t node; /* OS node (major,minor) */
  97. unsigned char atr[MAX_ATR];
  98. unsigned char rbuf[512];
  99. unsigned char sbuf[512];
  100. wait_queue_head_t devq; /* when removing cardman must not be
  101. zeroed! */
  102. wait_queue_head_t ioq; /* if IO is locked, wait on this Q */
  103. wait_queue_head_t atrq; /* wait for ATR valid */
  104. wait_queue_head_t readq; /* used by write to wake blk.read */
  105. /* warning: do not move this fields.
  106. * initialising to zero depends on it - see ZERO_DEV below. */
  107. unsigned char atr_csum;
  108. unsigned char atr_len_retry;
  109. unsigned short atr_len;
  110. unsigned short rlen; /* bytes avail. after write */
  111. unsigned short rpos; /* latest read pos. write zeroes */
  112. unsigned char procbyte; /* T=0 procedure byte */
  113. unsigned char mstate; /* state of card monitor */
  114. unsigned char cwarn; /* slow down warning */
  115. unsigned char flags0; /* cardman IO-flags 0 */
  116. unsigned char flags1; /* cardman IO-flags 1 */
  117. unsigned int mdelay; /* variable monitor speeds, in jiffies */
  118. unsigned int baudv; /* baud value for speed */
  119. unsigned char ta1;
  120. unsigned char proto; /* T=0, T=1, ... */
  121. unsigned long flags; /* lock+flags (MONITOR,IO,ATR) * for concurrent
  122. access */
  123. unsigned char pts[4];
  124. struct timer_list timer; /* used to keep monitor running */
  125. int monitor_running;
  126. };
  127. #define ZERO_DEV(dev) \
  128. memset(&dev->atr_csum,0, \
  129. sizeof(struct cm4000_dev) - \
  130. offsetof(struct cm4000_dev, atr_csum))
  131. static struct pcmcia_device *dev_table[CM4000_MAX_DEV];
  132. static struct class *cmm_class;
  133. /* This table doesn't use spaces after the comma between fields and thus
  134. * violates CodingStyle. However, I don't really think wrapping it around will
  135. * make it any clearer to read -HW */
  136. static unsigned char fi_di_table[10][14] = {
  137. /*FI 00 01 02 03 04 05 06 07 08 09 10 11 12 13 */
  138. /*DI */
  139. /* 0 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
  140. /* 1 */ {0x01,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x91,0x11,0x11,0x11,0x11},
  141. /* 2 */ {0x02,0x12,0x22,0x32,0x11,0x11,0x11,0x11,0x11,0x92,0xA2,0xB2,0x11,0x11},
  142. /* 3 */ {0x03,0x13,0x23,0x33,0x43,0x53,0x63,0x11,0x11,0x93,0xA3,0xB3,0xC3,0xD3},
  143. /* 4 */ {0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x11,0x11,0x94,0xA4,0xB4,0xC4,0xD4},
  144. /* 5 */ {0x00,0x15,0x25,0x35,0x45,0x55,0x65,0x11,0x11,0x95,0xA5,0xB5,0xC5,0xD5},
  145. /* 6 */ {0x06,0x16,0x26,0x36,0x46,0x56,0x66,0x11,0x11,0x96,0xA6,0xB6,0xC6,0xD6},
  146. /* 7 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
  147. /* 8 */ {0x08,0x11,0x28,0x38,0x48,0x58,0x68,0x11,0x11,0x98,0xA8,0xB8,0xC8,0xD8},
  148. /* 9 */ {0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x11,0x11,0x99,0xA9,0xB9,0xC9,0xD9}
  149. };
  150. #ifndef PCMCIA_DEBUG
  151. #define xoutb outb
  152. #define xinb inb
  153. #else
  154. static inline void xoutb(unsigned char val, unsigned short port)
  155. {
  156. if (pc_debug >= 7)
  157. printk(KERN_DEBUG "outb(val=%.2x,port=%.4x)\n", val, port);
  158. outb(val, port);
  159. }
  160. static inline unsigned char xinb(unsigned short port)
  161. {
  162. unsigned char val;
  163. val = inb(port);
  164. if (pc_debug >= 7)
  165. printk(KERN_DEBUG "%.2x=inb(%.4x)\n", val, port);
  166. return val;
  167. }
  168. #endif
  169. static inline unsigned char invert_revert(unsigned char ch)
  170. {
  171. return bitrev8(~ch);
  172. }
  173. static void str_invert_revert(unsigned char *b, int len)
  174. {
  175. int i;
  176. for (i = 0; i < len; i++)
  177. b[i] = invert_revert(b[i]);
  178. }
  179. #define ATRLENCK(dev,pos) \
  180. if (pos>=dev->atr_len || pos>=MAX_ATR) \
  181. goto return_0;
  182. static unsigned int calc_baudv(unsigned char fidi)
  183. {
  184. unsigned int wcrcf, wbrcf, fi_rfu, di_rfu;
  185. fi_rfu = 372;
  186. di_rfu = 1;
  187. /* FI */
  188. switch ((fidi >> 4) & 0x0F) {
  189. case 0x00:
  190. wcrcf = 372;
  191. break;
  192. case 0x01:
  193. wcrcf = 372;
  194. break;
  195. case 0x02:
  196. wcrcf = 558;
  197. break;
  198. case 0x03:
  199. wcrcf = 744;
  200. break;
  201. case 0x04:
  202. wcrcf = 1116;
  203. break;
  204. case 0x05:
  205. wcrcf = 1488;
  206. break;
  207. case 0x06:
  208. wcrcf = 1860;
  209. break;
  210. case 0x07:
  211. wcrcf = fi_rfu;
  212. break;
  213. case 0x08:
  214. wcrcf = fi_rfu;
  215. break;
  216. case 0x09:
  217. wcrcf = 512;
  218. break;
  219. case 0x0A:
  220. wcrcf = 768;
  221. break;
  222. case 0x0B:
  223. wcrcf = 1024;
  224. break;
  225. case 0x0C:
  226. wcrcf = 1536;
  227. break;
  228. case 0x0D:
  229. wcrcf = 2048;
  230. break;
  231. default:
  232. wcrcf = fi_rfu;
  233. break;
  234. }
  235. /* DI */
  236. switch (fidi & 0x0F) {
  237. case 0x00:
  238. wbrcf = di_rfu;
  239. break;
  240. case 0x01:
  241. wbrcf = 1;
  242. break;
  243. case 0x02:
  244. wbrcf = 2;
  245. break;
  246. case 0x03:
  247. wbrcf = 4;
  248. break;
  249. case 0x04:
  250. wbrcf = 8;
  251. break;
  252. case 0x05:
  253. wbrcf = 16;
  254. break;
  255. case 0x06:
  256. wbrcf = 32;
  257. break;
  258. case 0x07:
  259. wbrcf = di_rfu;
  260. break;
  261. case 0x08:
  262. wbrcf = 12;
  263. break;
  264. case 0x09:
  265. wbrcf = 20;
  266. break;
  267. default:
  268. wbrcf = di_rfu;
  269. break;
  270. }
  271. return (wcrcf / wbrcf);
  272. }
  273. static unsigned short io_read_num_rec_bytes(ioaddr_t iobase, unsigned short *s)
  274. {
  275. unsigned short tmp;
  276. tmp = *s = 0;
  277. do {
  278. *s = tmp;
  279. tmp = inb(REG_NUM_BYTES(iobase)) |
  280. (inb(REG_FLAGS0(iobase)) & 4 ? 0x100 : 0);
  281. } while (tmp != *s);
  282. return *s;
  283. }
  284. static int parse_atr(struct cm4000_dev *dev)
  285. {
  286. unsigned char any_t1, any_t0;
  287. unsigned char ch, ifno;
  288. int ix, done;
  289. DEBUGP(3, dev, "-> parse_atr: dev->atr_len = %i\n", dev->atr_len);
  290. if (dev->atr_len < 3) {
  291. DEBUGP(5, dev, "parse_atr: atr_len < 3\n");
  292. return 0;
  293. }
  294. if (dev->atr[0] == 0x3f)
  295. set_bit(IS_INVREV, &dev->flags);
  296. else
  297. clear_bit(IS_INVREV, &dev->flags);
  298. ix = 1;
  299. ifno = 1;
  300. ch = dev->atr[1];
  301. dev->proto = 0; /* XXX PROTO */
  302. any_t1 = any_t0 = done = 0;
  303. dev->ta1 = 0x11; /* defaults to 9600 baud */
  304. do {
  305. if (ifno == 1 && (ch & 0x10)) {
  306. /* read first interface byte and TA1 is present */
  307. dev->ta1 = dev->atr[2];
  308. DEBUGP(5, dev, "Card says FiDi is 0x%.2x\n", dev->ta1);
  309. ifno++;
  310. } else if ((ifno == 2) && (ch & 0x10)) { /* TA(2) */
  311. dev->ta1 = 0x11;
  312. ifno++;
  313. }
  314. DEBUGP(5, dev, "Yi=%.2x\n", ch & 0xf0);
  315. ix += ((ch & 0x10) >> 4) /* no of int.face chars */
  316. +((ch & 0x20) >> 5)
  317. + ((ch & 0x40) >> 6)
  318. + ((ch & 0x80) >> 7);
  319. /* ATRLENCK(dev,ix); */
  320. if (ch & 0x80) { /* TDi */
  321. ch = dev->atr[ix];
  322. if ((ch & 0x0f)) {
  323. any_t1 = 1;
  324. DEBUGP(5, dev, "card is capable of T=1\n");
  325. } else {
  326. any_t0 = 1;
  327. DEBUGP(5, dev, "card is capable of T=0\n");
  328. }
  329. } else
  330. done = 1;
  331. } while (!done);
  332. DEBUGP(5, dev, "ix=%d noHist=%d any_t1=%d\n",
  333. ix, dev->atr[1] & 15, any_t1);
  334. if (ix + 1 + (dev->atr[1] & 0x0f) + any_t1 != dev->atr_len) {
  335. DEBUGP(5, dev, "length error\n");
  336. return 0;
  337. }
  338. if (any_t0)
  339. set_bit(IS_ANY_T0, &dev->flags);
  340. if (any_t1) { /* compute csum */
  341. dev->atr_csum = 0;
  342. #ifdef ATR_CSUM
  343. for (i = 1; i < dev->atr_len; i++)
  344. dev->atr_csum ^= dev->atr[i];
  345. if (dev->atr_csum) {
  346. set_bit(IS_BAD_CSUM, &dev->flags);
  347. DEBUGP(5, dev, "bad checksum\n");
  348. goto return_0;
  349. }
  350. #endif
  351. if (any_t0 == 0)
  352. dev->proto = 1; /* XXX PROTO */
  353. set_bit(IS_ANY_T1, &dev->flags);
  354. }
  355. return 1;
  356. }
  357. struct card_fixup {
  358. char atr[12];
  359. u_int8_t atr_len;
  360. u_int8_t stopbits;
  361. };
  362. static struct card_fixup card_fixups[] = {
  363. { /* ACOS */
  364. .atr = { 0x3b, 0xb3, 0x11, 0x00, 0x00, 0x41, 0x01 },
  365. .atr_len = 7,
  366. .stopbits = 0x03,
  367. },
  368. { /* Motorola */
  369. .atr = {0x3b, 0x76, 0x13, 0x00, 0x00, 0x80, 0x62, 0x07,
  370. 0x41, 0x81, 0x81 },
  371. .atr_len = 11,
  372. .stopbits = 0x04,
  373. },
  374. };
  375. static void set_cardparameter(struct cm4000_dev *dev)
  376. {
  377. int i;
  378. ioaddr_t iobase = dev->p_dev->io.BasePort1;
  379. u_int8_t stopbits = 0x02; /* ISO default */
  380. DEBUGP(3, dev, "-> set_cardparameter\n");
  381. dev->flags1 = dev->flags1 | (((dev->baudv - 1) & 0x0100) >> 8);
  382. xoutb(dev->flags1, REG_FLAGS1(iobase));
  383. DEBUGP(5, dev, "flags1 = 0x%02x\n", dev->flags1);
  384. /* set baudrate */
  385. xoutb((unsigned char)((dev->baudv - 1) & 0xFF), REG_BAUDRATE(iobase));
  386. DEBUGP(5, dev, "baudv = %i -> write 0x%02x\n", dev->baudv,
  387. ((dev->baudv - 1) & 0xFF));
  388. /* set stopbits */
  389. for (i = 0; i < ARRAY_SIZE(card_fixups); i++) {
  390. if (!memcmp(dev->atr, card_fixups[i].atr,
  391. card_fixups[i].atr_len))
  392. stopbits = card_fixups[i].stopbits;
  393. }
  394. xoutb(stopbits, REG_STOPBITS(iobase));
  395. DEBUGP(3, dev, "<- set_cardparameter\n");
  396. }
  397. static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
  398. {
  399. unsigned long tmp, i;
  400. unsigned short num_bytes_read;
  401. unsigned char pts_reply[4];
  402. ssize_t rc;
  403. ioaddr_t iobase = dev->p_dev->io.BasePort1;
  404. rc = 0;
  405. DEBUGP(3, dev, "-> set_protocol\n");
  406. DEBUGP(5, dev, "ptsreq->Protocol = 0x%.8x, ptsreq->Flags=0x%.8x, "
  407. "ptsreq->pts1=0x%.2x, ptsreq->pts2=0x%.2x, "
  408. "ptsreq->pts3=0x%.2x\n", (unsigned int)ptsreq->protocol,
  409. (unsigned int)ptsreq->flags, ptsreq->pts1, ptsreq->pts2,
  410. ptsreq->pts3);
  411. /* Fill PTS structure */
  412. dev->pts[0] = 0xff;
  413. dev->pts[1] = 0x00;
  414. tmp = ptsreq->protocol;
  415. while ((tmp = (tmp >> 1)) > 0)
  416. dev->pts[1]++;
  417. dev->proto = dev->pts[1]; /* Set new protocol */
  418. dev->pts[1] = (0x01 << 4) | (dev->pts[1]);
  419. /* Correct Fi/Di according to CM4000 Fi/Di table */
  420. DEBUGP(5, dev, "Ta(1) from ATR is 0x%.2x\n", dev->ta1);
  421. /* set Fi/Di according to ATR TA(1) */
  422. dev->pts[2] = fi_di_table[dev->ta1 & 0x0F][(dev->ta1 >> 4) & 0x0F];
  423. /* Calculate PCK character */
  424. dev->pts[3] = dev->pts[0] ^ dev->pts[1] ^ dev->pts[2];
  425. DEBUGP(5, dev, "pts0=%.2x, pts1=%.2x, pts2=%.2x, pts3=%.2x\n",
  426. dev->pts[0], dev->pts[1], dev->pts[2], dev->pts[3]);
  427. /* check card convention */
  428. if (test_bit(IS_INVREV, &dev->flags))
  429. str_invert_revert(dev->pts, 4);
  430. /* reset SM */
  431. xoutb(0x80, REG_FLAGS0(iobase));
  432. /* Enable access to the message buffer */
  433. DEBUGP(5, dev, "Enable access to the messages buffer\n");
  434. dev->flags1 = 0x20 /* T_Active */
  435. | (test_bit(IS_INVREV, &dev->flags) ? 0x02 : 0x00) /* inv parity */
  436. | ((dev->baudv >> 8) & 0x01); /* MSB-baud */
  437. xoutb(dev->flags1, REG_FLAGS1(iobase));
  438. DEBUGP(5, dev, "Enable message buffer -> flags1 = 0x%.2x\n",
  439. dev->flags1);
  440. /* write challenge to the buffer */
  441. DEBUGP(5, dev, "Write challenge to buffer: ");
  442. for (i = 0; i < 4; i++) {
  443. xoutb(i, REG_BUF_ADDR(iobase));
  444. xoutb(dev->pts[i], REG_BUF_DATA(iobase)); /* buf data */
  445. #ifdef PCMCIA_DEBUG
  446. if (pc_debug >= 5)
  447. printk("0x%.2x ", dev->pts[i]);
  448. }
  449. if (pc_debug >= 5)
  450. printk("\n");
  451. #else
  452. }
  453. #endif
  454. /* set number of bytes to write */
  455. DEBUGP(5, dev, "Set number of bytes to write\n");
  456. xoutb(0x04, REG_NUM_SEND(iobase));
  457. /* Trigger CARDMAN CONTROLLER */
  458. xoutb(0x50, REG_FLAGS0(iobase));
  459. /* Monitor progress */
  460. /* wait for xmit done */
  461. DEBUGP(5, dev, "Waiting for NumRecBytes getting valid\n");
  462. for (i = 0; i < 100; i++) {
  463. if (inb(REG_FLAGS0(iobase)) & 0x08) {
  464. DEBUGP(5, dev, "NumRecBytes is valid\n");
  465. break;
  466. }
  467. mdelay(10);
  468. }
  469. if (i == 100) {
  470. DEBUGP(5, dev, "Timeout waiting for NumRecBytes getting "
  471. "valid\n");
  472. rc = -EIO;
  473. goto exit_setprotocol;
  474. }
  475. DEBUGP(5, dev, "Reading NumRecBytes\n");
  476. for (i = 0; i < 100; i++) {
  477. io_read_num_rec_bytes(iobase, &num_bytes_read);
  478. if (num_bytes_read >= 4) {
  479. DEBUGP(2, dev, "NumRecBytes = %i\n", num_bytes_read);
  480. break;
  481. }
  482. mdelay(10);
  483. }
  484. /* check whether it is a short PTS reply? */
  485. if (num_bytes_read == 3)
  486. i = 0;
  487. if (i == 100) {
  488. DEBUGP(5, dev, "Timeout reading num_bytes_read\n");
  489. rc = -EIO;
  490. goto exit_setprotocol;
  491. }
  492. DEBUGP(5, dev, "Reset the CARDMAN CONTROLLER\n");
  493. xoutb(0x80, REG_FLAGS0(iobase));
  494. /* Read PPS reply */
  495. DEBUGP(5, dev, "Read PPS reply\n");
  496. for (i = 0; i < num_bytes_read; i++) {
  497. xoutb(i, REG_BUF_ADDR(iobase));
  498. pts_reply[i] = inb(REG_BUF_DATA(iobase));
  499. }
  500. #ifdef PCMCIA_DEBUG
  501. DEBUGP(2, dev, "PTSreply: ");
  502. for (i = 0; i < num_bytes_read; i++) {
  503. if (pc_debug >= 5)
  504. printk("0x%.2x ", pts_reply[i]);
  505. }
  506. printk("\n");
  507. #endif /* PCMCIA_DEBUG */
  508. DEBUGP(5, dev, "Clear Tactive in Flags1\n");
  509. xoutb(0x20, REG_FLAGS1(iobase));
  510. /* Compare ptsreq and ptsreply */
  511. if ((dev->pts[0] == pts_reply[0]) &&
  512. (dev->pts[1] == pts_reply[1]) &&
  513. (dev->pts[2] == pts_reply[2]) && (dev->pts[3] == pts_reply[3])) {
  514. /* setcardparameter according to PPS */
  515. dev->baudv = calc_baudv(dev->pts[2]);
  516. set_cardparameter(dev);
  517. } else if ((dev->pts[0] == pts_reply[0]) &&
  518. ((dev->pts[1] & 0xef) == pts_reply[1]) &&
  519. ((pts_reply[0] ^ pts_reply[1]) == pts_reply[2])) {
  520. /* short PTS reply, set card parameter to default values */
  521. dev->baudv = calc_baudv(0x11);
  522. set_cardparameter(dev);
  523. } else
  524. rc = -EIO;
  525. exit_setprotocol:
  526. DEBUGP(3, dev, "<- set_protocol\n");
  527. return rc;
  528. }
  529. static int io_detect_cm4000(ioaddr_t iobase, struct cm4000_dev *dev)
  530. {
  531. /* note: statemachine is assumed to be reset */
  532. if (inb(REG_FLAGS0(iobase)) & 8) {
  533. clear_bit(IS_ATR_VALID, &dev->flags);
  534. set_bit(IS_CMM_ABSENT, &dev->flags);
  535. return 0; /* detect CMM = 1 -> failure */
  536. }
  537. /* xoutb(0x40, REG_FLAGS1(iobase)); detectCMM */
  538. xoutb(dev->flags1 | 0x40, REG_FLAGS1(iobase));
  539. if ((inb(REG_FLAGS0(iobase)) & 8) == 0) {
  540. clear_bit(IS_ATR_VALID, &dev->flags);
  541. set_bit(IS_CMM_ABSENT, &dev->flags);
  542. return 0; /* detect CMM=0 -> failure */
  543. }
  544. /* clear detectCMM again by restoring original flags1 */
  545. xoutb(dev->flags1, REG_FLAGS1(iobase));
  546. return 1;
  547. }
  548. static void terminate_monitor(struct cm4000_dev *dev)
  549. {
  550. /* tell the monitor to stop and wait until
  551. * it terminates.
  552. */
  553. DEBUGP(3, dev, "-> terminate_monitor\n");
  554. wait_event_interruptible(dev->devq,
  555. test_and_set_bit(LOCK_MONITOR,
  556. (void *)&dev->flags));
  557. /* now, LOCK_MONITOR has been set.
  558. * allow a last cycle in the monitor.
  559. * the monitor will indicate that it has
  560. * finished by clearing this bit.
  561. */
  562. DEBUGP(5, dev, "Now allow last cycle of monitor!\n");
  563. while (test_bit(LOCK_MONITOR, (void *)&dev->flags))
  564. msleep(25);
  565. DEBUGP(5, dev, "Delete timer\n");
  566. del_timer_sync(&dev->timer);
  567. #ifdef PCMCIA_DEBUG
  568. dev->monitor_running = 0;
  569. #endif
  570. DEBUGP(3, dev, "<- terminate_monitor\n");
  571. }
  572. /*
  573. * monitor the card every 50msec. as a side-effect, retrieve the
  574. * atr once a card is inserted. another side-effect of retrieving the
  575. * atr is that the card will be powered on, so there is no need to
  576. * power on the card explictely from the application: the driver
  577. * is already doing that for you.
  578. */
  579. static void monitor_card(unsigned long p)
  580. {
  581. struct cm4000_dev *dev = (struct cm4000_dev *) p;
  582. ioaddr_t iobase = dev->p_dev->io.BasePort1;
  583. unsigned short s;
  584. struct ptsreq ptsreq;
  585. int i, atrc;
  586. DEBUGP(7, dev, "-> monitor_card\n");
  587. /* if someone has set the lock for us: we're done! */
  588. if (test_and_set_bit(LOCK_MONITOR, &dev->flags)) {
  589. DEBUGP(4, dev, "About to stop monitor\n");
  590. /* no */
  591. dev->rlen =
  592. dev->rpos =
  593. dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
  594. dev->mstate = M_FETCH_ATR;
  595. clear_bit(LOCK_MONITOR, &dev->flags);
  596. /* close et al. are sleeping on devq, so wake it */
  597. wake_up_interruptible(&dev->devq);
  598. DEBUGP(2, dev, "<- monitor_card (we are done now)\n");
  599. return;
  600. }
  601. /* try to lock io: if it is already locked, just add another timer */
  602. if (test_and_set_bit(LOCK_IO, (void *)&dev->flags)) {
  603. DEBUGP(4, dev, "Couldn't get IO lock\n");
  604. goto return_with_timer;
  605. }
  606. /* is a card/a reader inserted at all ? */
  607. dev->flags0 = xinb(REG_FLAGS0(iobase));
  608. DEBUGP(7, dev, "dev->flags0 = 0x%2x\n", dev->flags0);
  609. DEBUGP(7, dev, "smartcard present: %s\n",
  610. dev->flags0 & 1 ? "yes" : "no");
  611. DEBUGP(7, dev, "cardman present: %s\n",
  612. dev->flags0 == 0xff ? "no" : "yes");
  613. if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
  614. || dev->flags0 == 0xff) { /* no cardman inserted */
  615. /* no */
  616. dev->rlen =
  617. dev->rpos =
  618. dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
  619. dev->mstate = M_FETCH_ATR;
  620. dev->flags &= 0x000000ff; /* only keep IO and MONITOR locks */
  621. if (dev->flags0 == 0xff) {
  622. DEBUGP(4, dev, "set IS_CMM_ABSENT bit\n");
  623. set_bit(IS_CMM_ABSENT, &dev->flags);
  624. } else if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
  625. DEBUGP(4, dev, "clear IS_CMM_ABSENT bit "
  626. "(card is removed)\n");
  627. clear_bit(IS_CMM_ABSENT, &dev->flags);
  628. }
  629. goto release_io;
  630. } else if ((dev->flags0 & 1) && test_bit(IS_CMM_ABSENT, &dev->flags)) {
  631. /* cardman and card present but cardman was absent before
  632. * (after suspend with inserted card) */
  633. DEBUGP(4, dev, "clear IS_CMM_ABSENT bit (card is inserted)\n");
  634. clear_bit(IS_CMM_ABSENT, &dev->flags);
  635. }
  636. if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
  637. DEBUGP(7, dev, "believe ATR is already valid (do nothing)\n");
  638. goto release_io;
  639. }
  640. switch (dev->mstate) {
  641. unsigned char flags0;
  642. case M_CARDOFF:
  643. DEBUGP(4, dev, "M_CARDOFF\n");
  644. flags0 = inb(REG_FLAGS0(iobase));
  645. if (flags0 & 0x02) {
  646. /* wait until Flags0 indicate power is off */
  647. dev->mdelay = T_10MSEC;
  648. } else {
  649. /* Flags0 indicate power off and no card inserted now;
  650. * Reset CARDMAN CONTROLLER */
  651. xoutb(0x80, REG_FLAGS0(iobase));
  652. /* prepare for fetching ATR again: after card off ATR
  653. * is read again automatically */
  654. dev->rlen =
  655. dev->rpos =
  656. dev->atr_csum =
  657. dev->atr_len_retry = dev->cwarn = 0;
  658. dev->mstate = M_FETCH_ATR;
  659. /* minimal gap between CARDOFF and read ATR is 50msec */
  660. dev->mdelay = T_50MSEC;
  661. }
  662. break;
  663. case M_FETCH_ATR:
  664. DEBUGP(4, dev, "M_FETCH_ATR\n");
  665. xoutb(0x80, REG_FLAGS0(iobase));
  666. DEBUGP(4, dev, "Reset BAUDV to 9600\n");
  667. dev->baudv = 0x173; /* 9600 */
  668. xoutb(0x02, REG_STOPBITS(iobase)); /* stopbits=2 */
  669. xoutb(0x73, REG_BAUDRATE(iobase)); /* baud value */
  670. xoutb(0x21, REG_FLAGS1(iobase)); /* T_Active=1, baud
  671. value */
  672. /* warm start vs. power on: */
  673. xoutb(dev->flags0 & 2 ? 0x46 : 0x44, REG_FLAGS0(iobase));
  674. dev->mdelay = T_40MSEC;
  675. dev->mstate = M_TIMEOUT_WAIT;
  676. break;
  677. case M_TIMEOUT_WAIT:
  678. DEBUGP(4, dev, "M_TIMEOUT_WAIT\n");
  679. /* numRecBytes */
  680. io_read_num_rec_bytes(iobase, &dev->atr_len);
  681. dev->mdelay = T_10MSEC;
  682. dev->mstate = M_READ_ATR_LEN;
  683. break;
  684. case M_READ_ATR_LEN:
  685. DEBUGP(4, dev, "M_READ_ATR_LEN\n");
  686. /* infinite loop possible, since there is no timeout */
  687. #define MAX_ATR_LEN_RETRY 100
  688. if (dev->atr_len == io_read_num_rec_bytes(iobase, &s)) {
  689. if (dev->atr_len_retry++ >= MAX_ATR_LEN_RETRY) { /* + XX msec */
  690. dev->mdelay = T_10MSEC;
  691. dev->mstate = M_READ_ATR;
  692. }
  693. } else {
  694. dev->atr_len = s;
  695. dev->atr_len_retry = 0; /* set new timeout */
  696. }
  697. DEBUGP(4, dev, "Current ATR_LEN = %i\n", dev->atr_len);
  698. break;
  699. case M_READ_ATR:
  700. DEBUGP(4, dev, "M_READ_ATR\n");
  701. xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
  702. for (i = 0; i < dev->atr_len; i++) {
  703. xoutb(i, REG_BUF_ADDR(iobase));
  704. dev->atr[i] = inb(REG_BUF_DATA(iobase));
  705. }
  706. /* Deactivate T_Active flags */
  707. DEBUGP(4, dev, "Deactivate T_Active flags\n");
  708. dev->flags1 = 0x01;
  709. xoutb(dev->flags1, REG_FLAGS1(iobase));
  710. /* atr is present (which doesnt mean it's valid) */
  711. set_bit(IS_ATR_PRESENT, &dev->flags);
  712. if (dev->atr[0] == 0x03)
  713. str_invert_revert(dev->atr, dev->atr_len);
  714. atrc = parse_atr(dev);
  715. if (atrc == 0) { /* atr invalid */
  716. dev->mdelay = 0;
  717. dev->mstate = M_BAD_CARD;
  718. } else {
  719. dev->mdelay = T_50MSEC;
  720. dev->mstate = M_ATR_PRESENT;
  721. set_bit(IS_ATR_VALID, &dev->flags);
  722. }
  723. if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
  724. DEBUGP(4, dev, "monitor_card: ATR valid\n");
  725. /* if ta1 == 0x11, no PPS necessary (default values) */
  726. /* do not do PPS with multi protocol cards */
  727. if ((test_bit(IS_AUTOPPS_ACT, &dev->flags) == 0) &&
  728. (dev->ta1 != 0x11) &&
  729. !(test_bit(IS_ANY_T0, &dev->flags) &&
  730. test_bit(IS_ANY_T1, &dev->flags))) {
  731. DEBUGP(4, dev, "Perform AUTOPPS\n");
  732. set_bit(IS_AUTOPPS_ACT, &dev->flags);
  733. ptsreq.protocol = ptsreq.protocol =
  734. (0x01 << dev->proto);
  735. ptsreq.flags = 0x01;
  736. ptsreq.pts1 = 0x00;
  737. ptsreq.pts2 = 0x00;
  738. ptsreq.pts3 = 0x00;
  739. if (set_protocol(dev, &ptsreq) == 0) {
  740. DEBUGP(4, dev, "AUTOPPS ret SUCC\n");
  741. clear_bit(IS_AUTOPPS_ACT, &dev->flags);
  742. wake_up_interruptible(&dev->atrq);
  743. } else {
  744. DEBUGP(4, dev, "AUTOPPS failed: "
  745. "repower using defaults\n");
  746. /* prepare for repowering */
  747. clear_bit(IS_ATR_PRESENT, &dev->flags);
  748. clear_bit(IS_ATR_VALID, &dev->flags);
  749. dev->rlen =
  750. dev->rpos =
  751. dev->atr_csum =
  752. dev->atr_len_retry = dev->cwarn = 0;
  753. dev->mstate = M_FETCH_ATR;
  754. dev->mdelay = T_50MSEC;
  755. }
  756. } else {
  757. /* for cards which use slightly different
  758. * params (extra guard time) */
  759. set_cardparameter(dev);
  760. if (test_bit(IS_AUTOPPS_ACT, &dev->flags) == 1)
  761. DEBUGP(4, dev, "AUTOPPS already active "
  762. "2nd try:use default values\n");
  763. if (dev->ta1 == 0x11)
  764. DEBUGP(4, dev, "No AUTOPPS necessary "
  765. "TA(1)==0x11\n");
  766. if (test_bit(IS_ANY_T0, &dev->flags)
  767. && test_bit(IS_ANY_T1, &dev->flags))
  768. DEBUGP(4, dev, "Do NOT perform AUTOPPS "
  769. "with multiprotocol cards\n");
  770. clear_bit(IS_AUTOPPS_ACT, &dev->flags);
  771. wake_up_interruptible(&dev->atrq);
  772. }
  773. } else {
  774. DEBUGP(4, dev, "ATR invalid\n");
  775. wake_up_interruptible(&dev->atrq);
  776. }
  777. break;
  778. case M_BAD_CARD:
  779. DEBUGP(4, dev, "M_BAD_CARD\n");
  780. /* slow down warning, but prompt immediately after insertion */
  781. if (dev->cwarn == 0 || dev->cwarn == 10) {
  782. set_bit(IS_BAD_CARD, &dev->flags);
  783. printk(KERN_WARNING MODULE_NAME ": device %s: ",
  784. dev->node.dev_name);
  785. if (test_bit(IS_BAD_CSUM, &dev->flags)) {
  786. DEBUGP(4, dev, "ATR checksum (0x%.2x, should "
  787. "be zero) failed\n", dev->atr_csum);
  788. }
  789. #ifdef PCMCIA_DEBUG
  790. else if (test_bit(IS_BAD_LENGTH, &dev->flags)) {
  791. DEBUGP(4, dev, "ATR length error\n");
  792. } else {
  793. DEBUGP(4, dev, "card damaged or wrong way "
  794. "inserted\n");
  795. }
  796. #endif
  797. dev->cwarn = 0;
  798. wake_up_interruptible(&dev->atrq); /* wake open */
  799. }
  800. dev->cwarn++;
  801. dev->mdelay = T_100MSEC;
  802. dev->mstate = M_FETCH_ATR;
  803. break;
  804. default:
  805. DEBUGP(7, dev, "Unknown action\n");
  806. break; /* nothing */
  807. }
  808. release_io:
  809. DEBUGP(7, dev, "release_io\n");
  810. clear_bit(LOCK_IO, &dev->flags);
  811. wake_up_interruptible(&dev->ioq); /* whoever needs IO */
  812. return_with_timer:
  813. DEBUGP(7, dev, "<- monitor_card (returns with timer)\n");
  814. mod_timer(&dev->timer, jiffies + dev->mdelay);
  815. clear_bit(LOCK_MONITOR, &dev->flags);
  816. }
  817. /* Interface to userland (file_operations) */
  818. static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
  819. loff_t *ppos)
  820. {
  821. struct cm4000_dev *dev = filp->private_data;
  822. ioaddr_t iobase = dev->p_dev->io.BasePort1;
  823. ssize_t rc;
  824. int i, j, k;
  825. DEBUGP(2, dev, "-> cmm_read(%s,%d)\n", current->comm, current->pid);
  826. if (count == 0) /* according to manpage */
  827. return 0;
  828. if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
  829. test_bit(IS_CMM_ABSENT, &dev->flags))
  830. return -ENODEV;
  831. if (test_bit(IS_BAD_CSUM, &dev->flags))
  832. return -EIO;
  833. /* also see the note about this in cmm_write */
  834. if (wait_event_interruptible
  835. (dev->atrq,
  836. ((filp->f_flags & O_NONBLOCK)
  837. || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
  838. if (filp->f_flags & O_NONBLOCK)
  839. return -EAGAIN;
  840. return -ERESTARTSYS;
  841. }
  842. if (test_bit(IS_ATR_VALID, &dev->flags) == 0)
  843. return -EIO;
  844. /* this one implements blocking IO */
  845. if (wait_event_interruptible
  846. (dev->readq,
  847. ((filp->f_flags & O_NONBLOCK) || (dev->rpos < dev->rlen)))) {
  848. if (filp->f_flags & O_NONBLOCK)
  849. return -EAGAIN;
  850. return -ERESTARTSYS;
  851. }
  852. /* lock io */
  853. if (wait_event_interruptible
  854. (dev->ioq,
  855. ((filp->f_flags & O_NONBLOCK)
  856. || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
  857. if (filp->f_flags & O_NONBLOCK)
  858. return -EAGAIN;
  859. return -ERESTARTSYS;
  860. }
  861. rc = 0;
  862. dev->flags0 = inb(REG_FLAGS0(iobase));
  863. if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
  864. || dev->flags0 == 0xff) { /* no cardman inserted */
  865. clear_bit(IS_ATR_VALID, &dev->flags);
  866. if (dev->flags0 & 1) {
  867. set_bit(IS_CMM_ABSENT, &dev->flags);
  868. rc = -ENODEV;
  869. }
  870. rc = -EIO;
  871. goto release_io;
  872. }
  873. DEBUGP(4, dev, "begin read answer\n");
  874. j = min(count, (size_t)(dev->rlen - dev->rpos));
  875. k = dev->rpos;
  876. if (k + j > 255)
  877. j = 256 - k;
  878. DEBUGP(4, dev, "read1 j=%d\n", j);
  879. for (i = 0; i < j; i++) {
  880. xoutb(k++, REG_BUF_ADDR(iobase));
  881. dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
  882. }
  883. j = min(count, (size_t)(dev->rlen - dev->rpos));
  884. if (k + j > 255) {
  885. DEBUGP(4, dev, "read2 j=%d\n", j);
  886. dev->flags1 |= 0x10; /* MSB buf addr set */
  887. xoutb(dev->flags1, REG_FLAGS1(iobase));
  888. for (; i < j; i++) {
  889. xoutb(k++, REG_BUF_ADDR(iobase));
  890. dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
  891. }
  892. }
  893. if (dev->proto == 0 && count > dev->rlen - dev->rpos) {
  894. DEBUGP(4, dev, "T=0 and count > buffer\n");
  895. dev->rbuf[i] = dev->rbuf[i - 1];
  896. dev->rbuf[i - 1] = dev->procbyte;
  897. j++;
  898. }
  899. count = j;
  900. dev->rpos = dev->rlen + 1;
  901. /* Clear T1Active */
  902. DEBUGP(4, dev, "Clear T1Active\n");
  903. dev->flags1 &= 0xdf;
  904. xoutb(dev->flags1, REG_FLAGS1(iobase));
  905. xoutb(0, REG_FLAGS1(iobase)); /* clear detectCMM */
  906. /* last check before exit */
  907. if (!io_detect_cm4000(iobase, dev))
  908. count = -ENODEV;
  909. if (test_bit(IS_INVREV, &dev->flags) && count > 0)
  910. str_invert_revert(dev->rbuf, count);
  911. if (copy_to_user(buf, dev->rbuf, count))
  912. return -EFAULT;
  913. release_io:
  914. clear_bit(LOCK_IO, &dev->flags);
  915. wake_up_interruptible(&dev->ioq);
  916. DEBUGP(2, dev, "<- cmm_read returns: rc = %Zi\n",
  917. (rc < 0 ? rc : count));
  918. return rc < 0 ? rc : count;
  919. }
  920. static ssize_t cmm_write(struct file *filp, const char __user *buf,
  921. size_t count, loff_t *ppos)
  922. {
  923. struct cm4000_dev *dev = (struct cm4000_dev *) filp->private_data;
  924. ioaddr_t iobase = dev->p_dev->io.BasePort1;
  925. unsigned short s;
  926. unsigned char tmp;
  927. unsigned char infolen;
  928. unsigned char sendT0;
  929. unsigned short nsend;
  930. unsigned short nr;
  931. ssize_t rc;
  932. int i;
  933. DEBUGP(2, dev, "-> cmm_write(%s,%d)\n", current->comm, current->pid);
  934. if (count == 0) /* according to manpage */
  935. return 0;
  936. if (dev->proto == 0 && count < 4) {
  937. /* T0 must have at least 4 bytes */
  938. DEBUGP(4, dev, "T0 short write\n");
  939. return -EIO;
  940. }
  941. nr = count & 0x1ff; /* max bytes to write */
  942. sendT0 = dev->proto ? 0 : nr > 5 ? 0x08 : 0;
  943. if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
  944. test_bit(IS_CMM_ABSENT, &dev->flags))
  945. return -ENODEV;
  946. if (test_bit(IS_BAD_CSUM, &dev->flags)) {
  947. DEBUGP(4, dev, "bad csum\n");
  948. return -EIO;
  949. }
  950. /*
  951. * wait for atr to become valid.
  952. * note: it is important to lock this code. if we dont, the monitor
  953. * could be run between test_bit and the call to sleep on the
  954. * atr-queue. if *then* the monitor detects atr valid, it will wake up
  955. * any process on the atr-queue, *but* since we have been interrupted,
  956. * we do not yet sleep on this queue. this would result in a missed
  957. * wake_up and the calling process would sleep forever (until
  958. * interrupted). also, do *not* restore_flags before sleep_on, because
  959. * this could result in the same situation!
  960. */
  961. if (wait_event_interruptible
  962. (dev->atrq,
  963. ((filp->f_flags & O_NONBLOCK)
  964. || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
  965. if (filp->f_flags & O_NONBLOCK)
  966. return -EAGAIN;
  967. return -ERESTARTSYS;
  968. }
  969. if (test_bit(IS_ATR_VALID, &dev->flags) == 0) { /* invalid atr */
  970. DEBUGP(4, dev, "invalid ATR\n");
  971. return -EIO;
  972. }
  973. /* lock io */
  974. if (wait_event_interruptible
  975. (dev->ioq,
  976. ((filp->f_flags & O_NONBLOCK)
  977. || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
  978. if (filp->f_flags & O_NONBLOCK)
  979. return -EAGAIN;
  980. return -ERESTARTSYS;
  981. }
  982. if (copy_from_user(dev->sbuf, buf, ((count > 512) ? 512 : count)))
  983. return -EFAULT;
  984. rc = 0;
  985. dev->flags0 = inb(REG_FLAGS0(iobase));
  986. if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
  987. || dev->flags0 == 0xff) { /* no cardman inserted */
  988. clear_bit(IS_ATR_VALID, &dev->flags);
  989. if (dev->flags0 & 1) {
  990. set_bit(IS_CMM_ABSENT, &dev->flags);
  991. rc = -ENODEV;
  992. } else {
  993. DEBUGP(4, dev, "IO error\n");
  994. rc = -EIO;
  995. }
  996. goto release_io;
  997. }
  998. xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
  999. if (!io_detect_cm4000(iobase, dev)) {
  1000. rc = -ENODEV;
  1001. goto release_io;
  1002. }
  1003. /* reflect T=0 send/read mode in flags1 */
  1004. dev->flags1 |= (sendT0);
  1005. set_cardparameter(dev);
  1006. /* dummy read, reset flag procedure received */
  1007. tmp = inb(REG_FLAGS1(iobase));
  1008. dev->flags1 = 0x20 /* T_Active */
  1009. | (sendT0)
  1010. | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)/* inverse parity */
  1011. | (((dev->baudv - 1) & 0x0100) >> 8); /* MSB-Baud */
  1012. DEBUGP(1, dev, "set dev->flags1 = 0x%.2x\n", dev->flags1);
  1013. xoutb(dev->flags1, REG_FLAGS1(iobase));
  1014. /* xmit data */
  1015. DEBUGP(4, dev, "Xmit data\n");
  1016. for (i = 0; i < nr; i++) {
  1017. if (i >= 256) {
  1018. dev->flags1 = 0x20 /* T_Active */
  1019. | (sendT0) /* SendT0 */
  1020. /* inverse parity: */
  1021. | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)
  1022. | (((dev->baudv - 1) & 0x0100) >> 8) /* MSB-Baud */
  1023. | 0x10; /* set address high */
  1024. DEBUGP(4, dev, "dev->flags = 0x%.2x - set address "
  1025. "high\n", dev->flags1);
  1026. xoutb(dev->flags1, REG_FLAGS1(iobase));
  1027. }
  1028. if (test_bit(IS_INVREV, &dev->flags)) {
  1029. DEBUGP(4, dev, "Apply inverse convention for 0x%.2x "
  1030. "-> 0x%.2x\n", (unsigned char)dev->sbuf[i],
  1031. invert_revert(dev->sbuf[i]));
  1032. xoutb(i, REG_BUF_ADDR(iobase));
  1033. xoutb(invert_revert(dev->sbuf[i]),
  1034. REG_BUF_DATA(iobase));
  1035. } else {
  1036. xoutb(i, REG_BUF_ADDR(iobase));
  1037. xoutb(dev->sbuf[i], REG_BUF_DATA(iobase));
  1038. }
  1039. }
  1040. DEBUGP(4, dev, "Xmit done\n");
  1041. if (dev->proto == 0) {
  1042. /* T=0 proto: 0 byte reply */
  1043. if (nr == 4) {
  1044. DEBUGP(4, dev, "T=0 assumes 0 byte reply\n");
  1045. xoutb(i, REG_BUF_ADDR(iobase));
  1046. if (test_bit(IS_INVREV, &dev->flags))
  1047. xoutb(0xff, REG_BUF_DATA(iobase));
  1048. else
  1049. xoutb(0x00, REG_BUF_DATA(iobase));
  1050. }
  1051. /* numSendBytes */
  1052. if (sendT0)
  1053. nsend = nr;
  1054. else {
  1055. if (nr == 4)
  1056. nsend = 5;
  1057. else {
  1058. nsend = 5 + (unsigned char)dev->sbuf[4];
  1059. if (dev->sbuf[4] == 0)
  1060. nsend += 0x100;
  1061. }
  1062. }
  1063. } else
  1064. nsend = nr;
  1065. /* T0: output procedure byte */
  1066. if (test_bit(IS_INVREV, &dev->flags)) {
  1067. DEBUGP(4, dev, "T=0 set Procedure byte (inverse-reverse) "
  1068. "0x%.2x\n", invert_revert(dev->sbuf[1]));
  1069. xoutb(invert_revert(dev->sbuf[1]), REG_NUM_BYTES(iobase));
  1070. } else {
  1071. DEBUGP(4, dev, "T=0 set Procedure byte 0x%.2x\n", dev->sbuf[1]);
  1072. xoutb(dev->sbuf[1], REG_NUM_BYTES(iobase));
  1073. }
  1074. DEBUGP(1, dev, "set NumSendBytes = 0x%.2x\n",
  1075. (unsigned char)(nsend & 0xff));
  1076. xoutb((unsigned char)(nsend & 0xff), REG_NUM_SEND(iobase));
  1077. DEBUGP(1, dev, "Trigger CARDMAN CONTROLLER (0x%.2x)\n",
  1078. 0x40 /* SM_Active */
  1079. | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */
  1080. |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */
  1081. |(nsend & 0x100) >> 8 /* MSB numSendBytes */ );
  1082. xoutb(0x40 /* SM_Active */
  1083. | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */
  1084. |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */
  1085. |(nsend & 0x100) >> 8, /* MSB numSendBytes */
  1086. REG_FLAGS0(iobase));
  1087. /* wait for xmit done */
  1088. if (dev->proto == 1) {
  1089. DEBUGP(4, dev, "Wait for xmit done\n");
  1090. for (i = 0; i < 1000; i++) {
  1091. if (inb(REG_FLAGS0(iobase)) & 0x08)
  1092. break;
  1093. msleep_interruptible(10);
  1094. }
  1095. if (i == 1000) {
  1096. DEBUGP(4, dev, "timeout waiting for xmit done\n");
  1097. rc = -EIO;
  1098. goto release_io;
  1099. }
  1100. }
  1101. /* T=1: wait for infoLen */
  1102. infolen = 0;
  1103. if (dev->proto) {
  1104. /* wait until infoLen is valid */
  1105. for (i = 0; i < 6000; i++) { /* max waiting time of 1 min */
  1106. io_read_num_rec_bytes(iobase, &s);
  1107. if (s >= 3) {
  1108. infolen = inb(REG_FLAGS1(iobase));
  1109. DEBUGP(4, dev, "infolen=%d\n", infolen);
  1110. break;
  1111. }
  1112. msleep_interruptible(10);
  1113. }
  1114. if (i == 6000) {
  1115. DEBUGP(4, dev, "timeout waiting for infoLen\n");
  1116. rc = -EIO;
  1117. goto release_io;
  1118. }
  1119. } else
  1120. clear_bit(IS_PROCBYTE_PRESENT, &dev->flags);
  1121. /* numRecBytes | bit9 of numRecytes */
  1122. io_read_num_rec_bytes(iobase, &dev->rlen);
  1123. for (i = 0; i < 600; i++) { /* max waiting time of 2 sec */
  1124. if (dev->proto) {
  1125. if (dev->rlen >= infolen + 4)
  1126. break;
  1127. }
  1128. msleep_interruptible(10);
  1129. /* numRecBytes | bit9 of numRecytes */
  1130. io_read_num_rec_bytes(iobase, &s);
  1131. if (s > dev->rlen) {
  1132. DEBUGP(1, dev, "NumRecBytes inc (reset timeout)\n");
  1133. i = 0; /* reset timeout */
  1134. dev->rlen = s;
  1135. }
  1136. /* T=0: we are done when numRecBytes doesn't
  1137. * increment any more and NoProcedureByte
  1138. * is set and numRecBytes == bytes sent + 6
  1139. * (header bytes + data + 1 for sw2)
  1140. * except when the card replies an error
  1141. * which means, no data will be sent back.
  1142. */
  1143. else if (dev->proto == 0) {
  1144. if ((inb(REG_BUF_ADDR(iobase)) & 0x80)) {
  1145. /* no procedure byte received since last read */
  1146. DEBUGP(1, dev, "NoProcedure byte set\n");
  1147. /* i=0; */
  1148. } else {
  1149. /* procedure byte received since last read */
  1150. DEBUGP(1, dev, "NoProcedure byte unset "
  1151. "(reset timeout)\n");
  1152. dev->procbyte = inb(REG_FLAGS1(iobase));
  1153. DEBUGP(1, dev, "Read procedure byte 0x%.2x\n",
  1154. dev->procbyte);
  1155. i = 0; /* resettimeout */
  1156. }
  1157. if (inb(REG_FLAGS0(iobase)) & 0x08) {
  1158. DEBUGP(1, dev, "T0Done flag (read reply)\n");
  1159. break;
  1160. }
  1161. }
  1162. if (dev->proto)
  1163. infolen = inb(REG_FLAGS1(iobase));
  1164. }
  1165. if (i == 600) {
  1166. DEBUGP(1, dev, "timeout waiting for numRecBytes\n");
  1167. rc = -EIO;
  1168. goto release_io;
  1169. } else {
  1170. if (dev->proto == 0) {
  1171. DEBUGP(1, dev, "Wait for T0Done bit to be set\n");
  1172. for (i = 0; i < 1000; i++) {
  1173. if (inb(REG_FLAGS0(iobase)) & 0x08)
  1174. break;
  1175. msleep_interruptible(10);
  1176. }
  1177. if (i == 1000) {
  1178. DEBUGP(1, dev, "timeout waiting for T0Done\n");
  1179. rc = -EIO;
  1180. goto release_io;
  1181. }
  1182. dev->procbyte = inb(REG_FLAGS1(iobase));
  1183. DEBUGP(4, dev, "Read procedure byte 0x%.2x\n",
  1184. dev->procbyte);
  1185. io_read_num_rec_bytes(iobase, &dev->rlen);
  1186. DEBUGP(4, dev, "Read NumRecBytes = %i\n", dev->rlen);
  1187. }
  1188. }
  1189. /* T=1: read offset=zero, T=0: read offset=after challenge */
  1190. dev->rpos = dev->proto ? 0 : nr == 4 ? 5 : nr > dev->rlen ? 5 : nr;
  1191. DEBUGP(4, dev, "dev->rlen = %i, dev->rpos = %i, nr = %i\n",
  1192. dev->rlen, dev->rpos, nr);
  1193. release_io:
  1194. DEBUGP(4, dev, "Reset SM\n");
  1195. xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
  1196. if (rc < 0) {
  1197. DEBUGP(4, dev, "Write failed but clear T_Active\n");
  1198. dev->flags1 &= 0xdf;
  1199. xoutb(dev->flags1, REG_FLAGS1(iobase));
  1200. }
  1201. clear_bit(LOCK_IO, &dev->flags);
  1202. wake_up_interruptible(&dev->ioq);
  1203. wake_up_interruptible(&dev->readq); /* tell read we have data */
  1204. /* ITSEC E2: clear write buffer */
  1205. memset((char *)dev->sbuf, 0, 512);
  1206. /* return error or actually written bytes */
  1207. DEBUGP(2, dev, "<- cmm_write\n");
  1208. return rc < 0 ? rc : nr;
  1209. }
  1210. static void start_monitor(struct cm4000_dev *dev)
  1211. {
  1212. DEBUGP(3, dev, "-> start_monitor\n");
  1213. if (!dev->monitor_running) {
  1214. DEBUGP(5, dev, "create, init and add timer\n");
  1215. setup_timer(&dev->timer, monitor_card, (unsigned long)dev);
  1216. dev->monitor_running = 1;
  1217. mod_timer(&dev->timer, jiffies);
  1218. } else
  1219. DEBUGP(5, dev, "monitor already running\n");
  1220. DEBUGP(3, dev, "<- start_monitor\n");
  1221. }
  1222. static void stop_monitor(struct cm4000_dev *dev)
  1223. {
  1224. DEBUGP(3, dev, "-> stop_monitor\n");
  1225. if (dev->monitor_running) {
  1226. DEBUGP(5, dev, "stopping monitor\n");
  1227. terminate_monitor(dev);
  1228. /* reset monitor SM */
  1229. clear_bit(IS_ATR_VALID, &dev->flags);
  1230. clear_bit(IS_ATR_PRESENT, &dev->flags);
  1231. } else
  1232. DEBUGP(5, dev, "monitor already stopped\n");
  1233. DEBUGP(3, dev, "<- stop_monitor\n");
  1234. }
  1235. static int cmm_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  1236. unsigned long arg)
  1237. {
  1238. struct cm4000_dev *dev = filp->private_data;
  1239. ioaddr_t iobase = dev->p_dev->io.BasePort1;
  1240. struct pcmcia_device *link;
  1241. int size;
  1242. int rc;
  1243. void __user *argp = (void __user *)arg;
  1244. #ifdef PCMCIA_DEBUG
  1245. char *ioctl_names[CM_IOC_MAXNR + 1] = {
  1246. [_IOC_NR(CM_IOCGSTATUS)] "CM_IOCGSTATUS",
  1247. [_IOC_NR(CM_IOCGATR)] "CM_IOCGATR",
  1248. [_IOC_NR(CM_IOCARDOFF)] "CM_IOCARDOFF",
  1249. [_IOC_NR(CM_IOCSPTS)] "CM_IOCSPTS",
  1250. [_IOC_NR(CM_IOSDBGLVL)] "CM4000_DBGLVL",
  1251. };
  1252. #endif
  1253. DEBUGP(3, dev, "cmm_ioctl(device=%d.%d) %s\n", imajor(inode),
  1254. iminor(inode), ioctl_names[_IOC_NR(cmd)]);
  1255. link = dev_table[iminor(inode)];
  1256. if (!pcmcia_dev_present(link)) {
  1257. DEBUGP(4, dev, "DEV_OK false\n");
  1258. return -ENODEV;
  1259. }
  1260. if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
  1261. DEBUGP(4, dev, "CMM_ABSENT flag set\n");
  1262. return -ENODEV;
  1263. }
  1264. if (_IOC_TYPE(cmd) != CM_IOC_MAGIC) {
  1265. DEBUGP(4, dev, "ioctype mismatch\n");
  1266. return -EINVAL;
  1267. }
  1268. if (_IOC_NR(cmd) > CM_IOC_MAXNR) {
  1269. DEBUGP(4, dev, "iocnr mismatch\n");
  1270. return -EINVAL;
  1271. }
  1272. size = _IOC_SIZE(cmd);
  1273. rc = 0;
  1274. DEBUGP(4, dev, "iocdir=%.4x iocr=%.4x iocw=%.4x iocsize=%d cmd=%.4x\n",
  1275. _IOC_DIR(cmd), _IOC_READ, _IOC_WRITE, size, cmd);
  1276. if (_IOC_DIR(cmd) & _IOC_READ) {
  1277. if (!access_ok(VERIFY_WRITE, argp, size))
  1278. return -EFAULT;
  1279. }
  1280. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  1281. if (!access_ok(VERIFY_READ, argp, size))
  1282. return -EFAULT;
  1283. }
  1284. switch (cmd) {
  1285. case CM_IOCGSTATUS:
  1286. DEBUGP(4, dev, " ... in CM_IOCGSTATUS\n");
  1287. {
  1288. int status;
  1289. /* clear other bits, but leave inserted & powered as
  1290. * they are */
  1291. status = dev->flags0 & 3;
  1292. if (test_bit(IS_ATR_PRESENT, &dev->flags))
  1293. status |= CM_ATR_PRESENT;
  1294. if (test_bit(IS_ATR_VALID, &dev->flags))
  1295. status |= CM_ATR_VALID;
  1296. if (test_bit(IS_CMM_ABSENT, &dev->flags))
  1297. status |= CM_NO_READER;
  1298. if (test_bit(IS_BAD_CARD, &dev->flags))
  1299. status |= CM_BAD_CARD;
  1300. if (copy_to_user(argp, &status, sizeof(int)))
  1301. return -EFAULT;
  1302. }
  1303. return 0;
  1304. case CM_IOCGATR:
  1305. DEBUGP(4, dev, "... in CM_IOCGATR\n");
  1306. {
  1307. struct atreq __user *atreq = argp;
  1308. int tmp;
  1309. /* allow nonblocking io and being interrupted */
  1310. if (wait_event_interruptible
  1311. (dev->atrq,
  1312. ((filp->f_flags & O_NONBLOCK)
  1313. || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
  1314. != 0)))) {
  1315. if (filp->f_flags & O_NONBLOCK)
  1316. return -EAGAIN;
  1317. return -ERESTARTSYS;
  1318. }
  1319. if (test_bit(IS_ATR_VALID, &dev->flags) == 0) {
  1320. tmp = -1;
  1321. if (copy_to_user(&(atreq->atr_len), &tmp,
  1322. sizeof(int)))
  1323. return -EFAULT;
  1324. } else {
  1325. if (copy_to_user(atreq->atr, dev->atr,
  1326. dev->atr_len))
  1327. return -EFAULT;
  1328. tmp = dev->atr_len;
  1329. if (copy_to_user(&(atreq->atr_len), &tmp, sizeof(int)))
  1330. return -EFAULT;
  1331. }
  1332. return 0;
  1333. }
  1334. case CM_IOCARDOFF:
  1335. #ifdef PCMCIA_DEBUG
  1336. DEBUGP(4, dev, "... in CM_IOCARDOFF\n");
  1337. if (dev->flags0 & 0x01) {
  1338. DEBUGP(4, dev, " Card inserted\n");
  1339. } else {
  1340. DEBUGP(2, dev, " No card inserted\n");
  1341. }
  1342. if (dev->flags0 & 0x02) {
  1343. DEBUGP(4, dev, " Card powered\n");
  1344. } else {
  1345. DEBUGP(2, dev, " Card not powered\n");
  1346. }
  1347. #endif
  1348. /* is a card inserted and powered? */
  1349. if ((dev->flags0 & 0x01) && (dev->flags0 & 0x02)) {
  1350. /* get IO lock */
  1351. if (wait_event_interruptible
  1352. (dev->ioq,
  1353. ((filp->f_flags & O_NONBLOCK)
  1354. || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
  1355. == 0)))) {
  1356. if (filp->f_flags & O_NONBLOCK)
  1357. return -EAGAIN;
  1358. return -ERESTARTSYS;
  1359. }
  1360. /* Set Flags0 = 0x42 */
  1361. DEBUGP(4, dev, "Set Flags0=0x42 \n");
  1362. xoutb(0x42, REG_FLAGS0(iobase));
  1363. clear_bit(IS_ATR_PRESENT, &dev->flags);
  1364. clear_bit(IS_ATR_VALID, &dev->flags);
  1365. dev->mstate = M_CARDOFF;
  1366. clear_bit(LOCK_IO, &dev->flags);
  1367. if (wait_event_interruptible
  1368. (dev->atrq,
  1369. ((filp->f_flags & O_NONBLOCK)
  1370. || (test_bit(IS_ATR_VALID, (void *)&dev->flags) !=
  1371. 0)))) {
  1372. if (filp->f_flags & O_NONBLOCK)
  1373. return -EAGAIN;
  1374. return -ERESTARTSYS;
  1375. }
  1376. }
  1377. /* release lock */
  1378. clear_bit(LOCK_IO, &dev->flags);
  1379. wake_up_interruptible(&dev->ioq);
  1380. return 0;
  1381. case CM_IOCSPTS:
  1382. {
  1383. struct ptsreq krnptsreq;
  1384. if (copy_from_user(&krnptsreq, argp,
  1385. sizeof(struct ptsreq)))
  1386. return -EFAULT;
  1387. rc = 0;
  1388. DEBUGP(4, dev, "... in CM_IOCSPTS\n");
  1389. /* wait for ATR to get valid */
  1390. if (wait_event_interruptible
  1391. (dev->atrq,
  1392. ((filp->f_flags & O_NONBLOCK)
  1393. || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
  1394. != 0)))) {
  1395. if (filp->f_flags & O_NONBLOCK)
  1396. return -EAGAIN;
  1397. return -ERESTARTSYS;
  1398. }
  1399. /* get IO lock */
  1400. if (wait_event_interruptible
  1401. (dev->ioq,
  1402. ((filp->f_flags & O_NONBLOCK)
  1403. || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
  1404. == 0)))) {
  1405. if (filp->f_flags & O_NONBLOCK)
  1406. return -EAGAIN;
  1407. return -ERESTARTSYS;
  1408. }
  1409. if ((rc = set_protocol(dev, &krnptsreq)) != 0) {
  1410. /* auto power_on again */
  1411. dev->mstate = M_FETCH_ATR;
  1412. clear_bit(IS_ATR_VALID, &dev->flags);
  1413. }
  1414. /* release lock */
  1415. clear_bit(LOCK_IO, &dev->flags);
  1416. wake_up_interruptible(&dev->ioq);
  1417. }
  1418. return rc;
  1419. #ifdef PCMCIA_DEBUG
  1420. case CM_IOSDBGLVL: /* set debug log level */
  1421. {
  1422. int old_pc_debug = 0;
  1423. old_pc_debug = pc_debug;
  1424. if (copy_from_user(&pc_debug, argp, sizeof(int)))
  1425. return -EFAULT;
  1426. if (old_pc_debug != pc_debug)
  1427. DEBUGP(0, dev, "Changed debug log level "
  1428. "to %i\n", pc_debug);
  1429. }
  1430. return rc;
  1431. #endif
  1432. default:
  1433. DEBUGP(4, dev, "... in default (unknown IOCTL code)\n");
  1434. return -EINVAL;
  1435. }
  1436. }
  1437. static int cmm_open(struct inode *inode, struct file *filp)
  1438. {
  1439. struct cm4000_dev *dev;
  1440. struct pcmcia_device *link;
  1441. int minor = iminor(inode);
  1442. if (minor >= CM4000_MAX_DEV)
  1443. return -ENODEV;
  1444. link = dev_table[minor];
  1445. if (link == NULL || !pcmcia_dev_present(link))
  1446. return -ENODEV;
  1447. if (link->open)
  1448. return -EBUSY;
  1449. dev = link->priv;
  1450. filp->private_data = dev;
  1451. DEBUGP(2, dev, "-> cmm_open(device=%d.%d process=%s,%d)\n",
  1452. imajor(inode), minor, current->comm, current->pid);
  1453. /* init device variables, they may be "polluted" after close
  1454. * or, the device may never have been closed (i.e. open failed)
  1455. */
  1456. ZERO_DEV(dev);
  1457. /* opening will always block since the
  1458. * monitor will be started by open, which
  1459. * means we have to wait for ATR becoming
  1460. * vaild = block until valid (or card
  1461. * inserted)
  1462. */
  1463. if (filp->f_flags & O_NONBLOCK)
  1464. return -EAGAIN;
  1465. dev->mdelay = T_50MSEC;
  1466. /* start monitoring the cardstatus */
  1467. start_monitor(dev);
  1468. link->open = 1; /* only one open per device */
  1469. DEBUGP(2, dev, "<- cmm_open\n");
  1470. return nonseekable_open(inode, filp);
  1471. }
  1472. static int cmm_close(struct inode *inode, struct file *filp)
  1473. {
  1474. struct cm4000_dev *dev;
  1475. struct pcmcia_device *link;
  1476. int minor = iminor(inode);
  1477. if (minor >= CM4000_MAX_DEV)
  1478. return -ENODEV;
  1479. link = dev_table[minor];
  1480. if (link == NULL)
  1481. return -ENODEV;
  1482. dev = link->priv;
  1483. DEBUGP(2, dev, "-> cmm_close(maj/min=%d.%d)\n",
  1484. imajor(inode), minor);
  1485. stop_monitor(dev);
  1486. ZERO_DEV(dev);
  1487. link->open = 0; /* only one open per device */
  1488. wake_up(&dev->devq); /* socket removed? */
  1489. DEBUGP(2, dev, "cmm_close\n");
  1490. return 0;
  1491. }
  1492. static void cmm_cm4000_release(struct pcmcia_device * link)
  1493. {
  1494. struct cm4000_dev *dev = link->priv;
  1495. /* dont terminate the monitor, rather rely on
  1496. * close doing that for us.
  1497. */
  1498. DEBUGP(3, dev, "-> cmm_cm4000_release\n");
  1499. while (link->open) {
  1500. printk(KERN_INFO MODULE_NAME ": delaying release until "
  1501. "process has terminated\n");
  1502. /* note: don't interrupt us:
  1503. * close the applications which own
  1504. * the devices _first_ !
  1505. */
  1506. wait_event(dev->devq, (link->open == 0));
  1507. }
  1508. /* dev->devq=NULL; this cannot be zeroed earlier */
  1509. DEBUGP(3, dev, "<- cmm_cm4000_release\n");
  1510. return;
  1511. }
  1512. /*==== Interface to PCMCIA Layer =======================================*/
  1513. static int cm4000_config(struct pcmcia_device * link, int devno)
  1514. {
  1515. struct cm4000_dev *dev;
  1516. tuple_t tuple;
  1517. cisparse_t parse;
  1518. u_char buf[64];
  1519. int fail_fn, fail_rc;
  1520. int rc;
  1521. /* read the config-tuples */
  1522. tuple.Attributes = 0;
  1523. tuple.TupleData = buf;
  1524. tuple.TupleDataMax = sizeof(buf);
  1525. tuple.TupleOffset = 0;
  1526. link->io.BasePort2 = 0;
  1527. link->io.NumPorts2 = 0;
  1528. link->io.Attributes2 = 0;
  1529. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  1530. for (rc = pcmcia_get_first_tuple(link, &tuple);
  1531. rc == CS_SUCCESS; rc = pcmcia_get_next_tuple(link, &tuple)) {
  1532. rc = pcmcia_get_tuple_data(link, &tuple);
  1533. if (rc != CS_SUCCESS)
  1534. continue;
  1535. rc = pcmcia_parse_tuple(link, &tuple, &parse);
  1536. if (rc != CS_SUCCESS)
  1537. continue;
  1538. link->conf.ConfigIndex = parse.cftable_entry.index;
  1539. if (!parse.cftable_entry.io.nwin)
  1540. continue;
  1541. /* Get the IOaddr */
  1542. link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
  1543. link->io.NumPorts1 = parse.cftable_entry.io.win[0].len;
  1544. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  1545. if (!(parse.cftable_entry.io.flags & CISTPL_IO_8BIT))
  1546. link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
  1547. if (!(parse.cftable_entry.io.flags & CISTPL_IO_16BIT))
  1548. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  1549. link->io.IOAddrLines = parse.cftable_entry.io.flags
  1550. & CISTPL_IO_LINES_MASK;
  1551. rc = pcmcia_request_io(link, &link->io);
  1552. if (rc == CS_SUCCESS)
  1553. break; /* we are done */
  1554. }
  1555. if (rc != CS_SUCCESS)
  1556. goto cs_release;
  1557. link->conf.IntType = 00000002;
  1558. if ((fail_rc =
  1559. pcmcia_request_configuration(link, &link->conf)) != CS_SUCCESS) {
  1560. fail_fn = RequestConfiguration;
  1561. goto cs_release;
  1562. }
  1563. dev = link->priv;
  1564. sprintf(dev->node.dev_name, DEVICE_NAME "%d", devno);
  1565. dev->node.major = major;
  1566. dev->node.minor = devno;
  1567. dev->node.next = NULL;
  1568. link->dev_node = &dev->node;
  1569. return 0;
  1570. cs_release:
  1571. cm4000_release(link);
  1572. return -ENODEV;
  1573. }
  1574. static int cm4000_suspend(struct pcmcia_device *link)
  1575. {
  1576. struct cm4000_dev *dev;
  1577. dev = link->priv;
  1578. stop_monitor(dev);
  1579. return 0;
  1580. }
  1581. static int cm4000_resume(struct pcmcia_device *link)
  1582. {
  1583. struct cm4000_dev *dev;
  1584. dev = link->priv;
  1585. if (link->open)
  1586. start_monitor(dev);
  1587. return 0;
  1588. }
  1589. static void cm4000_release(struct pcmcia_device *link)
  1590. {
  1591. cmm_cm4000_release(link); /* delay release until device closed */
  1592. pcmcia_disable_device(link);
  1593. }
  1594. static int cm4000_probe(struct pcmcia_device *link)
  1595. {
  1596. struct cm4000_dev *dev;
  1597. int i, ret;
  1598. for (i = 0; i < CM4000_MAX_DEV; i++)
  1599. if (dev_table[i] == NULL)
  1600. break;
  1601. if (i == CM4000_MAX_DEV) {
  1602. printk(KERN_NOTICE MODULE_NAME ": all devices in use\n");
  1603. return -ENODEV;
  1604. }
  1605. /* create a new cm4000_cs device */
  1606. dev = kzalloc(sizeof(struct cm4000_dev), GFP_KERNEL);
  1607. if (dev == NULL)
  1608. return -ENOMEM;
  1609. dev->p_dev = link;
  1610. link->priv = dev;
  1611. link->conf.IntType = INT_MEMORY_AND_IO;
  1612. dev_table[i] = link;
  1613. init_waitqueue_head(&dev->devq);
  1614. init_waitqueue_head(&dev->ioq);
  1615. init_waitqueue_head(&dev->atrq);
  1616. init_waitqueue_head(&dev->readq);
  1617. ret = cm4000_config(link, i);
  1618. if (ret) {
  1619. dev_table[i] = NULL;
  1620. kfree(dev);
  1621. return ret;
  1622. }
  1623. device_create(cmm_class, NULL, MKDEV(major, i), "cmm%d", i);
  1624. return 0;
  1625. }
  1626. static void cm4000_detach(struct pcmcia_device *link)
  1627. {
  1628. struct cm4000_dev *dev = link->priv;
  1629. int devno;
  1630. /* find device */
  1631. for (devno = 0; devno < CM4000_MAX_DEV; devno++)
  1632. if (dev_table[devno] == link)
  1633. break;
  1634. if (devno == CM4000_MAX_DEV)
  1635. return;
  1636. stop_monitor(dev);
  1637. cm4000_release(link);
  1638. dev_table[devno] = NULL;
  1639. kfree(dev);
  1640. device_destroy(cmm_class, MKDEV(major, devno));
  1641. return;
  1642. }
  1643. static const struct file_operations cm4000_fops = {
  1644. .owner = THIS_MODULE,
  1645. .read = cmm_read,
  1646. .write = cmm_write,
  1647. .ioctl = cmm_ioctl,
  1648. .open = cmm_open,
  1649. .release= cmm_close,
  1650. };
  1651. static struct pcmcia_device_id cm4000_ids[] = {
  1652. PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0002),
  1653. PCMCIA_DEVICE_PROD_ID12("CardMan", "4000", 0x2FB368CA, 0xA2BD8C39),
  1654. PCMCIA_DEVICE_NULL,
  1655. };
  1656. MODULE_DEVICE_TABLE(pcmcia, cm4000_ids);
  1657. static struct pcmcia_driver cm4000_driver = {
  1658. .owner = THIS_MODULE,
  1659. .drv = {
  1660. .name = "cm4000_cs",
  1661. },
  1662. .probe = cm4000_probe,
  1663. .remove = cm4000_detach,
  1664. .suspend = cm4000_suspend,
  1665. .resume = cm4000_resume,
  1666. .id_table = cm4000_ids,
  1667. };
  1668. static int __init cmm_init(void)
  1669. {
  1670. int rc;
  1671. printk(KERN_INFO "%s\n", version);
  1672. cmm_class = class_create(THIS_MODULE, "cardman_4000");
  1673. if (IS_ERR(cmm_class))
  1674. return PTR_ERR(cmm_class);
  1675. major = register_chrdev(0, DEVICE_NAME, &cm4000_fops);
  1676. if (major < 0) {
  1677. printk(KERN_WARNING MODULE_NAME
  1678. ": could not get major number\n");
  1679. class_destroy(cmm_class);
  1680. return major;
  1681. }
  1682. rc = pcmcia_register_driver(&cm4000_driver);
  1683. if (rc < 0) {
  1684. unregister_chrdev(major, DEVICE_NAME);
  1685. class_destroy(cmm_class);
  1686. return rc;
  1687. }
  1688. return 0;
  1689. }
  1690. static void __exit cmm_exit(void)
  1691. {
  1692. printk(KERN_INFO MODULE_NAME ": unloading\n");
  1693. pcmcia_unregister_driver(&cm4000_driver);
  1694. unregister_chrdev(major, DEVICE_NAME);
  1695. class_destroy(cmm_class);
  1696. };
  1697. module_init(cmm_init);
  1698. module_exit(cmm_exit);
  1699. MODULE_LICENSE("Dual BSD/GPL");