cm4000_cs.c 50 KB

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