whiteheat.c 24 KB

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