ds.c 39 KB

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