cm4000_cs.c 48 KB

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