cm4000_cs.c 49 KB

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