whiteheat.c 44 KB

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