cypress_m8.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. /*
  2. * USB Cypress M8 driver
  3. *
  4. * Copyright (C) 2004
  5. * Lonnie Mendez (dignome@gmail.com)
  6. * Copyright (C) 2003,2004
  7. * Neil Whelchel (koyama@firstlight.net)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * See Documentation/usb/usb-serial.txt for more information on using this
  15. * driver
  16. *
  17. * See http://geocities.com/i0xox0i for information on this driver and the
  18. * earthmate usb device.
  19. */
  20. /* Thanks to Neil Whelchel for writing the first cypress m8 implementation
  21. for linux. */
  22. /* Thanks to cypress for providing references for the hid reports. */
  23. /* Thanks to Jiang Zhang for providing links and for general help. */
  24. /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
  25. #include <linux/kernel.h>
  26. #include <linux/errno.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/tty.h>
  30. #include <linux/tty_driver.h>
  31. #include <linux/tty_flip.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/usb.h>
  36. #include <linux/usb/serial.h>
  37. #include <linux/serial.h>
  38. #include <linux/kfifo.h>
  39. #include <linux/delay.h>
  40. #include <linux/uaccess.h>
  41. #include <asm/unaligned.h>
  42. #include "cypress_m8.h"
  43. static bool debug;
  44. static bool stats;
  45. static int interval;
  46. static bool unstable_bauds;
  47. /*
  48. * Version Information
  49. */
  50. #define DRIVER_VERSION "v1.10"
  51. #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
  52. #define DRIVER_DESC "Cypress USB to Serial Driver"
  53. /* write buffer size defines */
  54. #define CYPRESS_BUF_SIZE 1024
  55. static const struct usb_device_id id_table_earthmate[] = {
  56. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  57. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  58. { } /* Terminating entry */
  59. };
  60. static const struct usb_device_id id_table_cyphidcomrs232[] = {
  61. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  62. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  63. { } /* Terminating entry */
  64. };
  65. static const struct usb_device_id id_table_nokiaca42v2[] = {
  66. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  67. { } /* Terminating entry */
  68. };
  69. static const struct usb_device_id id_table_combined[] = {
  70. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  71. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  72. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  73. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  74. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  75. { } /* Terminating entry */
  76. };
  77. MODULE_DEVICE_TABLE(usb, id_table_combined);
  78. static struct usb_driver cypress_driver = {
  79. .name = "cypress",
  80. .probe = usb_serial_probe,
  81. .disconnect = usb_serial_disconnect,
  82. .id_table = id_table_combined,
  83. .no_dynamic_id = 1,
  84. };
  85. enum packet_format {
  86. packet_format_1, /* b0:status, b1:payload count */
  87. packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
  88. };
  89. struct cypress_private {
  90. spinlock_t lock; /* private lock */
  91. int chiptype; /* identifier of device, for quirks/etc */
  92. int bytes_in; /* used for statistics */
  93. int bytes_out; /* used for statistics */
  94. int cmd_count; /* used for statistics */
  95. int cmd_ctrl; /* always set this to 1 before issuing a command */
  96. struct kfifo write_fifo; /* write fifo */
  97. int write_urb_in_use; /* write urb in use indicator */
  98. int write_urb_interval; /* interval to use for write urb */
  99. int read_urb_interval; /* interval to use for read urb */
  100. int comm_is_ok; /* true if communication is (still) ok */
  101. int termios_initialized;
  102. __u8 line_control; /* holds dtr / rts value */
  103. __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
  104. __u8 current_config; /* stores the current configuration byte */
  105. __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
  106. enum packet_format pkt_fmt; /* format to use for packet send / receive */
  107. int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
  108. int baud_rate; /* stores current baud rate in
  109. integer form */
  110. int isthrottled; /* if throttled, discard reads */
  111. wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
  112. char prev_status, diff_status; /* used for TIOCMIWAIT */
  113. /* we pass a pointer to this as the argument sent to
  114. cypress_set_termios old_termios */
  115. struct ktermios tmp_termios; /* stores the old termios settings */
  116. };
  117. /* function prototypes for the Cypress USB to serial device */
  118. static int cypress_earthmate_startup(struct usb_serial *serial);
  119. static int cypress_hidcom_startup(struct usb_serial *serial);
  120. static int cypress_ca42v2_startup(struct usb_serial *serial);
  121. static void cypress_release(struct usb_serial *serial);
  122. static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
  123. static void cypress_close(struct usb_serial_port *port);
  124. static void cypress_dtr_rts(struct usb_serial_port *port, int on);
  125. static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
  126. const unsigned char *buf, int count);
  127. static void cypress_send(struct usb_serial_port *port);
  128. static int cypress_write_room(struct tty_struct *tty);
  129. static int cypress_ioctl(struct tty_struct *tty,
  130. unsigned int cmd, unsigned long arg);
  131. static void cypress_set_termios(struct tty_struct *tty,
  132. struct usb_serial_port *port, struct ktermios *old);
  133. static int cypress_tiocmget(struct tty_struct *tty);
  134. static int cypress_tiocmset(struct tty_struct *tty,
  135. unsigned int set, unsigned int clear);
  136. static int cypress_chars_in_buffer(struct tty_struct *tty);
  137. static void cypress_throttle(struct tty_struct *tty);
  138. static void cypress_unthrottle(struct tty_struct *tty);
  139. static void cypress_set_dead(struct usb_serial_port *port);
  140. static void cypress_read_int_callback(struct urb *urb);
  141. static void cypress_write_int_callback(struct urb *urb);
  142. static struct usb_serial_driver cypress_earthmate_device = {
  143. .driver = {
  144. .owner = THIS_MODULE,
  145. .name = "earthmate",
  146. },
  147. .description = "DeLorme Earthmate USB",
  148. .usb_driver = &cypress_driver,
  149. .id_table = id_table_earthmate,
  150. .num_ports = 1,
  151. .attach = cypress_earthmate_startup,
  152. .release = cypress_release,
  153. .open = cypress_open,
  154. .close = cypress_close,
  155. .dtr_rts = cypress_dtr_rts,
  156. .write = cypress_write,
  157. .write_room = cypress_write_room,
  158. .ioctl = cypress_ioctl,
  159. .set_termios = cypress_set_termios,
  160. .tiocmget = cypress_tiocmget,
  161. .tiocmset = cypress_tiocmset,
  162. .chars_in_buffer = cypress_chars_in_buffer,
  163. .throttle = cypress_throttle,
  164. .unthrottle = cypress_unthrottle,
  165. .read_int_callback = cypress_read_int_callback,
  166. .write_int_callback = cypress_write_int_callback,
  167. };
  168. static struct usb_serial_driver cypress_hidcom_device = {
  169. .driver = {
  170. .owner = THIS_MODULE,
  171. .name = "cyphidcom",
  172. },
  173. .description = "HID->COM RS232 Adapter",
  174. .usb_driver = &cypress_driver,
  175. .id_table = id_table_cyphidcomrs232,
  176. .num_ports = 1,
  177. .attach = cypress_hidcom_startup,
  178. .release = cypress_release,
  179. .open = cypress_open,
  180. .close = cypress_close,
  181. .dtr_rts = cypress_dtr_rts,
  182. .write = cypress_write,
  183. .write_room = cypress_write_room,
  184. .ioctl = cypress_ioctl,
  185. .set_termios = cypress_set_termios,
  186. .tiocmget = cypress_tiocmget,
  187. .tiocmset = cypress_tiocmset,
  188. .chars_in_buffer = cypress_chars_in_buffer,
  189. .throttle = cypress_throttle,
  190. .unthrottle = cypress_unthrottle,
  191. .read_int_callback = cypress_read_int_callback,
  192. .write_int_callback = cypress_write_int_callback,
  193. };
  194. static struct usb_serial_driver cypress_ca42v2_device = {
  195. .driver = {
  196. .owner = THIS_MODULE,
  197. .name = "nokiaca42v2",
  198. },
  199. .description = "Nokia CA-42 V2 Adapter",
  200. .usb_driver = &cypress_driver,
  201. .id_table = id_table_nokiaca42v2,
  202. .num_ports = 1,
  203. .attach = cypress_ca42v2_startup,
  204. .release = cypress_release,
  205. .open = cypress_open,
  206. .close = cypress_close,
  207. .dtr_rts = cypress_dtr_rts,
  208. .write = cypress_write,
  209. .write_room = cypress_write_room,
  210. .ioctl = cypress_ioctl,
  211. .set_termios = cypress_set_termios,
  212. .tiocmget = cypress_tiocmget,
  213. .tiocmset = cypress_tiocmset,
  214. .chars_in_buffer = cypress_chars_in_buffer,
  215. .throttle = cypress_throttle,
  216. .unthrottle = cypress_unthrottle,
  217. .read_int_callback = cypress_read_int_callback,
  218. .write_int_callback = cypress_write_int_callback,
  219. };
  220. /*****************************************************************************
  221. * Cypress serial helper functions
  222. *****************************************************************************/
  223. static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
  224. {
  225. struct cypress_private *priv;
  226. priv = usb_get_serial_port_data(port);
  227. if (unstable_bauds)
  228. return new_rate;
  229. /*
  230. * The general purpose firmware for the Cypress M8 allows for
  231. * a maximum speed of 57600bps (I have no idea whether DeLorme
  232. * chose to use the general purpose firmware or not), if you
  233. * need to modify this speed setting for your own project
  234. * please add your own chiptype and modify the code likewise.
  235. * The Cypress HID->COM device will work successfully up to
  236. * 115200bps (but the actual throughput is around 3kBps).
  237. */
  238. if (port->serial->dev->speed == USB_SPEED_LOW) {
  239. /*
  240. * Mike Isely <isely@pobox.com> 2-Feb-2008: The
  241. * Cypress app note that describes this mechanism
  242. * states the the low-speed part can't handle more
  243. * than 800 bytes/sec, in which case 4800 baud is the
  244. * safest speed for a part like that.
  245. */
  246. if (new_rate > 4800) {
  247. dbg("%s - failed setting baud rate, device incapable "
  248. "speed %d", __func__, new_rate);
  249. return -1;
  250. }
  251. }
  252. switch (priv->chiptype) {
  253. case CT_EARTHMATE:
  254. if (new_rate <= 600) {
  255. /* 300 and 600 baud rates are supported under
  256. * the generic firmware, but are not used with
  257. * NMEA and SiRF protocols */
  258. dbg("%s - failed setting baud rate, unsupported speed "
  259. "of %d on Earthmate GPS", __func__, new_rate);
  260. return -1;
  261. }
  262. break;
  263. default:
  264. break;
  265. }
  266. return new_rate;
  267. }
  268. /* This function can either set or retrieve the current serial line settings */
  269. static int cypress_serial_control(struct tty_struct *tty,
  270. struct usb_serial_port *port, speed_t baud_rate, int data_bits,
  271. int stop_bits, int parity_enable, int parity_type, int reset,
  272. int cypress_request_type)
  273. {
  274. int new_baudrate = 0, retval = 0, tries = 0;
  275. struct cypress_private *priv;
  276. u8 *feature_buffer;
  277. const unsigned int feature_len = 5;
  278. unsigned long flags;
  279. dbg("%s", __func__);
  280. priv = usb_get_serial_port_data(port);
  281. if (!priv->comm_is_ok)
  282. return -ENODEV;
  283. feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
  284. if (!feature_buffer)
  285. return -ENOMEM;
  286. switch (cypress_request_type) {
  287. case CYPRESS_SET_CONFIG:
  288. /* 0 means 'Hang up' so doesn't change the true bit rate */
  289. new_baudrate = priv->baud_rate;
  290. if (baud_rate && baud_rate != priv->baud_rate) {
  291. dbg("%s - baud rate is changing", __func__);
  292. retval = analyze_baud_rate(port, baud_rate);
  293. if (retval >= 0) {
  294. new_baudrate = retval;
  295. dbg("%s - New baud rate set to %d",
  296. __func__, new_baudrate);
  297. }
  298. }
  299. dbg("%s - baud rate is being sent as %d",
  300. __func__, new_baudrate);
  301. /* fill the feature_buffer with new configuration */
  302. put_unaligned_le32(new_baudrate, feature_buffer);
  303. feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
  304. /* 1 bit gap */
  305. feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
  306. feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
  307. feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
  308. /* 1 bit gap */
  309. feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
  310. dbg("%s - device is being sent this feature report:",
  311. __func__);
  312. dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
  313. feature_buffer[0], feature_buffer[1],
  314. feature_buffer[2], feature_buffer[3],
  315. feature_buffer[4]);
  316. do {
  317. retval = usb_control_msg(port->serial->dev,
  318. usb_sndctrlpipe(port->serial->dev, 0),
  319. HID_REQ_SET_REPORT,
  320. USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  321. 0x0300, 0, feature_buffer,
  322. feature_len, 500);
  323. if (tries++ >= 3)
  324. break;
  325. } while (retval != feature_len &&
  326. retval != -ENODEV);
  327. if (retval != feature_len) {
  328. dev_err(&port->dev, "%s - failed sending serial "
  329. "line settings - %d\n", __func__, retval);
  330. cypress_set_dead(port);
  331. } else {
  332. spin_lock_irqsave(&priv->lock, flags);
  333. priv->baud_rate = new_baudrate;
  334. priv->current_config = feature_buffer[4];
  335. spin_unlock_irqrestore(&priv->lock, flags);
  336. /* If we asked for a speed change encode it */
  337. if (baud_rate)
  338. tty_encode_baud_rate(tty,
  339. new_baudrate, new_baudrate);
  340. }
  341. break;
  342. case CYPRESS_GET_CONFIG:
  343. if (priv->get_cfg_unsafe) {
  344. /* Not implemented for this device,
  345. and if we try to do it we're likely
  346. to crash the hardware. */
  347. retval = -ENOTTY;
  348. goto out;
  349. }
  350. dbg("%s - retreiving serial line settings", __func__);
  351. do {
  352. retval = usb_control_msg(port->serial->dev,
  353. usb_rcvctrlpipe(port->serial->dev, 0),
  354. HID_REQ_GET_REPORT,
  355. USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  356. 0x0300, 0, feature_buffer,
  357. feature_len, 500);
  358. if (tries++ >= 3)
  359. break;
  360. } while (retval != feature_len
  361. && retval != -ENODEV);
  362. if (retval != feature_len) {
  363. dev_err(&port->dev, "%s - failed to retrieve serial "
  364. "line settings - %d\n", __func__, retval);
  365. cypress_set_dead(port);
  366. goto out;
  367. } else {
  368. spin_lock_irqsave(&priv->lock, flags);
  369. /* store the config in one byte, and later
  370. use bit masks to check values */
  371. priv->current_config = feature_buffer[4];
  372. priv->baud_rate = get_unaligned_le32(feature_buffer);
  373. spin_unlock_irqrestore(&priv->lock, flags);
  374. }
  375. }
  376. spin_lock_irqsave(&priv->lock, flags);
  377. ++priv->cmd_count;
  378. spin_unlock_irqrestore(&priv->lock, flags);
  379. out:
  380. kfree(feature_buffer);
  381. return retval;
  382. } /* cypress_serial_control */
  383. static void cypress_set_dead(struct usb_serial_port *port)
  384. {
  385. struct cypress_private *priv = usb_get_serial_port_data(port);
  386. unsigned long flags;
  387. spin_lock_irqsave(&priv->lock, flags);
  388. if (!priv->comm_is_ok) {
  389. spin_unlock_irqrestore(&priv->lock, flags);
  390. return;
  391. }
  392. priv->comm_is_ok = 0;
  393. spin_unlock_irqrestore(&priv->lock, flags);
  394. dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
  395. "interval might be too short\n", port->number);
  396. }
  397. /*****************************************************************************
  398. * Cypress serial driver functions
  399. *****************************************************************************/
  400. static int generic_startup(struct usb_serial *serial)
  401. {
  402. struct cypress_private *priv;
  403. struct usb_serial_port *port = serial->port[0];
  404. dbg("%s - port %d", __func__, port->number);
  405. priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
  406. if (!priv)
  407. return -ENOMEM;
  408. priv->comm_is_ok = !0;
  409. spin_lock_init(&priv->lock);
  410. if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
  411. kfree(priv);
  412. return -ENOMEM;
  413. }
  414. init_waitqueue_head(&priv->delta_msr_wait);
  415. usb_reset_configuration(serial->dev);
  416. priv->cmd_ctrl = 0;
  417. priv->line_control = 0;
  418. priv->termios_initialized = 0;
  419. priv->rx_flags = 0;
  420. /* Default packet format setting is determined by packet size.
  421. Anything with a size larger then 9 must have a separate
  422. count field since the 3 bit count field is otherwise too
  423. small. Otherwise we can use the slightly more compact
  424. format. This is in accordance with the cypress_m8 serial
  425. converter app note. */
  426. if (port->interrupt_out_size > 9)
  427. priv->pkt_fmt = packet_format_1;
  428. else
  429. priv->pkt_fmt = packet_format_2;
  430. if (interval > 0) {
  431. priv->write_urb_interval = interval;
  432. priv->read_urb_interval = interval;
  433. dbg("%s - port %d read & write intervals forced to %d",
  434. __func__, port->number, interval);
  435. } else {
  436. priv->write_urb_interval = port->interrupt_out_urb->interval;
  437. priv->read_urb_interval = port->interrupt_in_urb->interval;
  438. dbg("%s - port %d intervals: read=%d write=%d",
  439. __func__, port->number,
  440. priv->read_urb_interval, priv->write_urb_interval);
  441. }
  442. usb_set_serial_port_data(port, priv);
  443. return 0;
  444. }
  445. static int cypress_earthmate_startup(struct usb_serial *serial)
  446. {
  447. struct cypress_private *priv;
  448. struct usb_serial_port *port = serial->port[0];
  449. dbg("%s", __func__);
  450. if (generic_startup(serial)) {
  451. dbg("%s - Failed setting up port %d", __func__,
  452. port->number);
  453. return 1;
  454. }
  455. priv = usb_get_serial_port_data(port);
  456. priv->chiptype = CT_EARTHMATE;
  457. /* All Earthmate devices use the separated-count packet
  458. format! Idiotic. */
  459. priv->pkt_fmt = packet_format_1;
  460. if (serial->dev->descriptor.idProduct !=
  461. cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
  462. /* The old original USB Earthmate seemed able to
  463. handle GET_CONFIG requests; everything they've
  464. produced since that time crashes if this command is
  465. attempted :-( */
  466. dbg("%s - Marking this device as unsafe for GET_CONFIG "
  467. "commands", __func__);
  468. priv->get_cfg_unsafe = !0;
  469. }
  470. return 0;
  471. } /* cypress_earthmate_startup */
  472. static int cypress_hidcom_startup(struct usb_serial *serial)
  473. {
  474. struct cypress_private *priv;
  475. dbg("%s", __func__);
  476. if (generic_startup(serial)) {
  477. dbg("%s - Failed setting up port %d", __func__,
  478. serial->port[0]->number);
  479. return 1;
  480. }
  481. priv = usb_get_serial_port_data(serial->port[0]);
  482. priv->chiptype = CT_CYPHIDCOM;
  483. return 0;
  484. } /* cypress_hidcom_startup */
  485. static int cypress_ca42v2_startup(struct usb_serial *serial)
  486. {
  487. struct cypress_private *priv;
  488. dbg("%s", __func__);
  489. if (generic_startup(serial)) {
  490. dbg("%s - Failed setting up port %d", __func__,
  491. serial->port[0]->number);
  492. return 1;
  493. }
  494. priv = usb_get_serial_port_data(serial->port[0]);
  495. priv->chiptype = CT_CA42V2;
  496. return 0;
  497. } /* cypress_ca42v2_startup */
  498. static void cypress_release(struct usb_serial *serial)
  499. {
  500. struct cypress_private *priv;
  501. dbg("%s - port %d", __func__, serial->port[0]->number);
  502. /* all open ports are closed at this point */
  503. priv = usb_get_serial_port_data(serial->port[0]);
  504. if (priv) {
  505. kfifo_free(&priv->write_fifo);
  506. kfree(priv);
  507. }
  508. }
  509. static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
  510. {
  511. struct cypress_private *priv = usb_get_serial_port_data(port);
  512. struct usb_serial *serial = port->serial;
  513. unsigned long flags;
  514. int result = 0;
  515. dbg("%s - port %d", __func__, port->number);
  516. if (!priv->comm_is_ok)
  517. return -EIO;
  518. /* clear halts before open */
  519. usb_clear_halt(serial->dev, 0x81);
  520. usb_clear_halt(serial->dev, 0x02);
  521. spin_lock_irqsave(&priv->lock, flags);
  522. /* reset read/write statistics */
  523. priv->bytes_in = 0;
  524. priv->bytes_out = 0;
  525. priv->cmd_count = 0;
  526. priv->rx_flags = 0;
  527. spin_unlock_irqrestore(&priv->lock, flags);
  528. /* Set termios */
  529. cypress_send(port);
  530. if (tty)
  531. cypress_set_termios(tty, port, &priv->tmp_termios);
  532. /* setup the port and start reading from the device */
  533. if (!port->interrupt_in_urb) {
  534. dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
  535. __func__);
  536. return -1;
  537. }
  538. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  539. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  540. port->interrupt_in_urb->transfer_buffer,
  541. port->interrupt_in_urb->transfer_buffer_length,
  542. cypress_read_int_callback, port, priv->read_urb_interval);
  543. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  544. if (result) {
  545. dev_err(&port->dev,
  546. "%s - failed submitting read urb, error %d\n",
  547. __func__, result);
  548. cypress_set_dead(port);
  549. }
  550. port->port.drain_delay = 256;
  551. return result;
  552. } /* cypress_open */
  553. static void cypress_dtr_rts(struct usb_serial_port *port, int on)
  554. {
  555. struct cypress_private *priv = usb_get_serial_port_data(port);
  556. /* drop dtr and rts */
  557. spin_lock_irq(&priv->lock);
  558. if (on == 0)
  559. priv->line_control = 0;
  560. else
  561. priv->line_control = CONTROL_DTR | CONTROL_RTS;
  562. priv->cmd_ctrl = 1;
  563. spin_unlock_irq(&priv->lock);
  564. cypress_write(NULL, port, NULL, 0);
  565. }
  566. static void cypress_close(struct usb_serial_port *port)
  567. {
  568. struct cypress_private *priv = usb_get_serial_port_data(port);
  569. unsigned long flags;
  570. dbg("%s - port %d", __func__, port->number);
  571. /* writing is potentially harmful, lock must be taken */
  572. mutex_lock(&port->serial->disc_mutex);
  573. if (port->serial->disconnected) {
  574. mutex_unlock(&port->serial->disc_mutex);
  575. return;
  576. }
  577. spin_lock_irqsave(&priv->lock, flags);
  578. kfifo_reset_out(&priv->write_fifo);
  579. spin_unlock_irqrestore(&priv->lock, flags);
  580. dbg("%s - stopping urbs", __func__);
  581. usb_kill_urb(port->interrupt_in_urb);
  582. usb_kill_urb(port->interrupt_out_urb);
  583. if (stats)
  584. dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
  585. priv->bytes_in, priv->bytes_out, priv->cmd_count);
  586. mutex_unlock(&port->serial->disc_mutex);
  587. } /* cypress_close */
  588. static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
  589. const unsigned char *buf, int count)
  590. {
  591. struct cypress_private *priv = usb_get_serial_port_data(port);
  592. dbg("%s - port %d, %d bytes", __func__, port->number, count);
  593. /* line control commands, which need to be executed immediately,
  594. are not put into the buffer for obvious reasons.
  595. */
  596. if (priv->cmd_ctrl) {
  597. count = 0;
  598. goto finish;
  599. }
  600. if (!count)
  601. return count;
  602. count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
  603. finish:
  604. cypress_send(port);
  605. return count;
  606. } /* cypress_write */
  607. static void cypress_send(struct usb_serial_port *port)
  608. {
  609. int count = 0, result, offset, actual_size;
  610. struct cypress_private *priv = usb_get_serial_port_data(port);
  611. unsigned long flags;
  612. if (!priv->comm_is_ok)
  613. return;
  614. dbg("%s - port %d", __func__, port->number);
  615. dbg("%s - interrupt out size is %d", __func__,
  616. port->interrupt_out_size);
  617. spin_lock_irqsave(&priv->lock, flags);
  618. if (priv->write_urb_in_use) {
  619. dbg("%s - can't write, urb in use", __func__);
  620. spin_unlock_irqrestore(&priv->lock, flags);
  621. return;
  622. }
  623. spin_unlock_irqrestore(&priv->lock, flags);
  624. /* clear buffer */
  625. memset(port->interrupt_out_urb->transfer_buffer, 0,
  626. port->interrupt_out_size);
  627. spin_lock_irqsave(&priv->lock, flags);
  628. switch (priv->pkt_fmt) {
  629. default:
  630. case packet_format_1:
  631. /* this is for the CY7C64013... */
  632. offset = 2;
  633. port->interrupt_out_buffer[0] = priv->line_control;
  634. break;
  635. case packet_format_2:
  636. /* this is for the CY7C63743... */
  637. offset = 1;
  638. port->interrupt_out_buffer[0] = priv->line_control;
  639. break;
  640. }
  641. if (priv->line_control & CONTROL_RESET)
  642. priv->line_control &= ~CONTROL_RESET;
  643. if (priv->cmd_ctrl) {
  644. priv->cmd_count++;
  645. dbg("%s - line control command being issued", __func__);
  646. spin_unlock_irqrestore(&priv->lock, flags);
  647. goto send;
  648. } else
  649. spin_unlock_irqrestore(&priv->lock, flags);
  650. count = kfifo_out_locked(&priv->write_fifo,
  651. &port->interrupt_out_buffer[offset],
  652. port->interrupt_out_size - offset,
  653. &priv->lock);
  654. if (count == 0)
  655. return;
  656. switch (priv->pkt_fmt) {
  657. default:
  658. case packet_format_1:
  659. port->interrupt_out_buffer[1] = count;
  660. break;
  661. case packet_format_2:
  662. port->interrupt_out_buffer[0] |= count;
  663. }
  664. dbg("%s - count is %d", __func__, count);
  665. send:
  666. spin_lock_irqsave(&priv->lock, flags);
  667. priv->write_urb_in_use = 1;
  668. spin_unlock_irqrestore(&priv->lock, flags);
  669. if (priv->cmd_ctrl)
  670. actual_size = 1;
  671. else
  672. actual_size = count +
  673. (priv->pkt_fmt == packet_format_1 ? 2 : 1);
  674. usb_serial_debug_data(debug, &port->dev, __func__,
  675. port->interrupt_out_size,
  676. port->interrupt_out_urb->transfer_buffer);
  677. usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
  678. usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
  679. port->interrupt_out_buffer, port->interrupt_out_size,
  680. cypress_write_int_callback, port, priv->write_urb_interval);
  681. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  682. if (result) {
  683. dev_err(&port->dev,
  684. "%s - failed submitting write urb, error %d\n",
  685. __func__, result);
  686. priv->write_urb_in_use = 0;
  687. cypress_set_dead(port);
  688. }
  689. spin_lock_irqsave(&priv->lock, flags);
  690. if (priv->cmd_ctrl)
  691. priv->cmd_ctrl = 0;
  692. /* do not count the line control and size bytes */
  693. priv->bytes_out += count;
  694. spin_unlock_irqrestore(&priv->lock, flags);
  695. usb_serial_port_softint(port);
  696. } /* cypress_send */
  697. /* returns how much space is available in the soft buffer */
  698. static int cypress_write_room(struct tty_struct *tty)
  699. {
  700. struct usb_serial_port *port = tty->driver_data;
  701. struct cypress_private *priv = usb_get_serial_port_data(port);
  702. int room = 0;
  703. unsigned long flags;
  704. dbg("%s - port %d", __func__, port->number);
  705. spin_lock_irqsave(&priv->lock, flags);
  706. room = kfifo_avail(&priv->write_fifo);
  707. spin_unlock_irqrestore(&priv->lock, flags);
  708. dbg("%s - returns %d", __func__, room);
  709. return room;
  710. }
  711. static int cypress_tiocmget(struct tty_struct *tty)
  712. {
  713. struct usb_serial_port *port = tty->driver_data;
  714. struct cypress_private *priv = usb_get_serial_port_data(port);
  715. __u8 status, control;
  716. unsigned int result = 0;
  717. unsigned long flags;
  718. dbg("%s - port %d", __func__, port->number);
  719. spin_lock_irqsave(&priv->lock, flags);
  720. control = priv->line_control;
  721. status = priv->current_status;
  722. spin_unlock_irqrestore(&priv->lock, flags);
  723. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  724. | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  725. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  726. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  727. | ((status & UART_RI) ? TIOCM_RI : 0)
  728. | ((status & UART_CD) ? TIOCM_CD : 0);
  729. dbg("%s - result = %x", __func__, result);
  730. return result;
  731. }
  732. static int cypress_tiocmset(struct tty_struct *tty,
  733. unsigned int set, unsigned int clear)
  734. {
  735. struct usb_serial_port *port = tty->driver_data;
  736. struct cypress_private *priv = usb_get_serial_port_data(port);
  737. unsigned long flags;
  738. dbg("%s - port %d", __func__, port->number);
  739. spin_lock_irqsave(&priv->lock, flags);
  740. if (set & TIOCM_RTS)
  741. priv->line_control |= CONTROL_RTS;
  742. if (set & TIOCM_DTR)
  743. priv->line_control |= CONTROL_DTR;
  744. if (clear & TIOCM_RTS)
  745. priv->line_control &= ~CONTROL_RTS;
  746. if (clear & TIOCM_DTR)
  747. priv->line_control &= ~CONTROL_DTR;
  748. priv->cmd_ctrl = 1;
  749. spin_unlock_irqrestore(&priv->lock, flags);
  750. return cypress_write(tty, port, NULL, 0);
  751. }
  752. static int cypress_ioctl(struct tty_struct *tty,
  753. unsigned int cmd, unsigned long arg)
  754. {
  755. struct usb_serial_port *port = tty->driver_data;
  756. struct cypress_private *priv = usb_get_serial_port_data(port);
  757. dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
  758. switch (cmd) {
  759. /* This code comes from drivers/char/serial.c and ftdi_sio.c */
  760. case TIOCMIWAIT:
  761. while (priv != NULL) {
  762. interruptible_sleep_on(&priv->delta_msr_wait);
  763. /* see if a signal did it */
  764. if (signal_pending(current))
  765. return -ERESTARTSYS;
  766. else {
  767. char diff = priv->diff_status;
  768. if (diff == 0)
  769. return -EIO; /* no change => error */
  770. /* consume all events */
  771. priv->diff_status = 0;
  772. /* return 0 if caller wanted to know about
  773. these bits */
  774. if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
  775. ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
  776. ((arg & TIOCM_CD) && (diff & UART_CD)) ||
  777. ((arg & TIOCM_CTS) && (diff & UART_CTS)))
  778. return 0;
  779. /* otherwise caller can't care less about what
  780. * happened, and so we continue to wait for
  781. * more events.
  782. */
  783. }
  784. }
  785. return 0;
  786. default:
  787. break;
  788. }
  789. dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
  790. return -ENOIOCTLCMD;
  791. } /* cypress_ioctl */
  792. static void cypress_set_termios(struct tty_struct *tty,
  793. struct usb_serial_port *port, struct ktermios *old_termios)
  794. {
  795. struct cypress_private *priv = usb_get_serial_port_data(port);
  796. int data_bits, stop_bits, parity_type, parity_enable;
  797. unsigned cflag, iflag;
  798. unsigned long flags;
  799. __u8 oldlines;
  800. int linechange = 0;
  801. dbg("%s - port %d", __func__, port->number);
  802. spin_lock_irqsave(&priv->lock, flags);
  803. /* We can't clean this one up as we don't know the device type
  804. early enough */
  805. if (!priv->termios_initialized) {
  806. if (priv->chiptype == CT_EARTHMATE) {
  807. *(tty->termios) = tty_std_termios;
  808. tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
  809. CLOCAL;
  810. tty->termios->c_ispeed = 4800;
  811. tty->termios->c_ospeed = 4800;
  812. } else if (priv->chiptype == CT_CYPHIDCOM) {
  813. *(tty->termios) = tty_std_termios;
  814. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  815. CLOCAL;
  816. tty->termios->c_ispeed = 9600;
  817. tty->termios->c_ospeed = 9600;
  818. } else if (priv->chiptype == CT_CA42V2) {
  819. *(tty->termios) = tty_std_termios;
  820. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  821. CLOCAL;
  822. tty->termios->c_ispeed = 9600;
  823. tty->termios->c_ospeed = 9600;
  824. }
  825. priv->termios_initialized = 1;
  826. }
  827. spin_unlock_irqrestore(&priv->lock, flags);
  828. /* Unsupported features need clearing */
  829. tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
  830. cflag = tty->termios->c_cflag;
  831. iflag = tty->termios->c_iflag;
  832. /* check if there are new settings */
  833. if (old_termios) {
  834. spin_lock_irqsave(&priv->lock, flags);
  835. priv->tmp_termios = *(tty->termios);
  836. spin_unlock_irqrestore(&priv->lock, flags);
  837. }
  838. /* set number of data bits, parity, stop bits */
  839. /* when parity is disabled the parity type bit is ignored */
  840. /* 1 means 2 stop bits, 0 means 1 stop bit */
  841. stop_bits = cflag & CSTOPB ? 1 : 0;
  842. if (cflag & PARENB) {
  843. parity_enable = 1;
  844. /* 1 means odd parity, 0 means even parity */
  845. parity_type = cflag & PARODD ? 1 : 0;
  846. } else
  847. parity_enable = parity_type = 0;
  848. switch (cflag & CSIZE) {
  849. case CS5:
  850. data_bits = 0;
  851. break;
  852. case CS6:
  853. data_bits = 1;
  854. break;
  855. case CS7:
  856. data_bits = 2;
  857. break;
  858. case CS8:
  859. data_bits = 3;
  860. break;
  861. default:
  862. dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
  863. __func__);
  864. data_bits = 3;
  865. }
  866. spin_lock_irqsave(&priv->lock, flags);
  867. oldlines = priv->line_control;
  868. if ((cflag & CBAUD) == B0) {
  869. /* drop dtr and rts */
  870. dbg("%s - dropping the lines, baud rate 0bps", __func__);
  871. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  872. } else
  873. priv->line_control = (CONTROL_DTR | CONTROL_RTS);
  874. spin_unlock_irqrestore(&priv->lock, flags);
  875. dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
  876. "%d data_bits (+5)", __func__, stop_bits,
  877. parity_enable, parity_type, data_bits);
  878. cypress_serial_control(tty, port, tty_get_baud_rate(tty),
  879. data_bits, stop_bits,
  880. parity_enable, parity_type,
  881. 0, CYPRESS_SET_CONFIG);
  882. /* we perform a CYPRESS_GET_CONFIG so that the current settings are
  883. * filled into the private structure this should confirm that all is
  884. * working if it returns what we just set */
  885. cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
  886. /* Here we can define custom tty settings for devices; the main tty
  887. * termios flag base comes from empeg.c */
  888. spin_lock_irqsave(&priv->lock, flags);
  889. if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
  890. dbg("Using custom termios settings for a baud rate of "
  891. "4800bps.");
  892. /* define custom termios settings for NMEA protocol */
  893. tty->termios->c_iflag /* input modes - */
  894. &= ~(IGNBRK /* disable ignore break */
  895. | BRKINT /* disable break causes interrupt */
  896. | PARMRK /* disable mark parity errors */
  897. | ISTRIP /* disable clear high bit of input char */
  898. | INLCR /* disable translate NL to CR */
  899. | IGNCR /* disable ignore CR */
  900. | ICRNL /* disable translate CR to NL */
  901. | IXON); /* disable enable XON/XOFF flow control */
  902. tty->termios->c_oflag /* output modes */
  903. &= ~OPOST; /* disable postprocess output char */
  904. tty->termios->c_lflag /* line discipline modes */
  905. &= ~(ECHO /* disable echo input characters */
  906. | ECHONL /* disable echo new line */
  907. | ICANON /* disable erase, kill, werase, and rprnt
  908. special characters */
  909. | ISIG /* disable interrupt, quit, and suspend
  910. special characters */
  911. | IEXTEN); /* disable non-POSIX special characters */
  912. } /* CT_CYPHIDCOM: Application should handle this for device */
  913. linechange = (priv->line_control != oldlines);
  914. spin_unlock_irqrestore(&priv->lock, flags);
  915. /* if necessary, set lines */
  916. if (linechange) {
  917. priv->cmd_ctrl = 1;
  918. cypress_write(tty, port, NULL, 0);
  919. }
  920. } /* cypress_set_termios */
  921. /* returns amount of data still left in soft buffer */
  922. static int cypress_chars_in_buffer(struct tty_struct *tty)
  923. {
  924. struct usb_serial_port *port = tty->driver_data;
  925. struct cypress_private *priv = usb_get_serial_port_data(port);
  926. int chars = 0;
  927. unsigned long flags;
  928. dbg("%s - port %d", __func__, port->number);
  929. spin_lock_irqsave(&priv->lock, flags);
  930. chars = kfifo_len(&priv->write_fifo);
  931. spin_unlock_irqrestore(&priv->lock, flags);
  932. dbg("%s - returns %d", __func__, chars);
  933. return chars;
  934. }
  935. static void cypress_throttle(struct tty_struct *tty)
  936. {
  937. struct usb_serial_port *port = tty->driver_data;
  938. struct cypress_private *priv = usb_get_serial_port_data(port);
  939. dbg("%s - port %d", __func__, port->number);
  940. spin_lock_irq(&priv->lock);
  941. priv->rx_flags = THROTTLED;
  942. spin_unlock_irq(&priv->lock);
  943. }
  944. static void cypress_unthrottle(struct tty_struct *tty)
  945. {
  946. struct usb_serial_port *port = tty->driver_data;
  947. struct cypress_private *priv = usb_get_serial_port_data(port);
  948. int actually_throttled, result;
  949. dbg("%s - port %d", __func__, port->number);
  950. spin_lock_irq(&priv->lock);
  951. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  952. priv->rx_flags = 0;
  953. spin_unlock_irq(&priv->lock);
  954. if (!priv->comm_is_ok)
  955. return;
  956. if (actually_throttled) {
  957. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  958. if (result) {
  959. dev_err(&port->dev, "%s - failed submitting read urb, "
  960. "error %d\n", __func__, result);
  961. cypress_set_dead(port);
  962. }
  963. }
  964. }
  965. static void cypress_read_int_callback(struct urb *urb)
  966. {
  967. struct usb_serial_port *port = urb->context;
  968. struct cypress_private *priv = usb_get_serial_port_data(port);
  969. struct tty_struct *tty;
  970. unsigned char *data = urb->transfer_buffer;
  971. unsigned long flags;
  972. char tty_flag = TTY_NORMAL;
  973. int havedata = 0;
  974. int bytes = 0;
  975. int result;
  976. int i = 0;
  977. int status = urb->status;
  978. dbg("%s - port %d", __func__, port->number);
  979. switch (status) {
  980. case 0: /* success */
  981. break;
  982. case -ECONNRESET:
  983. case -ENOENT:
  984. case -ESHUTDOWN:
  985. /* precursor to disconnect so just go away */
  986. return;
  987. case -EPIPE:
  988. /* Can't call usb_clear_halt while in_interrupt */
  989. /* FALLS THROUGH */
  990. default:
  991. /* something ugly is going on... */
  992. dev_err(&urb->dev->dev,
  993. "%s - unexpected nonzero read status received: %d\n",
  994. __func__, status);
  995. cypress_set_dead(port);
  996. return;
  997. }
  998. spin_lock_irqsave(&priv->lock, flags);
  999. if (priv->rx_flags & THROTTLED) {
  1000. dbg("%s - now throttling", __func__);
  1001. priv->rx_flags |= ACTUALLY_THROTTLED;
  1002. spin_unlock_irqrestore(&priv->lock, flags);
  1003. return;
  1004. }
  1005. spin_unlock_irqrestore(&priv->lock, flags);
  1006. tty = tty_port_tty_get(&port->port);
  1007. if (!tty) {
  1008. dbg("%s - bad tty pointer - exiting", __func__);
  1009. return;
  1010. }
  1011. spin_lock_irqsave(&priv->lock, flags);
  1012. result = urb->actual_length;
  1013. switch (priv->pkt_fmt) {
  1014. default:
  1015. case packet_format_1:
  1016. /* This is for the CY7C64013... */
  1017. priv->current_status = data[0] & 0xF8;
  1018. bytes = data[1] + 2;
  1019. i = 2;
  1020. if (bytes > 2)
  1021. havedata = 1;
  1022. break;
  1023. case packet_format_2:
  1024. /* This is for the CY7C63743... */
  1025. priv->current_status = data[0] & 0xF8;
  1026. bytes = (data[0] & 0x07) + 1;
  1027. i = 1;
  1028. if (bytes > 1)
  1029. havedata = 1;
  1030. break;
  1031. }
  1032. spin_unlock_irqrestore(&priv->lock, flags);
  1033. if (result < bytes) {
  1034. dbg("%s - wrong packet size - received %d bytes but packet "
  1035. "said %d bytes", __func__, result, bytes);
  1036. goto continue_read;
  1037. }
  1038. usb_serial_debug_data(debug, &port->dev, __func__,
  1039. urb->actual_length, data);
  1040. spin_lock_irqsave(&priv->lock, flags);
  1041. /* check to see if status has changed */
  1042. if (priv->current_status != priv->prev_status) {
  1043. priv->diff_status |= priv->current_status ^
  1044. priv->prev_status;
  1045. wake_up_interruptible(&priv->delta_msr_wait);
  1046. priv->prev_status = priv->current_status;
  1047. }
  1048. spin_unlock_irqrestore(&priv->lock, flags);
  1049. /* hangup, as defined in acm.c... this might be a bad place for it
  1050. * though */
  1051. if (tty && !(tty->termios->c_cflag & CLOCAL) &&
  1052. !(priv->current_status & UART_CD)) {
  1053. dbg("%s - calling hangup", __func__);
  1054. tty_hangup(tty);
  1055. goto continue_read;
  1056. }
  1057. /* There is one error bit... I'm assuming it is a parity error
  1058. * indicator as the generic firmware will set this bit to 1 if a
  1059. * parity error occurs.
  1060. * I can not find reference to any other error events. */
  1061. spin_lock_irqsave(&priv->lock, flags);
  1062. if (priv->current_status & CYP_ERROR) {
  1063. spin_unlock_irqrestore(&priv->lock, flags);
  1064. tty_flag = TTY_PARITY;
  1065. dbg("%s - Parity Error detected", __func__);
  1066. } else
  1067. spin_unlock_irqrestore(&priv->lock, flags);
  1068. /* process read if there is data other than line status */
  1069. if (tty && bytes > i) {
  1070. tty_insert_flip_string_fixed_flag(tty, data + i,
  1071. tty_flag, bytes - i);
  1072. tty_flip_buffer_push(tty);
  1073. }
  1074. spin_lock_irqsave(&priv->lock, flags);
  1075. /* control and status byte(s) are also counted */
  1076. priv->bytes_in += bytes;
  1077. spin_unlock_irqrestore(&priv->lock, flags);
  1078. continue_read:
  1079. tty_kref_put(tty);
  1080. /* Continue trying to always read */
  1081. if (priv->comm_is_ok) {
  1082. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  1083. usb_rcvintpipe(port->serial->dev,
  1084. port->interrupt_in_endpointAddress),
  1085. port->interrupt_in_urb->transfer_buffer,
  1086. port->interrupt_in_urb->transfer_buffer_length,
  1087. cypress_read_int_callback, port,
  1088. priv->read_urb_interval);
  1089. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  1090. if (result && result != -EPERM) {
  1091. dev_err(&urb->dev->dev, "%s - failed resubmitting "
  1092. "read urb, error %d\n", __func__,
  1093. result);
  1094. cypress_set_dead(port);
  1095. }
  1096. }
  1097. } /* cypress_read_int_callback */
  1098. static void cypress_write_int_callback(struct urb *urb)
  1099. {
  1100. struct usb_serial_port *port = urb->context;
  1101. struct cypress_private *priv = usb_get_serial_port_data(port);
  1102. int result;
  1103. int status = urb->status;
  1104. dbg("%s - port %d", __func__, port->number);
  1105. switch (status) {
  1106. case 0:
  1107. /* success */
  1108. break;
  1109. case -ECONNRESET:
  1110. case -ENOENT:
  1111. case -ESHUTDOWN:
  1112. /* this urb is terminated, clean up */
  1113. dbg("%s - urb shutting down with status: %d",
  1114. __func__, status);
  1115. priv->write_urb_in_use = 0;
  1116. return;
  1117. case -EPIPE: /* no break needed; clear halt and resubmit */
  1118. if (!priv->comm_is_ok)
  1119. break;
  1120. usb_clear_halt(port->serial->dev, 0x02);
  1121. /* error in the urb, so we have to resubmit it */
  1122. dbg("%s - nonzero write bulk status received: %d",
  1123. __func__, status);
  1124. port->interrupt_out_urb->transfer_buffer_length = 1;
  1125. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  1126. if (!result)
  1127. return;
  1128. dev_err(&urb->dev->dev,
  1129. "%s - failed resubmitting write urb, error %d\n",
  1130. __func__, result);
  1131. cypress_set_dead(port);
  1132. break;
  1133. default:
  1134. dev_err(&urb->dev->dev,
  1135. "%s - unexpected nonzero write status received: %d\n",
  1136. __func__, status);
  1137. cypress_set_dead(port);
  1138. break;
  1139. }
  1140. priv->write_urb_in_use = 0;
  1141. /* send any buffered data */
  1142. cypress_send(port);
  1143. }
  1144. /*****************************************************************************
  1145. * Module functions
  1146. *****************************************************************************/
  1147. static int __init cypress_init(void)
  1148. {
  1149. int retval;
  1150. dbg("%s", __func__);
  1151. retval = usb_serial_register(&cypress_earthmate_device);
  1152. if (retval)
  1153. goto failed_em_register;
  1154. retval = usb_serial_register(&cypress_hidcom_device);
  1155. if (retval)
  1156. goto failed_hidcom_register;
  1157. retval = usb_serial_register(&cypress_ca42v2_device);
  1158. if (retval)
  1159. goto failed_ca42v2_register;
  1160. retval = usb_register(&cypress_driver);
  1161. if (retval)
  1162. goto failed_usb_register;
  1163. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1164. DRIVER_DESC "\n");
  1165. return 0;
  1166. failed_usb_register:
  1167. usb_serial_deregister(&cypress_ca42v2_device);
  1168. failed_ca42v2_register:
  1169. usb_serial_deregister(&cypress_hidcom_device);
  1170. failed_hidcom_register:
  1171. usb_serial_deregister(&cypress_earthmate_device);
  1172. failed_em_register:
  1173. return retval;
  1174. }
  1175. static void __exit cypress_exit(void)
  1176. {
  1177. dbg("%s", __func__);
  1178. usb_deregister(&cypress_driver);
  1179. usb_serial_deregister(&cypress_earthmate_device);
  1180. usb_serial_deregister(&cypress_hidcom_device);
  1181. usb_serial_deregister(&cypress_ca42v2_device);
  1182. }
  1183. module_init(cypress_init);
  1184. module_exit(cypress_exit);
  1185. MODULE_AUTHOR(DRIVER_AUTHOR);
  1186. MODULE_DESCRIPTION(DRIVER_DESC);
  1187. MODULE_VERSION(DRIVER_VERSION);
  1188. MODULE_LICENSE("GPL");
  1189. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1190. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1191. module_param(stats, bool, S_IRUGO | S_IWUSR);
  1192. MODULE_PARM_DESC(stats, "Enable statistics or not");
  1193. module_param(interval, int, S_IRUGO | S_IWUSR);
  1194. MODULE_PARM_DESC(interval, "Overrides interrupt interval");
  1195. module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
  1196. MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");