whiteheat.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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. dev_err(&serial->dev->dev,
  275. "%s - request \"whiteheat.fw\" failed\n", __func__);
  276. goto out;
  277. }
  278. if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
  279. &serial->dev->dev)) {
  280. dev_err(&serial->dev->dev,
  281. "%s - request \"whiteheat_loader.fw\" failed\n",
  282. __func__);
  283. goto out;
  284. }
  285. ret = 0;
  286. response = ezusb_set_reset (serial, 1);
  287. record = (const struct ihex_binrec *)loader_fw->data;
  288. while (record) {
  289. response = ezusb_writememory (serial, be32_to_cpu(record->addr),
  290. (unsigned char *)record->data,
  291. be16_to_cpu(record->len), 0xa0);
  292. if (response < 0) {
  293. dev_err(&serial->dev->dev, "%s - ezusb_writememory "
  294. "failed for loader (%d %04X %p %d)\n",
  295. __func__, response, be32_to_cpu(record->addr),
  296. record->data, be16_to_cpu(record->len));
  297. break;
  298. }
  299. record = ihex_next_binrec(record);
  300. }
  301. response = ezusb_set_reset(serial, 0);
  302. record = (const struct ihex_binrec *)firmware_fw->data;
  303. while (record && be32_to_cpu(record->addr) < 0x1b40)
  304. record = ihex_next_binrec(record);
  305. while (record) {
  306. response = ezusb_writememory (serial, be32_to_cpu(record->addr),
  307. (unsigned char *)record->data,
  308. be16_to_cpu(record->len), 0xa3);
  309. if (response < 0) {
  310. dev_err(&serial->dev->dev, "%s - ezusb_writememory "
  311. "failed for first firmware step "
  312. "(%d %04X %p %d)\n", __func__, response,
  313. be32_to_cpu(record->addr), record->data,
  314. be16_to_cpu(record->len));
  315. break;
  316. }
  317. ++record;
  318. }
  319. response = ezusb_set_reset(serial, 1);
  320. record = (const struct ihex_binrec *)firmware_fw->data;
  321. while (record && be32_to_cpu(record->addr) < 0x1b40) {
  322. response = ezusb_writememory (serial, be32_to_cpu(record->addr),
  323. (unsigned char *)record->data,
  324. be16_to_cpu(record->len), 0xa0);
  325. if (response < 0) {
  326. dev_err(&serial->dev->dev, "%s - ezusb_writememory "
  327. "failed for second firmware step "
  328. "(%d %04X %p %d)\n", __func__, response,
  329. be32_to_cpu(record->addr), record->data,
  330. be16_to_cpu(record->len));
  331. break;
  332. }
  333. ++record;
  334. }
  335. ret = 0;
  336. response = ezusb_set_reset (serial, 0);
  337. out:
  338. release_firmware(loader_fw);
  339. release_firmware(firmware_fw);
  340. return ret;
  341. }
  342. static int whiteheat_firmware_attach(struct usb_serial *serial)
  343. {
  344. /* We want this device to fail to have a driver assigned to it */
  345. return 1;
  346. }
  347. /*****************************************************************************
  348. * Connect Tech's White Heat serial driver functions
  349. *****************************************************************************/
  350. static int whiteheat_attach(struct usb_serial *serial)
  351. {
  352. struct usb_serial_port *command_port;
  353. struct whiteheat_command_private *command_info;
  354. struct usb_serial_port *port;
  355. struct whiteheat_private *info;
  356. struct whiteheat_hw_info *hw_info;
  357. int pipe;
  358. int ret;
  359. int alen;
  360. __u8 *command;
  361. __u8 *result;
  362. int i;
  363. int j;
  364. struct urb *urb;
  365. int buf_size;
  366. struct whiteheat_urb_wrap *wrap;
  367. struct list_head *tmp;
  368. command_port = serial->port[COMMAND_PORT];
  369. pipe = usb_sndbulkpipe(serial->dev,
  370. command_port->bulk_out_endpointAddress);
  371. command = kmalloc(2, GFP_KERNEL);
  372. if (!command)
  373. goto no_command_buffer;
  374. command[0] = WHITEHEAT_GET_HW_INFO;
  375. command[1] = 0;
  376. result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
  377. if (!result)
  378. goto no_result_buffer;
  379. /*
  380. * When the module is reloaded the firmware is still there and
  381. * the endpoints are still in the usb core unchanged. This is the
  382. * unlinking bug in disguise. Same for the call below.
  383. */
  384. usb_clear_halt(serial->dev, pipe);
  385. ret = usb_bulk_msg(serial->dev, pipe, command, 2,
  386. &alen, COMMAND_TIMEOUT_MS);
  387. if (ret) {
  388. dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
  389. serial->type->description, ret);
  390. goto no_firmware;
  391. } else if (alen != 2) {
  392. dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
  393. serial->type->description, alen);
  394. goto no_firmware;
  395. }
  396. pipe = usb_rcvbulkpipe(serial->dev,
  397. command_port->bulk_in_endpointAddress);
  398. /* See the comment on the usb_clear_halt() above */
  399. usb_clear_halt(serial->dev, pipe);
  400. ret = usb_bulk_msg(serial->dev, pipe, result,
  401. sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
  402. if (ret) {
  403. dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
  404. serial->type->description, ret);
  405. goto no_firmware;
  406. } else if (alen != sizeof(*hw_info) + 1) {
  407. dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
  408. serial->type->description, alen);
  409. goto no_firmware;
  410. } else if (result[0] != command[0]) {
  411. dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
  412. serial->type->description, result[0]);
  413. goto no_firmware;
  414. }
  415. hw_info = (struct whiteheat_hw_info *)&result[1];
  416. dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
  417. serial->type->description, DRIVER_VERSION,
  418. hw_info->sw_major_rev, hw_info->sw_minor_rev);
  419. for (i = 0; i < serial->num_ports; i++) {
  420. port = serial->port[i];
  421. info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
  422. if (info == NULL) {
  423. dev_err(&port->dev,
  424. "%s: Out of memory for port structures\n",
  425. serial->type->description);
  426. goto no_private;
  427. }
  428. spin_lock_init(&info->lock);
  429. mutex_init(&info->deathwarrant);
  430. info->flags = 0;
  431. info->mcr = 0;
  432. INIT_WORK(&info->rx_work, rx_data_softint);
  433. info->port = port;
  434. INIT_LIST_HEAD(&info->rx_urbs_free);
  435. INIT_LIST_HEAD(&info->rx_urbs_submitted);
  436. INIT_LIST_HEAD(&info->rx_urb_q);
  437. INIT_LIST_HEAD(&info->tx_urbs_free);
  438. INIT_LIST_HEAD(&info->tx_urbs_submitted);
  439. for (j = 0; j < urb_pool_size; j++) {
  440. urb = usb_alloc_urb(0, GFP_KERNEL);
  441. if (!urb) {
  442. dev_err(&port->dev, "No free urbs available\n");
  443. goto no_rx_urb;
  444. }
  445. buf_size = port->read_urb->transfer_buffer_length;
  446. urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
  447. if (!urb->transfer_buffer) {
  448. dev_err(&port->dev,
  449. "Couldn't allocate urb buffer\n");
  450. goto no_rx_buf;
  451. }
  452. wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
  453. if (!wrap) {
  454. dev_err(&port->dev,
  455. "Couldn't allocate urb wrapper\n");
  456. goto no_rx_wrap;
  457. }
  458. usb_fill_bulk_urb(urb, serial->dev,
  459. usb_rcvbulkpipe(serial->dev,
  460. port->bulk_in_endpointAddress),
  461. urb->transfer_buffer, buf_size,
  462. whiteheat_read_callback, port);
  463. wrap->urb = urb;
  464. list_add(&wrap->list, &info->rx_urbs_free);
  465. urb = usb_alloc_urb(0, GFP_KERNEL);
  466. if (!urb) {
  467. dev_err(&port->dev, "No free urbs available\n");
  468. goto no_tx_urb;
  469. }
  470. buf_size = port->write_urb->transfer_buffer_length;
  471. urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
  472. if (!urb->transfer_buffer) {
  473. dev_err(&port->dev,
  474. "Couldn't allocate urb buffer\n");
  475. goto no_tx_buf;
  476. }
  477. wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
  478. if (!wrap) {
  479. dev_err(&port->dev,
  480. "Couldn't allocate urb wrapper\n");
  481. goto no_tx_wrap;
  482. }
  483. usb_fill_bulk_urb(urb, serial->dev,
  484. usb_sndbulkpipe(serial->dev,
  485. port->bulk_out_endpointAddress),
  486. urb->transfer_buffer, buf_size,
  487. whiteheat_write_callback, port);
  488. wrap->urb = urb;
  489. list_add(&wrap->list, &info->tx_urbs_free);
  490. }
  491. usb_set_serial_port_data(port, info);
  492. }
  493. command_info = kmalloc(sizeof(struct whiteheat_command_private),
  494. GFP_KERNEL);
  495. if (command_info == NULL) {
  496. dev_err(&serial->dev->dev,
  497. "%s: Out of memory for port structures\n",
  498. serial->type->description);
  499. goto no_command_private;
  500. }
  501. mutex_init(&command_info->mutex);
  502. command_info->port_running = 0;
  503. init_waitqueue_head(&command_info->wait_command);
  504. usb_set_serial_port_data(command_port, command_info);
  505. command_port->write_urb->complete = command_port_write_callback;
  506. command_port->read_urb->complete = command_port_read_callback;
  507. kfree(result);
  508. kfree(command);
  509. return 0;
  510. no_firmware:
  511. /* Firmware likely not running */
  512. dev_err(&serial->dev->dev,
  513. "%s: Unable to retrieve firmware version, try replugging\n",
  514. serial->type->description);
  515. dev_err(&serial->dev->dev,
  516. "%s: If the firmware is not running (status led not blinking)\n",
  517. serial->type->description);
  518. dev_err(&serial->dev->dev,
  519. "%s: please contact support@connecttech.com\n",
  520. serial->type->description);
  521. kfree(result);
  522. return -ENODEV;
  523. no_command_private:
  524. for (i = serial->num_ports - 1; i >= 0; i--) {
  525. port = serial->port[i];
  526. info = usb_get_serial_port_data(port);
  527. for (j = urb_pool_size - 1; j >= 0; j--) {
  528. tmp = list_first(&info->tx_urbs_free);
  529. list_del(tmp);
  530. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  531. urb = wrap->urb;
  532. kfree(wrap);
  533. no_tx_wrap:
  534. kfree(urb->transfer_buffer);
  535. no_tx_buf:
  536. usb_free_urb(urb);
  537. no_tx_urb:
  538. tmp = list_first(&info->rx_urbs_free);
  539. list_del(tmp);
  540. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  541. urb = wrap->urb;
  542. kfree(wrap);
  543. no_rx_wrap:
  544. kfree(urb->transfer_buffer);
  545. no_rx_buf:
  546. usb_free_urb(urb);
  547. no_rx_urb:
  548. ;
  549. }
  550. kfree(info);
  551. no_private:
  552. ;
  553. }
  554. kfree(result);
  555. no_result_buffer:
  556. kfree(command);
  557. no_command_buffer:
  558. return -ENOMEM;
  559. }
  560. static void whiteheat_shutdown(struct usb_serial *serial)
  561. {
  562. struct usb_serial_port *command_port;
  563. struct usb_serial_port *port;
  564. struct whiteheat_private *info;
  565. struct whiteheat_urb_wrap *wrap;
  566. struct urb *urb;
  567. struct list_head *tmp;
  568. struct list_head *tmp2;
  569. int i;
  570. dbg("%s", __func__);
  571. /* free up our private data for our command port */
  572. command_port = serial->port[COMMAND_PORT];
  573. kfree(usb_get_serial_port_data(command_port));
  574. for (i = 0; i < serial->num_ports; i++) {
  575. port = serial->port[i];
  576. info = usb_get_serial_port_data(port);
  577. list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
  578. list_del(tmp);
  579. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  580. urb = wrap->urb;
  581. kfree(wrap);
  582. kfree(urb->transfer_buffer);
  583. usb_free_urb(urb);
  584. }
  585. list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
  586. list_del(tmp);
  587. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  588. urb = wrap->urb;
  589. kfree(wrap);
  590. kfree(urb->transfer_buffer);
  591. usb_free_urb(urb);
  592. }
  593. kfree(info);
  594. }
  595. return;
  596. }
  597. static int whiteheat_open(struct tty_struct *tty,
  598. struct usb_serial_port *port, struct file *filp)
  599. {
  600. int retval = 0;
  601. dbg("%s - port %d", __func__, port->number);
  602. retval = start_command_port(port->serial);
  603. if (retval)
  604. goto exit;
  605. if (tty)
  606. tty->low_latency = 1;
  607. /* send an open port command */
  608. retval = firm_open(port);
  609. if (retval) {
  610. stop_command_port(port->serial);
  611. goto exit;
  612. }
  613. retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
  614. if (retval) {
  615. firm_close(port);
  616. stop_command_port(port->serial);
  617. goto exit;
  618. }
  619. if (tty)
  620. firm_setup_port(tty);
  621. /* Work around HCD bugs */
  622. usb_clear_halt(port->serial->dev, port->read_urb->pipe);
  623. usb_clear_halt(port->serial->dev, port->write_urb->pipe);
  624. /* Start reading from the device */
  625. retval = start_port_read(port);
  626. if (retval) {
  627. dev_err(&port->dev,
  628. "%s - failed submitting read urb, error %d\n",
  629. __func__, retval);
  630. firm_close(port);
  631. stop_command_port(port->serial);
  632. goto exit;
  633. }
  634. exit:
  635. dbg("%s - exit, retval = %d", __func__, retval);
  636. return retval;
  637. }
  638. static void whiteheat_close(struct tty_struct *tty,
  639. struct usb_serial_port *port, struct file *filp)
  640. {
  641. struct whiteheat_private *info = usb_get_serial_port_data(port);
  642. struct whiteheat_urb_wrap *wrap;
  643. struct urb *urb;
  644. struct list_head *tmp;
  645. struct list_head *tmp2;
  646. dbg("%s - port %d", __func__, port->number);
  647. mutex_lock(&port->serial->disc_mutex);
  648. /* filp is NULL when called from usb_serial_disconnect */
  649. if ((filp && (tty_hung_up_p(filp))) || port->serial->disconnected) {
  650. mutex_unlock(&port->serial->disc_mutex);
  651. return;
  652. }
  653. mutex_unlock(&port->serial->disc_mutex);
  654. tty->closing = 1;
  655. /*
  656. * Not currently in use; tty_wait_until_sent() calls
  657. * serial_chars_in_buffer() which deadlocks on the second semaphore
  658. * acquisition. This should be fixed at some point. Greg's been
  659. * notified.
  660. if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
  661. tty_wait_until_sent(tty, CLOSING_DELAY);
  662. }
  663. */
  664. tty_driver_flush_buffer(tty);
  665. tty_ldisc_flush(tty);
  666. firm_report_tx_done(port);
  667. firm_close(port);
  668. /* shutdown our bulk reads and writes */
  669. mutex_lock(&info->deathwarrant);
  670. spin_lock_irq(&info->lock);
  671. list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
  672. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  673. urb = wrap->urb;
  674. list_del(tmp);
  675. spin_unlock_irq(&info->lock);
  676. usb_kill_urb(urb);
  677. spin_lock_irq(&info->lock);
  678. list_add(tmp, &info->rx_urbs_free);
  679. }
  680. list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
  681. list_move(tmp, &info->rx_urbs_free);
  682. list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
  683. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  684. urb = wrap->urb;
  685. list_del(tmp);
  686. spin_unlock_irq(&info->lock);
  687. usb_kill_urb(urb);
  688. spin_lock_irq(&info->lock);
  689. list_add(tmp, &info->tx_urbs_free);
  690. }
  691. spin_unlock_irq(&info->lock);
  692. mutex_unlock(&info->deathwarrant);
  693. stop_command_port(port->serial);
  694. tty->closing = 0;
  695. }
  696. static int whiteheat_write(struct tty_struct *tty,
  697. struct usb_serial_port *port, const unsigned char *buf, int count)
  698. {
  699. struct usb_serial *serial = port->serial;
  700. struct whiteheat_private *info = usb_get_serial_port_data(port);
  701. struct whiteheat_urb_wrap *wrap;
  702. struct urb *urb;
  703. int result;
  704. int bytes;
  705. int sent = 0;
  706. unsigned long flags;
  707. struct list_head *tmp;
  708. dbg("%s - port %d", __func__, port->number);
  709. if (count == 0) {
  710. dbg("%s - write request of 0 bytes", __func__);
  711. return (0);
  712. }
  713. while (count) {
  714. spin_lock_irqsave(&info->lock, flags);
  715. if (list_empty(&info->tx_urbs_free)) {
  716. spin_unlock_irqrestore(&info->lock, flags);
  717. break;
  718. }
  719. tmp = list_first(&info->tx_urbs_free);
  720. list_del(tmp);
  721. spin_unlock_irqrestore(&info->lock, flags);
  722. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  723. urb = wrap->urb;
  724. bytes = (count > port->bulk_out_size) ?
  725. port->bulk_out_size : count;
  726. memcpy(urb->transfer_buffer, buf + sent, bytes);
  727. usb_serial_debug_data(debug, &port->dev,
  728. __func__, bytes, urb->transfer_buffer);
  729. urb->dev = serial->dev;
  730. urb->transfer_buffer_length = bytes;
  731. result = usb_submit_urb(urb, GFP_ATOMIC);
  732. if (result) {
  733. dev_err(&port->dev,
  734. "%s - failed submitting write urb, error %d\n",
  735. __func__, result);
  736. sent = result;
  737. spin_lock_irqsave(&info->lock, flags);
  738. list_add(tmp, &info->tx_urbs_free);
  739. spin_unlock_irqrestore(&info->lock, flags);
  740. break;
  741. } else {
  742. sent += bytes;
  743. count -= bytes;
  744. spin_lock_irqsave(&info->lock, flags);
  745. list_add(tmp, &info->tx_urbs_submitted);
  746. spin_unlock_irqrestore(&info->lock, flags);
  747. }
  748. }
  749. return sent;
  750. }
  751. static int whiteheat_write_room(struct tty_struct *tty)
  752. {
  753. struct usb_serial_port *port = tty->driver_data;
  754. struct whiteheat_private *info = usb_get_serial_port_data(port);
  755. struct list_head *tmp;
  756. int room = 0;
  757. unsigned long flags;
  758. dbg("%s - port %d", __func__, port->number);
  759. spin_lock_irqsave(&info->lock, flags);
  760. list_for_each(tmp, &info->tx_urbs_free)
  761. room++;
  762. spin_unlock_irqrestore(&info->lock, flags);
  763. room *= port->bulk_out_size;
  764. dbg("%s - returns %d", __func__, room);
  765. return (room);
  766. }
  767. static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file)
  768. {
  769. struct usb_serial_port *port = tty->driver_data;
  770. struct whiteheat_private *info = usb_get_serial_port_data(port);
  771. unsigned int modem_signals = 0;
  772. dbg("%s - port %d", __func__, port->number);
  773. firm_get_dtr_rts(port);
  774. if (info->mcr & UART_MCR_DTR)
  775. modem_signals |= TIOCM_DTR;
  776. if (info->mcr & UART_MCR_RTS)
  777. modem_signals |= TIOCM_RTS;
  778. return modem_signals;
  779. }
  780. static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
  781. unsigned int set, unsigned int clear)
  782. {
  783. struct usb_serial_port *port = tty->driver_data;
  784. struct whiteheat_private *info = usb_get_serial_port_data(port);
  785. dbg("%s - port %d", __func__, port->number);
  786. if (set & TIOCM_RTS)
  787. info->mcr |= UART_MCR_RTS;
  788. if (set & TIOCM_DTR)
  789. info->mcr |= UART_MCR_DTR;
  790. if (clear & TIOCM_RTS)
  791. info->mcr &= ~UART_MCR_RTS;
  792. if (clear & TIOCM_DTR)
  793. info->mcr &= ~UART_MCR_DTR;
  794. firm_set_dtr(port, info->mcr & UART_MCR_DTR);
  795. firm_set_rts(port, info->mcr & UART_MCR_RTS);
  796. return 0;
  797. }
  798. static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
  799. unsigned int cmd, unsigned long arg)
  800. {
  801. struct usb_serial_port *port = tty->driver_data;
  802. struct serial_struct serstruct;
  803. void __user *user_arg = (void __user *)arg;
  804. dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
  805. switch (cmd) {
  806. case TIOCGSERIAL:
  807. memset(&serstruct, 0, sizeof(serstruct));
  808. serstruct.type = PORT_16654;
  809. serstruct.line = port->serial->minor;
  810. serstruct.port = port->number;
  811. serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  812. serstruct.xmit_fifo_size = port->bulk_out_size;
  813. serstruct.custom_divisor = 0;
  814. serstruct.baud_base = 460800;
  815. serstruct.close_delay = CLOSING_DELAY;
  816. serstruct.closing_wait = CLOSING_DELAY;
  817. if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
  818. return -EFAULT;
  819. break;
  820. default:
  821. break;
  822. }
  823. return -ENOIOCTLCMD;
  824. }
  825. static void whiteheat_set_termios(struct tty_struct *tty,
  826. struct usb_serial_port *port, struct ktermios *old_termios)
  827. {
  828. firm_setup_port(tty);
  829. }
  830. static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
  831. {
  832. struct usb_serial_port *port = tty->driver_data;
  833. firm_set_break(port, break_state);
  834. }
  835. static int whiteheat_chars_in_buffer(struct tty_struct *tty)
  836. {
  837. struct usb_serial_port *port = tty->driver_data;
  838. struct whiteheat_private *info = usb_get_serial_port_data(port);
  839. struct list_head *tmp;
  840. struct whiteheat_urb_wrap *wrap;
  841. int chars = 0;
  842. unsigned long flags;
  843. dbg("%s - port %d", __func__, port->number);
  844. spin_lock_irqsave(&info->lock, flags);
  845. list_for_each(tmp, &info->tx_urbs_submitted) {
  846. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  847. chars += wrap->urb->transfer_buffer_length;
  848. }
  849. spin_unlock_irqrestore(&info->lock, flags);
  850. dbg("%s - returns %d", __func__, chars);
  851. return chars;
  852. }
  853. static void whiteheat_throttle(struct tty_struct *tty)
  854. {
  855. struct usb_serial_port *port = tty->driver_data;
  856. struct whiteheat_private *info = usb_get_serial_port_data(port);
  857. unsigned long flags;
  858. dbg("%s - port %d", __func__, port->number);
  859. spin_lock_irqsave(&info->lock, flags);
  860. info->flags |= THROTTLED;
  861. spin_unlock_irqrestore(&info->lock, flags);
  862. return;
  863. }
  864. static void whiteheat_unthrottle(struct tty_struct *tty)
  865. {
  866. struct usb_serial_port *port = tty->driver_data;
  867. struct whiteheat_private *info = usb_get_serial_port_data(port);
  868. int actually_throttled;
  869. unsigned long flags;
  870. dbg("%s - port %d", __func__, port->number);
  871. spin_lock_irqsave(&info->lock, flags);
  872. actually_throttled = info->flags & ACTUALLY_THROTTLED;
  873. info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  874. spin_unlock_irqrestore(&info->lock, flags);
  875. if (actually_throttled)
  876. rx_data_softint(&info->rx_work);
  877. return;
  878. }
  879. /*****************************************************************************
  880. * Connect Tech's White Heat callback routines
  881. *****************************************************************************/
  882. static void command_port_write_callback(struct urb *urb)
  883. {
  884. int status = urb->status;
  885. dbg("%s", __func__);
  886. if (status) {
  887. dbg("nonzero urb status: %d", status);
  888. return;
  889. }
  890. }
  891. static void command_port_read_callback(struct urb *urb)
  892. {
  893. struct usb_serial_port *command_port = urb->context;
  894. struct whiteheat_command_private *command_info;
  895. int status = urb->status;
  896. unsigned char *data = urb->transfer_buffer;
  897. int result;
  898. dbg("%s", __func__);
  899. command_info = usb_get_serial_port_data(command_port);
  900. if (!command_info) {
  901. dbg("%s - command_info is NULL, exiting.", __func__);
  902. return;
  903. }
  904. if (status) {
  905. dbg("%s - nonzero urb status: %d", __func__, status);
  906. if (status != -ENOENT)
  907. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  908. wake_up(&command_info->wait_command);
  909. return;
  910. }
  911. usb_serial_debug_data(debug, &command_port->dev,
  912. __func__, urb->actual_length, data);
  913. if (data[0] == WHITEHEAT_CMD_COMPLETE) {
  914. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  915. wake_up(&command_info->wait_command);
  916. } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
  917. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  918. wake_up(&command_info->wait_command);
  919. } else if (data[0] == WHITEHEAT_EVENT) {
  920. /* These are unsolicited reports from the firmware, hence no
  921. waiting command to wakeup */
  922. dbg("%s - event received", __func__);
  923. } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
  924. memcpy(command_info->result_buffer, &data[1],
  925. urb->actual_length - 1);
  926. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  927. wake_up(&command_info->wait_command);
  928. } else
  929. dbg("%s - bad reply from firmware", __func__);
  930. /* Continue trying to always read */
  931. command_port->read_urb->dev = command_port->serial->dev;
  932. result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
  933. if (result)
  934. dbg("%s - failed resubmitting read urb, error %d",
  935. __func__, result);
  936. }
  937. static void whiteheat_read_callback(struct urb *urb)
  938. {
  939. struct usb_serial_port *port = urb->context;
  940. struct whiteheat_urb_wrap *wrap;
  941. unsigned char *data = urb->transfer_buffer;
  942. struct whiteheat_private *info = usb_get_serial_port_data(port);
  943. int status = urb->status;
  944. dbg("%s - port %d", __func__, port->number);
  945. spin_lock(&info->lock);
  946. wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
  947. if (!wrap) {
  948. spin_unlock(&info->lock);
  949. dev_err(&port->dev, "%s - Not my urb!\n", __func__);
  950. return;
  951. }
  952. list_del(&wrap->list);
  953. spin_unlock(&info->lock);
  954. if (status) {
  955. dbg("%s - nonzero read bulk status received: %d",
  956. __func__, status);
  957. spin_lock(&info->lock);
  958. list_add(&wrap->list, &info->rx_urbs_free);
  959. spin_unlock(&info->lock);
  960. return;
  961. }
  962. usb_serial_debug_data(debug, &port->dev,
  963. __func__, urb->actual_length, data);
  964. spin_lock(&info->lock);
  965. list_add_tail(&wrap->list, &info->rx_urb_q);
  966. if (info->flags & THROTTLED) {
  967. info->flags |= ACTUALLY_THROTTLED;
  968. spin_unlock(&info->lock);
  969. return;
  970. }
  971. spin_unlock(&info->lock);
  972. schedule_work(&info->rx_work);
  973. }
  974. static void whiteheat_write_callback(struct urb *urb)
  975. {
  976. struct usb_serial_port *port = urb->context;
  977. struct whiteheat_private *info = usb_get_serial_port_data(port);
  978. struct whiteheat_urb_wrap *wrap;
  979. int status = urb->status;
  980. dbg("%s - port %d", __func__, port->number);
  981. spin_lock(&info->lock);
  982. wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
  983. if (!wrap) {
  984. spin_unlock(&info->lock);
  985. dev_err(&port->dev, "%s - Not my urb!\n", __func__);
  986. return;
  987. }
  988. list_move(&wrap->list, &info->tx_urbs_free);
  989. spin_unlock(&info->lock);
  990. if (status) {
  991. dbg("%s - nonzero write bulk status received: %d",
  992. __func__, status);
  993. return;
  994. }
  995. usb_serial_port_softint(port);
  996. }
  997. /*****************************************************************************
  998. * Connect Tech's White Heat firmware interface
  999. *****************************************************************************/
  1000. static int firm_send_command(struct usb_serial_port *port, __u8 command,
  1001. __u8 *data, __u8 datasize)
  1002. {
  1003. struct usb_serial_port *command_port;
  1004. struct whiteheat_command_private *command_info;
  1005. struct whiteheat_private *info;
  1006. __u8 *transfer_buffer;
  1007. int retval = 0;
  1008. int t;
  1009. dbg("%s - command %d", __func__, command);
  1010. command_port = port->serial->port[COMMAND_PORT];
  1011. command_info = usb_get_serial_port_data(command_port);
  1012. mutex_lock(&command_info->mutex);
  1013. command_info->command_finished = false;
  1014. transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
  1015. transfer_buffer[0] = command;
  1016. memcpy(&transfer_buffer[1], data, datasize);
  1017. command_port->write_urb->transfer_buffer_length = datasize + 1;
  1018. command_port->write_urb->dev = port->serial->dev;
  1019. retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
  1020. if (retval) {
  1021. dbg("%s - submit urb failed", __func__);
  1022. goto exit;
  1023. }
  1024. /* wait for the command to complete */
  1025. t = wait_event_timeout(command_info->wait_command,
  1026. (bool)command_info->command_finished, COMMAND_TIMEOUT);
  1027. if (!t)
  1028. usb_kill_urb(command_port->write_urb);
  1029. if (command_info->command_finished == false) {
  1030. dbg("%s - command timed out.", __func__);
  1031. retval = -ETIMEDOUT;
  1032. goto exit;
  1033. }
  1034. if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
  1035. dbg("%s - command failed.", __func__);
  1036. retval = -EIO;
  1037. goto exit;
  1038. }
  1039. if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
  1040. dbg("%s - command completed.", __func__);
  1041. switch (command) {
  1042. case WHITEHEAT_GET_DTR_RTS:
  1043. info = usb_get_serial_port_data(port);
  1044. memcpy(&info->mcr, command_info->result_buffer,
  1045. sizeof(struct whiteheat_dr_info));
  1046. break;
  1047. }
  1048. }
  1049. exit:
  1050. mutex_unlock(&command_info->mutex);
  1051. return retval;
  1052. }
  1053. static int firm_open(struct usb_serial_port *port)
  1054. {
  1055. struct whiteheat_simple open_command;
  1056. open_command.port = port->number - port->serial->minor + 1;
  1057. return firm_send_command(port, WHITEHEAT_OPEN,
  1058. (__u8 *)&open_command, sizeof(open_command));
  1059. }
  1060. static int firm_close(struct usb_serial_port *port)
  1061. {
  1062. struct whiteheat_simple close_command;
  1063. close_command.port = port->number - port->serial->minor + 1;
  1064. return firm_send_command(port, WHITEHEAT_CLOSE,
  1065. (__u8 *)&close_command, sizeof(close_command));
  1066. }
  1067. static int firm_setup_port(struct tty_struct *tty)
  1068. {
  1069. struct usb_serial_port *port = tty->driver_data;
  1070. struct whiteheat_port_settings port_settings;
  1071. unsigned int cflag = tty->termios->c_cflag;
  1072. port_settings.port = port->number + 1;
  1073. /* get the byte size */
  1074. switch (cflag & CSIZE) {
  1075. case CS5: port_settings.bits = 5; break;
  1076. case CS6: port_settings.bits = 6; break;
  1077. case CS7: port_settings.bits = 7; break;
  1078. default:
  1079. case CS8: port_settings.bits = 8; break;
  1080. }
  1081. dbg("%s - data bits = %d", __func__, port_settings.bits);
  1082. /* determine the parity */
  1083. if (cflag & PARENB)
  1084. if (cflag & CMSPAR)
  1085. if (cflag & PARODD)
  1086. port_settings.parity = WHITEHEAT_PAR_MARK;
  1087. else
  1088. port_settings.parity = WHITEHEAT_PAR_SPACE;
  1089. else
  1090. if (cflag & PARODD)
  1091. port_settings.parity = WHITEHEAT_PAR_ODD;
  1092. else
  1093. port_settings.parity = WHITEHEAT_PAR_EVEN;
  1094. else
  1095. port_settings.parity = WHITEHEAT_PAR_NONE;
  1096. dbg("%s - parity = %c", __func__, port_settings.parity);
  1097. /* figure out the stop bits requested */
  1098. if (cflag & CSTOPB)
  1099. port_settings.stop = 2;
  1100. else
  1101. port_settings.stop = 1;
  1102. dbg("%s - stop bits = %d", __func__, port_settings.stop);
  1103. /* figure out the flow control settings */
  1104. if (cflag & CRTSCTS)
  1105. port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
  1106. WHITEHEAT_HFLOW_RTS);
  1107. else
  1108. port_settings.hflow = WHITEHEAT_HFLOW_NONE;
  1109. dbg("%s - hardware flow control = %s %s %s %s", __func__,
  1110. (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
  1111. (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
  1112. (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
  1113. (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
  1114. /* determine software flow control */
  1115. if (I_IXOFF(tty))
  1116. port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
  1117. else
  1118. port_settings.sflow = WHITEHEAT_SFLOW_NONE;
  1119. dbg("%s - software flow control = %c", __func__, port_settings.sflow);
  1120. port_settings.xon = START_CHAR(tty);
  1121. port_settings.xoff = STOP_CHAR(tty);
  1122. dbg("%s - XON = %2x, XOFF = %2x",
  1123. __func__, port_settings.xon, port_settings.xoff);
  1124. /* get the baud rate wanted */
  1125. port_settings.baud = tty_get_baud_rate(tty);
  1126. dbg("%s - baud rate = %d", __func__, port_settings.baud);
  1127. /* fixme: should set validated settings */
  1128. tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
  1129. /* handle any settings that aren't specified in the tty structure */
  1130. port_settings.lloop = 0;
  1131. /* now send the message to the device */
  1132. return firm_send_command(port, WHITEHEAT_SETUP_PORT,
  1133. (__u8 *)&port_settings, sizeof(port_settings));
  1134. }
  1135. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
  1136. {
  1137. struct whiteheat_set_rdb rts_command;
  1138. rts_command.port = port->number - port->serial->minor + 1;
  1139. rts_command.state = onoff;
  1140. return firm_send_command(port, WHITEHEAT_SET_RTS,
  1141. (__u8 *)&rts_command, sizeof(rts_command));
  1142. }
  1143. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
  1144. {
  1145. struct whiteheat_set_rdb dtr_command;
  1146. dtr_command.port = port->number - port->serial->minor + 1;
  1147. dtr_command.state = onoff;
  1148. return firm_send_command(port, WHITEHEAT_SET_DTR,
  1149. (__u8 *)&dtr_command, sizeof(dtr_command));
  1150. }
  1151. static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
  1152. {
  1153. struct whiteheat_set_rdb break_command;
  1154. break_command.port = port->number - port->serial->minor + 1;
  1155. break_command.state = onoff;
  1156. return firm_send_command(port, WHITEHEAT_SET_BREAK,
  1157. (__u8 *)&break_command, sizeof(break_command));
  1158. }
  1159. static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
  1160. {
  1161. struct whiteheat_purge purge_command;
  1162. purge_command.port = port->number - port->serial->minor + 1;
  1163. purge_command.what = rxtx;
  1164. return firm_send_command(port, WHITEHEAT_PURGE,
  1165. (__u8 *)&purge_command, sizeof(purge_command));
  1166. }
  1167. static int firm_get_dtr_rts(struct usb_serial_port *port)
  1168. {
  1169. struct whiteheat_simple get_dr_command;
  1170. get_dr_command.port = port->number - port->serial->minor + 1;
  1171. return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
  1172. (__u8 *)&get_dr_command, sizeof(get_dr_command));
  1173. }
  1174. static int firm_report_tx_done(struct usb_serial_port *port)
  1175. {
  1176. struct whiteheat_simple close_command;
  1177. close_command.port = port->number - port->serial->minor + 1;
  1178. return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
  1179. (__u8 *)&close_command, sizeof(close_command));
  1180. }
  1181. /*****************************************************************************
  1182. * Connect Tech's White Heat utility functions
  1183. *****************************************************************************/
  1184. static int start_command_port(struct usb_serial *serial)
  1185. {
  1186. struct usb_serial_port *command_port;
  1187. struct whiteheat_command_private *command_info;
  1188. int retval = 0;
  1189. command_port = serial->port[COMMAND_PORT];
  1190. command_info = usb_get_serial_port_data(command_port);
  1191. mutex_lock(&command_info->mutex);
  1192. if (!command_info->port_running) {
  1193. /* Work around HCD bugs */
  1194. usb_clear_halt(serial->dev, command_port->read_urb->pipe);
  1195. command_port->read_urb->dev = serial->dev;
  1196. retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
  1197. if (retval) {
  1198. dev_err(&serial->dev->dev,
  1199. "%s - failed submitting read urb, error %d\n",
  1200. __func__, retval);
  1201. goto exit;
  1202. }
  1203. }
  1204. command_info->port_running++;
  1205. exit:
  1206. mutex_unlock(&command_info->mutex);
  1207. return retval;
  1208. }
  1209. static void stop_command_port(struct usb_serial *serial)
  1210. {
  1211. struct usb_serial_port *command_port;
  1212. struct whiteheat_command_private *command_info;
  1213. command_port = serial->port[COMMAND_PORT];
  1214. command_info = usb_get_serial_port_data(command_port);
  1215. mutex_lock(&command_info->mutex);
  1216. command_info->port_running--;
  1217. if (!command_info->port_running)
  1218. usb_kill_urb(command_port->read_urb);
  1219. mutex_unlock(&command_info->mutex);
  1220. }
  1221. static int start_port_read(struct usb_serial_port *port)
  1222. {
  1223. struct whiteheat_private *info = usb_get_serial_port_data(port);
  1224. struct whiteheat_urb_wrap *wrap;
  1225. struct urb *urb;
  1226. int retval = 0;
  1227. unsigned long flags;
  1228. struct list_head *tmp;
  1229. struct list_head *tmp2;
  1230. spin_lock_irqsave(&info->lock, flags);
  1231. list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
  1232. list_del(tmp);
  1233. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1234. urb = wrap->urb;
  1235. urb->dev = port->serial->dev;
  1236. spin_unlock_irqrestore(&info->lock, flags);
  1237. retval = usb_submit_urb(urb, GFP_KERNEL);
  1238. if (retval) {
  1239. spin_lock_irqsave(&info->lock, flags);
  1240. list_add(tmp, &info->rx_urbs_free);
  1241. list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
  1242. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1243. urb = wrap->urb;
  1244. list_del(tmp);
  1245. spin_unlock_irqrestore(&info->lock, flags);
  1246. usb_kill_urb(urb);
  1247. spin_lock_irqsave(&info->lock, flags);
  1248. list_add(tmp, &info->rx_urbs_free);
  1249. }
  1250. break;
  1251. }
  1252. spin_lock_irqsave(&info->lock, flags);
  1253. list_add(tmp, &info->rx_urbs_submitted);
  1254. }
  1255. spin_unlock_irqrestore(&info->lock, flags);
  1256. return retval;
  1257. }
  1258. static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
  1259. struct list_head *head)
  1260. {
  1261. struct whiteheat_urb_wrap *wrap;
  1262. struct list_head *tmp;
  1263. list_for_each(tmp, head) {
  1264. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1265. if (wrap->urb == urb)
  1266. return wrap;
  1267. }
  1268. return NULL;
  1269. }
  1270. static struct list_head *list_first(struct list_head *head)
  1271. {
  1272. return head->next;
  1273. }
  1274. static void rx_data_softint(struct work_struct *work)
  1275. {
  1276. struct whiteheat_private *info =
  1277. container_of(work, struct whiteheat_private, rx_work);
  1278. struct usb_serial_port *port = info->port;
  1279. struct tty_struct *tty = tty_port_tty_get(&port->port);
  1280. struct whiteheat_urb_wrap *wrap;
  1281. struct urb *urb;
  1282. unsigned long flags;
  1283. struct list_head *tmp;
  1284. struct list_head *tmp2;
  1285. int result;
  1286. int sent = 0;
  1287. spin_lock_irqsave(&info->lock, flags);
  1288. if (info->flags & THROTTLED) {
  1289. spin_unlock_irqrestore(&info->lock, flags);
  1290. goto out;
  1291. }
  1292. list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
  1293. list_del(tmp);
  1294. spin_unlock_irqrestore(&info->lock, flags);
  1295. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1296. urb = wrap->urb;
  1297. if (tty && urb->actual_length) {
  1298. int len = tty_buffer_request_room(tty,
  1299. urb->actual_length);
  1300. /* This stuff can go away now I suspect */
  1301. if (unlikely(len < urb->actual_length)) {
  1302. spin_lock_irqsave(&info->lock, flags);
  1303. list_add(tmp, &info->rx_urb_q);
  1304. spin_unlock_irqrestore(&info->lock, flags);
  1305. tty_flip_buffer_push(tty);
  1306. schedule_work(&info->rx_work);
  1307. goto out;
  1308. }
  1309. tty_insert_flip_string(tty, urb->transfer_buffer, len);
  1310. sent += len;
  1311. }
  1312. urb->dev = port->serial->dev;
  1313. result = usb_submit_urb(urb, GFP_ATOMIC);
  1314. if (result) {
  1315. dev_err(&port->dev,
  1316. "%s - failed resubmitting read urb, error %d\n",
  1317. __func__, result);
  1318. spin_lock_irqsave(&info->lock, flags);
  1319. list_add(tmp, &info->rx_urbs_free);
  1320. continue;
  1321. }
  1322. spin_lock_irqsave(&info->lock, flags);
  1323. list_add(tmp, &info->rx_urbs_submitted);
  1324. }
  1325. spin_unlock_irqrestore(&info->lock, flags);
  1326. if (sent)
  1327. tty_flip_buffer_push(tty);
  1328. out:
  1329. tty_kref_put(tty);
  1330. }
  1331. /*****************************************************************************
  1332. * Connect Tech's White Heat module functions
  1333. *****************************************************************************/
  1334. static int __init whiteheat_init(void)
  1335. {
  1336. int retval;
  1337. retval = usb_serial_register(&whiteheat_fake_device);
  1338. if (retval)
  1339. goto failed_fake_register;
  1340. retval = usb_serial_register(&whiteheat_device);
  1341. if (retval)
  1342. goto failed_device_register;
  1343. retval = usb_register(&whiteheat_driver);
  1344. if (retval)
  1345. goto failed_usb_register;
  1346. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1347. DRIVER_DESC "\n");
  1348. return 0;
  1349. failed_usb_register:
  1350. usb_serial_deregister(&whiteheat_device);
  1351. failed_device_register:
  1352. usb_serial_deregister(&whiteheat_fake_device);
  1353. failed_fake_register:
  1354. return retval;
  1355. }
  1356. static void __exit whiteheat_exit(void)
  1357. {
  1358. usb_deregister(&whiteheat_driver);
  1359. usb_serial_deregister(&whiteheat_fake_device);
  1360. usb_serial_deregister(&whiteheat_device);
  1361. }
  1362. module_init(whiteheat_init);
  1363. module_exit(whiteheat_exit);
  1364. MODULE_AUTHOR(DRIVER_AUTHOR);
  1365. MODULE_DESCRIPTION(DRIVER_DESC);
  1366. MODULE_LICENSE("GPL");
  1367. MODULE_FIRMWARE("whiteheat.fw");
  1368. MODULE_FIRMWARE("whiteheat_loader.fw");
  1369. module_param(urb_pool_size, int, 0);
  1370. MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
  1371. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1372. MODULE_PARM_DESC(debug, "Debug enabled or not");