ds.c 39 KB

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