cm4000_cs.c 51 KB

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