whiteheat.c 44 KB

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