ds.c 38 KB

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