whiteheat.c 27 KB

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