whiteheat.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*
  2. * USB ConnectTech WhiteHEAT driver
  3. *
  4. * Copyright (C) 2002
  5. * Connect Tech Inc.
  6. *
  7. * Copyright (C) 1999 - 2001
  8. * Greg Kroah-Hartman (greg@kroah.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * See Documentation/usb/usb-serial.txt for more information on using this
  16. * driver
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_driver.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/module.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/mutex.h>
  28. #include <linux/uaccess.h>
  29. #include <asm/termbits.h>
  30. #include <linux/usb.h>
  31. #include <linux/serial_reg.h>
  32. #include <linux/serial.h>
  33. #include <linux/usb/serial.h>
  34. #include <linux/firmware.h>
  35. #include <linux/ihex.h>
  36. #include "whiteheat.h" /* WhiteHEAT specific commands */
  37. static bool debug;
  38. #ifndef CMSPAR
  39. #define CMSPAR 0
  40. #endif
  41. /*
  42. * Version Information
  43. */
  44. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
  45. #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
  46. #define CONNECT_TECH_VENDOR_ID 0x0710
  47. #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
  48. #define CONNECT_TECH_WHITE_HEAT_ID 0x8001
  49. /*
  50. ID tables for whiteheat are unusual, because we want to different
  51. things for different versions of the device. Eventually, this
  52. will be doable from a single table. But, for now, we define two
  53. separate ID tables, and then a third table that combines them
  54. just for the purpose of exporting the autoloading information.
  55. */
  56. static const struct usb_device_id id_table_std[] = {
  57. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
  58. { } /* Terminating entry */
  59. };
  60. static const struct usb_device_id id_table_prerenumeration[] = {
  61. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
  62. { } /* Terminating entry */
  63. };
  64. static const struct usb_device_id id_table_combined[] = {
  65. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
  66. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
  67. { } /* Terminating entry */
  68. };
  69. MODULE_DEVICE_TABLE(usb, id_table_combined);
  70. static struct usb_driver whiteheat_driver = {
  71. .name = "whiteheat",
  72. .probe = usb_serial_probe,
  73. .disconnect = usb_serial_disconnect,
  74. .id_table = id_table_combined,
  75. };
  76. /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
  77. static int whiteheat_firmware_download(struct usb_serial *serial,
  78. const struct usb_device_id *id);
  79. static int whiteheat_firmware_attach(struct usb_serial *serial);
  80. /* function prototypes for the Connect Tech WhiteHEAT serial converter */
  81. static int whiteheat_attach(struct usb_serial *serial);
  82. static void whiteheat_release(struct usb_serial *serial);
  83. static int whiteheat_open(struct tty_struct *tty,
  84. struct usb_serial_port *port);
  85. static void whiteheat_close(struct usb_serial_port *port);
  86. static int whiteheat_ioctl(struct tty_struct *tty,
  87. unsigned int cmd, unsigned long arg);
  88. static void whiteheat_set_termios(struct tty_struct *tty,
  89. struct usb_serial_port *port, struct ktermios *old);
  90. static int whiteheat_tiocmget(struct tty_struct *tty);
  91. static int whiteheat_tiocmset(struct tty_struct *tty,
  92. unsigned int set, unsigned int clear);
  93. static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
  94. static struct usb_serial_driver whiteheat_fake_device = {
  95. .driver = {
  96. .owner = THIS_MODULE,
  97. .name = "whiteheatnofirm",
  98. },
  99. .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
  100. .id_table = id_table_prerenumeration,
  101. .num_ports = 1,
  102. .probe = whiteheat_firmware_download,
  103. .attach = whiteheat_firmware_attach,
  104. };
  105. static struct usb_serial_driver whiteheat_device = {
  106. .driver = {
  107. .owner = THIS_MODULE,
  108. .name = "whiteheat",
  109. },
  110. .description = "Connect Tech - WhiteHEAT",
  111. .id_table = id_table_std,
  112. .num_ports = 4,
  113. .attach = whiteheat_attach,
  114. .release = whiteheat_release,
  115. .open = whiteheat_open,
  116. .close = whiteheat_close,
  117. .ioctl = whiteheat_ioctl,
  118. .set_termios = whiteheat_set_termios,
  119. .break_ctl = whiteheat_break_ctl,
  120. .tiocmget = whiteheat_tiocmget,
  121. .tiocmset = whiteheat_tiocmset,
  122. .throttle = usb_serial_generic_throttle,
  123. .unthrottle = usb_serial_generic_unthrottle,
  124. };
  125. static struct usb_serial_driver * const serial_drivers[] = {
  126. &whiteheat_fake_device, &whiteheat_device, NULL
  127. };
  128. struct whiteheat_command_private {
  129. struct mutex mutex;
  130. __u8 port_running;
  131. __u8 command_finished;
  132. wait_queue_head_t wait_command; /* for handling sleeping whilst
  133. waiting for a command to
  134. finish */
  135. __u8 result_buffer[64];
  136. };
  137. struct whiteheat_private {
  138. __u8 mcr; /* FIXME: no locking on mcr */
  139. };
  140. /* local function prototypes */
  141. static int start_command_port(struct usb_serial *serial);
  142. static void stop_command_port(struct usb_serial *serial);
  143. static void command_port_write_callback(struct urb *urb);
  144. static void command_port_read_callback(struct urb *urb);
  145. static int firm_send_command(struct usb_serial_port *port, __u8 command,
  146. __u8 *data, __u8 datasize);
  147. static int firm_open(struct usb_serial_port *port);
  148. static int firm_close(struct usb_serial_port *port);
  149. static void firm_setup_port(struct tty_struct *tty);
  150. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
  151. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
  152. static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
  153. static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
  154. static int firm_get_dtr_rts(struct usb_serial_port *port);
  155. static int firm_report_tx_done(struct usb_serial_port *port);
  156. #define COMMAND_PORT 4
  157. #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
  158. #define COMMAND_TIMEOUT_MS 2000
  159. #define CLOSING_DELAY (30 * HZ)
  160. /*****************************************************************************
  161. * Connect Tech's White Heat prerenumeration driver functions
  162. *****************************************************************************/
  163. /* steps to download the firmware to the WhiteHEAT device:
  164. - hold the reset (by writing to the reset bit of the CPUCS register)
  165. - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
  166. - release the reset (by writing to the CPUCS register)
  167. - download the WH.HEX file for all addresses greater than 0x1b3f using
  168. VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
  169. - hold the reset
  170. - download the WH.HEX file for all addresses less than 0x1b40 using
  171. VENDOR_REQUEST_ANCHOR_LOAD
  172. - release the reset
  173. - device renumerated itself and comes up as new device id with all
  174. firmware download completed.
  175. */
  176. static int whiteheat_firmware_download(struct usb_serial *serial,
  177. const struct usb_device_id *id)
  178. {
  179. int response, ret = -ENOENT;
  180. const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
  181. const struct ihex_binrec *record;
  182. dbg("%s", __func__);
  183. if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
  184. &serial->dev->dev)) {
  185. dev_err(&serial->dev->dev,
  186. "%s - request \"whiteheat.fw\" failed\n", __func__);
  187. goto out;
  188. }
  189. if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
  190. &serial->dev->dev)) {
  191. dev_err(&serial->dev->dev,
  192. "%s - request \"whiteheat_loader.fw\" failed\n",
  193. __func__);
  194. goto out;
  195. }
  196. ret = 0;
  197. response = ezusb_set_reset (serial, 1);
  198. record = (const struct ihex_binrec *)loader_fw->data;
  199. while (record) {
  200. response = ezusb_writememory (serial, be32_to_cpu(record->addr),
  201. (unsigned char *)record->data,
  202. be16_to_cpu(record->len), 0xa0);
  203. if (response < 0) {
  204. dev_err(&serial->dev->dev, "%s - ezusb_writememory "
  205. "failed for loader (%d %04X %p %d)\n",
  206. __func__, response, be32_to_cpu(record->addr),
  207. record->data, be16_to_cpu(record->len));
  208. break;
  209. }
  210. record = ihex_next_binrec(record);
  211. }
  212. response = ezusb_set_reset(serial, 0);
  213. record = (const struct ihex_binrec *)firmware_fw->data;
  214. while (record && be32_to_cpu(record->addr) < 0x1b40)
  215. record = ihex_next_binrec(record);
  216. while (record) {
  217. response = ezusb_writememory (serial, be32_to_cpu(record->addr),
  218. (unsigned char *)record->data,
  219. be16_to_cpu(record->len), 0xa3);
  220. if (response < 0) {
  221. dev_err(&serial->dev->dev, "%s - ezusb_writememory "
  222. "failed for first firmware step "
  223. "(%d %04X %p %d)\n", __func__, response,
  224. be32_to_cpu(record->addr), record->data,
  225. be16_to_cpu(record->len));
  226. break;
  227. }
  228. ++record;
  229. }
  230. response = ezusb_set_reset(serial, 1);
  231. record = (const struct ihex_binrec *)firmware_fw->data;
  232. while (record && be32_to_cpu(record->addr) < 0x1b40) {
  233. response = ezusb_writememory (serial, be32_to_cpu(record->addr),
  234. (unsigned char *)record->data,
  235. be16_to_cpu(record->len), 0xa0);
  236. if (response < 0) {
  237. dev_err(&serial->dev->dev, "%s - ezusb_writememory "
  238. "failed for second firmware step "
  239. "(%d %04X %p %d)\n", __func__, response,
  240. be32_to_cpu(record->addr), record->data,
  241. be16_to_cpu(record->len));
  242. break;
  243. }
  244. ++record;
  245. }
  246. ret = 0;
  247. response = ezusb_set_reset (serial, 0);
  248. out:
  249. release_firmware(loader_fw);
  250. release_firmware(firmware_fw);
  251. return ret;
  252. }
  253. static int whiteheat_firmware_attach(struct usb_serial *serial)
  254. {
  255. /* We want this device to fail to have a driver assigned to it */
  256. return 1;
  257. }
  258. /*****************************************************************************
  259. * Connect Tech's White Heat serial driver functions
  260. *****************************************************************************/
  261. static int whiteheat_attach(struct usb_serial *serial)
  262. {
  263. struct usb_serial_port *command_port;
  264. struct whiteheat_command_private *command_info;
  265. struct usb_serial_port *port;
  266. struct whiteheat_private *info;
  267. struct whiteheat_hw_info *hw_info;
  268. int pipe;
  269. int ret;
  270. int alen;
  271. __u8 *command;
  272. __u8 *result;
  273. int i;
  274. command_port = serial->port[COMMAND_PORT];
  275. pipe = usb_sndbulkpipe(serial->dev,
  276. command_port->bulk_out_endpointAddress);
  277. command = kmalloc(2, GFP_KERNEL);
  278. if (!command)
  279. goto no_command_buffer;
  280. command[0] = WHITEHEAT_GET_HW_INFO;
  281. command[1] = 0;
  282. result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
  283. if (!result)
  284. goto no_result_buffer;
  285. /*
  286. * When the module is reloaded the firmware is still there and
  287. * the endpoints are still in the usb core unchanged. This is the
  288. * unlinking bug in disguise. Same for the call below.
  289. */
  290. usb_clear_halt(serial->dev, pipe);
  291. ret = usb_bulk_msg(serial->dev, pipe, command, 2,
  292. &alen, COMMAND_TIMEOUT_MS);
  293. if (ret) {
  294. dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
  295. serial->type->description, ret);
  296. goto no_firmware;
  297. } else if (alen != 2) {
  298. dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
  299. serial->type->description, alen);
  300. goto no_firmware;
  301. }
  302. pipe = usb_rcvbulkpipe(serial->dev,
  303. command_port->bulk_in_endpointAddress);
  304. /* See the comment on the usb_clear_halt() above */
  305. usb_clear_halt(serial->dev, pipe);
  306. ret = usb_bulk_msg(serial->dev, pipe, result,
  307. sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
  308. if (ret) {
  309. dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
  310. serial->type->description, ret);
  311. goto no_firmware;
  312. } else if (alen != sizeof(*hw_info) + 1) {
  313. dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
  314. serial->type->description, alen);
  315. goto no_firmware;
  316. } else if (result[0] != command[0]) {
  317. dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
  318. serial->type->description, result[0]);
  319. goto no_firmware;
  320. }
  321. hw_info = (struct whiteheat_hw_info *)&result[1];
  322. dev_info(&serial->dev->dev, "%s: Firmware v%d.%02d\n",
  323. serial->type->description,
  324. hw_info->sw_major_rev, hw_info->sw_minor_rev);
  325. for (i = 0; i < serial->num_ports; i++) {
  326. port = serial->port[i];
  327. info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
  328. if (info == NULL) {
  329. dev_err(&port->dev,
  330. "%s: Out of memory for port structures\n",
  331. serial->type->description);
  332. goto no_private;
  333. }
  334. info->mcr = 0;
  335. usb_set_serial_port_data(port, info);
  336. }
  337. command_info = kmalloc(sizeof(struct whiteheat_command_private),
  338. GFP_KERNEL);
  339. if (command_info == NULL) {
  340. dev_err(&serial->dev->dev,
  341. "%s: Out of memory for port structures\n",
  342. serial->type->description);
  343. goto no_command_private;
  344. }
  345. mutex_init(&command_info->mutex);
  346. command_info->port_running = 0;
  347. init_waitqueue_head(&command_info->wait_command);
  348. usb_set_serial_port_data(command_port, command_info);
  349. command_port->write_urb->complete = command_port_write_callback;
  350. command_port->read_urb->complete = command_port_read_callback;
  351. kfree(result);
  352. kfree(command);
  353. return 0;
  354. no_firmware:
  355. /* Firmware likely not running */
  356. dev_err(&serial->dev->dev,
  357. "%s: Unable to retrieve firmware version, try replugging\n",
  358. serial->type->description);
  359. dev_err(&serial->dev->dev,
  360. "%s: If the firmware is not running (status led not blinking)\n",
  361. serial->type->description);
  362. dev_err(&serial->dev->dev,
  363. "%s: please contact support@connecttech.com\n",
  364. serial->type->description);
  365. kfree(result);
  366. return -ENODEV;
  367. no_command_private:
  368. for (i = serial->num_ports - 1; i >= 0; i--) {
  369. port = serial->port[i];
  370. info = usb_get_serial_port_data(port);
  371. kfree(info);
  372. no_private:
  373. ;
  374. }
  375. kfree(result);
  376. no_result_buffer:
  377. kfree(command);
  378. no_command_buffer:
  379. return -ENOMEM;
  380. }
  381. static void whiteheat_release(struct usb_serial *serial)
  382. {
  383. struct usb_serial_port *command_port;
  384. struct whiteheat_private *info;
  385. int i;
  386. dbg("%s", __func__);
  387. /* free up our private data for our command port */
  388. command_port = serial->port[COMMAND_PORT];
  389. kfree(usb_get_serial_port_data(command_port));
  390. for (i = 0; i < serial->num_ports; i++) {
  391. info = usb_get_serial_port_data(serial->port[i]);
  392. kfree(info);
  393. }
  394. }
  395. static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
  396. {
  397. int retval;
  398. dbg("%s - port %d", __func__, port->number);
  399. retval = start_command_port(port->serial);
  400. if (retval)
  401. goto exit;
  402. /* send an open port command */
  403. retval = firm_open(port);
  404. if (retval) {
  405. stop_command_port(port->serial);
  406. goto exit;
  407. }
  408. retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
  409. if (retval) {
  410. firm_close(port);
  411. stop_command_port(port->serial);
  412. goto exit;
  413. }
  414. if (tty)
  415. firm_setup_port(tty);
  416. /* Work around HCD bugs */
  417. usb_clear_halt(port->serial->dev, port->read_urb->pipe);
  418. usb_clear_halt(port->serial->dev, port->write_urb->pipe);
  419. retval = usb_serial_generic_open(tty, port);
  420. if (retval) {
  421. firm_close(port);
  422. stop_command_port(port->serial);
  423. goto exit;
  424. }
  425. exit:
  426. dbg("%s - exit, retval = %d", __func__, retval);
  427. return retval;
  428. }
  429. static void whiteheat_close(struct usb_serial_port *port)
  430. {
  431. dbg("%s - port %d", __func__, port->number);
  432. firm_report_tx_done(port);
  433. firm_close(port);
  434. usb_serial_generic_close(port);
  435. stop_command_port(port->serial);
  436. }
  437. static int whiteheat_tiocmget(struct tty_struct *tty)
  438. {
  439. struct usb_serial_port *port = tty->driver_data;
  440. struct whiteheat_private *info = usb_get_serial_port_data(port);
  441. unsigned int modem_signals = 0;
  442. dbg("%s - port %d", __func__, port->number);
  443. firm_get_dtr_rts(port);
  444. if (info->mcr & UART_MCR_DTR)
  445. modem_signals |= TIOCM_DTR;
  446. if (info->mcr & UART_MCR_RTS)
  447. modem_signals |= TIOCM_RTS;
  448. return modem_signals;
  449. }
  450. static int whiteheat_tiocmset(struct tty_struct *tty,
  451. unsigned int set, unsigned int clear)
  452. {
  453. struct usb_serial_port *port = tty->driver_data;
  454. struct whiteheat_private *info = usb_get_serial_port_data(port);
  455. dbg("%s - port %d", __func__, port->number);
  456. if (set & TIOCM_RTS)
  457. info->mcr |= UART_MCR_RTS;
  458. if (set & TIOCM_DTR)
  459. info->mcr |= UART_MCR_DTR;
  460. if (clear & TIOCM_RTS)
  461. info->mcr &= ~UART_MCR_RTS;
  462. if (clear & TIOCM_DTR)
  463. info->mcr &= ~UART_MCR_DTR;
  464. firm_set_dtr(port, info->mcr & UART_MCR_DTR);
  465. firm_set_rts(port, info->mcr & UART_MCR_RTS);
  466. return 0;
  467. }
  468. static int whiteheat_ioctl(struct tty_struct *tty,
  469. unsigned int cmd, unsigned long arg)
  470. {
  471. struct usb_serial_port *port = tty->driver_data;
  472. struct serial_struct serstruct;
  473. void __user *user_arg = (void __user *)arg;
  474. dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
  475. switch (cmd) {
  476. case TIOCGSERIAL:
  477. memset(&serstruct, 0, sizeof(serstruct));
  478. serstruct.type = PORT_16654;
  479. serstruct.line = port->serial->minor;
  480. serstruct.port = port->number;
  481. serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  482. serstruct.xmit_fifo_size = kfifo_size(&port->write_fifo);
  483. serstruct.custom_divisor = 0;
  484. serstruct.baud_base = 460800;
  485. serstruct.close_delay = CLOSING_DELAY;
  486. serstruct.closing_wait = CLOSING_DELAY;
  487. if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
  488. return -EFAULT;
  489. break;
  490. default:
  491. break;
  492. }
  493. return -ENOIOCTLCMD;
  494. }
  495. static void whiteheat_set_termios(struct tty_struct *tty,
  496. struct usb_serial_port *port, struct ktermios *old_termios)
  497. {
  498. firm_setup_port(tty);
  499. }
  500. static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
  501. {
  502. struct usb_serial_port *port = tty->driver_data;
  503. firm_set_break(port, break_state);
  504. }
  505. /*****************************************************************************
  506. * Connect Tech's White Heat callback routines
  507. *****************************************************************************/
  508. static void command_port_write_callback(struct urb *urb)
  509. {
  510. int status = urb->status;
  511. dbg("%s", __func__);
  512. if (status) {
  513. dbg("nonzero urb status: %d", status);
  514. return;
  515. }
  516. }
  517. static void command_port_read_callback(struct urb *urb)
  518. {
  519. struct usb_serial_port *command_port = urb->context;
  520. struct whiteheat_command_private *command_info;
  521. int status = urb->status;
  522. unsigned char *data = urb->transfer_buffer;
  523. int result;
  524. dbg("%s", __func__);
  525. command_info = usb_get_serial_port_data(command_port);
  526. if (!command_info) {
  527. dbg("%s - command_info is NULL, exiting.", __func__);
  528. return;
  529. }
  530. if (status) {
  531. dbg("%s - nonzero urb status: %d", __func__, status);
  532. if (status != -ENOENT)
  533. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  534. wake_up(&command_info->wait_command);
  535. return;
  536. }
  537. usb_serial_debug_data(debug, &command_port->dev,
  538. __func__, urb->actual_length, data);
  539. if (data[0] == WHITEHEAT_CMD_COMPLETE) {
  540. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  541. wake_up(&command_info->wait_command);
  542. } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
  543. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  544. wake_up(&command_info->wait_command);
  545. } else if (data[0] == WHITEHEAT_EVENT) {
  546. /* These are unsolicited reports from the firmware, hence no
  547. waiting command to wakeup */
  548. dbg("%s - event received", __func__);
  549. } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
  550. memcpy(command_info->result_buffer, &data[1],
  551. urb->actual_length - 1);
  552. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  553. wake_up(&command_info->wait_command);
  554. } else
  555. dbg("%s - bad reply from firmware", __func__);
  556. /* Continue trying to always read */
  557. result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
  558. if (result)
  559. dbg("%s - failed resubmitting read urb, error %d",
  560. __func__, result);
  561. }
  562. /*****************************************************************************
  563. * Connect Tech's White Heat firmware interface
  564. *****************************************************************************/
  565. static int firm_send_command(struct usb_serial_port *port, __u8 command,
  566. __u8 *data, __u8 datasize)
  567. {
  568. struct usb_serial_port *command_port;
  569. struct whiteheat_command_private *command_info;
  570. struct whiteheat_private *info;
  571. __u8 *transfer_buffer;
  572. int retval = 0;
  573. int t;
  574. dbg("%s - command %d", __func__, command);
  575. command_port = port->serial->port[COMMAND_PORT];
  576. command_info = usb_get_serial_port_data(command_port);
  577. mutex_lock(&command_info->mutex);
  578. command_info->command_finished = false;
  579. transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
  580. transfer_buffer[0] = command;
  581. memcpy(&transfer_buffer[1], data, datasize);
  582. command_port->write_urb->transfer_buffer_length = datasize + 1;
  583. retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
  584. if (retval) {
  585. dbg("%s - submit urb failed", __func__);
  586. goto exit;
  587. }
  588. /* wait for the command to complete */
  589. t = wait_event_timeout(command_info->wait_command,
  590. (bool)command_info->command_finished, COMMAND_TIMEOUT);
  591. if (!t)
  592. usb_kill_urb(command_port->write_urb);
  593. if (command_info->command_finished == false) {
  594. dbg("%s - command timed out.", __func__);
  595. retval = -ETIMEDOUT;
  596. goto exit;
  597. }
  598. if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
  599. dbg("%s - command failed.", __func__);
  600. retval = -EIO;
  601. goto exit;
  602. }
  603. if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
  604. dbg("%s - command completed.", __func__);
  605. switch (command) {
  606. case WHITEHEAT_GET_DTR_RTS:
  607. info = usb_get_serial_port_data(port);
  608. memcpy(&info->mcr, command_info->result_buffer,
  609. sizeof(struct whiteheat_dr_info));
  610. break;
  611. }
  612. }
  613. exit:
  614. mutex_unlock(&command_info->mutex);
  615. return retval;
  616. }
  617. static int firm_open(struct usb_serial_port *port)
  618. {
  619. struct whiteheat_simple open_command;
  620. open_command.port = port->number - port->serial->minor + 1;
  621. return firm_send_command(port, WHITEHEAT_OPEN,
  622. (__u8 *)&open_command, sizeof(open_command));
  623. }
  624. static int firm_close(struct usb_serial_port *port)
  625. {
  626. struct whiteheat_simple close_command;
  627. close_command.port = port->number - port->serial->minor + 1;
  628. return firm_send_command(port, WHITEHEAT_CLOSE,
  629. (__u8 *)&close_command, sizeof(close_command));
  630. }
  631. static void firm_setup_port(struct tty_struct *tty)
  632. {
  633. struct usb_serial_port *port = tty->driver_data;
  634. struct whiteheat_port_settings port_settings;
  635. unsigned int cflag = tty->termios->c_cflag;
  636. port_settings.port = port->number + 1;
  637. /* get the byte size */
  638. switch (cflag & CSIZE) {
  639. case CS5: port_settings.bits = 5; break;
  640. case CS6: port_settings.bits = 6; break;
  641. case CS7: port_settings.bits = 7; break;
  642. default:
  643. case CS8: port_settings.bits = 8; break;
  644. }
  645. dbg("%s - data bits = %d", __func__, port_settings.bits);
  646. /* determine the parity */
  647. if (cflag & PARENB)
  648. if (cflag & CMSPAR)
  649. if (cflag & PARODD)
  650. port_settings.parity = WHITEHEAT_PAR_MARK;
  651. else
  652. port_settings.parity = WHITEHEAT_PAR_SPACE;
  653. else
  654. if (cflag & PARODD)
  655. port_settings.parity = WHITEHEAT_PAR_ODD;
  656. else
  657. port_settings.parity = WHITEHEAT_PAR_EVEN;
  658. else
  659. port_settings.parity = WHITEHEAT_PAR_NONE;
  660. dbg("%s - parity = %c", __func__, port_settings.parity);
  661. /* figure out the stop bits requested */
  662. if (cflag & CSTOPB)
  663. port_settings.stop = 2;
  664. else
  665. port_settings.stop = 1;
  666. dbg("%s - stop bits = %d", __func__, port_settings.stop);
  667. /* figure out the flow control settings */
  668. if (cflag & CRTSCTS)
  669. port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
  670. WHITEHEAT_HFLOW_RTS);
  671. else
  672. port_settings.hflow = WHITEHEAT_HFLOW_NONE;
  673. dbg("%s - hardware flow control = %s %s %s %s", __func__,
  674. (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
  675. (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
  676. (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
  677. (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
  678. /* determine software flow control */
  679. if (I_IXOFF(tty))
  680. port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
  681. else
  682. port_settings.sflow = WHITEHEAT_SFLOW_NONE;
  683. dbg("%s - software flow control = %c", __func__, port_settings.sflow);
  684. port_settings.xon = START_CHAR(tty);
  685. port_settings.xoff = STOP_CHAR(tty);
  686. dbg("%s - XON = %2x, XOFF = %2x",
  687. __func__, port_settings.xon, port_settings.xoff);
  688. /* get the baud rate wanted */
  689. port_settings.baud = tty_get_baud_rate(tty);
  690. dbg("%s - baud rate = %d", __func__, port_settings.baud);
  691. /* fixme: should set validated settings */
  692. tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
  693. /* handle any settings that aren't specified in the tty structure */
  694. port_settings.lloop = 0;
  695. /* now send the message to the device */
  696. firm_send_command(port, WHITEHEAT_SETUP_PORT,
  697. (__u8 *)&port_settings, sizeof(port_settings));
  698. }
  699. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
  700. {
  701. struct whiteheat_set_rdb rts_command;
  702. rts_command.port = port->number - port->serial->minor + 1;
  703. rts_command.state = onoff;
  704. return firm_send_command(port, WHITEHEAT_SET_RTS,
  705. (__u8 *)&rts_command, sizeof(rts_command));
  706. }
  707. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
  708. {
  709. struct whiteheat_set_rdb dtr_command;
  710. dtr_command.port = port->number - port->serial->minor + 1;
  711. dtr_command.state = onoff;
  712. return firm_send_command(port, WHITEHEAT_SET_DTR,
  713. (__u8 *)&dtr_command, sizeof(dtr_command));
  714. }
  715. static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
  716. {
  717. struct whiteheat_set_rdb break_command;
  718. break_command.port = port->number - port->serial->minor + 1;
  719. break_command.state = onoff;
  720. return firm_send_command(port, WHITEHEAT_SET_BREAK,
  721. (__u8 *)&break_command, sizeof(break_command));
  722. }
  723. static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
  724. {
  725. struct whiteheat_purge purge_command;
  726. purge_command.port = port->number - port->serial->minor + 1;
  727. purge_command.what = rxtx;
  728. return firm_send_command(port, WHITEHEAT_PURGE,
  729. (__u8 *)&purge_command, sizeof(purge_command));
  730. }
  731. static int firm_get_dtr_rts(struct usb_serial_port *port)
  732. {
  733. struct whiteheat_simple get_dr_command;
  734. get_dr_command.port = port->number - port->serial->minor + 1;
  735. return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
  736. (__u8 *)&get_dr_command, sizeof(get_dr_command));
  737. }
  738. static int firm_report_tx_done(struct usb_serial_port *port)
  739. {
  740. struct whiteheat_simple close_command;
  741. close_command.port = port->number - port->serial->minor + 1;
  742. return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
  743. (__u8 *)&close_command, sizeof(close_command));
  744. }
  745. /*****************************************************************************
  746. * Connect Tech's White Heat utility functions
  747. *****************************************************************************/
  748. static int start_command_port(struct usb_serial *serial)
  749. {
  750. struct usb_serial_port *command_port;
  751. struct whiteheat_command_private *command_info;
  752. int retval = 0;
  753. command_port = serial->port[COMMAND_PORT];
  754. command_info = usb_get_serial_port_data(command_port);
  755. mutex_lock(&command_info->mutex);
  756. if (!command_info->port_running) {
  757. /* Work around HCD bugs */
  758. usb_clear_halt(serial->dev, command_port->read_urb->pipe);
  759. retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
  760. if (retval) {
  761. dev_err(&serial->dev->dev,
  762. "%s - failed submitting read urb, error %d\n",
  763. __func__, retval);
  764. goto exit;
  765. }
  766. }
  767. command_info->port_running++;
  768. exit:
  769. mutex_unlock(&command_info->mutex);
  770. return retval;
  771. }
  772. static void stop_command_port(struct usb_serial *serial)
  773. {
  774. struct usb_serial_port *command_port;
  775. struct whiteheat_command_private *command_info;
  776. command_port = serial->port[COMMAND_PORT];
  777. command_info = usb_get_serial_port_data(command_port);
  778. mutex_lock(&command_info->mutex);
  779. command_info->port_running--;
  780. if (!command_info->port_running)
  781. usb_kill_urb(command_port->read_urb);
  782. mutex_unlock(&command_info->mutex);
  783. }
  784. module_usb_serial_driver(whiteheat_driver, serial_drivers);
  785. MODULE_AUTHOR(DRIVER_AUTHOR);
  786. MODULE_DESCRIPTION(DRIVER_DESC);
  787. MODULE_LICENSE("GPL");
  788. MODULE_FIRMWARE("whiteheat.fw");
  789. MODULE_FIRMWARE("whiteheat_loader.fw");
  790. module_param(debug, bool, S_IRUGO | S_IWUSR);
  791. MODULE_PARM_DESC(debug, "Debug enabled or not");