ds.c 39 KB

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