ds.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  1. /*
  2. * ds.c -- 16-bit PCMCIA core support
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * The initial developer of the original code is David A. Hinds
  9. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  10. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  11. *
  12. * (C) 1999 David A. Hinds
  13. * (C) 2003 - 2004 Dominik Brodowski
  14. */
  15. #include <linux/config.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/major.h>
  21. #include <linux/string.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/mm.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/sched.h>
  27. #include <linux/smp_lock.h>
  28. #include <linux/timer.h>
  29. #include <linux/ioctl.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/poll.h>
  32. #include <linux/pci.h>
  33. #include <linux/list.h>
  34. #include <linux/delay.h>
  35. #include <linux/kref.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/crc32.h>
  38. #include <asm/atomic.h>
  39. #define IN_CARD_SERVICES
  40. #include <pcmcia/version.h>
  41. #include <pcmcia/cs_types.h>
  42. #include <pcmcia/cs.h>
  43. #include <pcmcia/bulkmem.h>
  44. #include <pcmcia/cistpl.h>
  45. #include <pcmcia/ds.h>
  46. #include <pcmcia/ss.h>
  47. #include "cs_internal.h"
  48. /*====================================================================*/
  49. /* Module parameters */
  50. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  51. MODULE_DESCRIPTION("PCMCIA Driver Services");
  52. MODULE_LICENSE("GPL");
  53. #ifdef DEBUG
  54. int ds_pc_debug;
  55. module_param_named(pc_debug, ds_pc_debug, int, 0644);
  56. #define ds_dbg(lvl, fmt, arg...) do { \
  57. if (ds_pc_debug > (lvl)) \
  58. printk(KERN_DEBUG "ds: " fmt , ## arg); \
  59. } while (0)
  60. #else
  61. #define ds_dbg(lvl, fmt, arg...) do { } while (0)
  62. #endif
  63. /*====================================================================*/
  64. /* Device user information */
  65. #define MAX_EVENTS 32
  66. #define USER_MAGIC 0x7ea4
  67. #define CHECK_USER(u) \
  68. (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
  69. typedef struct user_info_t {
  70. u_int user_magic;
  71. int event_head, event_tail;
  72. event_t event[MAX_EVENTS];
  73. struct user_info_t *next;
  74. struct pcmcia_bus_socket *socket;
  75. } user_info_t;
  76. /* Socket state information */
  77. struct pcmcia_bus_socket {
  78. struct kref refcount;
  79. struct pcmcia_callback callback;
  80. int state;
  81. user_info_t *user;
  82. wait_queue_head_t queue;
  83. struct pcmcia_socket *parent;
  84. /* the PCMCIA devices connected to this socket (normally one, more
  85. * for multifunction devices: */
  86. struct list_head devices_list;
  87. u8 device_count; /* the number of devices, used
  88. * only internally and subject
  89. * to incorrectness and change */
  90. u8 device_add_pending;
  91. struct work_struct device_add;
  92. };
  93. static spinlock_t pcmcia_dev_list_lock;
  94. #define DS_SOCKET_PRESENT 0x01
  95. #define DS_SOCKET_BUSY 0x02
  96. #define DS_SOCKET_REMOVAL_PENDING 0x10
  97. #define DS_SOCKET_DEAD 0x80
  98. /*====================================================================*/
  99. static int major_dev = -1;
  100. static int unbind_request(struct pcmcia_bus_socket *s);
  101. /*====================================================================*/
  102. /* code which was in cs.c before */
  103. /* String tables for error messages */
  104. typedef struct lookup_t {
  105. int key;
  106. char *msg;
  107. } lookup_t;
  108. static const lookup_t error_table[] = {
  109. { CS_SUCCESS, "Operation succeeded" },
  110. { CS_BAD_ADAPTER, "Bad adapter" },
  111. { CS_BAD_ATTRIBUTE, "Bad attribute", },
  112. { CS_BAD_BASE, "Bad base address" },
  113. { CS_BAD_EDC, "Bad EDC" },
  114. { CS_BAD_IRQ, "Bad IRQ" },
  115. { CS_BAD_OFFSET, "Bad offset" },
  116. { CS_BAD_PAGE, "Bad page number" },
  117. { CS_READ_FAILURE, "Read failure" },
  118. { CS_BAD_SIZE, "Bad size" },
  119. { CS_BAD_SOCKET, "Bad socket" },
  120. { CS_BAD_TYPE, "Bad type" },
  121. { CS_BAD_VCC, "Bad Vcc" },
  122. { CS_BAD_VPP, "Bad Vpp" },
  123. { CS_BAD_WINDOW, "Bad window" },
  124. { CS_WRITE_FAILURE, "Write failure" },
  125. { CS_NO_CARD, "No card present" },
  126. { CS_UNSUPPORTED_FUNCTION, "Usupported function" },
  127. { CS_UNSUPPORTED_MODE, "Unsupported mode" },
  128. { CS_BAD_SPEED, "Bad speed" },
  129. { CS_BUSY, "Resource busy" },
  130. { CS_GENERAL_FAILURE, "General failure" },
  131. { CS_WRITE_PROTECTED, "Write protected" },
  132. { CS_BAD_ARG_LENGTH, "Bad argument length" },
  133. { CS_BAD_ARGS, "Bad arguments" },
  134. { CS_CONFIGURATION_LOCKED, "Configuration locked" },
  135. { CS_IN_USE, "Resource in use" },
  136. { CS_NO_MORE_ITEMS, "No more items" },
  137. { CS_OUT_OF_RESOURCE, "Out of resource" },
  138. { CS_BAD_HANDLE, "Bad handle" },
  139. { CS_BAD_TUPLE, "Bad CIS tuple" }
  140. };
  141. static const lookup_t service_table[] = {
  142. { AccessConfigurationRegister, "AccessConfigurationRegister" },
  143. { AddSocketServices, "AddSocketServices" },
  144. { AdjustResourceInfo, "AdjustResourceInfo" },
  145. { CheckEraseQueue, "CheckEraseQueue" },
  146. { CloseMemory, "CloseMemory" },
  147. { DeregisterClient, "DeregisterClient" },
  148. { DeregisterEraseQueue, "DeregisterEraseQueue" },
  149. { GetCardServicesInfo, "GetCardServicesInfo" },
  150. { GetClientInfo, "GetClientInfo" },
  151. { GetConfigurationInfo, "GetConfigurationInfo" },
  152. { GetEventMask, "GetEventMask" },
  153. { GetFirstClient, "GetFirstClient" },
  154. { GetFirstRegion, "GetFirstRegion" },
  155. { GetFirstTuple, "GetFirstTuple" },
  156. { GetNextClient, "GetNextClient" },
  157. { GetNextRegion, "GetNextRegion" },
  158. { GetNextTuple, "GetNextTuple" },
  159. { GetStatus, "GetStatus" },
  160. { GetTupleData, "GetTupleData" },
  161. { MapMemPage, "MapMemPage" },
  162. { ModifyConfiguration, "ModifyConfiguration" },
  163. { ModifyWindow, "ModifyWindow" },
  164. { OpenMemory, "OpenMemory" },
  165. { ParseTuple, "ParseTuple" },
  166. { ReadMemory, "ReadMemory" },
  167. { RegisterClient, "RegisterClient" },
  168. { RegisterEraseQueue, "RegisterEraseQueue" },
  169. { RegisterMTD, "RegisterMTD" },
  170. { ReleaseConfiguration, "ReleaseConfiguration" },
  171. { ReleaseIO, "ReleaseIO" },
  172. { ReleaseIRQ, "ReleaseIRQ" },
  173. { ReleaseWindow, "ReleaseWindow" },
  174. { RequestConfiguration, "RequestConfiguration" },
  175. { RequestIO, "RequestIO" },
  176. { RequestIRQ, "RequestIRQ" },
  177. { RequestSocketMask, "RequestSocketMask" },
  178. { RequestWindow, "RequestWindow" },
  179. { ResetCard, "ResetCard" },
  180. { SetEventMask, "SetEventMask" },
  181. { ValidateCIS, "ValidateCIS" },
  182. { WriteMemory, "WriteMemory" },
  183. { BindDevice, "BindDevice" },
  184. { BindMTD, "BindMTD" },
  185. { ReportError, "ReportError" },
  186. { SuspendCard, "SuspendCard" },
  187. { ResumeCard, "ResumeCard" },
  188. { EjectCard, "EjectCard" },
  189. { InsertCard, "InsertCard" },
  190. { ReplaceCIS, "ReplaceCIS" }
  191. };
  192. int pcmcia_report_error(client_handle_t handle, error_info_t *err)
  193. {
  194. int i;
  195. char *serv;
  196. if (CHECK_HANDLE(handle))
  197. printk(KERN_NOTICE);
  198. else {
  199. struct pcmcia_device *p_dev = handle_to_pdev(handle);
  200. printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
  201. }
  202. for (i = 0; i < ARRAY_SIZE(service_table); i++)
  203. if (service_table[i].key == err->func)
  204. break;
  205. if (i < ARRAY_SIZE(service_table))
  206. serv = service_table[i].msg;
  207. else
  208. serv = "Unknown service number";
  209. for (i = 0; i < ARRAY_SIZE(error_table); i++)
  210. if (error_table[i].key == err->retcode)
  211. break;
  212. if (i < ARRAY_SIZE(error_table))
  213. printk("%s: %s\n", serv, error_table[i].msg);
  214. else
  215. printk("%s: Unknown error code %#x\n", serv, err->retcode);
  216. return CS_SUCCESS;
  217. } /* report_error */
  218. EXPORT_SYMBOL(pcmcia_report_error);
  219. /* end of code which was in cs.c before */
  220. /*======================================================================*/
  221. void cs_error(client_handle_t handle, int func, int ret)
  222. {
  223. error_info_t err = { func, ret };
  224. pcmcia_report_error(handle, &err);
  225. }
  226. EXPORT_SYMBOL(cs_error);
  227. #ifdef CONFIG_PCMCIA_DEBUG
  228. static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
  229. {
  230. struct pcmcia_device_id *did = p_drv->id_table;
  231. unsigned int i;
  232. u32 hash;
  233. while (did && did->match_flags) {
  234. for (i=0; i<4; i++) {
  235. if (!did->prod_id[i])
  236. continue;
  237. hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
  238. if (hash == did->prod_id_hash[i])
  239. continue;
  240. printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
  241. "product string \"%s\": is 0x%x, should "
  242. "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
  243. did->prod_id_hash[i], hash);
  244. }
  245. did++;
  246. }
  247. return;
  248. }
  249. #else
  250. static inline void pcmcia_check_driver(struct pcmcia_driver *p_drv) {
  251. return;
  252. }
  253. #endif
  254. /*======================================================================*/
  255. static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info);
  256. static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr);
  257. static void pcmcia_release_bus_socket(struct kref *refcount)
  258. {
  259. struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount);
  260. pcmcia_put_socket(s->parent);
  261. kfree(s);
  262. }
  263. static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
  264. {
  265. kref_put(&s->refcount, pcmcia_release_bus_socket);
  266. }
  267. static struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s)
  268. {
  269. kref_get(&s->refcount);
  270. return (s);
  271. }
  272. /**
  273. * pcmcia_register_driver - register a PCMCIA driver with the bus core
  274. *
  275. * Registers a PCMCIA driver with the PCMCIA bus core.
  276. */
  277. static int pcmcia_device_probe(struct device *dev);
  278. static int pcmcia_device_remove(struct device * dev);
  279. int pcmcia_register_driver(struct pcmcia_driver *driver)
  280. {
  281. if (!driver)
  282. return -EINVAL;
  283. pcmcia_check_driver(driver);
  284. /* initialize common fields */
  285. driver->drv.bus = &pcmcia_bus_type;
  286. driver->drv.owner = driver->owner;
  287. driver->drv.probe = pcmcia_device_probe;
  288. driver->drv.remove = pcmcia_device_remove;
  289. return driver_register(&driver->drv);
  290. }
  291. EXPORT_SYMBOL(pcmcia_register_driver);
  292. /**
  293. * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
  294. */
  295. void pcmcia_unregister_driver(struct pcmcia_driver *driver)
  296. {
  297. driver_unregister(&driver->drv);
  298. }
  299. EXPORT_SYMBOL(pcmcia_unregister_driver);
  300. #ifdef CONFIG_PROC_FS
  301. static struct proc_dir_entry *proc_pccard = NULL;
  302. static int proc_read_drivers_callback(struct device_driver *driver, void *d)
  303. {
  304. char **p = d;
  305. struct pcmcia_driver *p_drv = container_of(driver,
  306. struct pcmcia_driver, drv);
  307. *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
  308. #ifdef CONFIG_MODULE_UNLOAD
  309. (p_drv->owner) ? module_refcount(p_drv->owner) : 1
  310. #else
  311. 1
  312. #endif
  313. );
  314. d = (void *) p;
  315. return 0;
  316. }
  317. static int proc_read_drivers(char *buf, char **start, off_t pos,
  318. int count, int *eof, void *data)
  319. {
  320. char *p = buf;
  321. bus_for_each_drv(&pcmcia_bus_type, NULL,
  322. (void *) &p, proc_read_drivers_callback);
  323. return (p - buf);
  324. }
  325. #endif
  326. /* pcmcia_device handling */
  327. static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
  328. {
  329. struct device *tmp_dev;
  330. tmp_dev = get_device(&p_dev->dev);
  331. if (!tmp_dev)
  332. return NULL;
  333. return to_pcmcia_dev(tmp_dev);
  334. }
  335. static void pcmcia_put_dev(struct pcmcia_device *p_dev)
  336. {
  337. if (p_dev)
  338. put_device(&p_dev->dev);
  339. }
  340. static void pcmcia_release_dev(struct device *dev)
  341. {
  342. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  343. ds_dbg(1, "releasing dev %p\n", p_dev);
  344. pcmcia_put_bus_socket(p_dev->socket->pcmcia);
  345. kfree(p_dev);
  346. }
  347. static int pcmcia_device_probe(struct device * dev)
  348. {
  349. struct pcmcia_device *p_dev;
  350. struct pcmcia_driver *p_drv;
  351. int ret = 0;
  352. dev = get_device(dev);
  353. if (!dev)
  354. return -ENODEV;
  355. p_dev = to_pcmcia_dev(dev);
  356. p_drv = to_pcmcia_drv(dev->driver);
  357. if (!try_module_get(p_drv->owner)) {
  358. ret = -EINVAL;
  359. goto put_dev;
  360. }
  361. if (p_drv->attach) {
  362. p_dev->instance = p_drv->attach();
  363. if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) {
  364. printk(KERN_NOTICE "ds: unable to create instance "
  365. "of '%s'!\n", p_drv->drv.name);
  366. ret = -EINVAL;
  367. }
  368. }
  369. if (ret)
  370. module_put(p_drv->owner);
  371. put_dev:
  372. if ((ret) || !(p_drv->attach))
  373. put_device(dev);
  374. return (ret);
  375. }
  376. static int pcmcia_device_remove(struct device * dev)
  377. {
  378. struct pcmcia_device *p_dev;
  379. struct pcmcia_driver *p_drv;
  380. /* detach the "instance" */
  381. p_dev = to_pcmcia_dev(dev);
  382. p_drv = to_pcmcia_drv(dev->driver);
  383. if (p_drv) {
  384. if ((p_drv->detach) && (p_dev->instance)) {
  385. p_drv->detach(p_dev->instance);
  386. /* from pcmcia_probe_device */
  387. put_device(&p_dev->dev);
  388. }
  389. module_put(p_drv->owner);
  390. }
  391. return 0;
  392. }
  393. /*
  394. * pcmcia_device_query -- determine information about a pcmcia device
  395. */
  396. static int pcmcia_device_query(struct pcmcia_device *p_dev)
  397. {
  398. cistpl_manfid_t manf_id;
  399. cistpl_funcid_t func_id;
  400. cistpl_vers_1_t vers1;
  401. unsigned int i;
  402. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  403. CISTPL_MANFID, &manf_id)) {
  404. p_dev->manf_id = manf_id.manf;
  405. p_dev->card_id = manf_id.card;
  406. p_dev->has_manf_id = 1;
  407. p_dev->has_card_id = 1;
  408. }
  409. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  410. CISTPL_FUNCID, &func_id)) {
  411. p_dev->func_id = func_id.func;
  412. p_dev->has_func_id = 1;
  413. } else {
  414. /* rule of thumb: cards with no FUNCID, but with
  415. * common memory device geometry information, are
  416. * probably memory cards (from pcmcia-cs) */
  417. cistpl_device_geo_t devgeo;
  418. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  419. CISTPL_DEVICE_GEO, &devgeo)) {
  420. ds_dbg(0, "mem device geometry probably means "
  421. "FUNCID_MEMORY\n");
  422. p_dev->func_id = CISTPL_FUNCID_MEMORY;
  423. p_dev->has_func_id = 1;
  424. }
  425. }
  426. if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
  427. &vers1)) {
  428. for (i=0; i < vers1.ns; i++) {
  429. char *tmp;
  430. unsigned int length;
  431. tmp = vers1.str + vers1.ofs[i];
  432. length = strlen(tmp) + 1;
  433. if ((length < 3) || (length > 255))
  434. continue;
  435. p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
  436. GFP_KERNEL);
  437. if (!p_dev->prod_id[i])
  438. continue;
  439. p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
  440. tmp, length);
  441. }
  442. }
  443. return 0;
  444. }
  445. /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
  446. * Serializes pcmcia_device_add; will most likely be removed in future.
  447. *
  448. * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
  449. * won't work, this doesn't matter much at the moment: the driver core doesn't
  450. * support it either.
  451. */
  452. static DECLARE_MUTEX(device_add_lock);
  453. static struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function)
  454. {
  455. struct pcmcia_device *p_dev;
  456. unsigned long flags;
  457. s = pcmcia_get_bus_socket(s);
  458. if (!s)
  459. return NULL;
  460. down(&device_add_lock);
  461. /* max of 2 devices per card */
  462. if (s->device_count == 2)
  463. goto err_put;
  464. p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
  465. if (!p_dev)
  466. goto err_put;
  467. memset(p_dev, 0, sizeof(struct pcmcia_device));
  468. p_dev->socket = s->parent;
  469. p_dev->device_no = (s->device_count++);
  470. p_dev->func = function;
  471. p_dev->dev.bus = &pcmcia_bus_type;
  472. p_dev->dev.parent = s->parent->dev.dev;
  473. p_dev->dev.release = pcmcia_release_dev;
  474. sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
  475. /* compat */
  476. p_dev->client.client_magic = CLIENT_MAGIC;
  477. p_dev->client.Socket = s->parent;
  478. p_dev->client.Function = function;
  479. p_dev->client.state = CLIENT_UNBOUND;
  480. /* Add to the list in pcmcia_bus_socket */
  481. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  482. list_add_tail(&p_dev->socket_device_list, &s->devices_list);
  483. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  484. pcmcia_device_query(p_dev);
  485. if (device_register(&p_dev->dev)) {
  486. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  487. list_del(&p_dev->socket_device_list);
  488. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  489. goto err_free;
  490. }
  491. up(&device_add_lock);
  492. return p_dev;
  493. err_free:
  494. kfree(p_dev);
  495. s->device_count--;
  496. err_put:
  497. up(&device_add_lock);
  498. pcmcia_put_bus_socket(s);
  499. return NULL;
  500. }
  501. static int pcmcia_card_add(struct pcmcia_socket *s)
  502. {
  503. cisinfo_t cisinfo;
  504. cistpl_longlink_mfc_t mfc;
  505. unsigned int no_funcs, i;
  506. int ret = 0;
  507. if (!(s->resource_setup_done))
  508. return -EAGAIN; /* try again, but later... */
  509. pcmcia_validate_mem(s);
  510. ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
  511. if (ret || !cisinfo.Chains) {
  512. ds_dbg(0, "invalid CIS or invalid resources\n");
  513. return -ENODEV;
  514. }
  515. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  516. no_funcs = mfc.nfn;
  517. else
  518. no_funcs = 1;
  519. /* this doesn't handle multifunction devices on one pcmcia function
  520. * yet. */
  521. for (i=0; i < no_funcs; i++)
  522. pcmcia_device_add(s->pcmcia, i);
  523. return (ret);
  524. }
  525. static void pcmcia_delayed_add_pseudo_device(void *data)
  526. {
  527. struct pcmcia_bus_socket *s = data;
  528. pcmcia_device_add(s, 0);
  529. s->device_add_pending = 0;
  530. }
  531. static inline void pcmcia_add_pseudo_device(struct pcmcia_bus_socket *s)
  532. {
  533. if (!s->device_add_pending) {
  534. schedule_work(&s->device_add);
  535. s->device_add_pending = 1;
  536. }
  537. return;
  538. }
  539. static void pcmcia_bus_rescan(void)
  540. {
  541. /* must be called with skt_sem held */
  542. bus_rescan_devices(&pcmcia_bus_type);
  543. }
  544. static inline int pcmcia_devmatch(struct pcmcia_device *dev,
  545. struct pcmcia_device_id *did)
  546. {
  547. if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
  548. if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
  549. return 0;
  550. }
  551. if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
  552. if ((!dev->has_card_id) || (dev->card_id != did->card_id))
  553. return 0;
  554. }
  555. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
  556. if (dev->func != did->function)
  557. return 0;
  558. }
  559. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
  560. if (!dev->prod_id[0])
  561. return 0;
  562. if (strcmp(did->prod_id[0], dev->prod_id[0]))
  563. return 0;
  564. }
  565. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
  566. if (!dev->prod_id[1])
  567. return 0;
  568. if (strcmp(did->prod_id[1], dev->prod_id[1]))
  569. return 0;
  570. }
  571. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
  572. if (!dev->prod_id[2])
  573. return 0;
  574. if (strcmp(did->prod_id[2], dev->prod_id[2]))
  575. return 0;
  576. }
  577. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
  578. if (!dev->prod_id[3])
  579. return 0;
  580. if (strcmp(did->prod_id[3], dev->prod_id[3]))
  581. return 0;
  582. }
  583. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
  584. /* handle pseudo multifunction devices:
  585. * there are at most two pseudo multifunction devices.
  586. * if we're matching against the first, schedule a
  587. * call which will then check whether there are two
  588. * pseudo devices, and if not, add the second one.
  589. */
  590. if (dev->device_no == 0)
  591. pcmcia_add_pseudo_device(dev->socket->pcmcia);
  592. if (dev->device_no != did->device_no)
  593. return 0;
  594. }
  595. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
  596. if ((!dev->has_func_id) || (dev->func_id != did->func_id))
  597. return 0;
  598. /* if this is a pseudo-multi-function device,
  599. * we need explicit matches */
  600. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
  601. return 0;
  602. if (dev->device_no)
  603. return 0;
  604. /* also, FUNC_ID matching needs to be activated by userspace
  605. * after it has re-checked that there is no possible module
  606. * with a prod_id/manf_id/card_id match.
  607. */
  608. if (!dev->allow_func_id_match)
  609. return 0;
  610. }
  611. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
  612. if (!dev->socket->fake_cis) {
  613. /* FIXME: evaluate using firmware helpers to
  614. * automagically load it from userspace */
  615. return 0;
  616. }
  617. }
  618. if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
  619. int i;
  620. for (i=0; i<4; i++)
  621. if (dev->prod_id[i])
  622. return 0;
  623. if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
  624. return 0;
  625. }
  626. dev->dev.driver_data = (void *) did;
  627. return 1;
  628. }
  629. static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
  630. struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
  631. struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
  632. struct pcmcia_device_id *did = p_drv->id_table;
  633. /* matching by cardmgr */
  634. if (p_dev->cardmgr == p_drv)
  635. return 1;
  636. while (did && did->match_flags) {
  637. if (pcmcia_devmatch(p_dev, did))
  638. return 1;
  639. did++;
  640. }
  641. return 0;
  642. }
  643. #ifdef CONFIG_HOTPLUG
  644. static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
  645. char *buffer, int buffer_size)
  646. {
  647. struct pcmcia_device *p_dev;
  648. int i, length = 0;
  649. u32 hash[4] = { 0, 0, 0, 0};
  650. if (!dev)
  651. return -ENODEV;
  652. p_dev = to_pcmcia_dev(dev);
  653. /* calculate hashes */
  654. for (i=0; i<4; i++) {
  655. if (!p_dev->prod_id[i])
  656. continue;
  657. hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
  658. }
  659. i = 0;
  660. if (add_hotplug_env_var(envp, num_envp, &i,
  661. buffer, buffer_size, &length,
  662. "SOCKET_NO=%u",
  663. p_dev->socket->sock))
  664. return -ENOMEM;
  665. if (add_hotplug_env_var(envp, num_envp, &i,
  666. buffer, buffer_size, &length,
  667. "DEVICE_NO=%02X",
  668. p_dev->device_no))
  669. return -ENOMEM;
  670. if (add_hotplug_env_var(envp, num_envp, &i,
  671. buffer, buffer_size, &length,
  672. "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  673. "pa%08Xpb%08Xpc%08Xpd%08X",
  674. p_dev->has_manf_id ? p_dev->manf_id : 0,
  675. p_dev->has_card_id ? p_dev->card_id : 0,
  676. p_dev->has_func_id ? p_dev->func_id : 0,
  677. p_dev->func,
  678. p_dev->device_no,
  679. hash[0],
  680. hash[1],
  681. hash[2],
  682. hash[3]))
  683. return -ENOMEM;
  684. envp[i] = NULL;
  685. return 0;
  686. }
  687. #else
  688. static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
  689. char *buffer, int buffer_size)
  690. {
  691. return -ENODEV;
  692. }
  693. #endif
  694. /************************ per-device sysfs output ***************************/
  695. #define pcmcia_device_attr(field, test, format) \
  696. static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  697. { \
  698. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  699. return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
  700. }
  701. #define pcmcia_device_stringattr(name, field) \
  702. static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  703. { \
  704. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  705. return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
  706. }
  707. pcmcia_device_attr(func, socket, "0x%02x\n");
  708. pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
  709. pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
  710. pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
  711. pcmcia_device_stringattr(prod_id1, prod_id[0]);
  712. pcmcia_device_stringattr(prod_id2, prod_id[1]);
  713. pcmcia_device_stringattr(prod_id3, prod_id[2]);
  714. pcmcia_device_stringattr(prod_id4, prod_id[3]);
  715. static struct device_attribute pcmcia_dev_attrs[] = {
  716. __ATTR(function, 0444, func_show, NULL),
  717. __ATTR_RO(func_id),
  718. __ATTR_RO(manf_id),
  719. __ATTR_RO(card_id),
  720. __ATTR_RO(prod_id1),
  721. __ATTR_RO(prod_id2),
  722. __ATTR_RO(prod_id3),
  723. __ATTR_RO(prod_id4),
  724. __ATTR_NULL,
  725. };
  726. /*======================================================================
  727. These manage a ring buffer of events pending for one user process
  728. ======================================================================*/
  729. static int queue_empty(user_info_t *user)
  730. {
  731. return (user->event_head == user->event_tail);
  732. }
  733. static event_t get_queued_event(user_info_t *user)
  734. {
  735. user->event_tail = (user->event_tail+1) % MAX_EVENTS;
  736. return user->event[user->event_tail];
  737. }
  738. static void queue_event(user_info_t *user, event_t event)
  739. {
  740. user->event_head = (user->event_head+1) % MAX_EVENTS;
  741. if (user->event_head == user->event_tail)
  742. user->event_tail = (user->event_tail+1) % MAX_EVENTS;
  743. user->event[user->event_head] = event;
  744. }
  745. static void handle_event(struct pcmcia_bus_socket *s, event_t event)
  746. {
  747. user_info_t *user;
  748. for (user = s->user; user; user = user->next)
  749. queue_event(user, event);
  750. wake_up_interruptible(&s->queue);
  751. }
  752. /*======================================================================
  753. The card status event handler.
  754. ======================================================================*/
  755. struct send_event_data {
  756. struct pcmcia_socket *skt;
  757. event_t event;
  758. int priority;
  759. };
  760. static int send_event_callback(struct device *dev, void * _data)
  761. {
  762. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  763. struct send_event_data *data = _data;
  764. /* we get called for all sockets, but may only pass the event
  765. * for drivers _on the affected socket_ */
  766. if (p_dev->socket != data->skt)
  767. return 0;
  768. if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE))
  769. return 0;
  770. if (p_dev->client.EventMask & data->event)
  771. return EVENT(&p_dev->client, data->event, data->priority);
  772. return 0;
  773. }
  774. static int send_event(struct pcmcia_socket *s, event_t event, int priority)
  775. {
  776. int ret = 0;
  777. struct send_event_data private;
  778. struct pcmcia_bus_socket *skt = pcmcia_get_bus_socket(s->pcmcia);
  779. if (!skt)
  780. return 0;
  781. private.skt = s;
  782. private.event = event;
  783. private.priority = priority;
  784. ret = bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
  785. pcmcia_put_bus_socket(skt);
  786. return ret;
  787. } /* send_event */
  788. /* Normally, the event is passed to individual drivers after
  789. * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
  790. * is inversed to maintain historic compatibility.
  791. */
  792. static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
  793. {
  794. struct pcmcia_bus_socket *s = skt->pcmcia;
  795. int ret = 0;
  796. ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
  797. event, priority, s);
  798. switch (event) {
  799. case CS_EVENT_CARD_REMOVAL:
  800. s->state &= ~DS_SOCKET_PRESENT;
  801. send_event(skt, event, priority);
  802. unbind_request(s);
  803. handle_event(s, event);
  804. break;
  805. case CS_EVENT_CARD_INSERTION:
  806. s->state |= DS_SOCKET_PRESENT;
  807. pcmcia_card_add(skt);
  808. handle_event(s, event);
  809. break;
  810. case CS_EVENT_EJECTION_REQUEST:
  811. ret = send_event(skt, event, priority);
  812. break;
  813. default:
  814. handle_event(s, event);
  815. send_event(skt, event, priority);
  816. break;
  817. }
  818. return 0;
  819. } /* ds_event */
  820. /*======================================================================
  821. bind_request() and bind_device() are merged by now. Register_client()
  822. is called right at the end of bind_request(), during the driver's
  823. ->attach() call. Individual descriptions:
  824. bind_request() connects a socket to a particular client driver.
  825. It looks up the specified device ID in the list of registered
  826. drivers, binds it to the socket, and tries to create an instance
  827. of the device. unbind_request() deletes a driver instance.
  828. Bind_device() associates a device driver with a particular socket.
  829. It is normally called by Driver Services after it has identified
  830. a newly inserted card. An instance of that driver will then be
  831. eligible to register as a client of this socket.
  832. Register_client() uses the dev_info_t handle to match the
  833. caller with a socket. The driver must have already been bound
  834. to a socket with bind_device() -- in fact, bind_device()
  835. allocates the client structure that will be used.
  836. ======================================================================*/
  837. static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
  838. {
  839. struct pcmcia_driver *p_drv;
  840. struct pcmcia_device *p_dev;
  841. int ret = 0;
  842. unsigned long flags;
  843. s = pcmcia_get_bus_socket(s);
  844. if (!s)
  845. return -EINVAL;
  846. ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
  847. (char *)bind_info->dev_info);
  848. p_drv = get_pcmcia_driver(&bind_info->dev_info);
  849. if (!p_drv) {
  850. ret = -EINVAL;
  851. goto err_put;
  852. }
  853. if (!try_module_get(p_drv->owner)) {
  854. ret = -EINVAL;
  855. goto err_put_driver;
  856. }
  857. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  858. list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
  859. if (p_dev->func == bind_info->function) {
  860. if ((p_dev->dev.driver == &p_drv->drv)) {
  861. if (p_dev->cardmgr) {
  862. /* if there's already a device
  863. * registered, and it was registered
  864. * by userspace before, we need to
  865. * return the "instance". */
  866. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  867. bind_info->instance = p_dev->instance;
  868. ret = -EBUSY;
  869. goto err_put_module;
  870. } else {
  871. /* the correct driver managed to bind
  872. * itself magically to the correct
  873. * device. */
  874. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  875. p_dev->cardmgr = p_drv;
  876. ret = 0;
  877. goto err_put_module;
  878. }
  879. } else if (!p_dev->dev.driver) {
  880. /* there's already a device available where
  881. * no device has been bound to yet. So we don't
  882. * need to register a device! */
  883. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  884. goto rescan;
  885. }
  886. }
  887. }
  888. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  889. p_dev = pcmcia_device_add(s, bind_info->function);
  890. if (!p_dev) {
  891. ret = -EIO;
  892. goto err_put_module;
  893. }
  894. rescan:
  895. p_dev->cardmgr = p_drv;
  896. /* if a driver is already running, we can abort */
  897. if (p_dev->dev.driver)
  898. goto err_put_module;
  899. /*
  900. * Prevent this racing with a card insertion.
  901. */
  902. down(&s->parent->skt_sem);
  903. bus_rescan_devices(&pcmcia_bus_type);
  904. up(&s->parent->skt_sem);
  905. /* check whether the driver indeed matched. I don't care if this
  906. * is racy or not, because it can only happen on cardmgr access
  907. * paths...
  908. */
  909. if (!(p_dev->dev.driver == &p_drv->drv))
  910. p_dev->cardmgr = NULL;
  911. err_put_module:
  912. module_put(p_drv->owner);
  913. err_put_driver:
  914. put_driver(&p_drv->drv);
  915. err_put:
  916. pcmcia_put_bus_socket(s);
  917. return (ret);
  918. } /* bind_request */
  919. int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
  920. {
  921. client_t *client = NULL;
  922. struct pcmcia_socket *s;
  923. struct pcmcia_bus_socket *skt = NULL;
  924. struct pcmcia_device *p_dev = NULL;
  925. /* Look for unbound client with matching dev_info */
  926. down_read(&pcmcia_socket_list_rwsem);
  927. list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
  928. unsigned long flags;
  929. if (s->state & SOCKET_CARDBUS)
  930. continue;
  931. skt = s->pcmcia;
  932. if (!skt)
  933. continue;
  934. skt = pcmcia_get_bus_socket(skt);
  935. if (!skt)
  936. continue;
  937. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  938. list_for_each_entry(p_dev, &skt->devices_list, socket_device_list) {
  939. struct pcmcia_driver *p_drv;
  940. p_dev = pcmcia_get_dev(p_dev);
  941. if (!p_dev)
  942. continue;
  943. if (!(p_dev->client.state & CLIENT_UNBOUND) ||
  944. (!p_dev->dev.driver)) {
  945. pcmcia_put_dev(p_dev);
  946. continue;
  947. }
  948. p_drv = to_pcmcia_drv(p_dev->dev.driver);
  949. if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
  950. client = &p_dev->client;
  951. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  952. goto found;
  953. }
  954. pcmcia_put_dev(p_dev);
  955. }
  956. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  957. pcmcia_put_bus_socket(skt);
  958. }
  959. found:
  960. up_read(&pcmcia_socket_list_rwsem);
  961. if (!p_dev || !client)
  962. return -ENODEV;
  963. pcmcia_put_bus_socket(skt); /* safe, as we already hold a reference from bind_device */
  964. *handle = client;
  965. client->state &= ~CLIENT_UNBOUND;
  966. client->Socket = s;
  967. client->EventMask = req->EventMask;
  968. client->event_handler = req->event_handler;
  969. client->event_callback_args = req->event_callback_args;
  970. client->event_callback_args.client_handle = client;
  971. if (s->state & SOCKET_CARDBUS)
  972. client->state |= CLIENT_CARDBUS;
  973. if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) &&
  974. (client->Function != BIND_FN_ALL)) {
  975. cistpl_longlink_mfc_t mfc;
  976. if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc)
  977. == CS_SUCCESS)
  978. s->functions = mfc.nfn;
  979. else
  980. s->functions = 1;
  981. s->config = kmalloc(sizeof(config_t) * s->functions,
  982. GFP_KERNEL);
  983. if (!s->config)
  984. goto out_no_resource;
  985. memset(s->config, 0, sizeof(config_t) * s->functions);
  986. }
  987. ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
  988. client, p_dev->dev.bus_id);
  989. if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE)
  990. EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW);
  991. if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
  992. if (client->EventMask & CS_EVENT_CARD_INSERTION)
  993. EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
  994. }
  995. return CS_SUCCESS;
  996. out_no_resource:
  997. pcmcia_put_dev(p_dev);
  998. return CS_OUT_OF_RESOURCE;
  999. } /* register_client */
  1000. EXPORT_SYMBOL(pcmcia_register_client);
  1001. /*====================================================================*/
  1002. extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);
  1003. static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
  1004. {
  1005. dev_node_t *node;
  1006. struct pcmcia_device *p_dev;
  1007. unsigned long flags;
  1008. int ret = 0;
  1009. #ifdef CONFIG_CARDBUS
  1010. /*
  1011. * Some unbelievably ugly code to associate the PCI cardbus
  1012. * device and its driver with the PCMCIA "bind" information.
  1013. */
  1014. {
  1015. struct pci_bus *bus;
  1016. bus = pcmcia_lookup_bus(s->parent);
  1017. if (bus) {
  1018. struct list_head *list;
  1019. struct pci_dev *dev = NULL;
  1020. list = bus->devices.next;
  1021. while (list != &bus->devices) {
  1022. struct pci_dev *pdev = pci_dev_b(list);
  1023. list = list->next;
  1024. if (first) {
  1025. dev = pdev;
  1026. break;
  1027. }
  1028. /* Try to handle "next" here some way? */
  1029. }
  1030. if (dev && dev->driver) {
  1031. strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
  1032. bind_info->major = 0;
  1033. bind_info->minor = 0;
  1034. bind_info->next = NULL;
  1035. return 0;
  1036. }
  1037. }
  1038. }
  1039. #endif
  1040. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  1041. list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
  1042. if (p_dev->func == bind_info->function) {
  1043. p_dev = pcmcia_get_dev(p_dev);
  1044. if (!p_dev)
  1045. continue;
  1046. goto found;
  1047. }
  1048. }
  1049. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  1050. return -ENODEV;
  1051. found:
  1052. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  1053. if ((!p_dev->instance) ||
  1054. (p_dev->instance->state & DEV_CONFIG_PENDING)) {
  1055. ret = -EAGAIN;
  1056. goto err_put;
  1057. }
  1058. if (first)
  1059. node = p_dev->instance->dev;
  1060. else
  1061. for (node = p_dev->instance->dev; node; node = node->next)
  1062. if (node == bind_info->next)
  1063. break;
  1064. if (!node) {
  1065. ret = -ENODEV;
  1066. goto err_put;
  1067. }
  1068. strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
  1069. bind_info->major = node->major;
  1070. bind_info->minor = node->minor;
  1071. bind_info->next = node->next;
  1072. err_put:
  1073. pcmcia_put_dev(p_dev);
  1074. return (ret);
  1075. } /* get_device_info */
  1076. /*====================================================================*/
  1077. /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
  1078. * drivers have been called with EVENT_CARD_REMOVAL before.
  1079. */
  1080. static int unbind_request(struct pcmcia_bus_socket *s)
  1081. {
  1082. struct pcmcia_device *p_dev;
  1083. unsigned long flags;
  1084. ds_dbg(2, "unbind_request(%d)\n", s->parent->sock);
  1085. s->device_count = 0;
  1086. for (;;) {
  1087. /* unregister all pcmcia_devices registered with this socket*/
  1088. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  1089. if (list_empty(&s->devices_list)) {
  1090. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  1091. return 0;
  1092. }
  1093. p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
  1094. list_del(&p_dev->socket_device_list);
  1095. p_dev->client.state |= CLIENT_STALE;
  1096. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  1097. device_unregister(&p_dev->dev);
  1098. }
  1099. return 0;
  1100. } /* unbind_request */
  1101. int pcmcia_deregister_client(client_handle_t handle)
  1102. {
  1103. struct pcmcia_socket *s;
  1104. int i;
  1105. struct pcmcia_device *p_dev = handle_to_pdev(handle);
  1106. if (CHECK_HANDLE(handle))
  1107. return CS_BAD_HANDLE;
  1108. s = SOCKET(handle);
  1109. ds_dbg(1, "deregister_client(%p)\n", handle);
  1110. if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
  1111. goto warn_out;
  1112. for (i = 0; i < MAX_WIN; i++)
  1113. if (handle->state & CLIENT_WIN_REQ(i))
  1114. goto warn_out;
  1115. if (handle->state & CLIENT_STALE) {
  1116. handle->client_magic = 0;
  1117. handle->state &= ~CLIENT_STALE;
  1118. pcmcia_put_dev(p_dev);
  1119. } else {
  1120. handle->state = CLIENT_UNBOUND;
  1121. handle->event_handler = NULL;
  1122. }
  1123. return CS_SUCCESS;
  1124. warn_out:
  1125. printk(KERN_WARNING "ds: deregister_client was called too early.\n");
  1126. return CS_IN_USE;
  1127. } /* deregister_client */
  1128. EXPORT_SYMBOL(pcmcia_deregister_client);
  1129. /*======================================================================
  1130. The user-mode PC Card device interface
  1131. ======================================================================*/
  1132. static int ds_open(struct inode *inode, struct file *file)
  1133. {
  1134. socket_t i = iminor(inode);
  1135. struct pcmcia_bus_socket *s;
  1136. user_info_t *user;
  1137. ds_dbg(0, "ds_open(socket %d)\n", i);
  1138. s = get_socket_info_by_nr(i);
  1139. if (!s)
  1140. return -ENODEV;
  1141. s = pcmcia_get_bus_socket(s);
  1142. if (!s)
  1143. return -ENODEV;
  1144. if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
  1145. if (s->state & DS_SOCKET_BUSY) {
  1146. pcmcia_put_bus_socket(s);
  1147. return -EBUSY;
  1148. }
  1149. else
  1150. s->state |= DS_SOCKET_BUSY;
  1151. }
  1152. user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
  1153. if (!user) {
  1154. pcmcia_put_bus_socket(s);
  1155. return -ENOMEM;
  1156. }
  1157. user->event_tail = user->event_head = 0;
  1158. user->next = s->user;
  1159. user->user_magic = USER_MAGIC;
  1160. user->socket = s;
  1161. s->user = user;
  1162. file->private_data = user;
  1163. if (s->state & DS_SOCKET_PRESENT)
  1164. queue_event(user, CS_EVENT_CARD_INSERTION);
  1165. return 0;
  1166. } /* ds_open */
  1167. /*====================================================================*/
  1168. static int ds_release(struct inode *inode, struct file *file)
  1169. {
  1170. struct pcmcia_bus_socket *s;
  1171. user_info_t *user, **link;
  1172. ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
  1173. user = file->private_data;
  1174. if (CHECK_USER(user))
  1175. goto out;
  1176. s = user->socket;
  1177. /* Unlink user data structure */
  1178. if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
  1179. s->state &= ~DS_SOCKET_BUSY;
  1180. }
  1181. file->private_data = NULL;
  1182. for (link = &s->user; *link; link = &(*link)->next)
  1183. if (*link == user) break;
  1184. if (link == NULL)
  1185. goto out;
  1186. *link = user->next;
  1187. user->user_magic = 0;
  1188. kfree(user);
  1189. pcmcia_put_bus_socket(s);
  1190. out:
  1191. return 0;
  1192. } /* ds_release */
  1193. /*====================================================================*/
  1194. static ssize_t ds_read(struct file *file, char __user *buf,
  1195. size_t count, loff_t *ppos)
  1196. {
  1197. struct pcmcia_bus_socket *s;
  1198. user_info_t *user;
  1199. int ret;
  1200. ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
  1201. if (count < 4)
  1202. return -EINVAL;
  1203. user = file->private_data;
  1204. if (CHECK_USER(user))
  1205. return -EIO;
  1206. s = user->socket;
  1207. if (s->state & DS_SOCKET_DEAD)
  1208. return -EIO;
  1209. ret = wait_event_interruptible(s->queue, !queue_empty(user));
  1210. if (ret == 0)
  1211. ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
  1212. return ret;
  1213. } /* ds_read */
  1214. /*====================================================================*/
  1215. static ssize_t ds_write(struct file *file, const char __user *buf,
  1216. size_t count, loff_t *ppos)
  1217. {
  1218. ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
  1219. if (count != 4)
  1220. return -EINVAL;
  1221. if ((file->f_flags & O_ACCMODE) == O_RDONLY)
  1222. return -EBADF;
  1223. return -EIO;
  1224. } /* ds_write */
  1225. /*====================================================================*/
  1226. /* No kernel lock - fine */
  1227. static u_int ds_poll(struct file *file, poll_table *wait)
  1228. {
  1229. struct pcmcia_bus_socket *s;
  1230. user_info_t *user;
  1231. ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
  1232. user = file->private_data;
  1233. if (CHECK_USER(user))
  1234. return POLLERR;
  1235. s = user->socket;
  1236. /*
  1237. * We don't check for a dead socket here since that
  1238. * will send cardmgr into an endless spin.
  1239. */
  1240. poll_wait(file, &s->queue, wait);
  1241. if (!queue_empty(user))
  1242. return POLLIN | POLLRDNORM;
  1243. return 0;
  1244. } /* ds_poll */
  1245. /*====================================================================*/
  1246. extern int pcmcia_adjust_resource_info(adjust_t *adj);
  1247. static int ds_ioctl(struct inode * inode, struct file * file,
  1248. u_int cmd, u_long arg)
  1249. {
  1250. struct pcmcia_bus_socket *s;
  1251. void __user *uarg = (char __user *)arg;
  1252. u_int size;
  1253. int ret, err;
  1254. ds_ioctl_arg_t *buf;
  1255. user_info_t *user;
  1256. ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
  1257. user = file->private_data;
  1258. if (CHECK_USER(user))
  1259. return -EIO;
  1260. s = user->socket;
  1261. if (s->state & DS_SOCKET_DEAD)
  1262. return -EIO;
  1263. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  1264. if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
  1265. /* Permission check */
  1266. if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
  1267. return -EPERM;
  1268. if (cmd & IOC_IN) {
  1269. if (!access_ok(VERIFY_READ, uarg, size)) {
  1270. ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
  1271. return -EFAULT;
  1272. }
  1273. }
  1274. if (cmd & IOC_OUT) {
  1275. if (!access_ok(VERIFY_WRITE, uarg, size)) {
  1276. ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
  1277. return -EFAULT;
  1278. }
  1279. }
  1280. buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
  1281. if (!buf)
  1282. return -ENOMEM;
  1283. err = ret = 0;
  1284. if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
  1285. switch (cmd) {
  1286. case DS_ADJUST_RESOURCE_INFO:
  1287. ret = pcmcia_adjust_resource_info(&buf->adjust);
  1288. break;
  1289. case DS_GET_CARD_SERVICES_INFO:
  1290. ret = pcmcia_get_card_services_info(&buf->servinfo);
  1291. break;
  1292. case DS_GET_CONFIGURATION_INFO:
  1293. if (buf->config.Function &&
  1294. (buf->config.Function >= s->parent->functions))
  1295. ret = CS_BAD_ARGS;
  1296. else
  1297. ret = pccard_get_configuration_info(s->parent,
  1298. buf->config.Function, &buf->config);
  1299. break;
  1300. case DS_GET_FIRST_TUPLE:
  1301. down(&s->parent->skt_sem);
  1302. pcmcia_validate_mem(s->parent);
  1303. up(&s->parent->skt_sem);
  1304. ret = pccard_get_first_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
  1305. break;
  1306. case DS_GET_NEXT_TUPLE:
  1307. ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
  1308. break;
  1309. case DS_GET_TUPLE_DATA:
  1310. buf->tuple.TupleData = buf->tuple_parse.data;
  1311. buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
  1312. ret = pccard_get_tuple_data(s->parent, &buf->tuple);
  1313. break;
  1314. case DS_PARSE_TUPLE:
  1315. buf->tuple.TupleData = buf->tuple_parse.data;
  1316. ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
  1317. break;
  1318. case DS_RESET_CARD:
  1319. ret = pccard_reset_card(s->parent);
  1320. break;
  1321. case DS_GET_STATUS:
  1322. if (buf->status.Function &&
  1323. (buf->status.Function >= s->parent->functions))
  1324. ret = CS_BAD_ARGS;
  1325. else
  1326. ret = pccard_get_status(s->parent, buf->status.Function, &buf->status);
  1327. break;
  1328. case DS_VALIDATE_CIS:
  1329. down(&s->parent->skt_sem);
  1330. pcmcia_validate_mem(s->parent);
  1331. up(&s->parent->skt_sem);
  1332. ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf->cisinfo);
  1333. break;
  1334. case DS_SUSPEND_CARD:
  1335. ret = pcmcia_suspend_card(s->parent);
  1336. break;
  1337. case DS_RESUME_CARD:
  1338. ret = pcmcia_resume_card(s->parent);
  1339. break;
  1340. case DS_EJECT_CARD:
  1341. err = pcmcia_eject_card(s->parent);
  1342. break;
  1343. case DS_INSERT_CARD:
  1344. err = pcmcia_insert_card(s->parent);
  1345. break;
  1346. case DS_ACCESS_CONFIGURATION_REGISTER:
  1347. if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
  1348. err = -EPERM;
  1349. goto free_out;
  1350. }
  1351. if (buf->conf_reg.Function &&
  1352. (buf->conf_reg.Function >= s->parent->functions))
  1353. ret = CS_BAD_ARGS;
  1354. else
  1355. ret = pccard_access_configuration_register(s->parent,
  1356. buf->conf_reg.Function, &buf->conf_reg);
  1357. break;
  1358. case DS_GET_FIRST_REGION:
  1359. case DS_GET_NEXT_REGION:
  1360. case DS_BIND_MTD:
  1361. if (!capable(CAP_SYS_ADMIN)) {
  1362. err = -EPERM;
  1363. goto free_out;
  1364. } else {
  1365. static int printed = 0;
  1366. if (!printed) {
  1367. printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
  1368. printk(KERN_WARNING "MTD handling any more.\n");
  1369. printed++;
  1370. }
  1371. }
  1372. err = -EINVAL;
  1373. goto free_out;
  1374. break;
  1375. case DS_GET_FIRST_WINDOW:
  1376. ret = pcmcia_get_window(s->parent, &buf->win_info.handle, 0,
  1377. &buf->win_info.window);
  1378. break;
  1379. case DS_GET_NEXT_WINDOW:
  1380. ret = pcmcia_get_window(s->parent, &buf->win_info.handle,
  1381. buf->win_info.handle->index + 1, &buf->win_info.window);
  1382. break;
  1383. case DS_GET_MEM_PAGE:
  1384. ret = pcmcia_get_mem_page(buf->win_info.handle,
  1385. &buf->win_info.map);
  1386. break;
  1387. case DS_REPLACE_CIS:
  1388. ret = pcmcia_replace_cis(s->parent, &buf->cisdump);
  1389. break;
  1390. case DS_BIND_REQUEST:
  1391. if (!capable(CAP_SYS_ADMIN)) {
  1392. err = -EPERM;
  1393. goto free_out;
  1394. }
  1395. err = bind_request(s, &buf->bind_info);
  1396. break;
  1397. case DS_GET_DEVICE_INFO:
  1398. err = get_device_info(s, &buf->bind_info, 1);
  1399. break;
  1400. case DS_GET_NEXT_DEVICE:
  1401. err = get_device_info(s, &buf->bind_info, 0);
  1402. break;
  1403. case DS_UNBIND_REQUEST:
  1404. err = 0;
  1405. break;
  1406. default:
  1407. err = -EINVAL;
  1408. }
  1409. if ((err == 0) && (ret != CS_SUCCESS)) {
  1410. ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
  1411. switch (ret) {
  1412. case CS_BAD_SOCKET: case CS_NO_CARD:
  1413. err = -ENODEV; break;
  1414. case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
  1415. case CS_BAD_TUPLE:
  1416. err = -EINVAL; break;
  1417. case CS_IN_USE:
  1418. err = -EBUSY; break;
  1419. case CS_OUT_OF_RESOURCE:
  1420. err = -ENOSPC; break;
  1421. case CS_NO_MORE_ITEMS:
  1422. err = -ENODATA; break;
  1423. case CS_UNSUPPORTED_FUNCTION:
  1424. err = -ENOSYS; break;
  1425. default:
  1426. err = -EIO; break;
  1427. }
  1428. }
  1429. if (cmd & IOC_OUT) {
  1430. if (__copy_to_user(uarg, (char *)buf, size))
  1431. err = -EFAULT;
  1432. }
  1433. free_out:
  1434. kfree(buf);
  1435. return err;
  1436. } /* ds_ioctl */
  1437. /*====================================================================*/
  1438. static struct file_operations ds_fops = {
  1439. .owner = THIS_MODULE,
  1440. .open = ds_open,
  1441. .release = ds_release,
  1442. .ioctl = ds_ioctl,
  1443. .read = ds_read,
  1444. .write = ds_write,
  1445. .poll = ds_poll,
  1446. };
  1447. static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
  1448. {
  1449. struct pcmcia_socket *socket = class_get_devdata(class_dev);
  1450. struct pcmcia_bus_socket *s;
  1451. int ret;
  1452. s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL);
  1453. if(!s)
  1454. return -ENOMEM;
  1455. memset(s, 0, sizeof(struct pcmcia_bus_socket));
  1456. /* get reference to parent socket */
  1457. s->parent = pcmcia_get_socket(socket);
  1458. if (!s->parent) {
  1459. printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
  1460. kfree (s);
  1461. return -ENODEV;
  1462. }
  1463. kref_init(&s->refcount);
  1464. /*
  1465. * Ugly. But we want to wait for the socket threads to have started up.
  1466. * We really should let the drivers themselves drive some of this..
  1467. */
  1468. msleep(250);
  1469. init_waitqueue_head(&s->queue);
  1470. INIT_LIST_HEAD(&s->devices_list);
  1471. INIT_WORK(&s->device_add, pcmcia_delayed_add_pseudo_device, s);
  1472. /* Set up hotline to Card Services */
  1473. s->callback.owner = THIS_MODULE;
  1474. s->callback.event = &ds_event;
  1475. s->callback.resources_done = &pcmcia_card_add;
  1476. s->callback.replace_cis = &pcmcia_bus_rescan;
  1477. socket->pcmcia = s;
  1478. ret = pccard_register_pcmcia(socket, &s->callback);
  1479. if (ret) {
  1480. printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
  1481. pcmcia_put_bus_socket(s);
  1482. socket->pcmcia = NULL;
  1483. return (ret);
  1484. }
  1485. return 0;
  1486. }
  1487. static void pcmcia_bus_remove_socket(struct class_device *class_dev)
  1488. {
  1489. struct pcmcia_socket *socket = class_get_devdata(class_dev);
  1490. if (!socket || !socket->pcmcia)
  1491. return;
  1492. pccard_register_pcmcia(socket, NULL);
  1493. socket->pcmcia->state |= DS_SOCKET_DEAD;
  1494. pcmcia_put_bus_socket(socket->pcmcia);
  1495. socket->pcmcia = NULL;
  1496. return;
  1497. }
  1498. /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
  1499. static struct class_interface pcmcia_bus_interface = {
  1500. .class = &pcmcia_socket_class,
  1501. .add = &pcmcia_bus_add_socket,
  1502. .remove = &pcmcia_bus_remove_socket,
  1503. };
  1504. struct bus_type pcmcia_bus_type = {
  1505. .name = "pcmcia",
  1506. .hotplug = pcmcia_bus_hotplug,
  1507. .match = pcmcia_bus_match,
  1508. .dev_attrs = pcmcia_dev_attrs,
  1509. };
  1510. EXPORT_SYMBOL(pcmcia_bus_type);
  1511. static int __init init_pcmcia_bus(void)
  1512. {
  1513. int i;
  1514. spin_lock_init(&pcmcia_dev_list_lock);
  1515. bus_register(&pcmcia_bus_type);
  1516. class_interface_register(&pcmcia_bus_interface);
  1517. /* Set up character device for user mode clients */
  1518. i = register_chrdev(0, "pcmcia", &ds_fops);
  1519. if (i < 0)
  1520. printk(KERN_NOTICE "unable to find a free device # for "
  1521. "Driver Services (error=%d)\n", i);
  1522. else
  1523. major_dev = i;
  1524. #ifdef CONFIG_PROC_FS
  1525. proc_pccard = proc_mkdir("pccard", proc_bus);
  1526. if (proc_pccard)
  1527. create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
  1528. #endif
  1529. return 0;
  1530. }
  1531. fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
  1532. * pcmcia_socket_class is already registered */
  1533. static void __exit exit_pcmcia_bus(void)
  1534. {
  1535. class_interface_unregister(&pcmcia_bus_interface);
  1536. #ifdef CONFIG_PROC_FS
  1537. if (proc_pccard) {
  1538. remove_proc_entry("drivers", proc_pccard);
  1539. remove_proc_entry("pccard", proc_bus);
  1540. }
  1541. #endif
  1542. if (major_dev != -1)
  1543. unregister_chrdev(major_dev, "pcmcia");
  1544. bus_unregister(&pcmcia_bus_type);
  1545. }
  1546. module_exit(exit_pcmcia_bus);
  1547. /* helpers for backwards-compatible functions */
  1548. static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
  1549. {
  1550. struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
  1551. if (s && s->pcmcia)
  1552. return s->pcmcia;
  1553. else
  1554. return NULL;
  1555. }
  1556. /* backwards-compatible accessing of driver --- by name! */
  1557. static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
  1558. {
  1559. struct device_driver *drv;
  1560. struct pcmcia_driver *p_drv;
  1561. drv = driver_find((char *) dev_info, &pcmcia_bus_type);
  1562. if (!drv)
  1563. return NULL;
  1564. p_drv = container_of(drv, struct pcmcia_driver, drv);
  1565. return (p_drv);
  1566. }
  1567. MODULE_ALIAS("ds");