cm4000_cs.c 48 KB

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