ds.c 31 KB

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