whiteheat.c 44 KB

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