ds.c 39 KB

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