cm4000_cs.c 50 KB

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