whiteheat.c 44 KB

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