ds.c 31 KB

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