cm4000_cs.c 50 KB

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