ds.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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 - 2006 Dominik Brodowski
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/errno.h>
  19. #include <linux/list.h>
  20. #include <linux/delay.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/crc32.h>
  23. #include <linux/firmware.h>
  24. #include <linux/kref.h>
  25. #define IN_CARD_SERVICES
  26. #include <pcmcia/cs_types.h>
  27. #include <pcmcia/cs.h>
  28. #include <pcmcia/cistpl.h>
  29. #include <pcmcia/ds.h>
  30. #include <pcmcia/ss.h>
  31. #include "cs_internal.h"
  32. #include "ds_internal.h"
  33. /*====================================================================*/
  34. /* Module parameters */
  35. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  36. MODULE_DESCRIPTION("PCMCIA Driver Services");
  37. MODULE_LICENSE("GPL");
  38. #ifdef DEBUG
  39. int ds_pc_debug;
  40. module_param_named(pc_debug, ds_pc_debug, int, 0644);
  41. #define ds_dbg(lvl, fmt, arg...) do { \
  42. if (ds_pc_debug > (lvl)) \
  43. printk(KERN_DEBUG "ds: " fmt , ## arg); \
  44. } while (0)
  45. #else
  46. #define ds_dbg(lvl, fmt, arg...) do { } while (0)
  47. #endif
  48. spinlock_t pcmcia_dev_list_lock;
  49. /*====================================================================*/
  50. /* code which was in cs.c before */
  51. /* String tables for error messages */
  52. typedef struct lookup_t {
  53. int key;
  54. char *msg;
  55. } lookup_t;
  56. static const lookup_t error_table[] = {
  57. { CS_SUCCESS, "Operation succeeded" },
  58. { CS_BAD_ADAPTER, "Bad adapter" },
  59. { CS_BAD_ATTRIBUTE, "Bad attribute", },
  60. { CS_BAD_BASE, "Bad base address" },
  61. { CS_BAD_EDC, "Bad EDC" },
  62. { CS_BAD_IRQ, "Bad IRQ" },
  63. { CS_BAD_OFFSET, "Bad offset" },
  64. { CS_BAD_PAGE, "Bad page number" },
  65. { CS_READ_FAILURE, "Read failure" },
  66. { CS_BAD_SIZE, "Bad size" },
  67. { CS_BAD_SOCKET, "Bad socket" },
  68. { CS_BAD_TYPE, "Bad type" },
  69. { CS_BAD_VCC, "Bad Vcc" },
  70. { CS_BAD_VPP, "Bad Vpp" },
  71. { CS_BAD_WINDOW, "Bad window" },
  72. { CS_WRITE_FAILURE, "Write failure" },
  73. { CS_NO_CARD, "No card present" },
  74. { CS_UNSUPPORTED_FUNCTION, "Usupported function" },
  75. { CS_UNSUPPORTED_MODE, "Unsupported mode" },
  76. { CS_BAD_SPEED, "Bad speed" },
  77. { CS_BUSY, "Resource busy" },
  78. { CS_GENERAL_FAILURE, "General failure" },
  79. { CS_WRITE_PROTECTED, "Write protected" },
  80. { CS_BAD_ARG_LENGTH, "Bad argument length" },
  81. { CS_BAD_ARGS, "Bad arguments" },
  82. { CS_CONFIGURATION_LOCKED, "Configuration locked" },
  83. { CS_IN_USE, "Resource in use" },
  84. { CS_NO_MORE_ITEMS, "No more items" },
  85. { CS_OUT_OF_RESOURCE, "Out of resource" },
  86. { CS_BAD_HANDLE, "Bad handle" },
  87. { CS_BAD_TUPLE, "Bad CIS tuple" }
  88. };
  89. static const lookup_t service_table[] = {
  90. { AccessConfigurationRegister, "AccessConfigurationRegister" },
  91. { AddSocketServices, "AddSocketServices" },
  92. { AdjustResourceInfo, "AdjustResourceInfo" },
  93. { CheckEraseQueue, "CheckEraseQueue" },
  94. { CloseMemory, "CloseMemory" },
  95. { DeregisterClient, "DeregisterClient" },
  96. { DeregisterEraseQueue, "DeregisterEraseQueue" },
  97. { GetCardServicesInfo, "GetCardServicesInfo" },
  98. { GetClientInfo, "GetClientInfo" },
  99. { GetConfigurationInfo, "GetConfigurationInfo" },
  100. { GetEventMask, "GetEventMask" },
  101. { GetFirstClient, "GetFirstClient" },
  102. { GetFirstRegion, "GetFirstRegion" },
  103. { GetFirstTuple, "GetFirstTuple" },
  104. { GetNextClient, "GetNextClient" },
  105. { GetNextRegion, "GetNextRegion" },
  106. { GetNextTuple, "GetNextTuple" },
  107. { GetStatus, "GetStatus" },
  108. { GetTupleData, "GetTupleData" },
  109. { MapMemPage, "MapMemPage" },
  110. { ModifyConfiguration, "ModifyConfiguration" },
  111. { ModifyWindow, "ModifyWindow" },
  112. { OpenMemory, "OpenMemory" },
  113. { ParseTuple, "ParseTuple" },
  114. { ReadMemory, "ReadMemory" },
  115. { RegisterClient, "RegisterClient" },
  116. { RegisterEraseQueue, "RegisterEraseQueue" },
  117. { RegisterMTD, "RegisterMTD" },
  118. { ReleaseConfiguration, "ReleaseConfiguration" },
  119. { ReleaseIO, "ReleaseIO" },
  120. { ReleaseIRQ, "ReleaseIRQ" },
  121. { ReleaseWindow, "ReleaseWindow" },
  122. { RequestConfiguration, "RequestConfiguration" },
  123. { RequestIO, "RequestIO" },
  124. { RequestIRQ, "RequestIRQ" },
  125. { RequestSocketMask, "RequestSocketMask" },
  126. { RequestWindow, "RequestWindow" },
  127. { ResetCard, "ResetCard" },
  128. { SetEventMask, "SetEventMask" },
  129. { ValidateCIS, "ValidateCIS" },
  130. { WriteMemory, "WriteMemory" },
  131. { BindDevice, "BindDevice" },
  132. { BindMTD, "BindMTD" },
  133. { ReportError, "ReportError" },
  134. { SuspendCard, "SuspendCard" },
  135. { ResumeCard, "ResumeCard" },
  136. { EjectCard, "EjectCard" },
  137. { InsertCard, "InsertCard" },
  138. { ReplaceCIS, "ReplaceCIS" }
  139. };
  140. static int pcmcia_report_error(struct pcmcia_device *p_dev, error_info_t *err)
  141. {
  142. int i;
  143. char *serv;
  144. if (!p_dev)
  145. printk(KERN_NOTICE);
  146. else
  147. printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
  148. for (i = 0; i < ARRAY_SIZE(service_table); i++)
  149. if (service_table[i].key == err->func)
  150. break;
  151. if (i < ARRAY_SIZE(service_table))
  152. serv = service_table[i].msg;
  153. else
  154. serv = "Unknown service number";
  155. for (i = 0; i < ARRAY_SIZE(error_table); i++)
  156. if (error_table[i].key == err->retcode)
  157. break;
  158. if (i < ARRAY_SIZE(error_table))
  159. printk("%s: %s\n", serv, error_table[i].msg);
  160. else
  161. printk("%s: Unknown error code %#x\n", serv, err->retcode);
  162. return CS_SUCCESS;
  163. } /* report_error */
  164. /* end of code which was in cs.c before */
  165. /*======================================================================*/
  166. void cs_error(struct pcmcia_device *p_dev, int func, int ret)
  167. {
  168. error_info_t err = { func, ret };
  169. pcmcia_report_error(p_dev, &err);
  170. }
  171. EXPORT_SYMBOL(cs_error);
  172. static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
  173. {
  174. struct pcmcia_device_id *did = p_drv->id_table;
  175. unsigned int i;
  176. u32 hash;
  177. if (!p_drv->probe || !p_drv->remove)
  178. printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
  179. "function\n", p_drv->drv.name);
  180. while (did && did->match_flags) {
  181. for (i=0; i<4; i++) {
  182. if (!did->prod_id[i])
  183. continue;
  184. hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
  185. if (hash == did->prod_id_hash[i])
  186. continue;
  187. printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
  188. "product string \"%s\": is 0x%x, should "
  189. "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
  190. did->prod_id_hash[i], hash);
  191. printk(KERN_DEBUG "pcmcia: see "
  192. "Documentation/pcmcia/devicetable.txt for "
  193. "details\n");
  194. }
  195. did++;
  196. }
  197. return;
  198. }
  199. /*======================================================================*/
  200. struct pcmcia_dynid {
  201. struct list_head node;
  202. struct pcmcia_device_id id;
  203. };
  204. /**
  205. * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
  206. * @driver: target device driver
  207. * @buf: buffer for scanning device ID data
  208. * @count: input size
  209. *
  210. * Adds a new dynamic PCMCIA device ID to this driver,
  211. * and causes the driver to probe for all devices again.
  212. */
  213. static ssize_t
  214. pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
  215. {
  216. struct pcmcia_dynid *dynid;
  217. struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
  218. __u16 match_flags, manf_id, card_id;
  219. __u8 func_id, function, device_no;
  220. __u32 prod_id_hash[4] = {0, 0, 0, 0};
  221. int fields=0;
  222. int retval = 0;
  223. fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
  224. &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
  225. &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
  226. if (fields < 6)
  227. return -EINVAL;
  228. dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
  229. if (!dynid)
  230. return -ENOMEM;
  231. INIT_LIST_HEAD(&dynid->node);
  232. dynid->id.match_flags = match_flags;
  233. dynid->id.manf_id = manf_id;
  234. dynid->id.card_id = card_id;
  235. dynid->id.func_id = func_id;
  236. dynid->id.function = function;
  237. dynid->id.device_no = device_no;
  238. memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
  239. spin_lock(&pdrv->dynids.lock);
  240. list_add_tail(&pdrv->dynids.list, &dynid->node);
  241. spin_unlock(&pdrv->dynids.lock);
  242. if (get_driver(&pdrv->drv)) {
  243. retval = driver_attach(&pdrv->drv);
  244. put_driver(&pdrv->drv);
  245. }
  246. if (retval)
  247. return retval;
  248. return count;
  249. }
  250. static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
  251. static void
  252. pcmcia_free_dynids(struct pcmcia_driver *drv)
  253. {
  254. struct pcmcia_dynid *dynid, *n;
  255. spin_lock(&drv->dynids.lock);
  256. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  257. list_del(&dynid->node);
  258. kfree(dynid);
  259. }
  260. spin_unlock(&drv->dynids.lock);
  261. }
  262. static int
  263. pcmcia_create_newid_file(struct pcmcia_driver *drv)
  264. {
  265. int error = 0;
  266. if (drv->probe != NULL)
  267. error = sysfs_create_file(&drv->drv.kobj,
  268. &driver_attr_new_id.attr);
  269. return error;
  270. }
  271. /**
  272. * pcmcia_register_driver - register a PCMCIA driver with the bus core
  273. *
  274. * Registers a PCMCIA driver with the PCMCIA bus core.
  275. */
  276. int pcmcia_register_driver(struct pcmcia_driver *driver)
  277. {
  278. int error;
  279. if (!driver)
  280. return -EINVAL;
  281. pcmcia_check_driver(driver);
  282. /* initialize common fields */
  283. driver->drv.bus = &pcmcia_bus_type;
  284. driver->drv.owner = driver->owner;
  285. spin_lock_init(&driver->dynids.lock);
  286. INIT_LIST_HEAD(&driver->dynids.list);
  287. ds_dbg(3, "registering driver %s\n", driver->drv.name);
  288. error = driver_register(&driver->drv);
  289. if (error < 0)
  290. return error;
  291. error = pcmcia_create_newid_file(driver);
  292. if (error)
  293. driver_unregister(&driver->drv);
  294. return error;
  295. }
  296. EXPORT_SYMBOL(pcmcia_register_driver);
  297. /**
  298. * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
  299. */
  300. void pcmcia_unregister_driver(struct pcmcia_driver *driver)
  301. {
  302. ds_dbg(3, "unregistering driver %s\n", driver->drv.name);
  303. driver_unregister(&driver->drv);
  304. pcmcia_free_dynids(driver);
  305. }
  306. EXPORT_SYMBOL(pcmcia_unregister_driver);
  307. /* pcmcia_device handling */
  308. struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
  309. {
  310. struct device *tmp_dev;
  311. tmp_dev = get_device(&p_dev->dev);
  312. if (!tmp_dev)
  313. return NULL;
  314. return to_pcmcia_dev(tmp_dev);
  315. }
  316. void pcmcia_put_dev(struct pcmcia_device *p_dev)
  317. {
  318. if (p_dev)
  319. put_device(&p_dev->dev);
  320. }
  321. static void pcmcia_release_function(struct kref *ref)
  322. {
  323. struct config_t *c = container_of(ref, struct config_t, ref);
  324. ds_dbg(1, "releasing config_t\n");
  325. kfree(c);
  326. }
  327. static void pcmcia_release_dev(struct device *dev)
  328. {
  329. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  330. ds_dbg(1, "releasing device %s\n", p_dev->dev.bus_id);
  331. pcmcia_put_socket(p_dev->socket);
  332. kfree(p_dev->devname);
  333. kref_put(&p_dev->function_config->ref, pcmcia_release_function);
  334. kfree(p_dev);
  335. }
  336. static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
  337. {
  338. if (!s->pcmcia_state.device_add_pending) {
  339. ds_dbg(1, "scheduling to add %s secondary"
  340. " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
  341. s->pcmcia_state.device_add_pending = 1;
  342. s->pcmcia_state.mfc_pfc = mfc;
  343. schedule_work(&s->device_add);
  344. }
  345. return;
  346. }
  347. static int pcmcia_device_probe(struct device * dev)
  348. {
  349. struct pcmcia_device *p_dev;
  350. struct pcmcia_driver *p_drv;
  351. struct pcmcia_device_id *did;
  352. struct pcmcia_socket *s;
  353. cistpl_config_t cis_config;
  354. int ret = 0;
  355. dev = get_device(dev);
  356. if (!dev)
  357. return -ENODEV;
  358. p_dev = to_pcmcia_dev(dev);
  359. p_drv = to_pcmcia_drv(dev->driver);
  360. s = p_dev->socket;
  361. ds_dbg(1, "trying to bind %s to %s\n", p_dev->dev.bus_id,
  362. p_drv->drv.name);
  363. if ((!p_drv->probe) || (!p_dev->function_config) ||
  364. (!try_module_get(p_drv->owner))) {
  365. ret = -EINVAL;
  366. goto put_dev;
  367. }
  368. /* set up some more device information */
  369. ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
  370. &cis_config);
  371. if (!ret) {
  372. p_dev->conf.ConfigBase = cis_config.base;
  373. p_dev->conf.Present = cis_config.rmask[0];
  374. } else {
  375. printk(KERN_INFO "pcmcia: could not parse base and rmask0 of CIS\n");
  376. p_dev->conf.ConfigBase = 0;
  377. p_dev->conf.Present = 0;
  378. }
  379. ret = p_drv->probe(p_dev);
  380. if (ret) {
  381. ds_dbg(1, "binding %s to %s failed with %d\n",
  382. p_dev->dev.bus_id, p_drv->drv.name, ret);
  383. goto put_module;
  384. }
  385. /* handle pseudo multifunction devices:
  386. * there are at most two pseudo multifunction devices.
  387. * if we're matching against the first, schedule a
  388. * call which will then check whether there are two
  389. * pseudo devices, and if not, add the second one.
  390. */
  391. did = p_dev->dev.driver_data;
  392. if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
  393. (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
  394. pcmcia_add_device_later(p_dev->socket, 0);
  395. put_module:
  396. if (ret)
  397. module_put(p_drv->owner);
  398. put_dev:
  399. if (ret)
  400. put_device(dev);
  401. return (ret);
  402. }
  403. /*
  404. * Removes a PCMCIA card from the device tree and socket list.
  405. */
  406. static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
  407. {
  408. struct pcmcia_device *p_dev;
  409. struct pcmcia_device *tmp;
  410. unsigned long flags;
  411. ds_dbg(2, "pcmcia_card_remove(%d) %s\n", s->sock,
  412. leftover ? leftover->devname : "");
  413. if (!leftover)
  414. s->device_count = 0;
  415. else
  416. s->device_count = 1;
  417. /* unregister all pcmcia_devices registered with this socket, except leftover */
  418. list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
  419. if (p_dev == leftover)
  420. continue;
  421. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  422. list_del(&p_dev->socket_device_list);
  423. p_dev->_removed=1;
  424. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  425. ds_dbg(2, "unregistering device %s\n", p_dev->dev.bus_id);
  426. device_unregister(&p_dev->dev);
  427. }
  428. return;
  429. }
  430. static int pcmcia_device_remove(struct device * dev)
  431. {
  432. struct pcmcia_device *p_dev;
  433. struct pcmcia_driver *p_drv;
  434. struct pcmcia_device_id *did;
  435. int i;
  436. p_dev = to_pcmcia_dev(dev);
  437. p_drv = to_pcmcia_drv(dev->driver);
  438. ds_dbg(1, "removing device %s\n", p_dev->dev.bus_id);
  439. /* If we're removing the primary module driving a
  440. * pseudo multi-function card, we need to unbind
  441. * all devices
  442. */
  443. did = p_dev->dev.driver_data;
  444. if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
  445. (p_dev->socket->device_count != 0) &&
  446. (p_dev->device_no == 0))
  447. pcmcia_card_remove(p_dev->socket, p_dev);
  448. /* detach the "instance" */
  449. if (!p_drv)
  450. return 0;
  451. if (p_drv->remove)
  452. p_drv->remove(p_dev);
  453. p_dev->dev_node = NULL;
  454. /* check for proper unloading */
  455. if (p_dev->_irq || p_dev->_io || p_dev->_locked)
  456. printk(KERN_INFO "pcmcia: driver %s did not release config properly\n",
  457. p_drv->drv.name);
  458. for (i = 0; i < MAX_WIN; i++)
  459. if (p_dev->_win & CLIENT_WIN_REQ(i))
  460. printk(KERN_INFO "pcmcia: driver %s did not release windows properly\n",
  461. p_drv->drv.name);
  462. /* references from pcmcia_probe_device */
  463. pcmcia_put_dev(p_dev);
  464. module_put(p_drv->owner);
  465. return 0;
  466. }
  467. /*
  468. * pcmcia_device_query -- determine information about a pcmcia device
  469. */
  470. static int pcmcia_device_query(struct pcmcia_device *p_dev)
  471. {
  472. cistpl_manfid_t manf_id;
  473. cistpl_funcid_t func_id;
  474. cistpl_vers_1_t *vers1;
  475. unsigned int i;
  476. vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
  477. if (!vers1)
  478. return -ENOMEM;
  479. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  480. CISTPL_MANFID, &manf_id)) {
  481. p_dev->manf_id = manf_id.manf;
  482. p_dev->card_id = manf_id.card;
  483. p_dev->has_manf_id = 1;
  484. p_dev->has_card_id = 1;
  485. }
  486. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  487. CISTPL_FUNCID, &func_id)) {
  488. p_dev->func_id = func_id.func;
  489. p_dev->has_func_id = 1;
  490. } else {
  491. /* rule of thumb: cards with no FUNCID, but with
  492. * common memory device geometry information, are
  493. * probably memory cards (from pcmcia-cs) */
  494. cistpl_device_geo_t *devgeo;
  495. devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
  496. if (!devgeo) {
  497. kfree(vers1);
  498. return -ENOMEM;
  499. }
  500. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  501. CISTPL_DEVICE_GEO, devgeo)) {
  502. ds_dbg(0, "mem device geometry probably means "
  503. "FUNCID_MEMORY\n");
  504. p_dev->func_id = CISTPL_FUNCID_MEMORY;
  505. p_dev->has_func_id = 1;
  506. }
  507. kfree(devgeo);
  508. }
  509. if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
  510. vers1)) {
  511. for (i=0; i < vers1->ns; i++) {
  512. char *tmp;
  513. unsigned int length;
  514. tmp = vers1->str + vers1->ofs[i];
  515. length = strlen(tmp) + 1;
  516. if ((length < 2) || (length > 255))
  517. continue;
  518. p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
  519. GFP_KERNEL);
  520. if (!p_dev->prod_id[i])
  521. continue;
  522. p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
  523. tmp, length);
  524. }
  525. }
  526. kfree(vers1);
  527. return 0;
  528. }
  529. /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
  530. * Serializes pcmcia_device_add; will most likely be removed in future.
  531. *
  532. * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
  533. * won't work, this doesn't matter much at the moment: the driver core doesn't
  534. * support it either.
  535. */
  536. static DEFINE_MUTEX(device_add_lock);
  537. struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
  538. {
  539. struct pcmcia_device *p_dev, *tmp_dev;
  540. unsigned long flags;
  541. int bus_id_len;
  542. s = pcmcia_get_socket(s);
  543. if (!s)
  544. return NULL;
  545. mutex_lock(&device_add_lock);
  546. ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
  547. /* max of 4 devices per card */
  548. if (s->device_count == 4)
  549. goto err_put;
  550. p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
  551. if (!p_dev)
  552. goto err_put;
  553. p_dev->socket = s;
  554. p_dev->device_no = (s->device_count++);
  555. p_dev->func = function;
  556. p_dev->dev.bus = &pcmcia_bus_type;
  557. p_dev->dev.parent = s->dev.parent;
  558. p_dev->dev.release = pcmcia_release_dev;
  559. bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
  560. p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL);
  561. if (!p_dev->devname)
  562. goto err_free;
  563. sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id);
  564. ds_dbg(3, "devname is %s\n", p_dev->devname);
  565. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  566. /*
  567. * p_dev->function_config must be the same for all card functions.
  568. * Note that this is serialized by the device_add_lock, so that
  569. * only one such struct will be created.
  570. */
  571. list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
  572. if (p_dev->func == tmp_dev->func) {
  573. p_dev->function_config = tmp_dev->function_config;
  574. kref_get(&p_dev->function_config->ref);
  575. }
  576. /* Add to the list in pcmcia_bus_socket */
  577. list_add(&p_dev->socket_device_list, &s->devices_list);
  578. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  579. if (!p_dev->function_config) {
  580. ds_dbg(3, "creating config_t for %s\n", p_dev->dev.bus_id);
  581. p_dev->function_config = kzalloc(sizeof(struct config_t),
  582. GFP_KERNEL);
  583. if (!p_dev->function_config)
  584. goto err_unreg;
  585. kref_init(&p_dev->function_config->ref);
  586. }
  587. printk(KERN_NOTICE "pcmcia: registering new device %s\n",
  588. p_dev->devname);
  589. pcmcia_device_query(p_dev);
  590. if (device_register(&p_dev->dev))
  591. goto err_unreg;
  592. mutex_unlock(&device_add_lock);
  593. return p_dev;
  594. err_unreg:
  595. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  596. list_del(&p_dev->socket_device_list);
  597. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  598. err_free:
  599. kfree(p_dev->devname);
  600. kfree(p_dev);
  601. s->device_count--;
  602. err_put:
  603. mutex_unlock(&device_add_lock);
  604. pcmcia_put_socket(s);
  605. return NULL;
  606. }
  607. static int pcmcia_card_add(struct pcmcia_socket *s)
  608. {
  609. cisinfo_t cisinfo;
  610. cistpl_longlink_mfc_t mfc;
  611. unsigned int no_funcs, i;
  612. int ret = 0;
  613. if (!(s->resource_setup_done)) {
  614. ds_dbg(3, "no resources available, delaying card_add\n");
  615. return -EAGAIN; /* try again, but later... */
  616. }
  617. if (pcmcia_validate_mem(s)) {
  618. ds_dbg(3, "validating mem resources failed, "
  619. "delaying card_add\n");
  620. return -EAGAIN; /* try again, but later... */
  621. }
  622. ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
  623. if (ret || !cisinfo.Chains) {
  624. ds_dbg(0, "invalid CIS or invalid resources\n");
  625. return -ENODEV;
  626. }
  627. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  628. no_funcs = mfc.nfn;
  629. else
  630. no_funcs = 1;
  631. s->functions = no_funcs;
  632. for (i=0; i < no_funcs; i++)
  633. pcmcia_device_add(s, i);
  634. return (ret);
  635. }
  636. static void pcmcia_delayed_add_device(struct work_struct *work)
  637. {
  638. struct pcmcia_socket *s =
  639. container_of(work, struct pcmcia_socket, device_add);
  640. ds_dbg(1, "adding additional device to %d\n", s->sock);
  641. pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
  642. s->pcmcia_state.device_add_pending = 0;
  643. s->pcmcia_state.mfc_pfc = 0;
  644. }
  645. static int pcmcia_requery(struct device *dev, void * _data)
  646. {
  647. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  648. if (!p_dev->dev.driver) {
  649. ds_dbg(1, "update device information for %s\n",
  650. p_dev->dev.bus_id);
  651. pcmcia_device_query(p_dev);
  652. }
  653. return 0;
  654. }
  655. static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
  656. {
  657. int no_devices = 0;
  658. int ret = 0;
  659. unsigned long flags;
  660. /* must be called with skt_mutex held */
  661. ds_dbg(0, "re-scanning socket %d\n", skt->sock);
  662. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  663. if (list_empty(&skt->devices_list))
  664. no_devices = 1;
  665. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  666. /* If this is because of a CIS override, start over */
  667. if (new_cis && !no_devices)
  668. pcmcia_card_remove(skt, NULL);
  669. /* if no devices were added for this socket yet because of
  670. * missing resource information or other trouble, we need to
  671. * do this now. */
  672. if (no_devices || new_cis) {
  673. ret = pcmcia_card_add(skt);
  674. if (ret)
  675. return;
  676. }
  677. /* some device information might have changed because of a CIS
  678. * update or because we can finally read it correctly... so
  679. * determine it again, overwriting old values if necessary. */
  680. bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
  681. /* we re-scan all devices, not just the ones connected to this
  682. * socket. This does not matter, though. */
  683. ret = bus_rescan_devices(&pcmcia_bus_type);
  684. if (ret)
  685. printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
  686. }
  687. #ifdef CONFIG_PCMCIA_LOAD_CIS
  688. /**
  689. * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
  690. * @dev - the pcmcia device which needs a CIS override
  691. * @filename - requested filename in /lib/firmware/
  692. *
  693. * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
  694. * the one provided by the card is broken. The firmware files reside in
  695. * /lib/firmware/ in userspace.
  696. */
  697. static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  698. {
  699. struct pcmcia_socket *s = dev->socket;
  700. const struct firmware *fw;
  701. char path[20];
  702. int ret = -ENOMEM;
  703. int no_funcs;
  704. int old_funcs;
  705. cisdump_t *cis;
  706. cistpl_longlink_mfc_t mfc;
  707. if (!filename)
  708. return -EINVAL;
  709. ds_dbg(1, "trying to load CIS file %s\n", filename);
  710. if (strlen(filename) > 14) {
  711. printk(KERN_WARNING "pcmcia: CIS filename is too long\n");
  712. return -EINVAL;
  713. }
  714. snprintf(path, 20, "%s", filename);
  715. if (request_firmware(&fw, path, &dev->dev) == 0) {
  716. if (fw->size >= CISTPL_MAX_CIS_SIZE) {
  717. ret = -EINVAL;
  718. printk(KERN_ERR "pcmcia: CIS override is too big\n");
  719. goto release;
  720. }
  721. cis = kzalloc(sizeof(cisdump_t), GFP_KERNEL);
  722. if (!cis) {
  723. ret = -ENOMEM;
  724. goto release;
  725. }
  726. cis->Length = fw->size + 1;
  727. memcpy(cis->Data, fw->data, fw->size);
  728. if (!pcmcia_replace_cis(s, cis))
  729. ret = 0;
  730. else {
  731. printk(KERN_ERR "pcmcia: CIS override failed\n");
  732. goto release;
  733. }
  734. /* update information */
  735. pcmcia_device_query(dev);
  736. /* does this cis override add or remove functions? */
  737. old_funcs = s->functions;
  738. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  739. no_funcs = mfc.nfn;
  740. else
  741. no_funcs = 1;
  742. s->functions = no_funcs;
  743. if (old_funcs > no_funcs)
  744. pcmcia_card_remove(s, dev);
  745. else if (no_funcs > old_funcs)
  746. pcmcia_add_device_later(s, 1);
  747. }
  748. release:
  749. release_firmware(fw);
  750. return (ret);
  751. }
  752. #else /* !CONFIG_PCMCIA_LOAD_CIS */
  753. static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  754. {
  755. return -ENODEV;
  756. }
  757. #endif
  758. static inline int pcmcia_devmatch(struct pcmcia_device *dev,
  759. struct pcmcia_device_id *did)
  760. {
  761. if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
  762. if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
  763. return 0;
  764. }
  765. if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
  766. if ((!dev->has_card_id) || (dev->card_id != did->card_id))
  767. return 0;
  768. }
  769. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
  770. if (dev->func != did->function)
  771. return 0;
  772. }
  773. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
  774. if (!dev->prod_id[0])
  775. return 0;
  776. if (strcmp(did->prod_id[0], dev->prod_id[0]))
  777. return 0;
  778. }
  779. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
  780. if (!dev->prod_id[1])
  781. return 0;
  782. if (strcmp(did->prod_id[1], dev->prod_id[1]))
  783. return 0;
  784. }
  785. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
  786. if (!dev->prod_id[2])
  787. return 0;
  788. if (strcmp(did->prod_id[2], dev->prod_id[2]))
  789. return 0;
  790. }
  791. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
  792. if (!dev->prod_id[3])
  793. return 0;
  794. if (strcmp(did->prod_id[3], dev->prod_id[3]))
  795. return 0;
  796. }
  797. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
  798. if (dev->device_no != did->device_no)
  799. return 0;
  800. }
  801. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
  802. if ((!dev->has_func_id) || (dev->func_id != did->func_id))
  803. return 0;
  804. /* if this is a pseudo-multi-function device,
  805. * we need explicit matches */
  806. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
  807. return 0;
  808. if (dev->device_no)
  809. return 0;
  810. /* also, FUNC_ID matching needs to be activated by userspace
  811. * after it has re-checked that there is no possible module
  812. * with a prod_id/manf_id/card_id match.
  813. */
  814. ds_dbg(0, "skipping FUNC_ID match for %s until userspace "
  815. "interaction\n", dev->dev.bus_id);
  816. if (!dev->allow_func_id_match)
  817. return 0;
  818. }
  819. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
  820. ds_dbg(0, "device %s needs a fake CIS\n", dev->dev.bus_id);
  821. if (!dev->socket->fake_cis)
  822. pcmcia_load_firmware(dev, did->cisfile);
  823. if (!dev->socket->fake_cis)
  824. return 0;
  825. }
  826. if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
  827. int i;
  828. for (i=0; i<4; i++)
  829. if (dev->prod_id[i])
  830. return 0;
  831. if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
  832. return 0;
  833. }
  834. dev->dev.driver_data = (void *) did;
  835. return 1;
  836. }
  837. static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
  838. struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
  839. struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
  840. struct pcmcia_device_id *did = p_drv->id_table;
  841. struct pcmcia_dynid *dynid;
  842. /* match dynamic devices first */
  843. spin_lock(&p_drv->dynids.lock);
  844. list_for_each_entry(dynid, &p_drv->dynids.list, node) {
  845. ds_dbg(3, "trying to match %s to %s\n", dev->bus_id,
  846. drv->name);
  847. if (pcmcia_devmatch(p_dev, &dynid->id)) {
  848. ds_dbg(0, "matched %s to %s\n", dev->bus_id,
  849. drv->name);
  850. spin_unlock(&p_drv->dynids.lock);
  851. return 1;
  852. }
  853. }
  854. spin_unlock(&p_drv->dynids.lock);
  855. #ifdef CONFIG_PCMCIA_IOCTL
  856. /* matching by cardmgr */
  857. if (p_dev->cardmgr == p_drv) {
  858. ds_dbg(0, "cardmgr matched %s to %s\n", dev->bus_id,
  859. drv->name);
  860. return 1;
  861. }
  862. #endif
  863. while (did && did->match_flags) {
  864. ds_dbg(3, "trying to match %s to %s\n", dev->bus_id,
  865. drv->name);
  866. if (pcmcia_devmatch(p_dev, did)) {
  867. ds_dbg(0, "matched %s to %s\n", dev->bus_id,
  868. drv->name);
  869. return 1;
  870. }
  871. did++;
  872. }
  873. return 0;
  874. }
  875. #ifdef CONFIG_HOTPLUG
  876. static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  877. {
  878. struct pcmcia_device *p_dev;
  879. int i;
  880. u32 hash[4] = { 0, 0, 0, 0};
  881. if (!dev)
  882. return -ENODEV;
  883. p_dev = to_pcmcia_dev(dev);
  884. /* calculate hashes */
  885. for (i=0; i<4; i++) {
  886. if (!p_dev->prod_id[i])
  887. continue;
  888. hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
  889. }
  890. if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
  891. return -ENOMEM;
  892. if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
  893. return -ENOMEM;
  894. if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  895. "pa%08Xpb%08Xpc%08Xpd%08X",
  896. p_dev->has_manf_id ? p_dev->manf_id : 0,
  897. p_dev->has_card_id ? p_dev->card_id : 0,
  898. p_dev->has_func_id ? p_dev->func_id : 0,
  899. p_dev->func,
  900. p_dev->device_no,
  901. hash[0],
  902. hash[1],
  903. hash[2],
  904. hash[3]))
  905. return -ENOMEM;
  906. return 0;
  907. }
  908. #else
  909. static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  910. {
  911. return -ENODEV;
  912. }
  913. #endif
  914. /************************ runtime PM support ***************************/
  915. static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
  916. static int pcmcia_dev_resume(struct device *dev);
  917. static int runtime_suspend(struct device *dev)
  918. {
  919. int rc;
  920. down(&dev->sem);
  921. rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
  922. up(&dev->sem);
  923. if (!rc)
  924. dev->power.power_state.event = PM_EVENT_SUSPEND;
  925. return rc;
  926. }
  927. static void runtime_resume(struct device *dev)
  928. {
  929. int rc;
  930. down(&dev->sem);
  931. rc = pcmcia_dev_resume(dev);
  932. up(&dev->sem);
  933. if (!rc)
  934. dev->power.power_state.event = PM_EVENT_ON;
  935. }
  936. /************************ per-device sysfs output ***************************/
  937. #define pcmcia_device_attr(field, test, format) \
  938. static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  939. { \
  940. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  941. return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
  942. }
  943. #define pcmcia_device_stringattr(name, field) \
  944. static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  945. { \
  946. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  947. return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
  948. }
  949. pcmcia_device_attr(func, socket, "0x%02x\n");
  950. pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
  951. pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
  952. pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
  953. pcmcia_device_stringattr(prod_id1, prod_id[0]);
  954. pcmcia_device_stringattr(prod_id2, prod_id[1]);
  955. pcmcia_device_stringattr(prod_id3, prod_id[2]);
  956. pcmcia_device_stringattr(prod_id4, prod_id[3]);
  957. static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
  958. {
  959. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  960. if (p_dev->suspended)
  961. return sprintf(buf, "off\n");
  962. else
  963. return sprintf(buf, "on\n");
  964. }
  965. static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
  966. const char *buf, size_t count)
  967. {
  968. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  969. int ret = 0;
  970. if (!count)
  971. return -EINVAL;
  972. if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
  973. ret = runtime_suspend(dev);
  974. else if (p_dev->suspended && !strncmp(buf, "on", 2))
  975. runtime_resume(dev);
  976. return ret ? ret : count;
  977. }
  978. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  979. {
  980. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  981. int i;
  982. u32 hash[4] = { 0, 0, 0, 0};
  983. /* calculate hashes */
  984. for (i=0; i<4; i++) {
  985. if (!p_dev->prod_id[i])
  986. continue;
  987. hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
  988. }
  989. return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  990. "pa%08Xpb%08Xpc%08Xpd%08X\n",
  991. p_dev->has_manf_id ? p_dev->manf_id : 0,
  992. p_dev->has_card_id ? p_dev->card_id : 0,
  993. p_dev->has_func_id ? p_dev->func_id : 0,
  994. p_dev->func, p_dev->device_no,
  995. hash[0], hash[1], hash[2], hash[3]);
  996. }
  997. static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
  998. struct device_attribute *attr, const char *buf, size_t count)
  999. {
  1000. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1001. int ret;
  1002. if (!count)
  1003. return -EINVAL;
  1004. mutex_lock(&p_dev->socket->skt_mutex);
  1005. p_dev->allow_func_id_match = 1;
  1006. mutex_unlock(&p_dev->socket->skt_mutex);
  1007. ret = bus_rescan_devices(&pcmcia_bus_type);
  1008. if (ret)
  1009. printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
  1010. "allowing func_id matches\n");
  1011. return count;
  1012. }
  1013. static struct device_attribute pcmcia_dev_attrs[] = {
  1014. __ATTR(function, 0444, func_show, NULL),
  1015. __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
  1016. __ATTR_RO(func_id),
  1017. __ATTR_RO(manf_id),
  1018. __ATTR_RO(card_id),
  1019. __ATTR_RO(prod_id1),
  1020. __ATTR_RO(prod_id2),
  1021. __ATTR_RO(prod_id3),
  1022. __ATTR_RO(prod_id4),
  1023. __ATTR_RO(modalias),
  1024. __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
  1025. __ATTR_NULL,
  1026. };
  1027. /* PM support, also needed for reset */
  1028. static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
  1029. {
  1030. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1031. struct pcmcia_driver *p_drv = NULL;
  1032. int ret = 0;
  1033. ds_dbg(2, "suspending %s\n", dev->bus_id);
  1034. if (dev->driver)
  1035. p_drv = to_pcmcia_drv(dev->driver);
  1036. if (!p_drv)
  1037. goto out;
  1038. if (p_drv->suspend) {
  1039. ret = p_drv->suspend(p_dev);
  1040. if (ret) {
  1041. printk(KERN_ERR "pcmcia: device %s (driver %s) did "
  1042. "not want to go to sleep (%d)\n",
  1043. p_dev->devname, p_drv->drv.name, ret);
  1044. goto out;
  1045. }
  1046. }
  1047. if (p_dev->device_no == p_dev->func) {
  1048. ds_dbg(2, "releasing configuration for %s\n", dev->bus_id);
  1049. pcmcia_release_configuration(p_dev);
  1050. }
  1051. out:
  1052. if (!ret)
  1053. p_dev->suspended = 1;
  1054. return ret;
  1055. }
  1056. static int pcmcia_dev_resume(struct device * dev)
  1057. {
  1058. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1059. struct pcmcia_driver *p_drv = NULL;
  1060. int ret = 0;
  1061. ds_dbg(2, "resuming %s\n", dev->bus_id);
  1062. if (dev->driver)
  1063. p_drv = to_pcmcia_drv(dev->driver);
  1064. if (!p_drv)
  1065. goto out;
  1066. if (p_dev->device_no == p_dev->func) {
  1067. ds_dbg(2, "requesting configuration for %s\n", dev->bus_id);
  1068. ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
  1069. if (ret)
  1070. goto out;
  1071. }
  1072. if (p_drv->resume)
  1073. ret = p_drv->resume(p_dev);
  1074. out:
  1075. if (!ret)
  1076. p_dev->suspended = 0;
  1077. return ret;
  1078. }
  1079. static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
  1080. {
  1081. struct pcmcia_socket *skt = _data;
  1082. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1083. if (p_dev->socket != skt || p_dev->suspended)
  1084. return 0;
  1085. return runtime_suspend(dev);
  1086. }
  1087. static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
  1088. {
  1089. struct pcmcia_socket *skt = _data;
  1090. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1091. if (p_dev->socket != skt || !p_dev->suspended)
  1092. return 0;
  1093. runtime_resume(dev);
  1094. return 0;
  1095. }
  1096. static int pcmcia_bus_resume(struct pcmcia_socket *skt)
  1097. {
  1098. ds_dbg(2, "resuming socket %d\n", skt->sock);
  1099. bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
  1100. return 0;
  1101. }
  1102. static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
  1103. {
  1104. ds_dbg(2, "suspending socket %d\n", skt->sock);
  1105. if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
  1106. pcmcia_bus_suspend_callback)) {
  1107. pcmcia_bus_resume(skt);
  1108. return -EIO;
  1109. }
  1110. return 0;
  1111. }
  1112. /*======================================================================
  1113. The card status event handler.
  1114. ======================================================================*/
  1115. /* Normally, the event is passed to individual drivers after
  1116. * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
  1117. * is inversed to maintain historic compatibility.
  1118. */
  1119. static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
  1120. {
  1121. struct pcmcia_socket *s = pcmcia_get_socket(skt);
  1122. if (!s) {
  1123. printk(KERN_ERR "PCMCIA obtaining reference to socket %p " \
  1124. "failed, event 0x%x lost!\n", skt, event);
  1125. return -ENODEV;
  1126. }
  1127. ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
  1128. event, priority, skt);
  1129. switch (event) {
  1130. case CS_EVENT_CARD_REMOVAL:
  1131. s->pcmcia_state.present = 0;
  1132. pcmcia_card_remove(skt, NULL);
  1133. handle_event(skt, event);
  1134. break;
  1135. case CS_EVENT_CARD_INSERTION:
  1136. s->pcmcia_state.present = 1;
  1137. pcmcia_card_add(skt);
  1138. handle_event(skt, event);
  1139. break;
  1140. case CS_EVENT_EJECTION_REQUEST:
  1141. break;
  1142. case CS_EVENT_PM_SUSPEND:
  1143. case CS_EVENT_PM_RESUME:
  1144. case CS_EVENT_RESET_PHYSICAL:
  1145. case CS_EVENT_CARD_RESET:
  1146. default:
  1147. handle_event(skt, event);
  1148. break;
  1149. }
  1150. pcmcia_put_socket(s);
  1151. return 0;
  1152. } /* ds_event */
  1153. struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
  1154. {
  1155. struct pcmcia_device *p_dev;
  1156. struct pcmcia_device *ret = NULL;
  1157. p_dev = pcmcia_get_dev(_p_dev);
  1158. if (!p_dev)
  1159. return NULL;
  1160. if (!p_dev->socket->pcmcia_state.present)
  1161. goto out;
  1162. if (p_dev->_removed)
  1163. goto out;
  1164. if (p_dev->suspended)
  1165. goto out;
  1166. ret = p_dev;
  1167. out:
  1168. pcmcia_put_dev(p_dev);
  1169. return ret;
  1170. }
  1171. EXPORT_SYMBOL(pcmcia_dev_present);
  1172. static struct pcmcia_callback pcmcia_bus_callback = {
  1173. .owner = THIS_MODULE,
  1174. .event = ds_event,
  1175. .requery = pcmcia_bus_rescan,
  1176. .suspend = pcmcia_bus_suspend,
  1177. .resume = pcmcia_bus_resume,
  1178. };
  1179. static int __devinit pcmcia_bus_add_socket(struct device *dev,
  1180. struct class_interface *class_intf)
  1181. {
  1182. struct pcmcia_socket *socket = dev_get_drvdata(dev);
  1183. int ret;
  1184. socket = pcmcia_get_socket(socket);
  1185. if (!socket) {
  1186. printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
  1187. return -ENODEV;
  1188. }
  1189. /*
  1190. * Ugly. But we want to wait for the socket threads to have started up.
  1191. * We really should let the drivers themselves drive some of this..
  1192. */
  1193. msleep(250);
  1194. #ifdef CONFIG_PCMCIA_IOCTL
  1195. init_waitqueue_head(&socket->queue);
  1196. #endif
  1197. INIT_LIST_HEAD(&socket->devices_list);
  1198. INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
  1199. memset(&socket->pcmcia_state, 0, sizeof(u8));
  1200. socket->device_count = 0;
  1201. ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
  1202. if (ret) {
  1203. printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
  1204. pcmcia_put_socket(socket);
  1205. return (ret);
  1206. }
  1207. return 0;
  1208. }
  1209. static void pcmcia_bus_remove_socket(struct device *dev,
  1210. struct class_interface *class_intf)
  1211. {
  1212. struct pcmcia_socket *socket = dev_get_drvdata(dev);
  1213. if (!socket)
  1214. return;
  1215. socket->pcmcia_state.dead = 1;
  1216. pccard_register_pcmcia(socket, NULL);
  1217. /* unregister any unbound devices */
  1218. mutex_lock(&socket->skt_mutex);
  1219. pcmcia_card_remove(socket, NULL);
  1220. mutex_unlock(&socket->skt_mutex);
  1221. pcmcia_put_socket(socket);
  1222. return;
  1223. }
  1224. /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
  1225. static struct class_interface pcmcia_bus_interface = {
  1226. .class = &pcmcia_socket_class,
  1227. .add_dev = &pcmcia_bus_add_socket,
  1228. .remove_dev = &pcmcia_bus_remove_socket,
  1229. };
  1230. struct bus_type pcmcia_bus_type = {
  1231. .name = "pcmcia",
  1232. .uevent = pcmcia_bus_uevent,
  1233. .match = pcmcia_bus_match,
  1234. .dev_attrs = pcmcia_dev_attrs,
  1235. .probe = pcmcia_device_probe,
  1236. .remove = pcmcia_device_remove,
  1237. .suspend = pcmcia_dev_suspend,
  1238. .resume = pcmcia_dev_resume,
  1239. };
  1240. static int __init init_pcmcia_bus(void)
  1241. {
  1242. int ret;
  1243. spin_lock_init(&pcmcia_dev_list_lock);
  1244. ret = bus_register(&pcmcia_bus_type);
  1245. if (ret < 0) {
  1246. printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
  1247. return ret;
  1248. }
  1249. ret = class_interface_register(&pcmcia_bus_interface);
  1250. if (ret < 0) {
  1251. printk(KERN_WARNING
  1252. "pcmcia: class_interface_register error: %d\n", ret);
  1253. bus_unregister(&pcmcia_bus_type);
  1254. return ret;
  1255. }
  1256. pcmcia_setup_ioctl();
  1257. return 0;
  1258. }
  1259. fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
  1260. * pcmcia_socket_class is already registered */
  1261. static void __exit exit_pcmcia_bus(void)
  1262. {
  1263. pcmcia_cleanup_ioctl();
  1264. class_interface_unregister(&pcmcia_bus_interface);
  1265. bus_unregister(&pcmcia_bus_type);
  1266. }
  1267. module_exit(exit_pcmcia_bus);
  1268. MODULE_ALIAS("ds");