ds.c 39 KB

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