ds.c 39 KB

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