ds.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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. ds_dev_dbg(1, dev, "trying to bind to %s\n", p_drv->drv.name);
  331. if ((!p_drv->probe) || (!p_dev->function_config) ||
  332. (!try_module_get(p_drv->owner))) {
  333. ret = -EINVAL;
  334. goto put_dev;
  335. }
  336. /* set up some more device information */
  337. ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
  338. &cis_config);
  339. if (!ret) {
  340. p_dev->conf.ConfigBase = cis_config.base;
  341. p_dev->conf.Present = cis_config.rmask[0];
  342. } else {
  343. dev_printk(KERN_INFO, dev,
  344. "pcmcia: could not parse base and rmask0 of CIS\n");
  345. p_dev->conf.ConfigBase = 0;
  346. p_dev->conf.Present = 0;
  347. }
  348. ret = p_drv->probe(p_dev);
  349. if (ret) {
  350. ds_dev_dbg(1, dev, "binding to %s failed with %d\n",
  351. p_drv->drv.name, ret);
  352. goto put_module;
  353. }
  354. /* handle pseudo multifunction devices:
  355. * there are at most two pseudo multifunction devices.
  356. * if we're matching against the first, schedule a
  357. * call which will then check whether there are two
  358. * pseudo devices, and if not, add the second one.
  359. */
  360. did = p_dev->dev.driver_data;
  361. if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
  362. (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
  363. pcmcia_add_device_later(p_dev->socket, 0);
  364. put_module:
  365. if (ret)
  366. module_put(p_drv->owner);
  367. put_dev:
  368. if (ret)
  369. put_device(dev);
  370. return (ret);
  371. }
  372. /*
  373. * Removes a PCMCIA card from the device tree and socket list.
  374. */
  375. static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
  376. {
  377. struct pcmcia_device *p_dev;
  378. struct pcmcia_device *tmp;
  379. unsigned long flags;
  380. ds_dev_dbg(2, leftover ? &leftover->dev : &s->dev,
  381. "pcmcia_card_remove(%d) %s\n", s->sock,
  382. leftover ? leftover->devname : "");
  383. if (!leftover)
  384. s->device_count = 0;
  385. else
  386. s->device_count = 1;
  387. /* unregister all pcmcia_devices registered with this socket, except leftover */
  388. list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
  389. if (p_dev == leftover)
  390. continue;
  391. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  392. list_del(&p_dev->socket_device_list);
  393. p_dev->_removed=1;
  394. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  395. ds_dev_dbg(2, &p_dev->dev, "unregistering device\n");
  396. device_unregister(&p_dev->dev);
  397. }
  398. return;
  399. }
  400. static int pcmcia_device_remove(struct device * dev)
  401. {
  402. struct pcmcia_device *p_dev;
  403. struct pcmcia_driver *p_drv;
  404. struct pcmcia_device_id *did;
  405. int i;
  406. p_dev = to_pcmcia_dev(dev);
  407. p_drv = to_pcmcia_drv(dev->driver);
  408. ds_dev_dbg(1, dev, "removing device\n");
  409. /* If we're removing the primary module driving a
  410. * pseudo multi-function card, we need to unbind
  411. * all devices
  412. */
  413. did = p_dev->dev.driver_data;
  414. if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
  415. (p_dev->socket->device_count != 0) &&
  416. (p_dev->device_no == 0))
  417. pcmcia_card_remove(p_dev->socket, p_dev);
  418. /* detach the "instance" */
  419. if (!p_drv)
  420. return 0;
  421. if (p_drv->remove)
  422. p_drv->remove(p_dev);
  423. p_dev->dev_node = NULL;
  424. /* check for proper unloading */
  425. if (p_dev->_irq || p_dev->_io || p_dev->_locked)
  426. dev_printk(KERN_INFO, dev,
  427. "pcmcia: driver %s did not release config properly\n",
  428. p_drv->drv.name);
  429. for (i = 0; i < MAX_WIN; i++)
  430. if (p_dev->_win & CLIENT_WIN_REQ(i))
  431. dev_printk(KERN_INFO, dev,
  432. "pcmcia: driver %s did not release window properly\n",
  433. p_drv->drv.name);
  434. /* references from pcmcia_probe_device */
  435. pcmcia_put_dev(p_dev);
  436. module_put(p_drv->owner);
  437. return 0;
  438. }
  439. /*
  440. * pcmcia_device_query -- determine information about a pcmcia device
  441. */
  442. static int pcmcia_device_query(struct pcmcia_device *p_dev)
  443. {
  444. cistpl_manfid_t manf_id;
  445. cistpl_funcid_t func_id;
  446. cistpl_vers_1_t *vers1;
  447. unsigned int i;
  448. vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
  449. if (!vers1)
  450. return -ENOMEM;
  451. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  452. CISTPL_MANFID, &manf_id)) {
  453. p_dev->manf_id = manf_id.manf;
  454. p_dev->card_id = manf_id.card;
  455. p_dev->has_manf_id = 1;
  456. p_dev->has_card_id = 1;
  457. }
  458. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  459. CISTPL_FUNCID, &func_id)) {
  460. p_dev->func_id = func_id.func;
  461. p_dev->has_func_id = 1;
  462. } else {
  463. /* rule of thumb: cards with no FUNCID, but with
  464. * common memory device geometry information, are
  465. * probably memory cards (from pcmcia-cs) */
  466. cistpl_device_geo_t *devgeo;
  467. devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
  468. if (!devgeo) {
  469. kfree(vers1);
  470. return -ENOMEM;
  471. }
  472. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  473. CISTPL_DEVICE_GEO, devgeo)) {
  474. ds_dev_dbg(0, &p_dev->dev,
  475. "mem device geometry probably means "
  476. "FUNCID_MEMORY\n");
  477. p_dev->func_id = CISTPL_FUNCID_MEMORY;
  478. p_dev->has_func_id = 1;
  479. }
  480. kfree(devgeo);
  481. }
  482. if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
  483. vers1)) {
  484. for (i=0; i < vers1->ns; i++) {
  485. char *tmp;
  486. unsigned int length;
  487. tmp = vers1->str + vers1->ofs[i];
  488. length = strlen(tmp) + 1;
  489. if ((length < 2) || (length > 255))
  490. continue;
  491. p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
  492. GFP_KERNEL);
  493. if (!p_dev->prod_id[i])
  494. continue;
  495. p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
  496. tmp, length);
  497. }
  498. }
  499. kfree(vers1);
  500. return 0;
  501. }
  502. /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
  503. * Serializes pcmcia_device_add; will most likely be removed in future.
  504. *
  505. * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
  506. * won't work, this doesn't matter much at the moment: the driver core doesn't
  507. * support it either.
  508. */
  509. static DEFINE_MUTEX(device_add_lock);
  510. struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
  511. {
  512. struct pcmcia_device *p_dev, *tmp_dev;
  513. unsigned long flags;
  514. int bus_id_len;
  515. s = pcmcia_get_socket(s);
  516. if (!s)
  517. return NULL;
  518. mutex_lock(&device_add_lock);
  519. ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
  520. /* max of 4 devices per card */
  521. if (s->device_count == 4)
  522. goto err_put;
  523. p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
  524. if (!p_dev)
  525. goto err_put;
  526. p_dev->socket = s;
  527. p_dev->device_no = (s->device_count++);
  528. p_dev->func = function;
  529. p_dev->dev.bus = &pcmcia_bus_type;
  530. p_dev->dev.parent = s->dev.parent;
  531. p_dev->dev.release = pcmcia_release_dev;
  532. /* by default don't allow DMA */
  533. p_dev->dma_mask = DMA_MASK_NONE;
  534. p_dev->dev.dma_mask = &p_dev->dma_mask;
  535. bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
  536. p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL);
  537. if (!p_dev->devname)
  538. goto err_free;
  539. sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id);
  540. ds_dev_dbg(3, &p_dev->dev, "devname is %s\n", p_dev->devname);
  541. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  542. /*
  543. * p_dev->function_config must be the same for all card functions.
  544. * Note that this is serialized by the device_add_lock, so that
  545. * only one such struct will be created.
  546. */
  547. list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
  548. if (p_dev->func == tmp_dev->func) {
  549. p_dev->function_config = tmp_dev->function_config;
  550. kref_get(&p_dev->function_config->ref);
  551. }
  552. /* Add to the list in pcmcia_bus_socket */
  553. list_add(&p_dev->socket_device_list, &s->devices_list);
  554. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  555. if (!p_dev->function_config) {
  556. ds_dev_dbg(3, &p_dev->dev, "creating config_t\n");
  557. p_dev->function_config = kzalloc(sizeof(struct config_t),
  558. GFP_KERNEL);
  559. if (!p_dev->function_config)
  560. goto err_unreg;
  561. kref_init(&p_dev->function_config->ref);
  562. }
  563. dev_printk(KERN_NOTICE, &p_dev->dev,
  564. "pcmcia: registering new device %s\n",
  565. p_dev->devname);
  566. pcmcia_device_query(p_dev);
  567. if (device_register(&p_dev->dev))
  568. goto err_unreg;
  569. mutex_unlock(&device_add_lock);
  570. return p_dev;
  571. err_unreg:
  572. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  573. list_del(&p_dev->socket_device_list);
  574. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  575. err_free:
  576. kfree(p_dev->devname);
  577. kfree(p_dev);
  578. s->device_count--;
  579. err_put:
  580. mutex_unlock(&device_add_lock);
  581. pcmcia_put_socket(s);
  582. return NULL;
  583. }
  584. static int pcmcia_card_add(struct pcmcia_socket *s)
  585. {
  586. cistpl_longlink_mfc_t mfc;
  587. unsigned int no_funcs, i, no_chains;
  588. int ret = 0;
  589. if (!(s->resource_setup_done)) {
  590. ds_dev_dbg(3, &s->dev,
  591. "no resources available, delaying card_add\n");
  592. return -EAGAIN; /* try again, but later... */
  593. }
  594. if (pcmcia_validate_mem(s)) {
  595. ds_dev_dbg(3, &s->dev, "validating mem resources failed, "
  596. "delaying card_add\n");
  597. return -EAGAIN; /* try again, but later... */
  598. }
  599. ret = pccard_validate_cis(s, BIND_FN_ALL, &no_chains);
  600. if (ret || !no_chains) {
  601. ds_dev_dbg(0, &s->dev, "invalid CIS or invalid resources\n");
  602. return -ENODEV;
  603. }
  604. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  605. no_funcs = mfc.nfn;
  606. else
  607. no_funcs = 1;
  608. s->functions = no_funcs;
  609. for (i=0; i < no_funcs; i++)
  610. pcmcia_device_add(s, i);
  611. return (ret);
  612. }
  613. static void pcmcia_delayed_add_device(struct work_struct *work)
  614. {
  615. struct pcmcia_socket *s =
  616. container_of(work, struct pcmcia_socket, device_add);
  617. ds_dev_dbg(1, &s->dev, "adding additional device to %d\n", s->sock);
  618. pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
  619. s->pcmcia_state.device_add_pending = 0;
  620. s->pcmcia_state.mfc_pfc = 0;
  621. }
  622. static int pcmcia_requery(struct device *dev, void * _data)
  623. {
  624. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  625. if (!p_dev->dev.driver) {
  626. ds_dev_dbg(1, dev, "update device information\n");
  627. pcmcia_device_query(p_dev);
  628. }
  629. return 0;
  630. }
  631. static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
  632. {
  633. int no_devices = 0;
  634. int ret = 0;
  635. unsigned long flags;
  636. /* must be called with skt_mutex held */
  637. ds_dev_dbg(0, &skt->dev, "re-scanning socket %d\n", skt->sock);
  638. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  639. if (list_empty(&skt->devices_list))
  640. no_devices = 1;
  641. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  642. /* If this is because of a CIS override, start over */
  643. if (new_cis && !no_devices)
  644. pcmcia_card_remove(skt, NULL);
  645. /* if no devices were added for this socket yet because of
  646. * missing resource information or other trouble, we need to
  647. * do this now. */
  648. if (no_devices || new_cis) {
  649. ret = pcmcia_card_add(skt);
  650. if (ret)
  651. return;
  652. }
  653. /* some device information might have changed because of a CIS
  654. * update or because we can finally read it correctly... so
  655. * determine it again, overwriting old values if necessary. */
  656. bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
  657. /* we re-scan all devices, not just the ones connected to this
  658. * socket. This does not matter, though. */
  659. ret = bus_rescan_devices(&pcmcia_bus_type);
  660. if (ret)
  661. printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
  662. }
  663. #ifdef CONFIG_PCMCIA_LOAD_CIS
  664. /**
  665. * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
  666. * @dev: the pcmcia device which needs a CIS override
  667. * @filename: requested filename in /lib/firmware/
  668. *
  669. * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
  670. * the one provided by the card is broken. The firmware files reside in
  671. * /lib/firmware/ in userspace.
  672. */
  673. static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  674. {
  675. struct pcmcia_socket *s = dev->socket;
  676. const struct firmware *fw;
  677. char path[FIRMWARE_NAME_MAX];
  678. int ret = -ENOMEM;
  679. int no_funcs;
  680. int old_funcs;
  681. cistpl_longlink_mfc_t mfc;
  682. if (!filename)
  683. return -EINVAL;
  684. ds_dev_dbg(1, &dev->dev, "trying to load CIS file %s\n", filename);
  685. if (strlen(filename) > (FIRMWARE_NAME_MAX - 1)) {
  686. dev_printk(KERN_WARNING, &dev->dev,
  687. "pcmcia: CIS filename is too long [%s]\n",
  688. filename);
  689. return -EINVAL;
  690. }
  691. snprintf(path, sizeof(path), "%s", filename);
  692. if (request_firmware(&fw, path, &dev->dev) == 0) {
  693. if (fw->size >= CISTPL_MAX_CIS_SIZE) {
  694. ret = -EINVAL;
  695. dev_printk(KERN_ERR, &dev->dev,
  696. "pcmcia: CIS override is too big\n");
  697. goto release;
  698. }
  699. if (!pcmcia_replace_cis(s, fw->data, fw->size))
  700. ret = 0;
  701. else {
  702. dev_printk(KERN_ERR, &dev->dev,
  703. "pcmcia: CIS override failed\n");
  704. goto release;
  705. }
  706. /* update information */
  707. pcmcia_device_query(dev);
  708. /* does this cis override add or remove functions? */
  709. old_funcs = s->functions;
  710. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  711. no_funcs = mfc.nfn;
  712. else
  713. no_funcs = 1;
  714. s->functions = no_funcs;
  715. if (old_funcs > no_funcs)
  716. pcmcia_card_remove(s, dev);
  717. else if (no_funcs > old_funcs)
  718. pcmcia_add_device_later(s, 1);
  719. }
  720. release:
  721. release_firmware(fw);
  722. return (ret);
  723. }
  724. #else /* !CONFIG_PCMCIA_LOAD_CIS */
  725. static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  726. {
  727. return -ENODEV;
  728. }
  729. #endif
  730. static inline int pcmcia_devmatch(struct pcmcia_device *dev,
  731. struct pcmcia_device_id *did)
  732. {
  733. if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
  734. if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
  735. return 0;
  736. }
  737. if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
  738. if ((!dev->has_card_id) || (dev->card_id != did->card_id))
  739. return 0;
  740. }
  741. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
  742. if (dev->func != did->function)
  743. return 0;
  744. }
  745. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
  746. if (!dev->prod_id[0])
  747. return 0;
  748. if (strcmp(did->prod_id[0], dev->prod_id[0]))
  749. return 0;
  750. }
  751. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
  752. if (!dev->prod_id[1])
  753. return 0;
  754. if (strcmp(did->prod_id[1], dev->prod_id[1]))
  755. return 0;
  756. }
  757. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
  758. if (!dev->prod_id[2])
  759. return 0;
  760. if (strcmp(did->prod_id[2], dev->prod_id[2]))
  761. return 0;
  762. }
  763. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
  764. if (!dev->prod_id[3])
  765. return 0;
  766. if (strcmp(did->prod_id[3], dev->prod_id[3]))
  767. return 0;
  768. }
  769. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
  770. if (dev->device_no != did->device_no)
  771. return 0;
  772. }
  773. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
  774. if ((!dev->has_func_id) || (dev->func_id != did->func_id))
  775. return 0;
  776. /* if this is a pseudo-multi-function device,
  777. * we need explicit matches */
  778. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
  779. return 0;
  780. if (dev->device_no)
  781. return 0;
  782. /* also, FUNC_ID matching needs to be activated by userspace
  783. * after it has re-checked that there is no possible module
  784. * with a prod_id/manf_id/card_id match.
  785. */
  786. ds_dev_dbg(0, &dev->dev,
  787. "skipping FUNC_ID match until userspace interaction\n");
  788. if (!dev->allow_func_id_match)
  789. return 0;
  790. }
  791. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
  792. ds_dev_dbg(0, &dev->dev, "device needs a fake CIS\n");
  793. if (!dev->socket->fake_cis)
  794. pcmcia_load_firmware(dev, did->cisfile);
  795. if (!dev->socket->fake_cis)
  796. return 0;
  797. }
  798. if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
  799. int i;
  800. for (i=0; i<4; i++)
  801. if (dev->prod_id[i])
  802. return 0;
  803. if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
  804. return 0;
  805. }
  806. dev->dev.driver_data = (void *) did;
  807. return 1;
  808. }
  809. static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
  810. struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
  811. struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
  812. struct pcmcia_device_id *did = p_drv->id_table;
  813. struct pcmcia_dynid *dynid;
  814. /* match dynamic devices first */
  815. spin_lock(&p_drv->dynids.lock);
  816. list_for_each_entry(dynid, &p_drv->dynids.list, node) {
  817. ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
  818. if (pcmcia_devmatch(p_dev, &dynid->id)) {
  819. ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
  820. spin_unlock(&p_drv->dynids.lock);
  821. return 1;
  822. }
  823. }
  824. spin_unlock(&p_drv->dynids.lock);
  825. #ifdef CONFIG_PCMCIA_IOCTL
  826. /* matching by cardmgr */
  827. if (p_dev->cardmgr == p_drv) {
  828. ds_dev_dbg(0, dev, "cardmgr matched to %s\n", drv->name);
  829. return 1;
  830. }
  831. #endif
  832. while (did && did->match_flags) {
  833. ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
  834. if (pcmcia_devmatch(p_dev, did)) {
  835. ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
  836. return 1;
  837. }
  838. did++;
  839. }
  840. return 0;
  841. }
  842. #ifdef CONFIG_HOTPLUG
  843. static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  844. {
  845. struct pcmcia_device *p_dev;
  846. int i;
  847. u32 hash[4] = { 0, 0, 0, 0};
  848. if (!dev)
  849. return -ENODEV;
  850. p_dev = to_pcmcia_dev(dev);
  851. /* calculate hashes */
  852. for (i=0; i<4; i++) {
  853. if (!p_dev->prod_id[i])
  854. continue;
  855. hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
  856. }
  857. if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
  858. return -ENOMEM;
  859. if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
  860. return -ENOMEM;
  861. if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  862. "pa%08Xpb%08Xpc%08Xpd%08X",
  863. p_dev->has_manf_id ? p_dev->manf_id : 0,
  864. p_dev->has_card_id ? p_dev->card_id : 0,
  865. p_dev->has_func_id ? p_dev->func_id : 0,
  866. p_dev->func,
  867. p_dev->device_no,
  868. hash[0],
  869. hash[1],
  870. hash[2],
  871. hash[3]))
  872. return -ENOMEM;
  873. return 0;
  874. }
  875. #else
  876. static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  877. {
  878. return -ENODEV;
  879. }
  880. #endif
  881. /************************ runtime PM support ***************************/
  882. static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
  883. static int pcmcia_dev_resume(struct device *dev);
  884. static int runtime_suspend(struct device *dev)
  885. {
  886. int rc;
  887. down(&dev->sem);
  888. rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
  889. up(&dev->sem);
  890. return rc;
  891. }
  892. static void runtime_resume(struct device *dev)
  893. {
  894. int rc;
  895. down(&dev->sem);
  896. rc = pcmcia_dev_resume(dev);
  897. up(&dev->sem);
  898. }
  899. /************************ per-device sysfs output ***************************/
  900. #define pcmcia_device_attr(field, test, format) \
  901. static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  902. { \
  903. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  904. return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
  905. }
  906. #define pcmcia_device_stringattr(name, field) \
  907. static ssize_t name##_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->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
  911. }
  912. pcmcia_device_attr(func, socket, "0x%02x\n");
  913. pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
  914. pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
  915. pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
  916. pcmcia_device_stringattr(prod_id1, prod_id[0]);
  917. pcmcia_device_stringattr(prod_id2, prod_id[1]);
  918. pcmcia_device_stringattr(prod_id3, prod_id[2]);
  919. pcmcia_device_stringattr(prod_id4, prod_id[3]);
  920. static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
  921. {
  922. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  923. if (p_dev->suspended)
  924. return sprintf(buf, "off\n");
  925. else
  926. return sprintf(buf, "on\n");
  927. }
  928. static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
  929. const char *buf, size_t count)
  930. {
  931. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  932. int ret = 0;
  933. if (!count)
  934. return -EINVAL;
  935. if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
  936. ret = runtime_suspend(dev);
  937. else if (p_dev->suspended && !strncmp(buf, "on", 2))
  938. runtime_resume(dev);
  939. return ret ? ret : count;
  940. }
  941. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  942. {
  943. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  944. int i;
  945. u32 hash[4] = { 0, 0, 0, 0};
  946. /* calculate hashes */
  947. for (i=0; i<4; i++) {
  948. if (!p_dev->prod_id[i])
  949. continue;
  950. hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
  951. }
  952. return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  953. "pa%08Xpb%08Xpc%08Xpd%08X\n",
  954. p_dev->has_manf_id ? p_dev->manf_id : 0,
  955. p_dev->has_card_id ? p_dev->card_id : 0,
  956. p_dev->has_func_id ? p_dev->func_id : 0,
  957. p_dev->func, p_dev->device_no,
  958. hash[0], hash[1], hash[2], hash[3]);
  959. }
  960. static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
  961. struct device_attribute *attr, const char *buf, size_t count)
  962. {
  963. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  964. int ret;
  965. if (!count)
  966. return -EINVAL;
  967. mutex_lock(&p_dev->socket->skt_mutex);
  968. p_dev->allow_func_id_match = 1;
  969. mutex_unlock(&p_dev->socket->skt_mutex);
  970. ret = bus_rescan_devices(&pcmcia_bus_type);
  971. if (ret)
  972. printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
  973. "allowing func_id matches\n");
  974. return count;
  975. }
  976. static struct device_attribute pcmcia_dev_attrs[] = {
  977. __ATTR(function, 0444, func_show, NULL),
  978. __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
  979. __ATTR_RO(func_id),
  980. __ATTR_RO(manf_id),
  981. __ATTR_RO(card_id),
  982. __ATTR_RO(prod_id1),
  983. __ATTR_RO(prod_id2),
  984. __ATTR_RO(prod_id3),
  985. __ATTR_RO(prod_id4),
  986. __ATTR_RO(modalias),
  987. __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
  988. __ATTR_NULL,
  989. };
  990. /* PM support, also needed for reset */
  991. static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
  992. {
  993. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  994. struct pcmcia_driver *p_drv = NULL;
  995. int ret = 0;
  996. if (p_dev->suspended)
  997. return 0;
  998. ds_dev_dbg(2, dev, "suspending\n");
  999. if (dev->driver)
  1000. p_drv = to_pcmcia_drv(dev->driver);
  1001. if (!p_drv)
  1002. goto out;
  1003. if (p_drv->suspend) {
  1004. ret = p_drv->suspend(p_dev);
  1005. if (ret) {
  1006. dev_printk(KERN_ERR, dev,
  1007. "pcmcia: device %s (driver %s) did "
  1008. "not want to go to sleep (%d)\n",
  1009. p_dev->devname, p_drv->drv.name, ret);
  1010. goto out;
  1011. }
  1012. }
  1013. if (p_dev->device_no == p_dev->func) {
  1014. ds_dev_dbg(2, dev, "releasing configuration\n");
  1015. pcmcia_release_configuration(p_dev);
  1016. }
  1017. out:
  1018. if (!ret)
  1019. p_dev->suspended = 1;
  1020. return ret;
  1021. }
  1022. static int pcmcia_dev_resume(struct device * dev)
  1023. {
  1024. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1025. struct pcmcia_driver *p_drv = NULL;
  1026. int ret = 0;
  1027. if (!p_dev->suspended)
  1028. return 0;
  1029. ds_dev_dbg(2, dev, "resuming\n");
  1030. if (dev->driver)
  1031. p_drv = to_pcmcia_drv(dev->driver);
  1032. if (!p_drv)
  1033. goto out;
  1034. if (p_dev->device_no == p_dev->func) {
  1035. ds_dev_dbg(2, dev, "requesting configuration\n");
  1036. ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
  1037. if (ret)
  1038. goto out;
  1039. }
  1040. if (p_drv->resume)
  1041. ret = p_drv->resume(p_dev);
  1042. out:
  1043. if (!ret)
  1044. p_dev->suspended = 0;
  1045. return ret;
  1046. }
  1047. static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
  1048. {
  1049. struct pcmcia_socket *skt = _data;
  1050. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1051. if (p_dev->socket != skt || p_dev->suspended)
  1052. return 0;
  1053. return runtime_suspend(dev);
  1054. }
  1055. static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
  1056. {
  1057. struct pcmcia_socket *skt = _data;
  1058. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1059. if (p_dev->socket != skt || !p_dev->suspended)
  1060. return 0;
  1061. runtime_resume(dev);
  1062. return 0;
  1063. }
  1064. static int pcmcia_bus_resume(struct pcmcia_socket *skt)
  1065. {
  1066. ds_dev_dbg(2, &skt->dev, "resuming socket %d\n", skt->sock);
  1067. bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
  1068. return 0;
  1069. }
  1070. static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
  1071. {
  1072. ds_dev_dbg(2, &skt->dev, "suspending socket %d\n", skt->sock);
  1073. if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
  1074. pcmcia_bus_suspend_callback)) {
  1075. pcmcia_bus_resume(skt);
  1076. return -EIO;
  1077. }
  1078. return 0;
  1079. }
  1080. /*======================================================================
  1081. The card status event handler.
  1082. ======================================================================*/
  1083. /* Normally, the event is passed to individual drivers after
  1084. * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
  1085. * is inversed to maintain historic compatibility.
  1086. */
  1087. static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
  1088. {
  1089. struct pcmcia_socket *s = pcmcia_get_socket(skt);
  1090. if (!s) {
  1091. dev_printk(KERN_ERR, &skt->dev,
  1092. "PCMCIA obtaining reference to socket " \
  1093. "failed, event 0x%x lost!\n", event);
  1094. return -ENODEV;
  1095. }
  1096. ds_dev_dbg(1, &skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
  1097. event, priority, skt);
  1098. switch (event) {
  1099. case CS_EVENT_CARD_REMOVAL:
  1100. s->pcmcia_state.present = 0;
  1101. pcmcia_card_remove(skt, NULL);
  1102. handle_event(skt, event);
  1103. break;
  1104. case CS_EVENT_CARD_INSERTION:
  1105. s->pcmcia_state.present = 1;
  1106. pcmcia_card_add(skt);
  1107. handle_event(skt, event);
  1108. break;
  1109. case CS_EVENT_EJECTION_REQUEST:
  1110. break;
  1111. case CS_EVENT_PM_SUSPEND:
  1112. case CS_EVENT_PM_RESUME:
  1113. case CS_EVENT_RESET_PHYSICAL:
  1114. case CS_EVENT_CARD_RESET:
  1115. default:
  1116. handle_event(skt, event);
  1117. break;
  1118. }
  1119. pcmcia_put_socket(s);
  1120. return 0;
  1121. } /* ds_event */
  1122. struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
  1123. {
  1124. struct pcmcia_device *p_dev;
  1125. struct pcmcia_device *ret = NULL;
  1126. p_dev = pcmcia_get_dev(_p_dev);
  1127. if (!p_dev)
  1128. return NULL;
  1129. if (!p_dev->socket->pcmcia_state.present)
  1130. goto out;
  1131. if (p_dev->_removed)
  1132. goto out;
  1133. if (p_dev->suspended)
  1134. goto out;
  1135. ret = p_dev;
  1136. out:
  1137. pcmcia_put_dev(p_dev);
  1138. return ret;
  1139. }
  1140. EXPORT_SYMBOL(pcmcia_dev_present);
  1141. static struct pcmcia_callback pcmcia_bus_callback = {
  1142. .owner = THIS_MODULE,
  1143. .event = ds_event,
  1144. .requery = pcmcia_bus_rescan,
  1145. .suspend = pcmcia_bus_suspend,
  1146. .resume = pcmcia_bus_resume,
  1147. };
  1148. static int __devinit pcmcia_bus_add_socket(struct device *dev,
  1149. struct class_interface *class_intf)
  1150. {
  1151. struct pcmcia_socket *socket = dev_get_drvdata(dev);
  1152. int ret;
  1153. socket = pcmcia_get_socket(socket);
  1154. if (!socket) {
  1155. dev_printk(KERN_ERR, dev,
  1156. "PCMCIA obtaining reference to socket failed\n");
  1157. return -ENODEV;
  1158. }
  1159. /*
  1160. * Ugly. But we want to wait for the socket threads to have started up.
  1161. * We really should let the drivers themselves drive some of this..
  1162. */
  1163. msleep(250);
  1164. #ifdef CONFIG_PCMCIA_IOCTL
  1165. init_waitqueue_head(&socket->queue);
  1166. #endif
  1167. INIT_LIST_HEAD(&socket->devices_list);
  1168. INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
  1169. memset(&socket->pcmcia_state, 0, sizeof(u8));
  1170. socket->device_count = 0;
  1171. ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
  1172. if (ret) {
  1173. dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
  1174. pcmcia_put_socket(socket);
  1175. return (ret);
  1176. }
  1177. return 0;
  1178. }
  1179. static void pcmcia_bus_remove_socket(struct device *dev,
  1180. struct class_interface *class_intf)
  1181. {
  1182. struct pcmcia_socket *socket = dev_get_drvdata(dev);
  1183. if (!socket)
  1184. return;
  1185. socket->pcmcia_state.dead = 1;
  1186. pccard_register_pcmcia(socket, NULL);
  1187. /* unregister any unbound devices */
  1188. mutex_lock(&socket->skt_mutex);
  1189. pcmcia_card_remove(socket, NULL);
  1190. mutex_unlock(&socket->skt_mutex);
  1191. pcmcia_put_socket(socket);
  1192. return;
  1193. }
  1194. /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
  1195. static struct class_interface pcmcia_bus_interface __refdata = {
  1196. .class = &pcmcia_socket_class,
  1197. .add_dev = &pcmcia_bus_add_socket,
  1198. .remove_dev = &pcmcia_bus_remove_socket,
  1199. };
  1200. struct bus_type pcmcia_bus_type = {
  1201. .name = "pcmcia",
  1202. .uevent = pcmcia_bus_uevent,
  1203. .match = pcmcia_bus_match,
  1204. .dev_attrs = pcmcia_dev_attrs,
  1205. .probe = pcmcia_device_probe,
  1206. .remove = pcmcia_device_remove,
  1207. .suspend = pcmcia_dev_suspend,
  1208. .resume = pcmcia_dev_resume,
  1209. };
  1210. static int __init init_pcmcia_bus(void)
  1211. {
  1212. int ret;
  1213. spin_lock_init(&pcmcia_dev_list_lock);
  1214. ret = bus_register(&pcmcia_bus_type);
  1215. if (ret < 0) {
  1216. printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
  1217. return ret;
  1218. }
  1219. ret = class_interface_register(&pcmcia_bus_interface);
  1220. if (ret < 0) {
  1221. printk(KERN_WARNING
  1222. "pcmcia: class_interface_register error: %d\n", ret);
  1223. bus_unregister(&pcmcia_bus_type);
  1224. return ret;
  1225. }
  1226. pcmcia_setup_ioctl();
  1227. return 0;
  1228. }
  1229. fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
  1230. * pcmcia_socket_class is already registered */
  1231. static void __exit exit_pcmcia_bus(void)
  1232. {
  1233. pcmcia_cleanup_ioctl();
  1234. class_interface_unregister(&pcmcia_bus_interface);
  1235. bus_unregister(&pcmcia_bus_type);
  1236. }
  1237. module_exit(exit_pcmcia_bus);
  1238. MODULE_ALIAS("ds");