ds.c 38 KB

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