whiteheat.c 42 KB

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