divasmain.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /* $Id: divasmain.c,v 1.55.4.6 2005/02/09 19:28:20 armin Exp $
  2. *
  3. * Low level driver for Eicon DIVA Server ISDN cards.
  4. *
  5. * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
  6. * Copyright 2000-2003 Cytronics & Melware (info@melware.de)
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. */
  11. #include <linux/config.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/io.h>
  18. #include <linux/ioport.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/pci.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/list.h>
  24. #include <linux/poll.h>
  25. #include <linux/kmod.h>
  26. #include "platform.h"
  27. #undef ID_MASK
  28. #undef N_DATA
  29. #include "pc.h"
  30. #include "di_defs.h"
  31. #include "divasync.h"
  32. #include "diva.h"
  33. #include "di.h"
  34. #include "io.h"
  35. #include "xdi_msg.h"
  36. #include "xdi_adapter.h"
  37. #include "xdi_vers.h"
  38. #include "diva_dma.h"
  39. #include "diva_pci.h"
  40. static char *main_revision = "$Revision: 1.55.4.6 $";
  41. static int major;
  42. static int dbgmask;
  43. MODULE_DESCRIPTION("Kernel driver for Eicon DIVA Server cards");
  44. MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
  45. MODULE_LICENSE("GPL");
  46. module_param(dbgmask, int, 0);
  47. MODULE_PARM_DESC(dbgmask, "initial debug mask");
  48. static char *DRIVERNAME =
  49. "Eicon DIVA Server driver (http://www.melware.net)";
  50. static char *DRIVERLNAME = "divas";
  51. static char *DEVNAME = "Divas";
  52. char *DRIVERRELEASE_DIVAS = "2.0";
  53. extern irqreturn_t diva_os_irq_wrapper(int irq, void *context,
  54. struct pt_regs *regs);
  55. extern int create_divas_proc(void);
  56. extern void remove_divas_proc(void);
  57. extern void diva_get_vserial_number(PISDN_ADAPTER IoAdapter, char *buf);
  58. extern int divasfunc_init(int dbgmask);
  59. extern void divasfunc_exit(void);
  60. typedef struct _diva_os_thread_dpc {
  61. struct tasklet_struct divas_task;
  62. diva_os_soft_isr_t *psoft_isr;
  63. } diva_os_thread_dpc_t;
  64. /* --------------------------------------------------------------------------
  65. PCI driver interface section
  66. -------------------------------------------------------------------------- */
  67. /*
  68. vendor, device Vendor and device ID to match (or PCI_ANY_ID)
  69. subvendor, Subsystem vendor and device ID to match (or PCI_ANY_ID)
  70. subdevice
  71. class, Device class to match. The class_mask tells which bits
  72. class_mask of the class are honored during the comparison.
  73. driver_data Data private to the driver.
  74. */
  75. #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2)
  76. #define PCI_DEVICE_ID_EICON_MAESTRAP_2 0xE015
  77. #endif
  78. #if !defined(PCI_DEVICE_ID_EICON_4BRI_VOIP)
  79. #define PCI_DEVICE_ID_EICON_4BRI_VOIP 0xE016
  80. #endif
  81. #if !defined(PCI_DEVICE_ID_EICON_4BRI_2_VOIP)
  82. #define PCI_DEVICE_ID_EICON_4BRI_2_VOIP 0xE017
  83. #endif
  84. #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2)
  85. #define PCI_DEVICE_ID_EICON_BRI2M_2 0xE018
  86. #endif
  87. #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP)
  88. #define PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP 0xE019
  89. #endif
  90. #if !defined(PCI_DEVICE_ID_EICON_2F)
  91. #define PCI_DEVICE_ID_EICON_2F 0xE01A
  92. #endif
  93. #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2_VOIP)
  94. #define PCI_DEVICE_ID_EICON_BRI2M_2_VOIP 0xE01B
  95. #endif
  96. /*
  97. This table should be sorted by PCI device ID
  98. */
  99. static struct pci_device_id divas_pci_tbl[] = {
  100. /* Diva Server BRI-2M PCI 0xE010 */
  101. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRA,
  102. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_MAESTRA_PCI},
  103. /* Diva Server 4BRI-8M PCI 0xE012 */
  104. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ,
  105. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_PCI},
  106. /* Diva Server 4BRI-8M 2.0 PCI 0xE013 */
  107. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ_U,
  108. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_V2_PCI},
  109. /* Diva Server PRI-30M PCI 0xE014 */
  110. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP,
  111. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_PCI},
  112. /* Diva Server PRI 2.0 adapter 0xE015 */
  113. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2,
  114. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_V2_PCI},
  115. /* Diva Server Voice 4BRI-8M PCI 0xE016 */
  116. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_VOIP,
  117. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_PCI},
  118. /* Diva Server Voice 4BRI-8M 2.0 PCI 0xE017 */
  119. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_2_VOIP,
  120. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_V2_PCI},
  121. /* Diva Server BRI-2M 2.0 PCI 0xE018 */
  122. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2,
  123. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2M_V2_PCI},
  124. /* Diva Server Voice PRI 2.0 PCI 0xE019 */
  125. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP,
  126. PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  127. CARDTYPE_DIVASRV_VOICE_P_30M_V2_PCI},
  128. /* Diva Server 2FX 0xE01A */
  129. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_2F,
  130. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2F_PCI},
  131. /* Diva Server Voice BRI-2M 2.0 PCI 0xE01B */
  132. {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2_VOIP,
  133. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_B_2M_V2_PCI},
  134. {0,} /* 0 terminated list. */
  135. };
  136. MODULE_DEVICE_TABLE(pci, divas_pci_tbl);
  137. static int divas_init_one(struct pci_dev *pdev,
  138. const struct pci_device_id *ent);
  139. static void __devexit divas_remove_one(struct pci_dev *pdev);
  140. static struct pci_driver diva_pci_driver = {
  141. .name = "divas",
  142. .probe = divas_init_one,
  143. .remove = __devexit_p(divas_remove_one),
  144. .id_table = divas_pci_tbl,
  145. };
  146. /*********************************************************
  147. ** little helper functions
  148. *********************************************************/
  149. static char *getrev(const char *revision)
  150. {
  151. char *rev;
  152. char *p;
  153. if ((p = strchr(revision, ':'))) {
  154. rev = p + 2;
  155. p = strchr(rev, '$');
  156. *--p = 0;
  157. } else
  158. rev = "1.0";
  159. return rev;
  160. }
  161. void diva_log_info(unsigned char *format, ...)
  162. {
  163. va_list args;
  164. unsigned char line[160];
  165. va_start(args, format);
  166. vsprintf(line, format, args);
  167. va_end(args);
  168. printk(KERN_INFO "%s: %s\n", DRIVERLNAME, line);
  169. }
  170. void divas_get_version(char *p)
  171. {
  172. char tmprev[32];
  173. strcpy(tmprev, main_revision);
  174. sprintf(p, "%s: %s(%s) %s(%s) major=%d\n", DRIVERLNAME, DRIVERRELEASE_DIVAS,
  175. getrev(tmprev), diva_xdi_common_code_build, DIVA_BUILD, major);
  176. }
  177. /* --------------------------------------------------------------------------
  178. PCI Bus services
  179. -------------------------------------------------------------------------- */
  180. byte diva_os_get_pci_bus(void *pci_dev_handle)
  181. {
  182. struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
  183. return ((byte) pdev->bus->number);
  184. }
  185. byte diva_os_get_pci_func(void *pci_dev_handle)
  186. {
  187. struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
  188. return ((byte) pdev->devfn);
  189. }
  190. unsigned long divasa_get_pci_irq(unsigned char bus, unsigned char func,
  191. void *pci_dev_handle)
  192. {
  193. unsigned char irq = 0;
  194. struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
  195. irq = dev->irq;
  196. return ((unsigned long) irq);
  197. }
  198. unsigned long divasa_get_pci_bar(unsigned char bus, unsigned char func,
  199. int bar, void *pci_dev_handle)
  200. {
  201. unsigned long ret = 0;
  202. struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
  203. if (bar < 6) {
  204. ret = dev->resource[bar].start;
  205. }
  206. DBG_TRC(("GOT BAR[%d]=%08x", bar, ret));
  207. {
  208. unsigned long type = (ret & 0x00000001);
  209. if (type & PCI_BASE_ADDRESS_SPACE_IO) {
  210. DBG_TRC((" I/O"));
  211. ret &= PCI_BASE_ADDRESS_IO_MASK;
  212. } else {
  213. DBG_TRC((" memory"));
  214. ret &= PCI_BASE_ADDRESS_MEM_MASK;
  215. }
  216. DBG_TRC((" final=%08x", ret));
  217. }
  218. return (ret);
  219. }
  220. void PCIwrite(byte bus, byte func, int offset, void *data, int length,
  221. void *pci_dev_handle)
  222. {
  223. struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
  224. switch (length) {
  225. case 1: /* byte */
  226. pci_write_config_byte(dev, offset,
  227. *(unsigned char *) data);
  228. break;
  229. case 2: /* word */
  230. pci_write_config_word(dev, offset,
  231. *(unsigned short *) data);
  232. break;
  233. case 4: /* dword */
  234. pci_write_config_dword(dev, offset,
  235. *(unsigned int *) data);
  236. break;
  237. default: /* buffer */
  238. if (!(length % 4) && !(length & 0x03)) { /* Copy as dword */
  239. dword *p = (dword *) data;
  240. length /= 4;
  241. while (length--) {
  242. pci_write_config_dword(dev, offset,
  243. *(unsigned int *)
  244. p++);
  245. }
  246. } else { /* copy as byte stream */
  247. byte *p = (byte *) data;
  248. while (length--) {
  249. pci_write_config_byte(dev, offset,
  250. *(unsigned char *)
  251. p++);
  252. }
  253. }
  254. }
  255. }
  256. void PCIread(byte bus, byte func, int offset, void *data, int length,
  257. void *pci_dev_handle)
  258. {
  259. struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
  260. switch (length) {
  261. case 1: /* byte */
  262. pci_read_config_byte(dev, offset, (unsigned char *) data);
  263. break;
  264. case 2: /* word */
  265. pci_read_config_word(dev, offset, (unsigned short *) data);
  266. break;
  267. case 4: /* dword */
  268. pci_read_config_dword(dev, offset, (unsigned int *) data);
  269. break;
  270. default: /* buffer */
  271. if (!(length % 4) && !(length & 0x03)) { /* Copy as dword */
  272. dword *p = (dword *) data;
  273. length /= 4;
  274. while (length--) {
  275. pci_read_config_dword(dev, offset,
  276. (unsigned int *)
  277. p++);
  278. }
  279. } else { /* copy as byte stream */
  280. byte *p = (byte *) data;
  281. while (length--) {
  282. pci_read_config_byte(dev, offset,
  283. (unsigned char *)
  284. p++);
  285. }
  286. }
  287. }
  288. }
  289. /*
  290. Init map with DMA pages. It is not problem if some allocations fail -
  291. the channels that will not get one DMA page will use standard PIO
  292. interface
  293. */
  294. static void *diva_pci_alloc_consistent(struct pci_dev *hwdev,
  295. size_t size,
  296. dma_addr_t * dma_handle,
  297. void **addr_handle)
  298. {
  299. void *addr = pci_alloc_consistent(hwdev, size, dma_handle);
  300. *addr_handle = addr;
  301. return (addr);
  302. }
  303. void diva_init_dma_map(void *hdev,
  304. struct _diva_dma_map_entry **ppmap, int nentries)
  305. {
  306. struct pci_dev *pdev = (struct pci_dev *) hdev;
  307. struct _diva_dma_map_entry *pmap =
  308. diva_alloc_dma_map(hdev, nentries);
  309. if (pmap) {
  310. int i;
  311. dma_addr_t dma_handle;
  312. void *cpu_addr;
  313. void *addr_handle;
  314. for (i = 0; i < nentries; i++) {
  315. if (!(cpu_addr = diva_pci_alloc_consistent(pdev,
  316. PAGE_SIZE,
  317. &dma_handle,
  318. &addr_handle)))
  319. {
  320. break;
  321. }
  322. diva_init_dma_map_entry(pmap, i, cpu_addr,
  323. (dword) dma_handle,
  324. addr_handle);
  325. DBG_TRC(("dma map alloc [%d]=(%08lx:%08x:%08lx)",
  326. i, (unsigned long) cpu_addr,
  327. (dword) dma_handle,
  328. (unsigned long) addr_handle))}
  329. }
  330. *ppmap = pmap;
  331. }
  332. /*
  333. Free all contained in the map entries and memory used by the map
  334. Should be always called after adapter removal from DIDD array
  335. */
  336. void diva_free_dma_map(void *hdev, struct _diva_dma_map_entry *pmap)
  337. {
  338. struct pci_dev *pdev = (struct pci_dev *) hdev;
  339. int i;
  340. dword phys_addr;
  341. void *cpu_addr;
  342. dma_addr_t dma_handle;
  343. void *addr_handle;
  344. for (i = 0; (pmap != 0); i++) {
  345. diva_get_dma_map_entry(pmap, i, &cpu_addr, &phys_addr);
  346. if (!cpu_addr) {
  347. break;
  348. }
  349. addr_handle = diva_get_entry_handle(pmap, i);
  350. dma_handle = (dma_addr_t) phys_addr;
  351. pci_free_consistent(pdev, PAGE_SIZE, addr_handle,
  352. dma_handle);
  353. DBG_TRC(("dma map free [%d]=(%08lx:%08x:%08lx)", i,
  354. (unsigned long) cpu_addr, (dword) dma_handle,
  355. (unsigned long) addr_handle))
  356. }
  357. diva_free_dma_mapping(pmap);
  358. }
  359. /*********************************************************
  360. ** I/O port utilities
  361. *********************************************************/
  362. int
  363. diva_os_register_io_port(void *adapter, int on, unsigned long port,
  364. unsigned long length, const char *name, int id)
  365. {
  366. if (on) {
  367. if (!request_region(port, length, name)) {
  368. DBG_ERR(("A: I/O: can't register port=%08x", port))
  369. return (-1);
  370. }
  371. } else {
  372. release_region(port, length);
  373. }
  374. return (0);
  375. }
  376. void __iomem *divasa_remap_pci_bar(diva_os_xdi_adapter_t *a, int id, unsigned long bar, unsigned long area_length)
  377. {
  378. void __iomem *ret = ioremap(bar, area_length);
  379. DBG_TRC(("remap(%08x)->%p", bar, ret));
  380. return (ret);
  381. }
  382. void divasa_unmap_pci_bar(void __iomem *bar)
  383. {
  384. if (bar) {
  385. iounmap(bar);
  386. }
  387. }
  388. /*********************************************************
  389. ** I/O port access
  390. *********************************************************/
  391. byte __inline__ inpp(void __iomem *addr)
  392. {
  393. return (inb((unsigned long) addr));
  394. }
  395. word __inline__ inppw(void __iomem *addr)
  396. {
  397. return (inw((unsigned long) addr));
  398. }
  399. void __inline__ inppw_buffer(void __iomem *addr, void *P, int length)
  400. {
  401. insw((unsigned long) addr, (word *) P, length >> 1);
  402. }
  403. void __inline__ outppw_buffer(void __iomem *addr, void *P, int length)
  404. {
  405. outsw((unsigned long) addr, (word *) P, length >> 1);
  406. }
  407. void __inline__ outppw(void __iomem *addr, word w)
  408. {
  409. outw(w, (unsigned long) addr);
  410. }
  411. void __inline__ outpp(void __iomem *addr, word p)
  412. {
  413. outb(p, (unsigned long) addr);
  414. }
  415. /* --------------------------------------------------------------------------
  416. IRQ request / remove
  417. -------------------------------------------------------------------------- */
  418. int diva_os_register_irq(void *context, byte irq, const char *name)
  419. {
  420. int result = request_irq(irq, diva_os_irq_wrapper,
  421. SA_INTERRUPT | SA_SHIRQ, name, context);
  422. return (result);
  423. }
  424. void diva_os_remove_irq(void *context, byte irq)
  425. {
  426. free_irq(irq, context);
  427. }
  428. /* --------------------------------------------------------------------------
  429. DPC framework implementation
  430. -------------------------------------------------------------------------- */
  431. static void diva_os_dpc_proc(unsigned long context)
  432. {
  433. diva_os_thread_dpc_t *psoft_isr = (diva_os_thread_dpc_t *) context;
  434. diva_os_soft_isr_t *pisr = psoft_isr->psoft_isr;
  435. (*(pisr->callback)) (pisr, pisr->callback_context);
  436. }
  437. int diva_os_initialize_soft_isr(diva_os_soft_isr_t * psoft_isr,
  438. diva_os_soft_isr_callback_t callback,
  439. void *callback_context)
  440. {
  441. diva_os_thread_dpc_t *pdpc;
  442. pdpc = (diva_os_thread_dpc_t *) diva_os_malloc(0, sizeof(*pdpc));
  443. if (!(psoft_isr->object = pdpc)) {
  444. return (-1);
  445. }
  446. memset(pdpc, 0x00, sizeof(*pdpc));
  447. psoft_isr->callback = callback;
  448. psoft_isr->callback_context = callback_context;
  449. pdpc->psoft_isr = psoft_isr;
  450. tasklet_init(&pdpc->divas_task, diva_os_dpc_proc, (unsigned long)pdpc);
  451. return (0);
  452. }
  453. int diva_os_schedule_soft_isr(diva_os_soft_isr_t * psoft_isr)
  454. {
  455. if (psoft_isr && psoft_isr->object) {
  456. diva_os_thread_dpc_t *pdpc =
  457. (diva_os_thread_dpc_t *) psoft_isr->object;
  458. tasklet_schedule(&pdpc->divas_task);
  459. }
  460. return (1);
  461. }
  462. int diva_os_cancel_soft_isr(diva_os_soft_isr_t * psoft_isr)
  463. {
  464. return (0);
  465. }
  466. void diva_os_remove_soft_isr(diva_os_soft_isr_t * psoft_isr)
  467. {
  468. if (psoft_isr && psoft_isr->object) {
  469. diva_os_thread_dpc_t *pdpc =
  470. (diva_os_thread_dpc_t *) psoft_isr->object;
  471. void *mem;
  472. tasklet_kill(&pdpc->divas_task);
  473. flush_scheduled_work();
  474. mem = psoft_isr->object;
  475. psoft_isr->object = NULL;
  476. diva_os_free(0, mem);
  477. }
  478. }
  479. /*
  480. * kernel/user space copy functions
  481. */
  482. static int
  483. xdi_copy_to_user(void *os_handle, void __user *dst, const void *src, int length)
  484. {
  485. if (copy_to_user(dst, src, length)) {
  486. return (-EFAULT);
  487. }
  488. return (length);
  489. }
  490. static int
  491. xdi_copy_from_user(void *os_handle, void *dst, const void __user *src, int length)
  492. {
  493. if (copy_from_user(dst, src, length)) {
  494. return (-EFAULT);
  495. }
  496. return (length);
  497. }
  498. /*
  499. * device node operations
  500. */
  501. static int divas_open(struct inode *inode, struct file *file)
  502. {
  503. return (0);
  504. }
  505. static int divas_release(struct inode *inode, struct file *file)
  506. {
  507. if (file->private_data) {
  508. diva_xdi_close_adapter(file->private_data, file);
  509. }
  510. return (0);
  511. }
  512. static ssize_t divas_write(struct file *file, const char __user *buf,
  513. size_t count, loff_t * ppos)
  514. {
  515. int ret = -EINVAL;
  516. if (!file->private_data) {
  517. file->private_data = diva_xdi_open_adapter(file, buf,
  518. count,
  519. xdi_copy_from_user);
  520. }
  521. if (!file->private_data) {
  522. return (-ENODEV);
  523. }
  524. ret = diva_xdi_write(file->private_data, file,
  525. buf, count, xdi_copy_from_user);
  526. switch (ret) {
  527. case -1: /* Message should be removed from rx mailbox first */
  528. ret = -EBUSY;
  529. break;
  530. case -2: /* invalid adapter was specified in this call */
  531. ret = -ENOMEM;
  532. break;
  533. case -3:
  534. ret = -ENXIO;
  535. break;
  536. }
  537. DBG_TRC(("write: ret %d", ret));
  538. return (ret);
  539. }
  540. static ssize_t divas_read(struct file *file, char __user *buf,
  541. size_t count, loff_t * ppos)
  542. {
  543. int ret = -EINVAL;
  544. if (!file->private_data) {
  545. file->private_data = diva_xdi_open_adapter(file, buf,
  546. count,
  547. xdi_copy_from_user);
  548. }
  549. if (!file->private_data) {
  550. return (-ENODEV);
  551. }
  552. ret = diva_xdi_read(file->private_data, file,
  553. buf, count, xdi_copy_to_user);
  554. switch (ret) {
  555. case -1: /* RX mailbox is empty */
  556. ret = -EAGAIN;
  557. break;
  558. case -2: /* no memory, mailbox was cleared, last command is failed */
  559. ret = -ENOMEM;
  560. break;
  561. case -3: /* can't copy to user, retry */
  562. ret = -EFAULT;
  563. break;
  564. }
  565. DBG_TRC(("read: ret %d", ret));
  566. return (ret);
  567. }
  568. static unsigned int divas_poll(struct file *file, poll_table * wait)
  569. {
  570. if (!file->private_data) {
  571. return (POLLERR);
  572. }
  573. return (POLLIN | POLLRDNORM);
  574. }
  575. static struct file_operations divas_fops = {
  576. .owner = THIS_MODULE,
  577. .llseek = no_llseek,
  578. .read = divas_read,
  579. .write = divas_write,
  580. .poll = divas_poll,
  581. .open = divas_open,
  582. .release = divas_release
  583. };
  584. static void divas_unregister_chrdev(void)
  585. {
  586. unregister_chrdev(major, DEVNAME);
  587. }
  588. static int DIVA_INIT_FUNCTION divas_register_chrdev(void)
  589. {
  590. if ((major = register_chrdev(0, DEVNAME, &divas_fops)) < 0)
  591. {
  592. printk(KERN_ERR "%s: failed to create /dev entry.\n",
  593. DRIVERLNAME);
  594. return (0);
  595. }
  596. return (1);
  597. }
  598. /* --------------------------------------------------------------------------
  599. PCI driver section
  600. -------------------------------------------------------------------------- */
  601. static int __devinit divas_init_one(struct pci_dev *pdev,
  602. const struct pci_device_id *ent)
  603. {
  604. void *pdiva = NULL;
  605. u8 pci_latency;
  606. u8 new_latency = 32;
  607. DBG_TRC(("%s bus: %08x fn: %08x insertion.\n",
  608. CardProperties[ent->driver_data].Name,
  609. pdev->bus->number, pdev->devfn))
  610. printk(KERN_INFO "%s: %s bus: %08x fn: %08x insertion.\n",
  611. DRIVERLNAME, CardProperties[ent->driver_data].Name,
  612. pdev->bus->number, pdev->devfn);
  613. if (pci_enable_device(pdev)) {
  614. DBG_TRC(("%s: %s bus: %08x fn: %08x device init failed.\n",
  615. DRIVERLNAME,
  616. CardProperties[ent->driver_data].Name,
  617. pdev->bus->number,
  618. pdev->devfn))
  619. printk(KERN_ERR
  620. "%s: %s bus: %08x fn: %08x device init failed.\n",
  621. DRIVERLNAME,
  622. CardProperties[ent->driver_data].
  623. Name, pdev->bus->number,
  624. pdev->devfn);
  625. return (-EIO);
  626. }
  627. pci_set_master(pdev);
  628. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
  629. if (!pci_latency) {
  630. DBG_TRC(("%s: bus: %08x fn: %08x fix latency.\n",
  631. DRIVERLNAME, pdev->bus->number, pdev->devfn))
  632. printk(KERN_INFO
  633. "%s: bus: %08x fn: %08x fix latency.\n",
  634. DRIVERLNAME, pdev->bus->number, pdev->devfn);
  635. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, new_latency);
  636. }
  637. if (!(pdiva = diva_driver_add_card(pdev, ent->driver_data))) {
  638. DBG_TRC(("%s: %s bus: %08x fn: %08x card init failed.\n",
  639. DRIVERLNAME,
  640. CardProperties[ent->driver_data].Name,
  641. pdev->bus->number,
  642. pdev->devfn))
  643. printk(KERN_ERR
  644. "%s: %s bus: %08x fn: %08x card init failed.\n",
  645. DRIVERLNAME,
  646. CardProperties[ent->driver_data].
  647. Name, pdev->bus->number,
  648. pdev->devfn);
  649. return (-EIO);
  650. }
  651. pci_set_drvdata(pdev, pdiva);
  652. return (0);
  653. }
  654. static void __devexit divas_remove_one(struct pci_dev *pdev)
  655. {
  656. void *pdiva = pci_get_drvdata(pdev);
  657. DBG_TRC(("bus: %08x fn: %08x removal.\n",
  658. pdev->bus->number, pdev->devfn))
  659. printk(KERN_INFO "%s: bus: %08x fn: %08x removal.\n",
  660. DRIVERLNAME, pdev->bus->number, pdev->devfn);
  661. if (pdiva) {
  662. diva_driver_remove_card(pdiva);
  663. }
  664. }
  665. /* --------------------------------------------------------------------------
  666. Driver Load / Startup
  667. -------------------------------------------------------------------------- */
  668. static int DIVA_INIT_FUNCTION divas_init(void)
  669. {
  670. char tmprev[50];
  671. int ret = 0;
  672. printk(KERN_INFO "%s\n", DRIVERNAME);
  673. printk(KERN_INFO "%s: Rel:%s Rev:", DRIVERLNAME, DRIVERRELEASE_DIVAS);
  674. strcpy(tmprev, main_revision);
  675. printk("%s Build: %s(%s)\n", getrev(tmprev),
  676. diva_xdi_common_code_build, DIVA_BUILD);
  677. printk(KERN_INFO "%s: support for: ", DRIVERLNAME);
  678. #ifdef CONFIG_ISDN_DIVAS_BRIPCI
  679. printk("BRI/PCI ");
  680. #endif
  681. #ifdef CONFIG_ISDN_DIVAS_PRIPCI
  682. printk("PRI/PCI ");
  683. #endif
  684. printk("adapters\n");
  685. if (!divasfunc_init(dbgmask)) {
  686. printk(KERN_ERR "%s: failed to connect to DIDD.\n",
  687. DRIVERLNAME);
  688. ret = -EIO;
  689. goto out;
  690. }
  691. if (!divas_register_chrdev()) {
  692. #ifdef MODULE
  693. divasfunc_exit();
  694. #endif
  695. ret = -EIO;
  696. goto out;
  697. }
  698. if (!create_divas_proc()) {
  699. #ifdef MODULE
  700. remove_divas_proc();
  701. divas_unregister_chrdev();
  702. divasfunc_exit();
  703. #endif
  704. printk(KERN_ERR "%s: failed to create proc entry.\n",
  705. DRIVERLNAME);
  706. ret = -EIO;
  707. goto out;
  708. }
  709. if ((ret = pci_register_driver(&diva_pci_driver))) {
  710. #ifdef MODULE
  711. remove_divas_proc();
  712. divas_unregister_chrdev();
  713. divasfunc_exit();
  714. #endif
  715. printk(KERN_ERR "%s: failed to init pci driver.\n",
  716. DRIVERLNAME);
  717. goto out;
  718. }
  719. printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
  720. out:
  721. return (ret);
  722. }
  723. /* --------------------------------------------------------------------------
  724. Driver Unload
  725. -------------------------------------------------------------------------- */
  726. static void DIVA_EXIT_FUNCTION divas_exit(void)
  727. {
  728. pci_unregister_driver(&diva_pci_driver);
  729. remove_divas_proc();
  730. divas_unregister_chrdev();
  731. divasfunc_exit();
  732. printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
  733. }
  734. module_init(divas_init);
  735. module_exit(divas_exit);