ds.c 38 KB

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