ds.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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 - 2005 Dominik Brodowski
  14. */
  15. #include <linux/config.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/errno.h>
  20. #include <linux/list.h>
  21. #include <linux/delay.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/crc32.h>
  24. #include <linux/firmware.h>
  25. #define IN_CARD_SERVICES
  26. #include <pcmcia/cs_types.h>
  27. #include <pcmcia/cs.h>
  28. #include <pcmcia/cistpl.h>
  29. #include <pcmcia/ds.h>
  30. #include <pcmcia/ss.h>
  31. #include "cs_internal.h"
  32. #include "ds_internal.h"
  33. /*====================================================================*/
  34. /* Module parameters */
  35. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  36. MODULE_DESCRIPTION("PCMCIA Driver Services");
  37. MODULE_LICENSE("GPL");
  38. #ifdef DEBUG
  39. int ds_pc_debug;
  40. module_param_named(pc_debug, ds_pc_debug, int, 0644);
  41. #define ds_dbg(lvl, fmt, arg...) do { \
  42. if (ds_pc_debug > (lvl)) \
  43. printk(KERN_DEBUG "ds: " fmt , ## arg); \
  44. } while (0)
  45. #else
  46. #define ds_dbg(lvl, fmt, arg...) do { } while (0)
  47. #endif
  48. spinlock_t pcmcia_dev_list_lock;
  49. static int unbind_request(struct pcmcia_socket *s);
  50. /*====================================================================*/
  51. /* code which was in cs.c before */
  52. /* String tables for error messages */
  53. typedef struct lookup_t {
  54. int key;
  55. char *msg;
  56. } lookup_t;
  57. static const lookup_t error_table[] = {
  58. { CS_SUCCESS, "Operation succeeded" },
  59. { CS_BAD_ADAPTER, "Bad adapter" },
  60. { CS_BAD_ATTRIBUTE, "Bad attribute", },
  61. { CS_BAD_BASE, "Bad base address" },
  62. { CS_BAD_EDC, "Bad EDC" },
  63. { CS_BAD_IRQ, "Bad IRQ" },
  64. { CS_BAD_OFFSET, "Bad offset" },
  65. { CS_BAD_PAGE, "Bad page number" },
  66. { CS_READ_FAILURE, "Read failure" },
  67. { CS_BAD_SIZE, "Bad size" },
  68. { CS_BAD_SOCKET, "Bad socket" },
  69. { CS_BAD_TYPE, "Bad type" },
  70. { CS_BAD_VCC, "Bad Vcc" },
  71. { CS_BAD_VPP, "Bad Vpp" },
  72. { CS_BAD_WINDOW, "Bad window" },
  73. { CS_WRITE_FAILURE, "Write failure" },
  74. { CS_NO_CARD, "No card present" },
  75. { CS_UNSUPPORTED_FUNCTION, "Usupported function" },
  76. { CS_UNSUPPORTED_MODE, "Unsupported mode" },
  77. { CS_BAD_SPEED, "Bad speed" },
  78. { CS_BUSY, "Resource busy" },
  79. { CS_GENERAL_FAILURE, "General failure" },
  80. { CS_WRITE_PROTECTED, "Write protected" },
  81. { CS_BAD_ARG_LENGTH, "Bad argument length" },
  82. { CS_BAD_ARGS, "Bad arguments" },
  83. { CS_CONFIGURATION_LOCKED, "Configuration locked" },
  84. { CS_IN_USE, "Resource in use" },
  85. { CS_NO_MORE_ITEMS, "No more items" },
  86. { CS_OUT_OF_RESOURCE, "Out of resource" },
  87. { CS_BAD_HANDLE, "Bad handle" },
  88. { CS_BAD_TUPLE, "Bad CIS tuple" }
  89. };
  90. static const lookup_t service_table[] = {
  91. { AccessConfigurationRegister, "AccessConfigurationRegister" },
  92. { AddSocketServices, "AddSocketServices" },
  93. { AdjustResourceInfo, "AdjustResourceInfo" },
  94. { CheckEraseQueue, "CheckEraseQueue" },
  95. { CloseMemory, "CloseMemory" },
  96. { DeregisterClient, "DeregisterClient" },
  97. { DeregisterEraseQueue, "DeregisterEraseQueue" },
  98. { GetCardServicesInfo, "GetCardServicesInfo" },
  99. { GetClientInfo, "GetClientInfo" },
  100. { GetConfigurationInfo, "GetConfigurationInfo" },
  101. { GetEventMask, "GetEventMask" },
  102. { GetFirstClient, "GetFirstClient" },
  103. { GetFirstRegion, "GetFirstRegion" },
  104. { GetFirstTuple, "GetFirstTuple" },
  105. { GetNextClient, "GetNextClient" },
  106. { GetNextRegion, "GetNextRegion" },
  107. { GetNextTuple, "GetNextTuple" },
  108. { GetStatus, "GetStatus" },
  109. { GetTupleData, "GetTupleData" },
  110. { MapMemPage, "MapMemPage" },
  111. { ModifyConfiguration, "ModifyConfiguration" },
  112. { ModifyWindow, "ModifyWindow" },
  113. { OpenMemory, "OpenMemory" },
  114. { ParseTuple, "ParseTuple" },
  115. { ReadMemory, "ReadMemory" },
  116. { RegisterClient, "RegisterClient" },
  117. { RegisterEraseQueue, "RegisterEraseQueue" },
  118. { RegisterMTD, "RegisterMTD" },
  119. { ReleaseConfiguration, "ReleaseConfiguration" },
  120. { ReleaseIO, "ReleaseIO" },
  121. { ReleaseIRQ, "ReleaseIRQ" },
  122. { ReleaseWindow, "ReleaseWindow" },
  123. { RequestConfiguration, "RequestConfiguration" },
  124. { RequestIO, "RequestIO" },
  125. { RequestIRQ, "RequestIRQ" },
  126. { RequestSocketMask, "RequestSocketMask" },
  127. { RequestWindow, "RequestWindow" },
  128. { ResetCard, "ResetCard" },
  129. { SetEventMask, "SetEventMask" },
  130. { ValidateCIS, "ValidateCIS" },
  131. { WriteMemory, "WriteMemory" },
  132. { BindDevice, "BindDevice" },
  133. { BindMTD, "BindMTD" },
  134. { ReportError, "ReportError" },
  135. { SuspendCard, "SuspendCard" },
  136. { ResumeCard, "ResumeCard" },
  137. { EjectCard, "EjectCard" },
  138. { InsertCard, "InsertCard" },
  139. { ReplaceCIS, "ReplaceCIS" }
  140. };
  141. static int pcmcia_report_error(client_handle_t handle, error_info_t *err)
  142. {
  143. int i;
  144. char *serv;
  145. if (CHECK_HANDLE(handle))
  146. printk(KERN_NOTICE);
  147. else {
  148. struct pcmcia_device *p_dev = handle_to_pdev(handle);
  149. printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
  150. }
  151. for (i = 0; i < ARRAY_SIZE(service_table); i++)
  152. if (service_table[i].key == err->func)
  153. break;
  154. if (i < ARRAY_SIZE(service_table))
  155. serv = service_table[i].msg;
  156. else
  157. serv = "Unknown service number";
  158. for (i = 0; i < ARRAY_SIZE(error_table); i++)
  159. if (error_table[i].key == err->retcode)
  160. break;
  161. if (i < ARRAY_SIZE(error_table))
  162. printk("%s: %s\n", serv, error_table[i].msg);
  163. else
  164. printk("%s: Unknown error code %#x\n", serv, err->retcode);
  165. return CS_SUCCESS;
  166. } /* report_error */
  167. /* end of code which was in cs.c before */
  168. /*======================================================================*/
  169. void cs_error(client_handle_t handle, int func, int ret)
  170. {
  171. error_info_t err = { func, ret };
  172. pcmcia_report_error(handle, &err);
  173. }
  174. EXPORT_SYMBOL(cs_error);
  175. static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
  176. {
  177. struct pcmcia_device_id *did = p_drv->id_table;
  178. unsigned int i;
  179. u32 hash;
  180. while (did && did->match_flags) {
  181. for (i=0; i<4; i++) {
  182. if (!did->prod_id[i])
  183. continue;
  184. hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
  185. if (hash == did->prod_id_hash[i])
  186. continue;
  187. printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
  188. "product string \"%s\": is 0x%x, should "
  189. "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
  190. did->prod_id_hash[i], hash);
  191. printk(KERN_DEBUG "pcmcia: see "
  192. "Documentation/pcmcia/devicetable.txt for "
  193. "details\n");
  194. }
  195. did++;
  196. }
  197. return;
  198. }
  199. #ifdef CONFIG_PCMCIA_LOAD_CIS
  200. /**
  201. * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
  202. * @dev - the pcmcia device which needs a CIS override
  203. * @filename - requested filename in /lib/firmware/cis/
  204. *
  205. * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
  206. * the one provided by the card is broken. The firmware files reside in
  207. * /lib/firmware/cis/ in userspace.
  208. */
  209. static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  210. {
  211. struct pcmcia_socket *s = dev->socket;
  212. const struct firmware *fw;
  213. char path[20];
  214. int ret=-ENOMEM;
  215. cisdump_t *cis;
  216. if (!filename)
  217. return -EINVAL;
  218. ds_dbg(1, "trying to load firmware %s\n", filename);
  219. if (strlen(filename) > 14)
  220. return -EINVAL;
  221. snprintf(path, 20, "%s", filename);
  222. if (request_firmware(&fw, path, &dev->dev) == 0) {
  223. if (fw->size >= CISTPL_MAX_CIS_SIZE)
  224. goto release;
  225. cis = kmalloc(sizeof(cisdump_t), GFP_KERNEL);
  226. if (!cis)
  227. goto release;
  228. memset(cis, 0, sizeof(cisdump_t));
  229. cis->Length = fw->size + 1;
  230. memcpy(cis->Data, fw->data, fw->size);
  231. if (!pcmcia_replace_cis(s, cis))
  232. ret = 0;
  233. }
  234. release:
  235. release_firmware(fw);
  236. return (ret);
  237. }
  238. #else /* !CONFIG_PCMCIA_LOAD_CIS */
  239. static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  240. {
  241. return -ENODEV;
  242. }
  243. #endif
  244. /*======================================================================*/
  245. /**
  246. * pcmcia_register_driver - register a PCMCIA driver with the bus core
  247. *
  248. * Registers a PCMCIA driver with the PCMCIA bus core.
  249. */
  250. static int pcmcia_device_probe(struct device *dev);
  251. static int pcmcia_device_remove(struct device * dev);
  252. int pcmcia_register_driver(struct pcmcia_driver *driver)
  253. {
  254. if (!driver)
  255. return -EINVAL;
  256. pcmcia_check_driver(driver);
  257. /* initialize common fields */
  258. driver->drv.bus = &pcmcia_bus_type;
  259. driver->drv.owner = driver->owner;
  260. driver->drv.probe = pcmcia_device_probe;
  261. driver->drv.remove = pcmcia_device_remove;
  262. return driver_register(&driver->drv);
  263. }
  264. EXPORT_SYMBOL(pcmcia_register_driver);
  265. /**
  266. * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
  267. */
  268. void pcmcia_unregister_driver(struct pcmcia_driver *driver)
  269. {
  270. driver_unregister(&driver->drv);
  271. }
  272. EXPORT_SYMBOL(pcmcia_unregister_driver);
  273. /* pcmcia_device handling */
  274. struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
  275. {
  276. struct device *tmp_dev;
  277. tmp_dev = get_device(&p_dev->dev);
  278. if (!tmp_dev)
  279. return NULL;
  280. return to_pcmcia_dev(tmp_dev);
  281. }
  282. void pcmcia_put_dev(struct pcmcia_device *p_dev)
  283. {
  284. if (p_dev)
  285. put_device(&p_dev->dev);
  286. }
  287. static void pcmcia_release_dev(struct device *dev)
  288. {
  289. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  290. ds_dbg(1, "releasing dev %p\n", p_dev);
  291. pcmcia_put_socket(p_dev->socket);
  292. kfree(p_dev);
  293. }
  294. static int pcmcia_device_probe(struct device * dev)
  295. {
  296. struct pcmcia_device *p_dev;
  297. struct pcmcia_driver *p_drv;
  298. int ret = 0;
  299. dev = get_device(dev);
  300. if (!dev)
  301. return -ENODEV;
  302. p_dev = to_pcmcia_dev(dev);
  303. p_drv = to_pcmcia_drv(dev->driver);
  304. if (!try_module_get(p_drv->owner)) {
  305. ret = -EINVAL;
  306. goto put_dev;
  307. }
  308. if (p_drv->attach) {
  309. p_dev->instance = p_drv->attach();
  310. if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) {
  311. printk(KERN_NOTICE "ds: unable to create instance "
  312. "of '%s'!\n", p_drv->drv.name);
  313. ret = -EINVAL;
  314. }
  315. }
  316. if (ret)
  317. module_put(p_drv->owner);
  318. put_dev:
  319. if ((ret) || !(p_drv->attach))
  320. put_device(dev);
  321. return (ret);
  322. }
  323. static int pcmcia_device_remove(struct device * dev)
  324. {
  325. struct pcmcia_device *p_dev;
  326. struct pcmcia_driver *p_drv;
  327. /* detach the "instance" */
  328. p_dev = to_pcmcia_dev(dev);
  329. p_drv = to_pcmcia_drv(dev->driver);
  330. if (p_drv) {
  331. if ((p_drv->detach) && (p_dev->instance)) {
  332. p_drv->detach(p_dev->instance);
  333. /* from pcmcia_probe_device */
  334. put_device(&p_dev->dev);
  335. }
  336. module_put(p_drv->owner);
  337. }
  338. return 0;
  339. }
  340. /*
  341. * pcmcia_device_query -- determine information about a pcmcia device
  342. */
  343. static int pcmcia_device_query(struct pcmcia_device *p_dev)
  344. {
  345. cistpl_manfid_t manf_id;
  346. cistpl_funcid_t func_id;
  347. cistpl_vers_1_t vers1;
  348. unsigned int i;
  349. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  350. CISTPL_MANFID, &manf_id)) {
  351. p_dev->manf_id = manf_id.manf;
  352. p_dev->card_id = manf_id.card;
  353. p_dev->has_manf_id = 1;
  354. p_dev->has_card_id = 1;
  355. }
  356. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  357. CISTPL_FUNCID, &func_id)) {
  358. p_dev->func_id = func_id.func;
  359. p_dev->has_func_id = 1;
  360. } else {
  361. /* rule of thumb: cards with no FUNCID, but with
  362. * common memory device geometry information, are
  363. * probably memory cards (from pcmcia-cs) */
  364. cistpl_device_geo_t devgeo;
  365. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  366. CISTPL_DEVICE_GEO, &devgeo)) {
  367. ds_dbg(0, "mem device geometry probably means "
  368. "FUNCID_MEMORY\n");
  369. p_dev->func_id = CISTPL_FUNCID_MEMORY;
  370. p_dev->has_func_id = 1;
  371. }
  372. }
  373. if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
  374. &vers1)) {
  375. for (i=0; i < vers1.ns; i++) {
  376. char *tmp;
  377. unsigned int length;
  378. tmp = vers1.str + vers1.ofs[i];
  379. length = strlen(tmp) + 1;
  380. if ((length < 3) || (length > 255))
  381. continue;
  382. p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
  383. GFP_KERNEL);
  384. if (!p_dev->prod_id[i])
  385. continue;
  386. p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
  387. tmp, length);
  388. }
  389. }
  390. return 0;
  391. }
  392. /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
  393. * Serializes pcmcia_device_add; will most likely be removed in future.
  394. *
  395. * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
  396. * won't work, this doesn't matter much at the moment: the driver core doesn't
  397. * support it either.
  398. */
  399. static DECLARE_MUTEX(device_add_lock);
  400. struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
  401. {
  402. struct pcmcia_device *p_dev;
  403. unsigned long flags;
  404. s = pcmcia_get_socket(s);
  405. if (!s)
  406. return NULL;
  407. down(&device_add_lock);
  408. /* max of 2 devices per card */
  409. if (s->device_count == 2)
  410. goto err_put;
  411. p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
  412. if (!p_dev)
  413. goto err_put;
  414. memset(p_dev, 0, sizeof(struct pcmcia_device));
  415. p_dev->socket = s;
  416. p_dev->device_no = (s->device_count++);
  417. p_dev->func = function;
  418. p_dev->dev.bus = &pcmcia_bus_type;
  419. p_dev->dev.parent = s->dev.dev;
  420. p_dev->dev.release = pcmcia_release_dev;
  421. sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
  422. /* compat */
  423. p_dev->client.client_magic = CLIENT_MAGIC;
  424. p_dev->client.Socket = s;
  425. p_dev->client.Function = function;
  426. p_dev->client.state = CLIENT_UNBOUND;
  427. /* Add to the list in pcmcia_bus_socket */
  428. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  429. list_add_tail(&p_dev->socket_device_list, &s->devices_list);
  430. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  431. pcmcia_device_query(p_dev);
  432. if (device_register(&p_dev->dev)) {
  433. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  434. list_del(&p_dev->socket_device_list);
  435. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  436. goto err_free;
  437. }
  438. up(&device_add_lock);
  439. return p_dev;
  440. err_free:
  441. kfree(p_dev);
  442. s->device_count--;
  443. err_put:
  444. up(&device_add_lock);
  445. pcmcia_put_socket(s);
  446. return NULL;
  447. }
  448. static int pcmcia_card_add(struct pcmcia_socket *s)
  449. {
  450. cisinfo_t cisinfo;
  451. cistpl_longlink_mfc_t mfc;
  452. unsigned int no_funcs, i;
  453. int ret = 0;
  454. if (!(s->resource_setup_done))
  455. return -EAGAIN; /* try again, but later... */
  456. pcmcia_validate_mem(s);
  457. ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
  458. if (ret || !cisinfo.Chains) {
  459. ds_dbg(0, "invalid CIS or invalid resources\n");
  460. return -ENODEV;
  461. }
  462. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  463. no_funcs = mfc.nfn;
  464. else
  465. no_funcs = 1;
  466. /* this doesn't handle multifunction devices on one pcmcia function
  467. * yet. */
  468. for (i=0; i < no_funcs; i++)
  469. pcmcia_device_add(s, i);
  470. return (ret);
  471. }
  472. static void pcmcia_delayed_add_pseudo_device(void *data)
  473. {
  474. struct pcmcia_socket *s = data;
  475. pcmcia_device_add(s, 0);
  476. s->pcmcia_state.device_add_pending = 0;
  477. }
  478. static inline void pcmcia_add_pseudo_device(struct pcmcia_socket *s)
  479. {
  480. if (!s->pcmcia_state.device_add_pending) {
  481. schedule_work(&s->device_add);
  482. s->pcmcia_state.device_add_pending = 1;
  483. }
  484. return;
  485. }
  486. static int pcmcia_requery(struct device *dev, void * _data)
  487. {
  488. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  489. if (!p_dev->dev.driver)
  490. pcmcia_device_query(p_dev);
  491. return 0;
  492. }
  493. static void pcmcia_bus_rescan(struct pcmcia_socket *skt)
  494. {
  495. int no_devices=0;
  496. unsigned long flags;
  497. /* must be called with skt_sem held */
  498. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  499. if (list_empty(&skt->devices_list))
  500. no_devices=1;
  501. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  502. /* if no devices were added for this socket yet because of
  503. * missing resource information or other trouble, we need to
  504. * do this now. */
  505. if (no_devices) {
  506. int ret = pcmcia_card_add(skt);
  507. if (ret)
  508. return;
  509. }
  510. /* some device information might have changed because of a CIS
  511. * update or because we can finally read it correctly... so
  512. * determine it again, overwriting old values if necessary. */
  513. bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
  514. /* we re-scan all devices, not just the ones connected to this
  515. * socket. This does not matter, though. */
  516. bus_rescan_devices(&pcmcia_bus_type);
  517. }
  518. static inline int pcmcia_devmatch(struct pcmcia_device *dev,
  519. struct pcmcia_device_id *did)
  520. {
  521. if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
  522. if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
  523. return 0;
  524. }
  525. if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
  526. if ((!dev->has_card_id) || (dev->card_id != did->card_id))
  527. return 0;
  528. }
  529. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
  530. if (dev->func != did->function)
  531. return 0;
  532. }
  533. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
  534. if (!dev->prod_id[0])
  535. return 0;
  536. if (strcmp(did->prod_id[0], dev->prod_id[0]))
  537. return 0;
  538. }
  539. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
  540. if (!dev->prod_id[1])
  541. return 0;
  542. if (strcmp(did->prod_id[1], dev->prod_id[1]))
  543. return 0;
  544. }
  545. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
  546. if (!dev->prod_id[2])
  547. return 0;
  548. if (strcmp(did->prod_id[2], dev->prod_id[2]))
  549. return 0;
  550. }
  551. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
  552. if (!dev->prod_id[3])
  553. return 0;
  554. if (strcmp(did->prod_id[3], dev->prod_id[3]))
  555. return 0;
  556. }
  557. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
  558. /* handle pseudo multifunction devices:
  559. * there are at most two pseudo multifunction devices.
  560. * if we're matching against the first, schedule a
  561. * call which will then check whether there are two
  562. * pseudo devices, and if not, add the second one.
  563. */
  564. if (dev->device_no == 0)
  565. pcmcia_add_pseudo_device(dev->socket);
  566. if (dev->device_no != did->device_no)
  567. return 0;
  568. }
  569. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
  570. if ((!dev->has_func_id) || (dev->func_id != did->func_id))
  571. return 0;
  572. /* if this is a pseudo-multi-function device,
  573. * we need explicit matches */
  574. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
  575. return 0;
  576. if (dev->device_no)
  577. return 0;
  578. /* also, FUNC_ID matching needs to be activated by userspace
  579. * after it has re-checked that there is no possible module
  580. * with a prod_id/manf_id/card_id match.
  581. */
  582. if (!dev->allow_func_id_match)
  583. return 0;
  584. }
  585. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
  586. if (!dev->socket->fake_cis)
  587. pcmcia_load_firmware(dev, did->cisfile);
  588. if (!dev->socket->fake_cis)
  589. return 0;
  590. }
  591. if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
  592. int i;
  593. for (i=0; i<4; i++)
  594. if (dev->prod_id[i])
  595. return 0;
  596. if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
  597. return 0;
  598. }
  599. dev->dev.driver_data = (void *) did;
  600. return 1;
  601. }
  602. static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
  603. struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
  604. struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
  605. struct pcmcia_device_id *did = p_drv->id_table;
  606. /* matching by cardmgr */
  607. if (p_dev->cardmgr == p_drv)
  608. return 1;
  609. while (did && did->match_flags) {
  610. if (pcmcia_devmatch(p_dev, did))
  611. return 1;
  612. did++;
  613. }
  614. return 0;
  615. }
  616. #ifdef CONFIG_HOTPLUG
  617. static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
  618. char *buffer, int buffer_size)
  619. {
  620. struct pcmcia_device *p_dev;
  621. int i, length = 0;
  622. u32 hash[4] = { 0, 0, 0, 0};
  623. if (!dev)
  624. return -ENODEV;
  625. p_dev = to_pcmcia_dev(dev);
  626. /* calculate hashes */
  627. for (i=0; i<4; i++) {
  628. if (!p_dev->prod_id[i])
  629. continue;
  630. hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
  631. }
  632. i = 0;
  633. if (add_hotplug_env_var(envp, num_envp, &i,
  634. buffer, buffer_size, &length,
  635. "SOCKET_NO=%u",
  636. p_dev->socket->sock))
  637. return -ENOMEM;
  638. if (add_hotplug_env_var(envp, num_envp, &i,
  639. buffer, buffer_size, &length,
  640. "DEVICE_NO=%02X",
  641. p_dev->device_no))
  642. return -ENOMEM;
  643. if (add_hotplug_env_var(envp, num_envp, &i,
  644. buffer, buffer_size, &length,
  645. "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  646. "pa%08Xpb%08Xpc%08Xpd%08X",
  647. p_dev->has_manf_id ? p_dev->manf_id : 0,
  648. p_dev->has_card_id ? p_dev->card_id : 0,
  649. p_dev->has_func_id ? p_dev->func_id : 0,
  650. p_dev->func,
  651. p_dev->device_no,
  652. hash[0],
  653. hash[1],
  654. hash[2],
  655. hash[3]))
  656. return -ENOMEM;
  657. envp[i] = NULL;
  658. return 0;
  659. }
  660. #else
  661. static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
  662. char *buffer, int buffer_size)
  663. {
  664. return -ENODEV;
  665. }
  666. #endif
  667. /************************ per-device sysfs output ***************************/
  668. #define pcmcia_device_attr(field, test, format) \
  669. static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  670. { \
  671. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  672. return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
  673. }
  674. #define pcmcia_device_stringattr(name, field) \
  675. static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  676. { \
  677. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  678. return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
  679. }
  680. pcmcia_device_attr(func, socket, "0x%02x\n");
  681. pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
  682. pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
  683. pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
  684. pcmcia_device_stringattr(prod_id1, prod_id[0]);
  685. pcmcia_device_stringattr(prod_id2, prod_id[1]);
  686. pcmcia_device_stringattr(prod_id3, prod_id[2]);
  687. pcmcia_device_stringattr(prod_id4, prod_id[3]);
  688. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  689. {
  690. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  691. int i;
  692. u32 hash[4] = { 0, 0, 0, 0};
  693. /* calculate hashes */
  694. for (i=0; i<4; i++) {
  695. if (!p_dev->prod_id[i])
  696. continue;
  697. hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
  698. }
  699. return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  700. "pa%08Xpb%08Xpc%08Xpd%08X\n",
  701. p_dev->has_manf_id ? p_dev->manf_id : 0,
  702. p_dev->has_card_id ? p_dev->card_id : 0,
  703. p_dev->has_func_id ? p_dev->func_id : 0,
  704. p_dev->func, p_dev->device_no,
  705. hash[0], hash[1], hash[2], hash[3]);
  706. }
  707. static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
  708. struct device_attribute *attr, const char *buf, size_t count)
  709. {
  710. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  711. if (!count)
  712. return -EINVAL;
  713. down(&p_dev->socket->skt_sem);
  714. p_dev->allow_func_id_match = 1;
  715. up(&p_dev->socket->skt_sem);
  716. bus_rescan_devices(&pcmcia_bus_type);
  717. return count;
  718. }
  719. static struct device_attribute pcmcia_dev_attrs[] = {
  720. __ATTR(function, 0444, func_show, NULL),
  721. __ATTR_RO(func_id),
  722. __ATTR_RO(manf_id),
  723. __ATTR_RO(card_id),
  724. __ATTR_RO(prod_id1),
  725. __ATTR_RO(prod_id2),
  726. __ATTR_RO(prod_id3),
  727. __ATTR_RO(prod_id4),
  728. __ATTR_RO(modalias),
  729. __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
  730. __ATTR_NULL,
  731. };
  732. /*======================================================================
  733. The card status event handler.
  734. ======================================================================*/
  735. struct send_event_data {
  736. struct pcmcia_socket *skt;
  737. event_t event;
  738. int priority;
  739. };
  740. static int send_event_callback(struct device *dev, void * _data)
  741. {
  742. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  743. struct send_event_data *data = _data;
  744. /* we get called for all sockets, but may only pass the event
  745. * for drivers _on the affected socket_ */
  746. if (p_dev->socket != data->skt)
  747. return 0;
  748. if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE))
  749. return 0;
  750. if (p_dev->client.EventMask & data->event)
  751. return EVENT(&p_dev->client, data->event, data->priority);
  752. return 0;
  753. }
  754. static int send_event(struct pcmcia_socket *s, event_t event, int priority)
  755. {
  756. struct send_event_data private;
  757. private.skt = s;
  758. private.event = event;
  759. private.priority = priority;
  760. return bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
  761. } /* send_event */
  762. /* Normally, the event is passed to individual drivers after
  763. * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
  764. * is inversed to maintain historic compatibility.
  765. */
  766. static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
  767. {
  768. struct pcmcia_socket *s = pcmcia_get_socket(skt);
  769. int ret = 0;
  770. ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
  771. event, priority, skt);
  772. switch (event) {
  773. case CS_EVENT_CARD_REMOVAL:
  774. s->pcmcia_state.present = 0;
  775. send_event(skt, event, priority);
  776. unbind_request(skt);
  777. handle_event(skt, event);
  778. break;
  779. case CS_EVENT_CARD_INSERTION:
  780. s->pcmcia_state.present = 1;
  781. pcmcia_card_add(skt);
  782. handle_event(skt, event);
  783. break;
  784. case CS_EVENT_EJECTION_REQUEST:
  785. ret = send_event(skt, event, priority);
  786. break;
  787. default:
  788. handle_event(skt, event);
  789. send_event(skt, event, priority);
  790. break;
  791. }
  792. pcmcia_put_socket(s);
  793. return 0;
  794. } /* ds_event */
  795. int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
  796. {
  797. client_t *client = NULL;
  798. struct pcmcia_socket *s = NULL;
  799. struct pcmcia_device *p_dev = NULL;
  800. /* Look for unbound client with matching dev_info */
  801. down_read(&pcmcia_socket_list_rwsem);
  802. list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
  803. unsigned long flags;
  804. if (s->state & SOCKET_CARDBUS)
  805. continue;
  806. s = pcmcia_get_socket(s);
  807. if (!s)
  808. continue;
  809. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  810. list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
  811. struct pcmcia_driver *p_drv;
  812. p_dev = pcmcia_get_dev(p_dev);
  813. if (!p_dev)
  814. continue;
  815. if (!(p_dev->client.state & CLIENT_UNBOUND) ||
  816. (!p_dev->dev.driver)) {
  817. pcmcia_put_dev(p_dev);
  818. continue;
  819. }
  820. p_drv = to_pcmcia_drv(p_dev->dev.driver);
  821. if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
  822. client = &p_dev->client;
  823. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  824. goto found;
  825. }
  826. pcmcia_put_dev(p_dev);
  827. }
  828. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  829. pcmcia_put_socket(s);
  830. }
  831. found:
  832. up_read(&pcmcia_socket_list_rwsem);
  833. if (!p_dev || !client)
  834. return -ENODEV;
  835. pcmcia_put_socket(s); /* safe, as we already hold a reference from bind_device */
  836. *handle = client;
  837. client->state &= ~CLIENT_UNBOUND;
  838. client->Socket = s;
  839. client->EventMask = req->EventMask;
  840. client->event_handler = req->event_handler;
  841. client->event_callback_args = req->event_callback_args;
  842. client->event_callback_args.client_handle = client;
  843. if (s->state & SOCKET_CARDBUS)
  844. client->state |= CLIENT_CARDBUS;
  845. if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) &&
  846. (client->Function != BIND_FN_ALL)) {
  847. cistpl_longlink_mfc_t mfc;
  848. if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc)
  849. == CS_SUCCESS)
  850. s->functions = mfc.nfn;
  851. else
  852. s->functions = 1;
  853. s->config = kmalloc(sizeof(config_t) * s->functions,
  854. GFP_KERNEL);
  855. if (!s->config)
  856. goto out_no_resource;
  857. memset(s->config, 0, sizeof(config_t) * s->functions);
  858. }
  859. ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
  860. client, p_dev->dev.bus_id);
  861. if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE)
  862. EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW);
  863. if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
  864. if (client->EventMask & CS_EVENT_CARD_INSERTION)
  865. EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
  866. }
  867. return CS_SUCCESS;
  868. out_no_resource:
  869. pcmcia_put_dev(p_dev);
  870. return CS_OUT_OF_RESOURCE;
  871. } /* register_client */
  872. EXPORT_SYMBOL(pcmcia_register_client);
  873. /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
  874. * drivers have been called with EVENT_CARD_REMOVAL before.
  875. */
  876. static int unbind_request(struct pcmcia_socket *s)
  877. {
  878. struct pcmcia_device *p_dev;
  879. unsigned long flags;
  880. ds_dbg(2, "unbind_request(%d)\n", s->sock);
  881. s->device_count = 0;
  882. for (;;) {
  883. /* unregister all pcmcia_devices registered with this socket*/
  884. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  885. if (list_empty(&s->devices_list)) {
  886. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  887. return 0;
  888. }
  889. p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
  890. list_del(&p_dev->socket_device_list);
  891. p_dev->client.state |= CLIENT_STALE;
  892. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  893. device_unregister(&p_dev->dev);
  894. }
  895. return 0;
  896. } /* unbind_request */
  897. int pcmcia_deregister_client(client_handle_t handle)
  898. {
  899. struct pcmcia_socket *s;
  900. int i;
  901. struct pcmcia_device *p_dev = handle_to_pdev(handle);
  902. if (CHECK_HANDLE(handle))
  903. return CS_BAD_HANDLE;
  904. s = SOCKET(handle);
  905. ds_dbg(1, "deregister_client(%p)\n", handle);
  906. if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
  907. goto warn_out;
  908. for (i = 0; i < MAX_WIN; i++)
  909. if (handle->state & CLIENT_WIN_REQ(i))
  910. goto warn_out;
  911. if (handle->state & CLIENT_STALE) {
  912. handle->client_magic = 0;
  913. handle->state &= ~CLIENT_STALE;
  914. pcmcia_put_dev(p_dev);
  915. } else {
  916. handle->state = CLIENT_UNBOUND;
  917. handle->event_handler = NULL;
  918. }
  919. return CS_SUCCESS;
  920. warn_out:
  921. printk(KERN_WARNING "ds: deregister_client was called too early.\n");
  922. return CS_IN_USE;
  923. } /* deregister_client */
  924. EXPORT_SYMBOL(pcmcia_deregister_client);
  925. static struct pcmcia_callback pcmcia_bus_callback = {
  926. .owner = THIS_MODULE,
  927. .event = ds_event,
  928. .requery = pcmcia_bus_rescan,
  929. };
  930. static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
  931. {
  932. struct pcmcia_socket *socket = class_get_devdata(class_dev);
  933. int ret;
  934. socket = pcmcia_get_socket(socket);
  935. if (!socket) {
  936. printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
  937. return -ENODEV;
  938. }
  939. /*
  940. * Ugly. But we want to wait for the socket threads to have started up.
  941. * We really should let the drivers themselves drive some of this..
  942. */
  943. msleep(250);
  944. #ifdef CONFIG_PCMCIA_IOCTL
  945. init_waitqueue_head(&socket->queue);
  946. #endif
  947. INIT_LIST_HEAD(&socket->devices_list);
  948. INIT_WORK(&socket->device_add, pcmcia_delayed_add_pseudo_device, socket);
  949. memset(&socket->pcmcia_state, 0, sizeof(u8));
  950. socket->device_count = 0;
  951. ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
  952. if (ret) {
  953. printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
  954. pcmcia_put_socket(socket);
  955. return (ret);
  956. }
  957. return 0;
  958. }
  959. static void pcmcia_bus_remove_socket(struct class_device *class_dev)
  960. {
  961. struct pcmcia_socket *socket = class_get_devdata(class_dev);
  962. if (!socket)
  963. return;
  964. socket->pcmcia_state.dead = 1;
  965. pccard_register_pcmcia(socket, NULL);
  966. pcmcia_put_socket(socket);
  967. return;
  968. }
  969. /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
  970. static struct class_interface pcmcia_bus_interface = {
  971. .class = &pcmcia_socket_class,
  972. .add = &pcmcia_bus_add_socket,
  973. .remove = &pcmcia_bus_remove_socket,
  974. };
  975. struct bus_type pcmcia_bus_type = {
  976. .name = "pcmcia",
  977. .hotplug = pcmcia_bus_hotplug,
  978. .match = pcmcia_bus_match,
  979. .dev_attrs = pcmcia_dev_attrs,
  980. };
  981. static int __init init_pcmcia_bus(void)
  982. {
  983. spin_lock_init(&pcmcia_dev_list_lock);
  984. bus_register(&pcmcia_bus_type);
  985. class_interface_register(&pcmcia_bus_interface);
  986. pcmcia_setup_ioctl();
  987. return 0;
  988. }
  989. fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
  990. * pcmcia_socket_class is already registered */
  991. static void __exit exit_pcmcia_bus(void)
  992. {
  993. pcmcia_cleanup_ioctl();
  994. class_interface_unregister(&pcmcia_bus_interface);
  995. bus_unregister(&pcmcia_bus_type);
  996. }
  997. module_exit(exit_pcmcia_bus);
  998. MODULE_ALIAS("ds");