cm4000_cs.c 51 KB

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