cm4000_cs.c 49 KB

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