ds.c 38 KB

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