Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1372 lines
39 KiB

  1. /*-------------------------------------------------------------------
  2. | options.c - Handle options.
  3. Copyright 1996-98 Comtrol Corporation. All rights reserved.
  4. |--------------------------------------------------------------------*/
  5. #include "precomp.h"
  6. #define D_Level D_Options
  7. static int set_reg_option(IN int OptionVarType,
  8. IN HANDLE Handle,
  9. IN const char *szVarName,
  10. IN VOID *Value);
  11. static int get_reg_option(IN int OptionVarType,
  12. IN HANDLE Handle,
  13. IN HANDLE DefHandle,
  14. IN const char *szVarName,
  15. OUT char *szValue,
  16. IN int szValueSize);
  17. static int atomac(BYTE *mac, char *str);
  18. static int SetMainOption(int index, char *value);
  19. static int SetDeviceOption(int device_index, int option_index, char *value);
  20. static int SetPortOption(int device_index,
  21. int port_index,
  22. int option_index,
  23. char *value);
  24. //--- country codes for SocketModem support
  25. #define mcNotUsed 0
  26. #define mcAustria 1
  27. #define mcBelgium 2
  28. #define mcDenmark 3
  29. #define mcFinland 4
  30. #define mcFrance 5
  31. #define mcGermany 6
  32. #define mcIreland 7
  33. #define mcItaly 8
  34. #define mcLuxembourg 9
  35. #define mcNetherlands 10
  36. #define mcNorway 11
  37. #define mcPortugal 12
  38. #define mcSpain 13
  39. #define mcSweden 14
  40. #define mcSwitzerland 15
  41. #define mcUK 16
  42. #define mcGreece 17
  43. #define mcIsrael 18
  44. #define mcCzechRep 19
  45. #define mcCanada 20
  46. #define mcMexico 21
  47. #define mcUSA 22
  48. #define mcNA mcUSA // North America
  49. #define mcHungary 23
  50. #define mcPoland 24
  51. #define mcRussia 25
  52. #define mcSlovacRep 26
  53. #define mcBulgaria 27
  54. // 28
  55. // 29
  56. #define mcIndia 30
  57. // 31
  58. // 32
  59. // 33
  60. // 34
  61. // 35
  62. // 36
  63. // 37
  64. // 38
  65. // 39
  66. #define mcAustralia 40
  67. #define mcChina 41
  68. #define mcHongKong 42
  69. #define mcJapan 43
  70. #define mcPhilippines mcJapan
  71. #define mcKorea 44
  72. // 45
  73. #define mcTaiwan 46
  74. #define mcSingapore 47
  75. #define mcNewZealand 48
  76. #ifdef NT50
  77. /*----------------------------------------------------------------------
  78. write_device_options - Normally the driver just reads the config
  79. from the registry, but NT5.0 is a bit more dynamic(it gets started
  80. prior to configuration, and we want to write out the port names
  81. if the driver has no defaults. This enables the config prop pages
  82. to be in sync when we fire it up.
  83. |----------------------------------------------------------------------*/
  84. int write_device_options(PSERIAL_DEVICE_EXTENSION ext)
  85. {
  86. #if 0
  87. int port_i, stat;
  88. PSERIAL_DEVICE_EXTENSION port_ext;
  89. HANDLE DevHandle;
  90. DEVICE_CONFIG *dev_config;
  91. // make sure \\Parameters subkey is made
  92. MakeRegPath(szParameters); // this forms Driver.OptionRegPath
  93. RtlCreateRegistryKey(RTL_REGISTRY_ABSOLUTE, Driver.OptionRegPath.Buffer);
  94. // form "\Parameters\Device#" or "\Parameters\Device<pnp-id>"
  95. stat = make_device_keystr(ext, devstr);
  96. if (stat)
  97. return 1; // err
  98. // make sure \Parameters\Device# subkey is made
  99. MakeRegPath(devstr); // this forms Driver.OptionRegPath
  100. RtlCreateRegistryKey(RTL_REGISTRY_ABSOLUTE, Driver.OptionRegPath.Buffer);
  101. #endif
  102. #if 0
  103. // no, setup program can grab pnp hardware id, etc as well as we can.
  104. //---- open and write out critical device entries so setup
  105. // sees what we have.
  106. stat = our_open_device_reg(&DevHandle, dev_ext, KEY_ALL_ACCESS);
  107. if (stat == 0)
  108. {
  109. dev_config = ext->config;
  110. stat = set_reg_option(OP_T_DWORD, // dword, string, etc
  111. DevHandle,
  112. szNumPort, // name of var. to set
  113. (VOID *)dev_config->NumPorts);
  114. if (stat)
  115. { MyKdPrint(D_Error, ("Write Err B\n")) }
  116. ZwClose(DevHandle);
  117. }
  118. #endif
  119. #if 0
  120. //---- now write out the port names
  121. port_i = 0;
  122. port_ext = ext->port_ext;
  123. while (port_ext != NULL)
  124. {
  125. write_port_name(ext, port_i);
  126. ++port_i;
  127. port_ext = port_ext->port_ext;
  128. }
  129. #endif
  130. return 0;
  131. }
  132. /*----------------------------------------------------------------------
  133. write_port_name -
  134. |----------------------------------------------------------------------*/
  135. int write_port_name(PSERIAL_DEVICE_EXTENSION dev_ext, int port_index)
  136. {
  137. //char devstr[60];
  138. char portstr[20];
  139. //char tmpstr[80];
  140. int stat;
  141. PORT_CONFIG *port_config;
  142. HANDLE DevHandle = NULL;
  143. HANDLE PortHandle = NULL;
  144. port_config = &dev_ext->config->port[port_index];
  145. MyKdPrint(D_Init, ("write_port_name:%s\n",port_config->Name))
  146. // make sure \Parameters\Device# subkey is made
  147. stat = our_open_device_reg(&DevHandle, dev_ext, KEY_ALL_ACCESS);
  148. if (stat)
  149. {
  150. MyKdPrint(D_Error, ("write_port_name, error\n"))
  151. return 1;
  152. }
  153. Sprintf(portstr, "Port%d", port_index);
  154. stat = our_open_key(&PortHandle,
  155. DevHandle,
  156. portstr,
  157. KEY_ALL_ACCESS);
  158. if (stat == 0)
  159. {
  160. MyKdPrint(D_Init, ("set_reg1, writing %s=%s\n",
  161. szName, port_config->Name))
  162. stat = set_reg_option(OP_T_STRING, // dword, string, etc
  163. PortHandle,
  164. szName, // name of var. to set
  165. (VOID *)port_config->Name);
  166. if (stat)
  167. { MyKdPrint(D_Error, ("Write Err B\n")) }
  168. ZwClose(PortHandle);
  169. }
  170. ZwClose(DevHandle);
  171. return 0;
  172. }
  173. #endif
  174. /*----------------------------------------------------------------------
  175. write_dev_mac - Used for auto-config, writes mac-addr out to reg
  176. |----------------------------------------------------------------------*/
  177. int write_dev_mac(PSERIAL_DEVICE_EXTENSION dev_ext)
  178. {
  179. char macstr[30];
  180. int stat;
  181. HANDLE DevHandle = NULL;
  182. BYTE *mac;
  183. MyKdPrint(D_Init, ("write_dev_mac\n"))
  184. // make sure \Parameters\Device# subkey is made
  185. stat = our_open_device_reg(&DevHandle, dev_ext, KEY_ALL_ACCESS);
  186. if (stat)
  187. {
  188. MyKdPrint(D_Error, ("write_port_name, error\n"))
  189. return 1;
  190. }
  191. mac = dev_ext->config->MacAddr;
  192. Sprintf(macstr, "%02x %02x %02x %02x %02x %02x",
  193. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  194. MyKdPrint(D_Init, ("set_mac, writing %s=%s\n",
  195. szMacAddr, macstr))
  196. stat = set_reg_option(OP_T_STRING, // dword, string, etc
  197. DevHandle,
  198. szMacAddr, // name of var. to set
  199. (VOID *)macstr);
  200. if (stat)
  201. { MyKdPrint(D_Error, ("Write Err 5\n")) }
  202. ZwClose(DevHandle);
  203. return 0;
  204. }
  205. /*----------------------------------------------------------------------
  206. read_device_options - Read in the program options from the registry.
  207. These options are at device level, and port level. The device holds
  208. all the config options for the ports as well.
  209. |----------------------------------------------------------------------*/
  210. int read_device_options(PSERIAL_DEVICE_EXTENSION ext)
  211. {
  212. int j, port_i, stat;
  213. ULONG dstat;
  214. char tmpstr[80];
  215. char option_str[62];
  216. char small_str[20];
  217. HANDLE DevHandle = NULL;
  218. HANDLE PortHandle = NULL;
  219. HANDLE DefDevHandle = NULL;
  220. HANDLE DefPortHandle = NULL;
  221. HANDLE DriverHandle = NULL;
  222. //DEVICE_CONFIG *dev_config;
  223. // dev_config = (DEVICE_CONFIG *) ExAllocatePool(NonPagedPool,sizeof(*dev_config));
  224. // ExFreePool(dev_config);
  225. MyKdPrint(D_Init, ("read_device_options\n"))
  226. dstat = our_open_driver_reg(&DriverHandle, KEY_READ);
  227. if (dstat == 0)
  228. {
  229. // open up a "default" registry areas, where we look for config
  230. // if the main one does not exist.
  231. dstat = our_open_key(&DefDevHandle, DriverHandle, "DefDev", KEY_READ);
  232. dstat = our_open_key(&DefPortHandle, DriverHandle, "DefPort", KEY_READ);
  233. our_close_key(DriverHandle);
  234. }
  235. stat = our_open_device_reg(&DevHandle, ext, KEY_READ);
  236. if (stat)
  237. {
  238. MyKdPrint(D_Error, ("read_device_options: Err1\n"))
  239. }
  240. //------ read in the device options
  241. j = 0;
  242. while (device_options[j].name != NULL)
  243. {
  244. dstat = get_reg_option(device_options[j].var_type, // dword, string, etc
  245. DevHandle,
  246. DefDevHandle, //DefDevHandle,
  247. device_options[j].name, // name of var. to get
  248. option_str, 60); // return string value
  249. if (dstat == 0) // ok we read it
  250. {
  251. Sprintf(tmpstr,"device[%d].%s=%s",
  252. BoardExtToNumber(ext),
  253. device_options[j].name,
  254. option_str);
  255. dstat = SetOptionStr(tmpstr);
  256. if (dstat != 0)
  257. {
  258. MyKdPrint(D_Init, (" Err %d, last option\n", dstat))
  259. }
  260. }
  261. else
  262. {
  263. MyKdPrint(D_Init, ("No %s option in reg\n", device_options[j].name))
  264. }
  265. ++j;
  266. }
  267. #if DBG
  268. if (ext == NULL)
  269. {
  270. MyKdPrint(D_Init, ("ErrD\n"))
  271. return 1;
  272. }
  273. if (ext->config == NULL)
  274. {
  275. MyKdPrint(D_Init, ("ErrE\n"))
  276. return 1;
  277. }
  278. #endif
  279. #ifdef S_VS
  280. if (mac_match(ext->config->MacAddr, mac_zero_addr)) // set to auto
  281. {
  282. #ifndef NT50
  283. Eprintf("Error, Device address not setup");
  284. #endif
  285. // allow to load using bogus mac-address, so driver stays loaded.
  286. //0 c0 4e # # #
  287. memcpy(ext->config->MacAddr, mac_bogus_addr, 6);
  288. //0,0xc0,0x4e,0,0,0
  289. }
  290. #endif
  291. if ((DevHandle != NULL) || (DefPortHandle != NULL))
  292. {
  293. //------ get the Port information from setup.exe
  294. for (port_i=0; port_i<ext->config->NumPorts; port_i++)
  295. {
  296. Sprintf(small_str, "Port%d", port_i);
  297. stat = our_open_key(&PortHandle,
  298. DevHandle, // relative to this handle
  299. small_str,
  300. KEY_READ);
  301. if (stat)
  302. {
  303. MyKdPrint(D_Error, ("read_device_options: port Err2\n"))
  304. }
  305. j = 0;
  306. while (port_options[j].name != NULL)
  307. {
  308. dstat = get_reg_option(port_options[j].var_type, // dword, string, etc
  309. PortHandle,
  310. DefPortHandle, // DefPortHandle,
  311. port_options[j].name, // name of var. to get
  312. option_str, 60); // return string value
  313. if (dstat == 0) // ok we read it
  314. {
  315. Sprintf(tmpstr,"device[%d].port[%d].%s=%s",
  316. BoardExtToNumber(ext), port_i, port_options[j].name, option_str);
  317. dstat = SetOptionStr(tmpstr);
  318. if (dstat)
  319. {
  320. MyKdPrint(D_Error, ("Err %d, Option:%s\n",dstat, tmpstr))
  321. }
  322. }
  323. ++j;
  324. }
  325. our_close_key(PortHandle);
  326. } // ports
  327. our_close_key(DefPortHandle);
  328. our_close_key(DefDevHandle);
  329. our_close_key(DevHandle);
  330. }
  331. return 0;
  332. }
  333. /*----------------------------------------------------------------------
  334. read_driver_options - Read in the initial program options from the registry.
  335. These options are at driver level.
  336. |----------------------------------------------------------------------*/
  337. int read_driver_options(void)
  338. {
  339. int i;
  340. ULONG dstat;
  341. char tmpstr[80];
  342. char option_str[62];
  343. HANDLE DriverHandle = NULL;
  344. HANDLE DefDriverHandle = NULL;
  345. MyKdPrint(D_Init, ("read_driver_options\n"))
  346. // set some default options
  347. Driver.MdmCountryCode = mcNA; // North America
  348. dstat = our_open_driver_reg(&DriverHandle, KEY_READ);
  349. if (dstat == 0)
  350. {
  351. // open up a "default" registry area, where we look for config
  352. // if the main one does not exist.
  353. dstat = our_open_key(&DefDriverHandle, DriverHandle, "DefDrv", KEY_READ);
  354. MyKdPrint(D_Init, ("driver Defh:%x\n", DefDriverHandle))
  355. i = 0;
  356. while (driver_options[i].name != NULL)
  357. {
  358. MyKdPrint(D_Init, ("get %s\n", driver_options[i].name))
  359. dstat = get_reg_option(driver_options[i].var_type, // dword, string, etc
  360. DriverHandle,
  361. DefDriverHandle,
  362. driver_options[i].name, // name of var. to get
  363. option_str, 60); // return string value
  364. if (dstat == 0) // ok we read it
  365. {
  366. MyKdPrint(D_Init, ("got %s\n", option_str))
  367. Sprintf(tmpstr,"%s=%s",driver_options[i].name, option_str);
  368. dstat = SetOptionStr(tmpstr);
  369. if (dstat != 0)
  370. {
  371. Sprintf(tmpstr,"Err %d, last option\n",dstat);
  372. MyKdPrint(D_Error, (tmpstr))
  373. }
  374. }
  375. ++i;
  376. }
  377. }
  378. else
  379. {
  380. MyKdPrint(D_Error, ("Read driver failed key open"))
  381. }
  382. our_close_key(DefDriverHandle);
  383. our_close_key(DriverHandle);
  384. if (Driver.NumDevices == 0)
  385. Driver.NumDevices = 1;
  386. if (Driver.NumDevices > MAX_NUM_BOXES)
  387. Driver.NumDevices = MAX_NUM_BOXES;
  388. return 0;
  389. }
  390. /*----------------------------------------------------------------------
  391. set_reg_option - write out a option to the registry
  392. |----------------------------------------------------------------------*/
  393. static int set_reg_option(IN int OptionVarType,
  394. IN HANDLE Handle,
  395. IN const char *szVarName,
  396. IN VOID *Value)
  397. {
  398. int dstat = 1; // err
  399. MyKdPrint(D_Init, ("set_reg_option %s=", szVarName))
  400. if (OptionVarType == OP_T_STRING) // string option type
  401. {
  402. MyKdPrint(D_Init, ("%s\n", (char *)Value))
  403. dstat = our_set_value(Handle,
  404. (char *)szVarName,
  405. Value,
  406. strlen((char *) Value),
  407. REG_SZ);
  408. }
  409. else // DWORD option type
  410. {
  411. MyKdPrint(D_Init, ("DWORD\n"))
  412. dstat = our_set_value(Handle,
  413. (char *)szVarName,
  414. Value,
  415. sizeof(DWORD),
  416. REG_DWORD);
  417. }
  418. if (dstat)
  419. {
  420. MyKdPrint(D_Error, ("set_reg_option:err\n"))
  421. }
  422. return dstat;
  423. }
  424. /*----------------------------------------------------------------------
  425. get_reg_option - read in a option from the registry, and convert it to
  426. ascii.
  427. |----------------------------------------------------------------------*/
  428. static int get_reg_option(IN int OptionVarType,
  429. IN HANDLE Handle,
  430. IN HANDLE DefHandle,
  431. IN const char *szVarName,
  432. OUT char *szValue,
  433. IN int szValueSize)
  434. {
  435. int dstat = 1; // err
  436. ULONG dwValue;
  437. char buffer[200];
  438. char *ret_str;
  439. ULONG data_type;
  440. //MyKdPrint(D_Init, ("get_reg_option\n"))
  441. szValue[0] = 0;
  442. dstat = our_query_value(Handle,
  443. (char *)szVarName,
  444. buffer,
  445. sizeof(buffer),
  446. &data_type,
  447. &ret_str);
  448. if ((dstat != 0) && (DefHandle != NULL))
  449. {
  450. dstat = our_query_value(DefHandle,
  451. (char *)szVarName,
  452. buffer,
  453. sizeof(buffer),
  454. &data_type,
  455. &ret_str);
  456. if (dstat == 0)
  457. {
  458. MyKdPrint(D_Test, ("query default reg val\n"))
  459. }
  460. }
  461. if (OptionVarType == OP_T_STRING) // string option type
  462. {
  463. if (dstat == 0)
  464. {
  465. WStrToCStr(szValue, (PWCHAR)ret_str, szValueSize);
  466. MyKdPrint(D_Test, ("reg read:%s\n", ret_str))
  467. }
  468. }
  469. else // DWORD option type
  470. {
  471. if (dstat == 0) // value read ok
  472. dwValue = *((ULONG *) ret_str);
  473. else
  474. dwValue = 0;
  475. Sprintf(szValue,"%d", dwValue);
  476. }
  477. if (dstat)
  478. {
  479. //MyKdPrint(D_Init, ("get_reg_option:No value for:%s\n", szVarName))
  480. }
  481. else
  482. {
  483. MyKdPrint(D_Init, ("get_reg_option:%s=%s\n", szVarName, szValue))
  484. }
  485. //MyKdPrint(D_Init, ("End get_reg_option\n"))
  486. return dstat;
  487. }
  488. /*-----------------------------------------------------------------------
  489. SetOptionStr - set an option, based on simple ascii command line
  490. entry. Allow:
  491. GlobalVar1 = value;
  492. GlobalVar2 = value;
  493. box[0].BoxVar = value;
  494. box[0].port[5].PortVar = value;
  495. |-----------------------------------------------------------------------*/
  496. int SetOptionStr(char *option_str)
  497. {
  498. int i;
  499. int option_i = -1;
  500. int box_i = -1;
  501. int port_i = -1;
  502. int option_id = -1;
  503. int stat;
  504. PSERIAL_DEVICE_EXTENSION board_ext = NULL;
  505. MyKdPrint(D_Level, ("SetOptionStr:%s\n", option_str))
  506. if (my_sub_lstricmp("device", option_str) == 0) // match
  507. {
  508. option_str += 6; // pass up "device"
  509. if (*option_str++ != '[')
  510. return 1;
  511. #if (defined(NT50))
  512. if (my_toupper(*option_str) == 'D') // it's a nt5.0 keyword device name
  513. {
  514. int k;
  515. // instead of an index, we key off a pnp-device name
  516. // this is because nt50 stores devices under a pnp-tree dynamically
  517. // and does not have just a simple array list of devices.
  518. board_ext = Driver.board_ext;
  519. box_i = -1;
  520. k = 0;
  521. while (board_ext != NULL)
  522. {
  523. if (my_sub_lstricmp(board_ext->config->szNt50DevObjName,
  524. option_str) == 0) // match
  525. {
  526. i = strlen(board_ext->config->szNt50DevObjName);
  527. box_i = k;
  528. }
  529. board_ext = board_ext->board_ext;
  530. ++k;
  531. }
  532. if (box_i == -1)
  533. {
  534. MyKdPrint(D_Error, ("Pnp key not found.\n"))
  535. return 15; // err
  536. }
  537. }
  538. else // set option by device by index(which our reg-reading one does.)
  539. {
  540. box_i = getint(option_str, &i); // get the box index [#]
  541. if (i==0)
  542. return 2;
  543. if (find_ext_by_index(box_i, -1) == NULL) // if no device exists)
  544. {
  545. return 3;
  546. }
  547. }
  548. #else
  549. box_i = getint(option_str, &i); // get the box index [#]
  550. if (i==0)
  551. return 2;
  552. if (find_ext_by_index(box_i, -1) == NULL) // if no device exists)
  553. {
  554. return 3;
  555. }
  556. #endif
  557. option_str += i;
  558. if (*option_str++ != ']')
  559. return 4;
  560. if (*option_str++ != '.')
  561. return 5;
  562. if (my_sub_lstricmp("port[", option_str) == 0) // match
  563. {
  564. // its a port option
  565. option_str += 4; // pass up "port"
  566. if (*option_str++ != '[')
  567. return 20;
  568. port_i = getint(option_str, &i); // get the port index [#]
  569. if (i==0)
  570. return 21;
  571. option_str += i;
  572. if (*option_str++ != ']')
  573. return 23;
  574. if (*option_str++ != '.')
  575. return 34;
  576. //-- find the option-string index
  577. i = 0;
  578. while (port_options[i].name != NULL)
  579. {
  580. if (my_sub_lstricmp(port_options[i].name, option_str) == 0) // match
  581. {
  582. option_i = i;
  583. option_id = port_options[i].id;
  584. }
  585. ++i;
  586. }
  587. if (option_i == -1)
  588. return 24; // option not found
  589. option_str += strlen(port_options[option_i].name);
  590. while (*option_str == ' ')
  591. ++option_str;
  592. if (*option_str++ != '=')
  593. return 25;
  594. while (*option_str == ' ')
  595. ++option_str;
  596. stat = SetPortOption(box_i, port_i, option_id, option_str);
  597. if (stat)
  598. return (50+stat); // option not set
  599. return 0; // ok
  600. } // == port[
  601. //-------- its a device level option, find the option-string index
  602. i = 0;
  603. while (device_options[i].name != NULL)
  604. {
  605. if (my_sub_lstricmp(device_options[i].name, option_str) == 0) // match
  606. {
  607. option_i = i;
  608. option_id = device_options[i].id;
  609. }
  610. ++i;
  611. }
  612. if (option_i == -1)
  613. {
  614. MyKdPrint(D_Error, ("Option not found:%s\n", option_str))
  615. return 6; // option not found
  616. }
  617. option_str += strlen(device_options[option_i].name);
  618. while (*option_str == ' ')
  619. ++option_str;
  620. if (*option_str++ != '=')
  621. return 7;
  622. while (*option_str == ' ')
  623. ++option_str;
  624. stat = SetDeviceOption(box_i, option_id, option_str);
  625. if (stat)
  626. return (50+stat); // option not set
  627. return 0; // ok
  628. }
  629. //-- assume a global option string
  630. //-- find the option-string index
  631. i = 0;
  632. while (driver_options[i].name != NULL)
  633. {
  634. if (my_sub_lstricmp(driver_options[i].name, option_str) == 0) // match
  635. {
  636. option_i = i;
  637. option_id = driver_options[i].id;
  638. }
  639. ++i;
  640. }
  641. if (option_i == -1)
  642. return 7; // option not found
  643. option_str += strlen(driver_options[option_i].name);
  644. while (*option_str == ' ')
  645. ++option_str;
  646. if (*option_str++ != '=')
  647. return 7;
  648. while (*option_str == ' ')
  649. ++option_str;
  650. stat = SetMainOption(option_id, option_str);
  651. if (stat)
  652. return (50+stat); // option not set
  653. return 0;
  654. }
  655. /*-----------------------------------------------------------------------
  656. SetMainOption -
  657. |-----------------------------------------------------------------------*/
  658. static int SetMainOption(int index, char *value)
  659. {
  660. int j;
  661. int ret_stat = 2; // default, return an error, unknown option
  662. //MyKdPrint(D_Init, ("SetMainOp[%d]:%s\n", index, value))
  663. switch (index)
  664. {
  665. case OP_VerboseLog:
  666. Driver.VerboseLog = (WORD)getnum(value,&j);
  667. ret_stat = 0; // ok
  668. break;
  669. case OP_NumDevices:
  670. if (NumDevices() == 0)
  671. {
  672. Driver.NumDevices = getnum(value,&j);
  673. if (Driver.NumDevices > MAX_NUM_BOXES)
  674. Driver.NumDevices = MAX_NUM_BOXES;
  675. ret_stat = 0;
  676. }
  677. else
  678. {
  679. // if this gets changed on the fly, this could kill us!!!!!
  680. ret_stat = 1; // not allowed
  681. }
  682. break;
  683. case OP_ScanRate:
  684. Driver.ScanRate = (WORD)getnum(value,&j);
  685. if (Driver.ScanRate == 0) Driver.ScanRate = 10;
  686. if (Driver.ScanRate < 1) Driver.ScanRate = 1;
  687. if (Driver.ScanRate > 50) Driver.ScanRate = 50;
  688. Driver.PollIntervalTime.QuadPart = Driver.ScanRate * -10000;
  689. #ifdef NT50
  690. ExSetTimerResolution(Driver.ScanRate, 1);
  691. //ExSetTimerResolution(-Driver.PollIntervalTime.QuadPart, 1);
  692. #endif
  693. ret_stat = 0; // ok
  694. break;
  695. case OP_ModemCountry :
  696. Driver.MdmCountryCode = (WORD)getnum(value,&j);
  697. MyKdPrint(D_Level, ("ModemCountry=%d\n", Driver.MdmCountryCode))
  698. ret_stat = 1; // probably need to restart to reinit modems
  699. break;
  700. // case OP_ModemSettleTime :
  701. // Driver.MdmSettleTime = getnum(value,&j);
  702. // ret_stat = 1; // probably need to reinit modems
  703. // break;
  704. #ifdef NT50
  705. case OP_NoPnpPorts :
  706. Driver.NoPnpPorts = getnum(value,&j);
  707. ret_stat = 0; // ok
  708. // if boards and ports started
  709. if (Driver.board_ext != NULL)
  710. {
  711. if (Driver.board_ext->port_ext != NULL)
  712. {
  713. ret_stat = 1; // currently need a reset to get this operational
  714. }
  715. }
  716. break;
  717. #endif
  718. // case OP_PreScaler :
  719. // Driver.PreScaler = getnum(value,&j);
  720. // ret_stat = 1; // currently need a reset to get this going
  721. // break;
  722. default:
  723. return 2; // err, option unknown
  724. }
  725. return ret_stat;
  726. }
  727. /*-----------------------------------------------------------------------
  728. SetDeviceOption -
  729. |-----------------------------------------------------------------------*/
  730. static int SetDeviceOption(int device_index, int option_index, char *value)
  731. {
  732. int stat,j, num;
  733. int ret_stat = 2; // default, return an error, unknown option
  734. DEVICE_CONFIG *dev_config;
  735. PSERIAL_DEVICE_EXTENSION board_ext = NULL;
  736. //MyKdPrint(D_Level, ("SetDeviceOp[%d.%d]:%s\n", device_index, option_index, value))
  737. board_ext = find_ext_by_index(device_index, -1);
  738. if (board_ext == NULL) // if no device exists)
  739. {
  740. MyKdPrint(D_Error, ("Err, SetDevOpt, No Dev"))
  741. return 6; // no device found
  742. }
  743. dev_config = board_ext->config;
  744. if (dev_config == NULL) // if no device exists)
  745. {
  746. MyKdPrint(D_Error, ("Err, SetDevOpt, No Config"))
  747. return 6; // no device found
  748. }
  749. switch (option_index)
  750. {
  751. #if 0
  752. case OP_StartComIndex :
  753. num = getnum(value,&j);
  754. break;
  755. #endif
  756. case OP_NumPorts :
  757. num = getnum(value,&j);
  758. if (NumPorts(board_ext) == 0)
  759. {
  760. // assume start up reading in, and other code will adjust
  761. dev_config->NumPorts = num;
  762. ret_stat = 0;
  763. }
  764. else
  765. {
  766. if (num == NumPorts(board_ext))
  767. ret_stat = 0;
  768. else // different number of ports asked for.
  769. {
  770. stat = CreateReconfigPortDevices(board_ext, num);
  771. if (stat == STATUS_SUCCESS)
  772. ret_stat = 0;
  773. else
  774. {
  775. ret_stat = 1; // err, need reboot
  776. MyKdPrint(D_Init, ("NumPorts chg needs reboot\n"))
  777. }
  778. }
  779. }
  780. break;
  781. case OP_IoAddress :
  782. if (dev_config->IoAddress == 0)
  783. {
  784. // assume startup of nt40.
  785. dev_config->IoAddress = getnum(value,&j);
  786. ret_stat = 0;
  787. }
  788. else
  789. {
  790. MyKdPrint(D_Init, ("Io chg needs reboot\n"))
  791. ret_stat = 1; // err, need reboot
  792. }
  793. break;
  794. case OP_ModemDevice :
  795. dev_config->ModemDevice = getnum(value, &j);
  796. ret_stat = 0; // ok
  797. break;
  798. case OP_Name:
  799. ret_stat = 0; // ok
  800. break;
  801. case OP_ModelName:
  802. ret_stat = 0; // ok
  803. break;
  804. case OP_HubDevice:
  805. ret_stat = 0; // ok
  806. break;
  807. #ifdef S_VS
  808. case OP_MacAddr :
  809. ret_stat = 0; // ok, took
  810. stat = atomac(dev_config->MacAddr, value);
  811. if (stat)
  812. {
  813. MyKdPrint(D_Error, ("Error%x device:%d, MAC addr\n",stat, device_index+1))
  814. ret_stat = 1;
  815. }
  816. else
  817. {
  818. if (!mac_match(dev_config->MacAddr, board_ext->hd->dest_addr))
  819. {
  820. MyKdPrint(D_Init, ("MacAddr:%x %x %x %x %x %x\n",
  821. dev_config->MacAddr[0],dev_config->MacAddr[1],dev_config->MacAddr[2],
  822. dev_config->MacAddr[3],dev_config->MacAddr[4],dev_config->MacAddr[5]))
  823. #if DBG
  824. if (board_ext->pm->hd == NULL)
  825. {
  826. MyKdPrint(D_Error, ("Err, null pm or hd\n"))
  827. break;
  828. }
  829. #endif
  830. port_set_new_mac_addr(board_ext->pm, dev_config->MacAddr);
  831. }
  832. }
  833. MyKdPrint(D_Error, ("End Mac Chg\n"))
  834. break;
  835. case OP_BackupServer :
  836. dev_config->BackupServer = getnum(value,&j);
  837. board_ext->pm->backup_server = dev_config->BackupServer;
  838. ret_stat = 0; // ok, took
  839. break;
  840. case OP_BackupTimer :
  841. dev_config->BackupTimer = getnum(value,&j);
  842. board_ext->pm->backup_timer = dev_config->BackupTimer;
  843. ret_stat = 0; // ok, took
  844. break;
  845. #endif
  846. default:
  847. return 2;
  848. }
  849. return ret_stat;
  850. }
  851. /*-----------------------------------------------------------------------
  852. SetPortOption -
  853. |-----------------------------------------------------------------------*/
  854. static int SetPortOption(int device_index,
  855. int port_index,
  856. int option_index,
  857. char *value)
  858. {
  859. int j;
  860. int i = device_index;
  861. int ret_stat = 2; // default, return an error, unknown option
  862. PSERIAL_DEVICE_EXTENSION board_ext = NULL;
  863. PSERIAL_DEVICE_EXTENSION ext = NULL;
  864. PORT_CONFIG *port_config;
  865. MyKdPrint(D_Level, ("SetPortOp[%d.%d,%x]:%s\n",
  866. device_index, port_index, option_index, value))
  867. board_ext = find_ext_by_index(device_index, -1);
  868. if (board_ext == NULL)
  869. {
  870. MyKdPrint(D_Error, ("Can't find board\n"))
  871. return 6;
  872. }
  873. ext = find_ext_by_index(device_index, port_index);
  874. if (ext == NULL)
  875. {
  876. // so point it at the boards port config(which is what the ports
  877. // ptr points to anyway.
  878. port_config = &board_ext->config->port[port_index];
  879. }
  880. else
  881. port_config = ext->port_config;
  882. if (port_config == NULL)
  883. {
  884. MyKdPrint(D_Error, ("Err 8U\n"))
  885. return 7;
  886. }
  887. switch (option_index)
  888. {
  889. case OP_WaitOnTx :
  890. port_config->WaitOnTx = getnum(value,&j);
  891. ret_stat = 0; // ok, took
  892. break;
  893. case OP_RS485Override :
  894. // will take next port open
  895. port_config->RS485Override = getnum(value,&j);
  896. ret_stat = 0; // ok, took
  897. break;
  898. case OP_RS485Low :
  899. port_config->RS485Low = getnum(value,&j);
  900. // will take next port open
  901. ret_stat = 0; // ok, took
  902. break;
  903. case OP_TxCloseTime :
  904. port_config->TxCloseTime = getnum(value,&j);
  905. ret_stat = 0; // ok, took
  906. break;
  907. case OP_LockBaud :
  908. port_config->LockBaud = getnum(value,&j);
  909. if (ext != NULL)
  910. ProgramBaudRate(ext, ext->BaudRate);
  911. ret_stat = 0; // ok, took
  912. break;
  913. case OP_Map2StopsTo1 :
  914. port_config->Map2StopsTo1 = getnum(value,&j);
  915. ret_stat = 0; // ok, took
  916. break;
  917. case OP_MapCdToDsr :
  918. port_config->MapCdToDsr = getnum(value,&j);
  919. ret_stat = 0; // ok, took
  920. break;
  921. case OP_RingEmulate :
  922. port_config->RingEmulate = getnum(value,&j);
  923. ret_stat = 0; // ok, took
  924. break;
  925. case OP_PortName :
  926. if (ext == NULL) // must be initial load prior to port ext creation
  927. {
  928. strcpy(port_config->Name, value);
  929. ret_stat = 0; // ok, took
  930. break;
  931. }
  932. // not init time, runtime
  933. ret_stat = 1; // err, need reboot
  934. if (ext == NULL)
  935. {
  936. MyKdPrint(D_Error,("Err7K\n"))
  937. break;
  938. }
  939. #define ALLOW_RENAMING_ON_FLY
  940. #ifdef ALLOW_RENAMING_ON_FLY
  941. {
  942. PSERIAL_DEVICE_EXTENSION other_ext = NULL;
  943. char othername[20];
  944. MyKdPrint(D_Init,("NewName:%s OldName:%s\n",
  945. value, ext->SymbolicLinkName))
  946. // see if some other port has the name we want
  947. other_ext = find_ext_by_name(value, NULL);
  948. if (other_ext == ext) //it's the same
  949. {
  950. ret_stat = 0; // ok, took
  951. break;
  952. }
  953. if (other_ext)
  954. {
  955. MyKdPrint(D_Init,("Change other name\n"))
  956. // it does, so rename it to ours
  957. strcpy(othername, other_ext->SymbolicLinkName);
  958. SerialCleanupExternalNaming(other_ext);
  959. strcpy(other_ext->port_config->Name, ext->port_config->Name);
  960. strcpy(other_ext->SymbolicLinkName, ext->port_config->Name); // "COM#"
  961. }
  962. SerialCleanupExternalNaming(ext);
  963. if (other_ext)
  964. {
  965. SerialSetupExternalNaming(other_ext); // Configure port
  966. }
  967. // copy over the name in the configuration for dos-name
  968. strcpy(port_config->Name, value);
  969. strcpy(ext->SymbolicLinkName, value); // "COM#"
  970. MyKdPrint(D_Init,("NewName:%s\n", ext->SymbolicLinkName))
  971. SerialSetupExternalNaming(ext); // Configure port
  972. MyKdPrint(D_Init,("Done renaming\n"))
  973. ret_stat = 0; // ok
  974. }
  975. break;
  976. #endif
  977. default:
  978. return 2;
  979. }
  980. return ret_stat;
  981. }
  982. /*-----------------------------------------------------------------------
  983. SaveRegPath - Make a copy of the DriverEntry() RegistryPath unicode
  984. string into the registry area we reside.
  985. Create and save into Driver.RegPath.
  986. |-----------------------------------------------------------------------*/
  987. int SaveRegPath(PUNICODE_STRING RegistryPath)
  988. {
  989. int len;
  990. //MyKdPrint(D_Init, ("SaveRegPath A:%s\n", UToC1(RegistryPath)))
  991. // if RegPath buffer not allocated, then take care of that
  992. if (Driver.RegPath.Buffer == NULL)
  993. {
  994. // allocate buffer space for original regpath
  995. len = RegistryPath->Length + 2;
  996. Driver.RegPath.Buffer = ExAllocatePool(PagedPool, len);
  997. if ( Driver.RegPath.Buffer == NULL ) {
  998. Eprintf("SaveRegPath no memory");
  999. return -1;
  1000. }
  1001. Driver.RegPath.MaximumLength = (WORD)len;
  1002. Driver.RegPath.Length = 0;
  1003. }
  1004. RtlZeroMemory(Driver.RegPath.Buffer, Driver.RegPath.MaximumLength);
  1005. //--- copy registry path to our local copy
  1006. RtlMoveMemory(Driver.RegPath.Buffer,
  1007. RegistryPath->Buffer,
  1008. RegistryPath->Length);
  1009. Driver.RegPath.Length = RegistryPath->Length; // set unicode length
  1010. return 0;
  1011. }
  1012. /*-----------------------------------------------------------------------
  1013. MakeRegPath - Form a unicode Registry string to an area where we get
  1014. info from the registry. Concat's str onto original RegistryPath
  1015. and forms a unicode string at Driver.OptionRegPath.
  1016. |-----------------------------------------------------------------------*/
  1017. int MakeRegPath(CHAR *optionstr)
  1018. {
  1019. //UCHAR *upath; // a byte ptr for byte indexing path stuff
  1020. //WCHAR *pwstr;
  1021. int len;
  1022. USTR_80 utmpstr;
  1023. if (Driver.RegPath.Buffer == NULL)
  1024. return 1;
  1025. //MyKdPrint(D_Init, ("MakeRegPath A:%s\n", UToC1(&Driver.RegPath)))
  1026. // if OptionRegPath buffer not allocated, then take care of that
  1027. if (Driver.OptionRegPath.Buffer == NULL)
  1028. {
  1029. // allocate buffer space for original regpath + room to tack on option
  1030. // strings.
  1031. len = Driver.RegPath.Length + (128*(sizeof(WCHAR)));
  1032. Driver.OptionRegPath.Buffer = ExAllocatePool(PagedPool, len);
  1033. if ( Driver.OptionRegPath.Buffer == NULL ) {
  1034. Eprintf("MakeRegPath no memory");
  1035. return -1;
  1036. }
  1037. Driver.OptionRegPath.MaximumLength = (WORD)len;
  1038. Driver.OptionRegPath.Length = 0;
  1039. }
  1040. RtlZeroMemory(Driver.OptionRegPath.Buffer,
  1041. Driver.OptionRegPath.MaximumLength);
  1042. // copy over the orignal RegPath
  1043. RtlMoveMemory(Driver.OptionRegPath.Buffer,
  1044. Driver.RegPath.Buffer,
  1045. Driver.RegPath.Length);
  1046. Driver.OptionRegPath.Length = Driver.RegPath.Length;
  1047. //---- now tack on what we want to concatinate(example: L"\\Parameters")
  1048. if (optionstr != NULL)
  1049. {
  1050. // convert to unicode
  1051. CToUStr((PUNICODE_STRING) &utmpstr, optionstr, sizeof(utmpstr));
  1052. // Copy the key string over
  1053. RtlCopyMemory( ((UCHAR *) Driver.OptionRegPath.Buffer) +
  1054. Driver.OptionRegPath.Length,
  1055. utmpstr.ustr.Buffer,
  1056. utmpstr.ustr.Length);
  1057. Driver.OptionRegPath.Length += utmpstr.ustr.Length;
  1058. }
  1059. //MyKdPrint(D_Init, ("MakeRegPath B:%s\n", UToC1(&Driver.OptionRegPath)))
  1060. return 0; // ok
  1061. }
  1062. #if 0
  1063. /*-----------------------------------------------------------------
  1064. reg_get_str - get a str value out of the registry.
  1065. |------------------------------------------------------------------*/
  1066. int reg_get_str(IN WCHAR *RegPath,
  1067. int reg_location,
  1068. const char *str_id,
  1069. char *dest,
  1070. int max_dest_len)
  1071. {
  1072. RTL_QUERY_REGISTRY_TABLE paramTable[2];
  1073. PUNICODE_STRING ustr;
  1074. USTR_80 ustr_id;
  1075. USTR_80 ustr_val;
  1076. char *ret_str;
  1077. CToUStr((PUNICODE_STRING)&ustr_id, str_id, sizeof(ustr_id));
  1078. RtlZeroMemory(&paramTable[0],sizeof(paramTable));
  1079. //ustr = CToU2(""); // allocated static space for unicode
  1080. ustr = CToUStr((PUNICODE_STRING)&ustr_val, "", sizeof(ustr_val));
  1081. ustr = (PUNICODE_STRING) &ustr_val; // allocated static space for unicode
  1082. paramTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
  1083. paramTable[0].Name = ustr_id.ustr.Buffer;
  1084. paramTable[0].EntryContext = ustr;
  1085. paramTable[0].DefaultType = 0;
  1086. paramTable[0].DefaultData = 0;
  1087. paramTable[0].DefaultLength = 0;
  1088. if (!NT_SUCCESS(RtlQueryRegistryValues(
  1089. // RTL_REGISTRY_ABSOLUTE | RTL_REGISTRY_OPTIONAL,
  1090. reg_location | RTL_REGISTRY_OPTIONAL,
  1091. RegPath,
  1092. &paramTable[0],
  1093. NULL,
  1094. NULL)))
  1095. {
  1096. dest[0] = 0;
  1097. return 1;
  1098. }
  1099. ret_str = (char *) &ustr_id; // reuse this stack space for u to c conv.
  1100. UToCStr(ret_str, ustr, 80);
  1101. if ((int)strlen(ret_str) > max_dest_len)
  1102. ret_str[max_dest_len] = 0;
  1103. strcpy(dest, ret_str);
  1104. return 0;
  1105. }
  1106. /*-----------------------------------------------------------------
  1107. reg_get_dword - get a dword value out of the registry.
  1108. |------------------------------------------------------------------*/
  1109. int reg_get_dword(IN WCHAR *RegPath,
  1110. const char *str_id,
  1111. ULONG *dest)
  1112. {
  1113. ULONG DataValue;
  1114. RTL_QUERY_REGISTRY_TABLE paramTable[2];
  1115. ULONG notThereDefault = 12345678;
  1116. USTR_80 ustr_id;
  1117. CToUStr((PUNICODE_STRING)&ustr_id, str_id, sizeof(ustr_id));
  1118. RtlZeroMemory(&paramTable[0],sizeof(paramTable));
  1119. paramTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
  1120. paramTable[0].Name = ustr_id.ustr.Buffer;
  1121. paramTable[0].EntryContext = &DataValue;
  1122. paramTable[0].DefaultType = REG_DWORD;
  1123. paramTable[0].DefaultData = &notThereDefault;
  1124. paramTable[0].DefaultLength = sizeof(ULONG);
  1125. if (!NT_SUCCESS(RtlQueryRegistryValues(
  1126. RTL_REGISTRY_ABSOLUTE | RTL_REGISTRY_OPTIONAL,
  1127. RegPath,
  1128. &paramTable[0],
  1129. NULL,
  1130. NULL)))
  1131. {
  1132. return 1;
  1133. }
  1134. if (DataValue == 12345678)
  1135. return 2;
  1136. *dest = DataValue;
  1137. return 0;
  1138. }
  1139. /*-----------------------------------------------------------------
  1140. reg_set_str - get a dword value out of the registry.
  1141. |------------------------------------------------------------------*/
  1142. static int reg_set_str(IN WCHAR *RegPath,
  1143. IN const char *str_id,
  1144. IN const char *str_val)
  1145. {
  1146. int status;
  1147. USTR_80 ustr_id;
  1148. USTR_80 ustr_val;
  1149. MyKdPrint(D_Init, ("Reg_set, writing %s=%s\n", str_id, str_val))
  1150. CToUStr((PUNICODE_STRING)&ustr_id, str_id, sizeof(ustr_id));
  1151. CToUStr((PUNICODE_STRING)&ustr_val, str_val, sizeof(ustr_val));
  1152. status = RtlWriteRegistryValue(
  1153. RTL_REGISTRY_ABSOLUTE,
  1154. RegPath,
  1155. ustr_id.ustr.Buffer,
  1156. REG_SZ,
  1157. ustr_val.ustr.Buffer,
  1158. ustr_val.ustr.Length);
  1159. if (status != STATUS_SUCCESS)
  1160. {
  1161. MyKdPrint(D_Error, ("Error, writing %s=%s\n", str_id, str_val))
  1162. MyKdPrint(D_Error, (" Path:%s\n", RegPath))
  1163. return 1;
  1164. }
  1165. return 0;
  1166. }
  1167. /*-----------------------------------------------------------------
  1168. reg_set_dword - get a dword value out of the registry.
  1169. |------------------------------------------------------------------*/
  1170. int reg_set_dword(IN WCHAR *RegPath,
  1171. const char *str_id,
  1172. ULONG val)
  1173. {
  1174. int status;
  1175. USTR_80 ustr_id;
  1176. CToUStr((PUNICODE_STRING)&ustr_id, str_id, sizeof(ustr_id));
  1177. status = RtlWriteRegistryValue(
  1178. RTL_REGISTRY_ABSOLUTE,
  1179. RegPath,
  1180. ustr_id.ustr.Buffer,
  1181. REG_DWORD,
  1182. &val,
  1183. sizeof(ULONG));
  1184. if (status != STATUS_SUCCESS)
  1185. {
  1186. return 1;
  1187. }
  1188. return 0;
  1189. }
  1190. #endif
  1191. /*-----------------------------------------------------------------
  1192. atomac - convert from ascii to mac-addr.
  1193. |------------------------------------------------------------------*/
  1194. static int atomac(BYTE *mac, char *str)
  1195. {
  1196. int i,j;
  1197. WORD h;
  1198. for (i=0; i<6; i++)
  1199. {
  1200. j = 0;
  1201. h = 0xffff;
  1202. h = (WORD)gethint(str, &j);
  1203. str += j;
  1204. if ((h > 0xff) || (j == 0))
  1205. return 1;
  1206. mac[i] = (BYTE) h;
  1207. }
  1208. return 0;
  1209. }