whiteheat.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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_VERSION "v2.0"
  45. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
  46. #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
  47. #define CONNECT_TECH_VENDOR_ID 0x0710
  48. #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
  49. #define CONNECT_TECH_WHITE_HEAT_ID 0x8001
  50. /*
  51. ID tables for whiteheat are unusual, because we want to different
  52. things for different versions of the device. Eventually, this
  53. will be doable from a single table. But, for now, we define two
  54. separate ID tables, and then a third table that combines them
  55. just for the purpose of exporting the autoloading information.
  56. */
  57. static const struct usb_device_id id_table_std[] = {
  58. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
  59. { } /* Terminating entry */
  60. };
  61. static const struct usb_device_id id_table_prerenumeration[] = {
  62. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
  63. { } /* Terminating entry */
  64. };
  65. static const struct usb_device_id id_table_combined[] = {
  66. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
  67. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
  68. { } /* Terminating entry */
  69. };
  70. MODULE_DEVICE_TABLE(usb, id_table_combined);
  71. static struct usb_driver whiteheat_driver = {
  72. .name = "whiteheat",
  73. .probe = usb_serial_probe,
  74. .disconnect = usb_serial_disconnect,
  75. .id_table = id_table_combined,
  76. };
  77. /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
  78. static int whiteheat_firmware_download(struct usb_serial *serial,
  79. const struct usb_device_id *id);
  80. static int whiteheat_firmware_attach(struct usb_serial *serial);
  81. /* function prototypes for the Connect Tech WhiteHEAT serial converter */
  82. static int whiteheat_attach(struct usb_serial *serial);
  83. static void whiteheat_release(struct usb_serial *serial);
  84. static int whiteheat_open(struct tty_struct *tty,
  85. struct usb_serial_port *port);
  86. static void whiteheat_close(struct usb_serial_port *port);
  87. static int whiteheat_write(struct tty_struct *tty,
  88. struct usb_serial_port *port,
  89. const unsigned char *buf, int count);
  90. static int whiteheat_write_room(struct tty_struct *tty);
  91. static int whiteheat_ioctl(struct tty_struct *tty,
  92. unsigned int cmd, unsigned long arg);
  93. static void whiteheat_set_termios(struct tty_struct *tty,
  94. struct usb_serial_port *port, struct ktermios *old);
  95. static int whiteheat_tiocmget(struct tty_struct *tty);
  96. static int whiteheat_tiocmset(struct tty_struct *tty,
  97. unsigned int set, unsigned int clear);
  98. static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
  99. static int whiteheat_chars_in_buffer(struct tty_struct *tty);
  100. static void whiteheat_throttle(struct tty_struct *tty);
  101. static void whiteheat_unthrottle(struct tty_struct *tty);
  102. static void whiteheat_read_callback(struct urb *urb);
  103. static void whiteheat_write_callback(struct urb *urb);
  104. static struct usb_serial_driver whiteheat_fake_device = {
  105. .driver = {
  106. .owner = THIS_MODULE,
  107. .name = "whiteheatnofirm",
  108. },
  109. .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
  110. .id_table = id_table_prerenumeration,
  111. .num_ports = 1,
  112. .probe = whiteheat_firmware_download,
  113. .attach = whiteheat_firmware_attach,
  114. };
  115. static struct usb_serial_driver whiteheat_device = {
  116. .driver = {
  117. .owner = THIS_MODULE,
  118. .name = "whiteheat",
  119. },
  120. .description = "Connect Tech - WhiteHEAT",
  121. .id_table = id_table_std,
  122. .num_ports = 4,
  123. .attach = whiteheat_attach,
  124. .release = whiteheat_release,
  125. .open = whiteheat_open,
  126. .close = whiteheat_close,
  127. .write = whiteheat_write,
  128. .write_room = whiteheat_write_room,
  129. .ioctl = whiteheat_ioctl,
  130. .set_termios = whiteheat_set_termios,
  131. .break_ctl = whiteheat_break_ctl,
  132. .tiocmget = whiteheat_tiocmget,
  133. .tiocmset = whiteheat_tiocmset,
  134. .chars_in_buffer = whiteheat_chars_in_buffer,
  135. .throttle = whiteheat_throttle,
  136. .unthrottle = whiteheat_unthrottle,
  137. .read_bulk_callback = whiteheat_read_callback,
  138. .write_bulk_callback = whiteheat_write_callback,
  139. };
  140. static struct usb_serial_driver * const serial_drivers[] = {
  141. &whiteheat_fake_device, &whiteheat_device, NULL
  142. };
  143. struct whiteheat_command_private {
  144. struct mutex mutex;
  145. __u8 port_running;
  146. __u8 command_finished;
  147. wait_queue_head_t wait_command; /* for handling sleeping whilst
  148. waiting for a command to
  149. finish */
  150. __u8 result_buffer[64];
  151. };
  152. #define THROTTLED 0x01
  153. #define ACTUALLY_THROTTLED 0x02
  154. static int urb_pool_size = 8;
  155. struct whiteheat_urb_wrap {
  156. struct list_head list;
  157. struct urb *urb;
  158. };
  159. struct whiteheat_private {
  160. spinlock_t lock;
  161. __u8 flags;
  162. __u8 mcr; /* FIXME: no locking on mcr */
  163. struct list_head rx_urbs_free;
  164. struct list_head rx_urbs_submitted;
  165. struct list_head rx_urb_q;
  166. struct work_struct rx_work;
  167. struct usb_serial_port *port;
  168. struct list_head tx_urbs_free;
  169. struct list_head tx_urbs_submitted;
  170. struct mutex deathwarrant;
  171. };
  172. /* local function prototypes */
  173. static int start_command_port(struct usb_serial *serial);
  174. static void stop_command_port(struct usb_serial *serial);
  175. static void command_port_write_callback(struct urb *urb);
  176. static void command_port_read_callback(struct urb *urb);
  177. static int start_port_read(struct usb_serial_port *port);
  178. static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
  179. struct list_head *head);
  180. static struct list_head *list_first(struct list_head *head);
  181. static void rx_data_softint(struct work_struct *work);
  182. static int firm_send_command(struct usb_serial_port *port, __u8 command,
  183. __u8 *data, __u8 datasize);
  184. static int firm_open(struct usb_serial_port *port);
  185. static int firm_close(struct usb_serial_port *port);
  186. static void firm_setup_port(struct tty_struct *tty);
  187. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
  188. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
  189. static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
  190. static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
  191. static int firm_get_dtr_rts(struct usb_serial_port *port);
  192. static int firm_report_tx_done(struct usb_serial_port *port);
  193. #define COMMAND_PORT 4
  194. #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
  195. #define COMMAND_TIMEOUT_MS 2000
  196. #define CLOSING_DELAY (30 * HZ)
  197. /*****************************************************************************
  198. * Connect Tech's White Heat prerenumeration driver functions
  199. *****************************************************************************/
  200. /* steps to download the firmware to the WhiteHEAT device:
  201. - hold the reset (by writing to the reset bit of the CPUCS register)
  202. - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
  203. - release the reset (by writing to the CPUCS register)
  204. - download the WH.HEX file for all addresses greater than 0x1b3f using
  205. VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
  206. - hold the reset
  207. - download the WH.HEX file for all addresses less than 0x1b40 using
  208. VENDOR_REQUEST_ANCHOR_LOAD
  209. - release the reset
  210. - device renumerated itself and comes up as new device id with all
  211. firmware download completed.
  212. */
  213. static int whiteheat_firmware_download(struct usb_serial *serial,
  214. const struct usb_device_id *id)
  215. {
  216. int response, ret = -ENOENT;
  217. const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
  218. const struct ihex_binrec *record;
  219. dbg("%s", __func__);
  220. if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
  221. &serial->dev->dev)) {
  222. dev_err(&serial->dev->dev,
  223. "%s - request \"whiteheat.fw\" failed\n", __func__);
  224. goto out;
  225. }
  226. if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
  227. &serial->dev->dev)) {
  228. dev_err(&serial->dev->dev,
  229. "%s - request \"whiteheat_loader.fw\" failed\n",
  230. __func__);
  231. goto out;
  232. }
  233. ret = 0;
  234. response = ezusb_set_reset (serial, 1);
  235. record = (const struct ihex_binrec *)loader_fw->data;
  236. while (record) {
  237. response = ezusb_writememory (serial, be32_to_cpu(record->addr),
  238. (unsigned char *)record->data,
  239. be16_to_cpu(record->len), 0xa0);
  240. if (response < 0) {
  241. dev_err(&serial->dev->dev, "%s - ezusb_writememory "
  242. "failed for loader (%d %04X %p %d)\n",
  243. __func__, response, be32_to_cpu(record->addr),
  244. record->data, be16_to_cpu(record->len));
  245. break;
  246. }
  247. record = ihex_next_binrec(record);
  248. }
  249. response = ezusb_set_reset(serial, 0);
  250. record = (const struct ihex_binrec *)firmware_fw->data;
  251. while (record && be32_to_cpu(record->addr) < 0x1b40)
  252. record = ihex_next_binrec(record);
  253. while (record) {
  254. response = ezusb_writememory (serial, be32_to_cpu(record->addr),
  255. (unsigned char *)record->data,
  256. be16_to_cpu(record->len), 0xa3);
  257. if (response < 0) {
  258. dev_err(&serial->dev->dev, "%s - ezusb_writememory "
  259. "failed for first firmware step "
  260. "(%d %04X %p %d)\n", __func__, response,
  261. be32_to_cpu(record->addr), record->data,
  262. be16_to_cpu(record->len));
  263. break;
  264. }
  265. ++record;
  266. }
  267. response = ezusb_set_reset(serial, 1);
  268. record = (const struct ihex_binrec *)firmware_fw->data;
  269. while (record && be32_to_cpu(record->addr) < 0x1b40) {
  270. response = ezusb_writememory (serial, be32_to_cpu(record->addr),
  271. (unsigned char *)record->data,
  272. be16_to_cpu(record->len), 0xa0);
  273. if (response < 0) {
  274. dev_err(&serial->dev->dev, "%s - ezusb_writememory "
  275. "failed for second firmware step "
  276. "(%d %04X %p %d)\n", __func__, response,
  277. be32_to_cpu(record->addr), record->data,
  278. be16_to_cpu(record->len));
  279. break;
  280. }
  281. ++record;
  282. }
  283. ret = 0;
  284. response = ezusb_set_reset (serial, 0);
  285. out:
  286. release_firmware(loader_fw);
  287. release_firmware(firmware_fw);
  288. return ret;
  289. }
  290. static int whiteheat_firmware_attach(struct usb_serial *serial)
  291. {
  292. /* We want this device to fail to have a driver assigned to it */
  293. return 1;
  294. }
  295. /*****************************************************************************
  296. * Connect Tech's White Heat serial driver functions
  297. *****************************************************************************/
  298. static int whiteheat_attach(struct usb_serial *serial)
  299. {
  300. struct usb_serial_port *command_port;
  301. struct whiteheat_command_private *command_info;
  302. struct usb_serial_port *port;
  303. struct whiteheat_private *info;
  304. struct whiteheat_hw_info *hw_info;
  305. int pipe;
  306. int ret;
  307. int alen;
  308. __u8 *command;
  309. __u8 *result;
  310. int i;
  311. int j;
  312. struct urb *urb;
  313. int buf_size;
  314. struct whiteheat_urb_wrap *wrap;
  315. struct list_head *tmp;
  316. command_port = serial->port[COMMAND_PORT];
  317. pipe = usb_sndbulkpipe(serial->dev,
  318. command_port->bulk_out_endpointAddress);
  319. command = kmalloc(2, GFP_KERNEL);
  320. if (!command)
  321. goto no_command_buffer;
  322. command[0] = WHITEHEAT_GET_HW_INFO;
  323. command[1] = 0;
  324. result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
  325. if (!result)
  326. goto no_result_buffer;
  327. /*
  328. * When the module is reloaded the firmware is still there and
  329. * the endpoints are still in the usb core unchanged. This is the
  330. * unlinking bug in disguise. Same for the call below.
  331. */
  332. usb_clear_halt(serial->dev, pipe);
  333. ret = usb_bulk_msg(serial->dev, pipe, command, 2,
  334. &alen, COMMAND_TIMEOUT_MS);
  335. if (ret) {
  336. dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
  337. serial->type->description, ret);
  338. goto no_firmware;
  339. } else if (alen != 2) {
  340. dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
  341. serial->type->description, alen);
  342. goto no_firmware;
  343. }
  344. pipe = usb_rcvbulkpipe(serial->dev,
  345. command_port->bulk_in_endpointAddress);
  346. /* See the comment on the usb_clear_halt() above */
  347. usb_clear_halt(serial->dev, pipe);
  348. ret = usb_bulk_msg(serial->dev, pipe, result,
  349. sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
  350. if (ret) {
  351. dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
  352. serial->type->description, ret);
  353. goto no_firmware;
  354. } else if (alen != sizeof(*hw_info) + 1) {
  355. dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
  356. serial->type->description, alen);
  357. goto no_firmware;
  358. } else if (result[0] != command[0]) {
  359. dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
  360. serial->type->description, result[0]);
  361. goto no_firmware;
  362. }
  363. hw_info = (struct whiteheat_hw_info *)&result[1];
  364. dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
  365. serial->type->description, DRIVER_VERSION,
  366. hw_info->sw_major_rev, hw_info->sw_minor_rev);
  367. for (i = 0; i < serial->num_ports; i++) {
  368. port = serial->port[i];
  369. info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
  370. if (info == NULL) {
  371. dev_err(&port->dev,
  372. "%s: Out of memory for port structures\n",
  373. serial->type->description);
  374. goto no_private;
  375. }
  376. spin_lock_init(&info->lock);
  377. mutex_init(&info->deathwarrant);
  378. info->flags = 0;
  379. info->mcr = 0;
  380. INIT_WORK(&info->rx_work, rx_data_softint);
  381. info->port = port;
  382. INIT_LIST_HEAD(&info->rx_urbs_free);
  383. INIT_LIST_HEAD(&info->rx_urbs_submitted);
  384. INIT_LIST_HEAD(&info->rx_urb_q);
  385. INIT_LIST_HEAD(&info->tx_urbs_free);
  386. INIT_LIST_HEAD(&info->tx_urbs_submitted);
  387. for (j = 0; j < urb_pool_size; j++) {
  388. urb = usb_alloc_urb(0, GFP_KERNEL);
  389. if (!urb) {
  390. dev_err(&port->dev, "No free urbs available\n");
  391. goto no_rx_urb;
  392. }
  393. buf_size = port->read_urb->transfer_buffer_length;
  394. urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
  395. if (!urb->transfer_buffer) {
  396. dev_err(&port->dev,
  397. "Couldn't allocate urb buffer\n");
  398. goto no_rx_buf;
  399. }
  400. wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
  401. if (!wrap) {
  402. dev_err(&port->dev,
  403. "Couldn't allocate urb wrapper\n");
  404. goto no_rx_wrap;
  405. }
  406. usb_fill_bulk_urb(urb, serial->dev,
  407. usb_rcvbulkpipe(serial->dev,
  408. port->bulk_in_endpointAddress),
  409. urb->transfer_buffer, buf_size,
  410. whiteheat_read_callback, port);
  411. wrap->urb = urb;
  412. list_add(&wrap->list, &info->rx_urbs_free);
  413. urb = usb_alloc_urb(0, GFP_KERNEL);
  414. if (!urb) {
  415. dev_err(&port->dev, "No free urbs available\n");
  416. goto no_tx_urb;
  417. }
  418. buf_size = port->write_urb->transfer_buffer_length;
  419. urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
  420. if (!urb->transfer_buffer) {
  421. dev_err(&port->dev,
  422. "Couldn't allocate urb buffer\n");
  423. goto no_tx_buf;
  424. }
  425. wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
  426. if (!wrap) {
  427. dev_err(&port->dev,
  428. "Couldn't allocate urb wrapper\n");
  429. goto no_tx_wrap;
  430. }
  431. usb_fill_bulk_urb(urb, serial->dev,
  432. usb_sndbulkpipe(serial->dev,
  433. port->bulk_out_endpointAddress),
  434. urb->transfer_buffer, buf_size,
  435. whiteheat_write_callback, port);
  436. wrap->urb = urb;
  437. list_add(&wrap->list, &info->tx_urbs_free);
  438. }
  439. usb_set_serial_port_data(port, info);
  440. }
  441. command_info = kmalloc(sizeof(struct whiteheat_command_private),
  442. GFP_KERNEL);
  443. if (command_info == NULL) {
  444. dev_err(&serial->dev->dev,
  445. "%s: Out of memory for port structures\n",
  446. serial->type->description);
  447. goto no_command_private;
  448. }
  449. mutex_init(&command_info->mutex);
  450. command_info->port_running = 0;
  451. init_waitqueue_head(&command_info->wait_command);
  452. usb_set_serial_port_data(command_port, command_info);
  453. command_port->write_urb->complete = command_port_write_callback;
  454. command_port->read_urb->complete = command_port_read_callback;
  455. kfree(result);
  456. kfree(command);
  457. return 0;
  458. no_firmware:
  459. /* Firmware likely not running */
  460. dev_err(&serial->dev->dev,
  461. "%s: Unable to retrieve firmware version, try replugging\n",
  462. serial->type->description);
  463. dev_err(&serial->dev->dev,
  464. "%s: If the firmware is not running (status led not blinking)\n",
  465. serial->type->description);
  466. dev_err(&serial->dev->dev,
  467. "%s: please contact support@connecttech.com\n",
  468. serial->type->description);
  469. kfree(result);
  470. return -ENODEV;
  471. no_command_private:
  472. for (i = serial->num_ports - 1; i >= 0; i--) {
  473. port = serial->port[i];
  474. info = usb_get_serial_port_data(port);
  475. for (j = urb_pool_size - 1; j >= 0; j--) {
  476. tmp = list_first(&info->tx_urbs_free);
  477. list_del(tmp);
  478. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  479. urb = wrap->urb;
  480. kfree(wrap);
  481. no_tx_wrap:
  482. kfree(urb->transfer_buffer);
  483. no_tx_buf:
  484. usb_free_urb(urb);
  485. no_tx_urb:
  486. tmp = list_first(&info->rx_urbs_free);
  487. list_del(tmp);
  488. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  489. urb = wrap->urb;
  490. kfree(wrap);
  491. no_rx_wrap:
  492. kfree(urb->transfer_buffer);
  493. no_rx_buf:
  494. usb_free_urb(urb);
  495. no_rx_urb:
  496. ;
  497. }
  498. kfree(info);
  499. no_private:
  500. ;
  501. }
  502. kfree(result);
  503. no_result_buffer:
  504. kfree(command);
  505. no_command_buffer:
  506. return -ENOMEM;
  507. }
  508. static void whiteheat_release(struct usb_serial *serial)
  509. {
  510. struct usb_serial_port *command_port;
  511. struct usb_serial_port *port;
  512. struct whiteheat_private *info;
  513. struct whiteheat_urb_wrap *wrap;
  514. struct urb *urb;
  515. struct list_head *tmp;
  516. struct list_head *tmp2;
  517. int i;
  518. dbg("%s", __func__);
  519. /* free up our private data for our command port */
  520. command_port = serial->port[COMMAND_PORT];
  521. kfree(usb_get_serial_port_data(command_port));
  522. for (i = 0; i < serial->num_ports; i++) {
  523. port = serial->port[i];
  524. info = usb_get_serial_port_data(port);
  525. list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
  526. list_del(tmp);
  527. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  528. urb = wrap->urb;
  529. kfree(wrap);
  530. kfree(urb->transfer_buffer);
  531. usb_free_urb(urb);
  532. }
  533. list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
  534. list_del(tmp);
  535. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  536. urb = wrap->urb;
  537. kfree(wrap);
  538. kfree(urb->transfer_buffer);
  539. usb_free_urb(urb);
  540. }
  541. kfree(info);
  542. }
  543. }
  544. static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
  545. {
  546. int retval = 0;
  547. dbg("%s - port %d", __func__, port->number);
  548. retval = start_command_port(port->serial);
  549. if (retval)
  550. goto exit;
  551. if (tty)
  552. tty->low_latency = 1;
  553. /* send an open port command */
  554. retval = firm_open(port);
  555. if (retval) {
  556. stop_command_port(port->serial);
  557. goto exit;
  558. }
  559. retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
  560. if (retval) {
  561. firm_close(port);
  562. stop_command_port(port->serial);
  563. goto exit;
  564. }
  565. if (tty)
  566. firm_setup_port(tty);
  567. /* Work around HCD bugs */
  568. usb_clear_halt(port->serial->dev, port->read_urb->pipe);
  569. usb_clear_halt(port->serial->dev, port->write_urb->pipe);
  570. /* Start reading from the device */
  571. retval = start_port_read(port);
  572. if (retval) {
  573. dev_err(&port->dev,
  574. "%s - failed submitting read urb, error %d\n",
  575. __func__, retval);
  576. firm_close(port);
  577. stop_command_port(port->serial);
  578. goto exit;
  579. }
  580. exit:
  581. dbg("%s - exit, retval = %d", __func__, retval);
  582. return retval;
  583. }
  584. static void whiteheat_close(struct usb_serial_port *port)
  585. {
  586. struct whiteheat_private *info = usb_get_serial_port_data(port);
  587. struct whiteheat_urb_wrap *wrap;
  588. struct urb *urb;
  589. struct list_head *tmp;
  590. struct list_head *tmp2;
  591. dbg("%s - port %d", __func__, port->number);
  592. firm_report_tx_done(port);
  593. firm_close(port);
  594. /* shutdown our bulk reads and writes */
  595. mutex_lock(&info->deathwarrant);
  596. spin_lock_irq(&info->lock);
  597. list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
  598. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  599. urb = wrap->urb;
  600. list_del(tmp);
  601. spin_unlock_irq(&info->lock);
  602. usb_kill_urb(urb);
  603. spin_lock_irq(&info->lock);
  604. list_add(tmp, &info->rx_urbs_free);
  605. }
  606. list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
  607. list_move(tmp, &info->rx_urbs_free);
  608. list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
  609. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  610. urb = wrap->urb;
  611. list_del(tmp);
  612. spin_unlock_irq(&info->lock);
  613. usb_kill_urb(urb);
  614. spin_lock_irq(&info->lock);
  615. list_add(tmp, &info->tx_urbs_free);
  616. }
  617. spin_unlock_irq(&info->lock);
  618. mutex_unlock(&info->deathwarrant);
  619. stop_command_port(port->serial);
  620. }
  621. static int whiteheat_write(struct tty_struct *tty,
  622. struct usb_serial_port *port, const unsigned char *buf, int count)
  623. {
  624. struct whiteheat_private *info = usb_get_serial_port_data(port);
  625. struct whiteheat_urb_wrap *wrap;
  626. struct urb *urb;
  627. int result;
  628. int bytes;
  629. int sent = 0;
  630. unsigned long flags;
  631. struct list_head *tmp;
  632. dbg("%s - port %d", __func__, port->number);
  633. if (count == 0) {
  634. dbg("%s - write request of 0 bytes", __func__);
  635. return (0);
  636. }
  637. while (count) {
  638. spin_lock_irqsave(&info->lock, flags);
  639. if (list_empty(&info->tx_urbs_free)) {
  640. spin_unlock_irqrestore(&info->lock, flags);
  641. break;
  642. }
  643. tmp = list_first(&info->tx_urbs_free);
  644. list_del(tmp);
  645. spin_unlock_irqrestore(&info->lock, flags);
  646. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  647. urb = wrap->urb;
  648. bytes = (count > port->bulk_out_size) ?
  649. port->bulk_out_size : count;
  650. memcpy(urb->transfer_buffer, buf + sent, bytes);
  651. usb_serial_debug_data(debug, &port->dev,
  652. __func__, bytes, urb->transfer_buffer);
  653. urb->transfer_buffer_length = bytes;
  654. result = usb_submit_urb(urb, GFP_ATOMIC);
  655. if (result) {
  656. dev_err_console(port,
  657. "%s - failed submitting write urb, error %d\n",
  658. __func__, result);
  659. sent = result;
  660. spin_lock_irqsave(&info->lock, flags);
  661. list_add(tmp, &info->tx_urbs_free);
  662. spin_unlock_irqrestore(&info->lock, flags);
  663. break;
  664. } else {
  665. sent += bytes;
  666. count -= bytes;
  667. spin_lock_irqsave(&info->lock, flags);
  668. list_add(tmp, &info->tx_urbs_submitted);
  669. spin_unlock_irqrestore(&info->lock, flags);
  670. }
  671. }
  672. return sent;
  673. }
  674. static int whiteheat_write_room(struct tty_struct *tty)
  675. {
  676. struct usb_serial_port *port = tty->driver_data;
  677. struct whiteheat_private *info = usb_get_serial_port_data(port);
  678. struct list_head *tmp;
  679. int room = 0;
  680. unsigned long flags;
  681. dbg("%s - port %d", __func__, port->number);
  682. spin_lock_irqsave(&info->lock, flags);
  683. list_for_each(tmp, &info->tx_urbs_free)
  684. room++;
  685. spin_unlock_irqrestore(&info->lock, flags);
  686. room *= port->bulk_out_size;
  687. dbg("%s - returns %d", __func__, room);
  688. return (room);
  689. }
  690. static int whiteheat_tiocmget(struct tty_struct *tty)
  691. {
  692. struct usb_serial_port *port = tty->driver_data;
  693. struct whiteheat_private *info = usb_get_serial_port_data(port);
  694. unsigned int modem_signals = 0;
  695. dbg("%s - port %d", __func__, port->number);
  696. firm_get_dtr_rts(port);
  697. if (info->mcr & UART_MCR_DTR)
  698. modem_signals |= TIOCM_DTR;
  699. if (info->mcr & UART_MCR_RTS)
  700. modem_signals |= TIOCM_RTS;
  701. return modem_signals;
  702. }
  703. static int whiteheat_tiocmset(struct tty_struct *tty,
  704. unsigned int set, unsigned int clear)
  705. {
  706. struct usb_serial_port *port = tty->driver_data;
  707. struct whiteheat_private *info = usb_get_serial_port_data(port);
  708. dbg("%s - port %d", __func__, port->number);
  709. if (set & TIOCM_RTS)
  710. info->mcr |= UART_MCR_RTS;
  711. if (set & TIOCM_DTR)
  712. info->mcr |= UART_MCR_DTR;
  713. if (clear & TIOCM_RTS)
  714. info->mcr &= ~UART_MCR_RTS;
  715. if (clear & TIOCM_DTR)
  716. info->mcr &= ~UART_MCR_DTR;
  717. firm_set_dtr(port, info->mcr & UART_MCR_DTR);
  718. firm_set_rts(port, info->mcr & UART_MCR_RTS);
  719. return 0;
  720. }
  721. static int whiteheat_ioctl(struct tty_struct *tty,
  722. unsigned int cmd, unsigned long arg)
  723. {
  724. struct usb_serial_port *port = tty->driver_data;
  725. struct serial_struct serstruct;
  726. void __user *user_arg = (void __user *)arg;
  727. dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
  728. switch (cmd) {
  729. case TIOCGSERIAL:
  730. memset(&serstruct, 0, sizeof(serstruct));
  731. serstruct.type = PORT_16654;
  732. serstruct.line = port->serial->minor;
  733. serstruct.port = port->number;
  734. serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  735. serstruct.xmit_fifo_size = port->bulk_out_size;
  736. serstruct.custom_divisor = 0;
  737. serstruct.baud_base = 460800;
  738. serstruct.close_delay = CLOSING_DELAY;
  739. serstruct.closing_wait = CLOSING_DELAY;
  740. if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
  741. return -EFAULT;
  742. break;
  743. default:
  744. break;
  745. }
  746. return -ENOIOCTLCMD;
  747. }
  748. static void whiteheat_set_termios(struct tty_struct *tty,
  749. struct usb_serial_port *port, struct ktermios *old_termios)
  750. {
  751. firm_setup_port(tty);
  752. }
  753. static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
  754. {
  755. struct usb_serial_port *port = tty->driver_data;
  756. firm_set_break(port, break_state);
  757. }
  758. static int whiteheat_chars_in_buffer(struct tty_struct *tty)
  759. {
  760. struct usb_serial_port *port = tty->driver_data;
  761. struct whiteheat_private *info = usb_get_serial_port_data(port);
  762. struct list_head *tmp;
  763. struct whiteheat_urb_wrap *wrap;
  764. int chars = 0;
  765. unsigned long flags;
  766. dbg("%s - port %d", __func__, port->number);
  767. spin_lock_irqsave(&info->lock, flags);
  768. list_for_each(tmp, &info->tx_urbs_submitted) {
  769. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  770. chars += wrap->urb->transfer_buffer_length;
  771. }
  772. spin_unlock_irqrestore(&info->lock, flags);
  773. dbg("%s - returns %d", __func__, chars);
  774. return chars;
  775. }
  776. static void whiteheat_throttle(struct tty_struct *tty)
  777. {
  778. struct usb_serial_port *port = tty->driver_data;
  779. struct whiteheat_private *info = usb_get_serial_port_data(port);
  780. dbg("%s - port %d", __func__, port->number);
  781. spin_lock_irq(&info->lock);
  782. info->flags |= THROTTLED;
  783. spin_unlock_irq(&info->lock);
  784. }
  785. static void whiteheat_unthrottle(struct tty_struct *tty)
  786. {
  787. struct usb_serial_port *port = tty->driver_data;
  788. struct whiteheat_private *info = usb_get_serial_port_data(port);
  789. int actually_throttled;
  790. dbg("%s - port %d", __func__, port->number);
  791. spin_lock_irq(&info->lock);
  792. actually_throttled = info->flags & ACTUALLY_THROTTLED;
  793. info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  794. spin_unlock_irq(&info->lock);
  795. if (actually_throttled)
  796. rx_data_softint(&info->rx_work);
  797. }
  798. /*****************************************************************************
  799. * Connect Tech's White Heat callback routines
  800. *****************************************************************************/
  801. static void command_port_write_callback(struct urb *urb)
  802. {
  803. int status = urb->status;
  804. dbg("%s", __func__);
  805. if (status) {
  806. dbg("nonzero urb status: %d", status);
  807. return;
  808. }
  809. }
  810. static void command_port_read_callback(struct urb *urb)
  811. {
  812. struct usb_serial_port *command_port = urb->context;
  813. struct whiteheat_command_private *command_info;
  814. int status = urb->status;
  815. unsigned char *data = urb->transfer_buffer;
  816. int result;
  817. dbg("%s", __func__);
  818. command_info = usb_get_serial_port_data(command_port);
  819. if (!command_info) {
  820. dbg("%s - command_info is NULL, exiting.", __func__);
  821. return;
  822. }
  823. if (status) {
  824. dbg("%s - nonzero urb status: %d", __func__, status);
  825. if (status != -ENOENT)
  826. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  827. wake_up(&command_info->wait_command);
  828. return;
  829. }
  830. usb_serial_debug_data(debug, &command_port->dev,
  831. __func__, urb->actual_length, data);
  832. if (data[0] == WHITEHEAT_CMD_COMPLETE) {
  833. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  834. wake_up(&command_info->wait_command);
  835. } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
  836. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  837. wake_up(&command_info->wait_command);
  838. } else if (data[0] == WHITEHEAT_EVENT) {
  839. /* These are unsolicited reports from the firmware, hence no
  840. waiting command to wakeup */
  841. dbg("%s - event received", __func__);
  842. } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
  843. memcpy(command_info->result_buffer, &data[1],
  844. urb->actual_length - 1);
  845. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  846. wake_up(&command_info->wait_command);
  847. } else
  848. dbg("%s - bad reply from firmware", __func__);
  849. /* Continue trying to always read */
  850. result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
  851. if (result)
  852. dbg("%s - failed resubmitting read urb, error %d",
  853. __func__, result);
  854. }
  855. static void whiteheat_read_callback(struct urb *urb)
  856. {
  857. struct usb_serial_port *port = urb->context;
  858. struct whiteheat_urb_wrap *wrap;
  859. unsigned char *data = urb->transfer_buffer;
  860. struct whiteheat_private *info = usb_get_serial_port_data(port);
  861. int status = urb->status;
  862. dbg("%s - port %d", __func__, port->number);
  863. spin_lock(&info->lock);
  864. wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
  865. if (!wrap) {
  866. spin_unlock(&info->lock);
  867. dev_err(&port->dev, "%s - Not my urb!\n", __func__);
  868. return;
  869. }
  870. list_del(&wrap->list);
  871. spin_unlock(&info->lock);
  872. if (status) {
  873. dbg("%s - nonzero read bulk status received: %d",
  874. __func__, status);
  875. spin_lock(&info->lock);
  876. list_add(&wrap->list, &info->rx_urbs_free);
  877. spin_unlock(&info->lock);
  878. return;
  879. }
  880. usb_serial_debug_data(debug, &port->dev,
  881. __func__, urb->actual_length, data);
  882. spin_lock(&info->lock);
  883. list_add_tail(&wrap->list, &info->rx_urb_q);
  884. if (info->flags & THROTTLED) {
  885. info->flags |= ACTUALLY_THROTTLED;
  886. spin_unlock(&info->lock);
  887. return;
  888. }
  889. spin_unlock(&info->lock);
  890. schedule_work(&info->rx_work);
  891. }
  892. static void whiteheat_write_callback(struct urb *urb)
  893. {
  894. struct usb_serial_port *port = urb->context;
  895. struct whiteheat_private *info = usb_get_serial_port_data(port);
  896. struct whiteheat_urb_wrap *wrap;
  897. int status = urb->status;
  898. dbg("%s - port %d", __func__, port->number);
  899. spin_lock(&info->lock);
  900. wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
  901. if (!wrap) {
  902. spin_unlock(&info->lock);
  903. dev_err(&port->dev, "%s - Not my urb!\n", __func__);
  904. return;
  905. }
  906. list_move(&wrap->list, &info->tx_urbs_free);
  907. spin_unlock(&info->lock);
  908. if (status) {
  909. dbg("%s - nonzero write bulk status received: %d",
  910. __func__, status);
  911. return;
  912. }
  913. usb_serial_port_softint(port);
  914. }
  915. /*****************************************************************************
  916. * Connect Tech's White Heat firmware interface
  917. *****************************************************************************/
  918. static int firm_send_command(struct usb_serial_port *port, __u8 command,
  919. __u8 *data, __u8 datasize)
  920. {
  921. struct usb_serial_port *command_port;
  922. struct whiteheat_command_private *command_info;
  923. struct whiteheat_private *info;
  924. __u8 *transfer_buffer;
  925. int retval = 0;
  926. int t;
  927. dbg("%s - command %d", __func__, command);
  928. command_port = port->serial->port[COMMAND_PORT];
  929. command_info = usb_get_serial_port_data(command_port);
  930. mutex_lock(&command_info->mutex);
  931. command_info->command_finished = false;
  932. transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
  933. transfer_buffer[0] = command;
  934. memcpy(&transfer_buffer[1], data, datasize);
  935. command_port->write_urb->transfer_buffer_length = datasize + 1;
  936. retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
  937. if (retval) {
  938. dbg("%s - submit urb failed", __func__);
  939. goto exit;
  940. }
  941. /* wait for the command to complete */
  942. t = wait_event_timeout(command_info->wait_command,
  943. (bool)command_info->command_finished, COMMAND_TIMEOUT);
  944. if (!t)
  945. usb_kill_urb(command_port->write_urb);
  946. if (command_info->command_finished == false) {
  947. dbg("%s - command timed out.", __func__);
  948. retval = -ETIMEDOUT;
  949. goto exit;
  950. }
  951. if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
  952. dbg("%s - command failed.", __func__);
  953. retval = -EIO;
  954. goto exit;
  955. }
  956. if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
  957. dbg("%s - command completed.", __func__);
  958. switch (command) {
  959. case WHITEHEAT_GET_DTR_RTS:
  960. info = usb_get_serial_port_data(port);
  961. memcpy(&info->mcr, command_info->result_buffer,
  962. sizeof(struct whiteheat_dr_info));
  963. break;
  964. }
  965. }
  966. exit:
  967. mutex_unlock(&command_info->mutex);
  968. return retval;
  969. }
  970. static int firm_open(struct usb_serial_port *port)
  971. {
  972. struct whiteheat_simple open_command;
  973. open_command.port = port->number - port->serial->minor + 1;
  974. return firm_send_command(port, WHITEHEAT_OPEN,
  975. (__u8 *)&open_command, sizeof(open_command));
  976. }
  977. static int firm_close(struct usb_serial_port *port)
  978. {
  979. struct whiteheat_simple close_command;
  980. close_command.port = port->number - port->serial->minor + 1;
  981. return firm_send_command(port, WHITEHEAT_CLOSE,
  982. (__u8 *)&close_command, sizeof(close_command));
  983. }
  984. static void firm_setup_port(struct tty_struct *tty)
  985. {
  986. struct usb_serial_port *port = tty->driver_data;
  987. struct whiteheat_port_settings port_settings;
  988. unsigned int cflag = tty->termios->c_cflag;
  989. port_settings.port = port->number + 1;
  990. /* get the byte size */
  991. switch (cflag & CSIZE) {
  992. case CS5: port_settings.bits = 5; break;
  993. case CS6: port_settings.bits = 6; break;
  994. case CS7: port_settings.bits = 7; break;
  995. default:
  996. case CS8: port_settings.bits = 8; break;
  997. }
  998. dbg("%s - data bits = %d", __func__, port_settings.bits);
  999. /* determine the parity */
  1000. if (cflag & PARENB)
  1001. if (cflag & CMSPAR)
  1002. if (cflag & PARODD)
  1003. port_settings.parity = WHITEHEAT_PAR_MARK;
  1004. else
  1005. port_settings.parity = WHITEHEAT_PAR_SPACE;
  1006. else
  1007. if (cflag & PARODD)
  1008. port_settings.parity = WHITEHEAT_PAR_ODD;
  1009. else
  1010. port_settings.parity = WHITEHEAT_PAR_EVEN;
  1011. else
  1012. port_settings.parity = WHITEHEAT_PAR_NONE;
  1013. dbg("%s - parity = %c", __func__, port_settings.parity);
  1014. /* figure out the stop bits requested */
  1015. if (cflag & CSTOPB)
  1016. port_settings.stop = 2;
  1017. else
  1018. port_settings.stop = 1;
  1019. dbg("%s - stop bits = %d", __func__, port_settings.stop);
  1020. /* figure out the flow control settings */
  1021. if (cflag & CRTSCTS)
  1022. port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
  1023. WHITEHEAT_HFLOW_RTS);
  1024. else
  1025. port_settings.hflow = WHITEHEAT_HFLOW_NONE;
  1026. dbg("%s - hardware flow control = %s %s %s %s", __func__,
  1027. (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
  1028. (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
  1029. (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
  1030. (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
  1031. /* determine software flow control */
  1032. if (I_IXOFF(tty))
  1033. port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
  1034. else
  1035. port_settings.sflow = WHITEHEAT_SFLOW_NONE;
  1036. dbg("%s - software flow control = %c", __func__, port_settings.sflow);
  1037. port_settings.xon = START_CHAR(tty);
  1038. port_settings.xoff = STOP_CHAR(tty);
  1039. dbg("%s - XON = %2x, XOFF = %2x",
  1040. __func__, port_settings.xon, port_settings.xoff);
  1041. /* get the baud rate wanted */
  1042. port_settings.baud = tty_get_baud_rate(tty);
  1043. dbg("%s - baud rate = %d", __func__, port_settings.baud);
  1044. /* fixme: should set validated settings */
  1045. tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
  1046. /* handle any settings that aren't specified in the tty structure */
  1047. port_settings.lloop = 0;
  1048. /* now send the message to the device */
  1049. firm_send_command(port, WHITEHEAT_SETUP_PORT,
  1050. (__u8 *)&port_settings, sizeof(port_settings));
  1051. }
  1052. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
  1053. {
  1054. struct whiteheat_set_rdb rts_command;
  1055. rts_command.port = port->number - port->serial->minor + 1;
  1056. rts_command.state = onoff;
  1057. return firm_send_command(port, WHITEHEAT_SET_RTS,
  1058. (__u8 *)&rts_command, sizeof(rts_command));
  1059. }
  1060. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
  1061. {
  1062. struct whiteheat_set_rdb dtr_command;
  1063. dtr_command.port = port->number - port->serial->minor + 1;
  1064. dtr_command.state = onoff;
  1065. return firm_send_command(port, WHITEHEAT_SET_DTR,
  1066. (__u8 *)&dtr_command, sizeof(dtr_command));
  1067. }
  1068. static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
  1069. {
  1070. struct whiteheat_set_rdb break_command;
  1071. break_command.port = port->number - port->serial->minor + 1;
  1072. break_command.state = onoff;
  1073. return firm_send_command(port, WHITEHEAT_SET_BREAK,
  1074. (__u8 *)&break_command, sizeof(break_command));
  1075. }
  1076. static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
  1077. {
  1078. struct whiteheat_purge purge_command;
  1079. purge_command.port = port->number - port->serial->minor + 1;
  1080. purge_command.what = rxtx;
  1081. return firm_send_command(port, WHITEHEAT_PURGE,
  1082. (__u8 *)&purge_command, sizeof(purge_command));
  1083. }
  1084. static int firm_get_dtr_rts(struct usb_serial_port *port)
  1085. {
  1086. struct whiteheat_simple get_dr_command;
  1087. get_dr_command.port = port->number - port->serial->minor + 1;
  1088. return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
  1089. (__u8 *)&get_dr_command, sizeof(get_dr_command));
  1090. }
  1091. static int firm_report_tx_done(struct usb_serial_port *port)
  1092. {
  1093. struct whiteheat_simple close_command;
  1094. close_command.port = port->number - port->serial->minor + 1;
  1095. return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
  1096. (__u8 *)&close_command, sizeof(close_command));
  1097. }
  1098. /*****************************************************************************
  1099. * Connect Tech's White Heat utility functions
  1100. *****************************************************************************/
  1101. static int start_command_port(struct usb_serial *serial)
  1102. {
  1103. struct usb_serial_port *command_port;
  1104. struct whiteheat_command_private *command_info;
  1105. int retval = 0;
  1106. command_port = serial->port[COMMAND_PORT];
  1107. command_info = usb_get_serial_port_data(command_port);
  1108. mutex_lock(&command_info->mutex);
  1109. if (!command_info->port_running) {
  1110. /* Work around HCD bugs */
  1111. usb_clear_halt(serial->dev, command_port->read_urb->pipe);
  1112. retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
  1113. if (retval) {
  1114. dev_err(&serial->dev->dev,
  1115. "%s - failed submitting read urb, error %d\n",
  1116. __func__, retval);
  1117. goto exit;
  1118. }
  1119. }
  1120. command_info->port_running++;
  1121. exit:
  1122. mutex_unlock(&command_info->mutex);
  1123. return retval;
  1124. }
  1125. static void stop_command_port(struct usb_serial *serial)
  1126. {
  1127. struct usb_serial_port *command_port;
  1128. struct whiteheat_command_private *command_info;
  1129. command_port = serial->port[COMMAND_PORT];
  1130. command_info = usb_get_serial_port_data(command_port);
  1131. mutex_lock(&command_info->mutex);
  1132. command_info->port_running--;
  1133. if (!command_info->port_running)
  1134. usb_kill_urb(command_port->read_urb);
  1135. mutex_unlock(&command_info->mutex);
  1136. }
  1137. static int start_port_read(struct usb_serial_port *port)
  1138. {
  1139. struct whiteheat_private *info = usb_get_serial_port_data(port);
  1140. struct whiteheat_urb_wrap *wrap;
  1141. struct urb *urb;
  1142. int retval = 0;
  1143. unsigned long flags;
  1144. struct list_head *tmp;
  1145. struct list_head *tmp2;
  1146. spin_lock_irqsave(&info->lock, flags);
  1147. list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
  1148. list_del(tmp);
  1149. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1150. urb = wrap->urb;
  1151. spin_unlock_irqrestore(&info->lock, flags);
  1152. retval = usb_submit_urb(urb, GFP_KERNEL);
  1153. if (retval) {
  1154. spin_lock_irqsave(&info->lock, flags);
  1155. list_add(tmp, &info->rx_urbs_free);
  1156. list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
  1157. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1158. urb = wrap->urb;
  1159. list_del(tmp);
  1160. spin_unlock_irqrestore(&info->lock, flags);
  1161. usb_kill_urb(urb);
  1162. spin_lock_irqsave(&info->lock, flags);
  1163. list_add(tmp, &info->rx_urbs_free);
  1164. }
  1165. break;
  1166. }
  1167. spin_lock_irqsave(&info->lock, flags);
  1168. list_add(tmp, &info->rx_urbs_submitted);
  1169. }
  1170. spin_unlock_irqrestore(&info->lock, flags);
  1171. return retval;
  1172. }
  1173. static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
  1174. struct list_head *head)
  1175. {
  1176. struct whiteheat_urb_wrap *wrap;
  1177. struct list_head *tmp;
  1178. list_for_each(tmp, head) {
  1179. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1180. if (wrap->urb == urb)
  1181. return wrap;
  1182. }
  1183. return NULL;
  1184. }
  1185. static struct list_head *list_first(struct list_head *head)
  1186. {
  1187. return head->next;
  1188. }
  1189. static void rx_data_softint(struct work_struct *work)
  1190. {
  1191. struct whiteheat_private *info =
  1192. container_of(work, struct whiteheat_private, rx_work);
  1193. struct usb_serial_port *port = info->port;
  1194. struct tty_struct *tty = tty_port_tty_get(&port->port);
  1195. struct whiteheat_urb_wrap *wrap;
  1196. struct urb *urb;
  1197. unsigned long flags;
  1198. struct list_head *tmp;
  1199. struct list_head *tmp2;
  1200. int result;
  1201. int sent = 0;
  1202. spin_lock_irqsave(&info->lock, flags);
  1203. if (info->flags & THROTTLED) {
  1204. spin_unlock_irqrestore(&info->lock, flags);
  1205. goto out;
  1206. }
  1207. list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
  1208. list_del(tmp);
  1209. spin_unlock_irqrestore(&info->lock, flags);
  1210. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1211. urb = wrap->urb;
  1212. if (tty && urb->actual_length)
  1213. sent += tty_insert_flip_string(tty,
  1214. urb->transfer_buffer, urb->actual_length);
  1215. result = usb_submit_urb(urb, GFP_ATOMIC);
  1216. if (result) {
  1217. dev_err(&port->dev,
  1218. "%s - failed resubmitting read urb, error %d\n",
  1219. __func__, result);
  1220. spin_lock_irqsave(&info->lock, flags);
  1221. list_add(tmp, &info->rx_urbs_free);
  1222. continue;
  1223. }
  1224. spin_lock_irqsave(&info->lock, flags);
  1225. list_add(tmp, &info->rx_urbs_submitted);
  1226. }
  1227. spin_unlock_irqrestore(&info->lock, flags);
  1228. if (sent)
  1229. tty_flip_buffer_push(tty);
  1230. out:
  1231. tty_kref_put(tty);
  1232. }
  1233. module_usb_serial_driver(whiteheat_driver, serial_drivers);
  1234. MODULE_AUTHOR(DRIVER_AUTHOR);
  1235. MODULE_DESCRIPTION(DRIVER_DESC);
  1236. MODULE_LICENSE("GPL");
  1237. MODULE_FIRMWARE("whiteheat.fw");
  1238. MODULE_FIRMWARE("whiteheat_loader.fw");
  1239. module_param(urb_pool_size, int, 0);
  1240. MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
  1241. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1242. MODULE_PARM_DESC(debug, "Debug enabled or not");