whiteheat.c 23 KB

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