visor.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * USB HandSpring Visor, Palm m50x, and Sony Clie driver
  3. * (supports all of the Palm OS USB devices)
  4. *
  5. * Copyright (C) 1999 - 2004
  6. * Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * See Documentation/usb/usb-serial.txt for more information on using this
  13. * driver
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/serial.h>
  29. #include "visor.h"
  30. /*
  31. * Version Information
  32. */
  33. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  34. #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
  35. /* function prototypes for a handspring visor */
  36. static int visor_open(struct tty_struct *tty, struct usb_serial_port *port);
  37. static void visor_close(struct usb_serial_port *port);
  38. static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
  39. const unsigned char *buf, int count);
  40. static int visor_write_room(struct tty_struct *tty);
  41. static void visor_throttle(struct tty_struct *tty);
  42. static void visor_unthrottle(struct tty_struct *tty);
  43. static int visor_probe(struct usb_serial *serial,
  44. const struct usb_device_id *id);
  45. static int visor_calc_num_ports(struct usb_serial *serial);
  46. static void visor_release(struct usb_serial *serial);
  47. static void visor_write_bulk_callback(struct urb *urb);
  48. static void visor_read_bulk_callback(struct urb *urb);
  49. static void visor_read_int_callback(struct urb *urb);
  50. static int clie_3_5_startup(struct usb_serial *serial);
  51. static int treo_attach(struct usb_serial *serial);
  52. static int clie_5_attach(struct usb_serial *serial);
  53. static int palm_os_3_probe(struct usb_serial *serial,
  54. const struct usb_device_id *id);
  55. static int palm_os_4_probe(struct usb_serial *serial,
  56. const struct usb_device_id *id);
  57. /* Parameters that may be passed into the module. */
  58. static int debug;
  59. static __u16 vendor;
  60. static __u16 product;
  61. static struct usb_device_id id_table [] = {
  62. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
  63. .driver_info = (kernel_ulong_t)&palm_os_3_probe },
  64. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
  65. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  66. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
  67. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  68. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID),
  69. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  70. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
  71. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  72. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
  73. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  74. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
  75. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  76. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
  77. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  78. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
  79. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  80. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
  81. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  82. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
  83. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  84. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
  85. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  86. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650),
  87. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  88. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
  89. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  90. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
  91. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  92. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
  93. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  94. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
  95. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  96. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
  97. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  98. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
  99. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  100. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
  101. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  102. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
  103. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  104. { USB_DEVICE(ACER_VENDOR_ID, ACER_S10_ID),
  105. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  106. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID),
  107. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  108. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID),
  109. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  110. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID),
  111. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  112. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID),
  113. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  114. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
  115. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  116. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID),
  117. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  118. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID),
  119. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  120. { }, /* optional parameter entry */
  121. { } /* Terminating entry */
  122. };
  123. static struct usb_device_id clie_id_5_table [] = {
  124. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
  125. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  126. { }, /* optional parameter entry */
  127. { } /* Terminating entry */
  128. };
  129. static struct usb_device_id clie_id_3_5_table [] = {
  130. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  131. { } /* Terminating entry */
  132. };
  133. static struct usb_device_id id_table_combined [] = {
  134. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
  135. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
  136. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
  137. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID) },
  138. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
  139. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
  140. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
  141. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
  142. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
  143. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
  144. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
  145. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
  146. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650) },
  147. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
  148. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
  149. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  150. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
  151. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
  152. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
  153. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
  154. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
  155. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
  156. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
  157. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
  158. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
  159. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID) },
  160. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
  161. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
  162. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID) },
  163. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID) },
  164. { }, /* optional parameter entry */
  165. { } /* Terminating entry */
  166. };
  167. MODULE_DEVICE_TABLE(usb, id_table_combined);
  168. static struct usb_driver visor_driver = {
  169. .name = "visor",
  170. .probe = usb_serial_probe,
  171. .disconnect = usb_serial_disconnect,
  172. .id_table = id_table_combined,
  173. .no_dynamic_id = 1,
  174. };
  175. /* All of the device info needed for the Handspring Visor,
  176. and Palm 4.0 devices */
  177. static struct usb_serial_driver handspring_device = {
  178. .driver = {
  179. .owner = THIS_MODULE,
  180. .name = "visor",
  181. },
  182. .description = "Handspring Visor / Palm OS",
  183. .usb_driver = &visor_driver,
  184. .id_table = id_table,
  185. .num_ports = 2,
  186. .open = visor_open,
  187. .close = visor_close,
  188. .throttle = visor_throttle,
  189. .unthrottle = visor_unthrottle,
  190. .attach = treo_attach,
  191. .probe = visor_probe,
  192. .calc_num_ports = visor_calc_num_ports,
  193. .release = visor_release,
  194. .write = visor_write,
  195. .write_room = visor_write_room,
  196. .write_bulk_callback = visor_write_bulk_callback,
  197. .read_bulk_callback = visor_read_bulk_callback,
  198. .read_int_callback = visor_read_int_callback,
  199. };
  200. /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
  201. static struct usb_serial_driver clie_5_device = {
  202. .driver = {
  203. .owner = THIS_MODULE,
  204. .name = "clie_5",
  205. },
  206. .description = "Sony Clie 5.0",
  207. .usb_driver = &visor_driver,
  208. .id_table = clie_id_5_table,
  209. .num_ports = 2,
  210. .open = visor_open,
  211. .close = visor_close,
  212. .throttle = visor_throttle,
  213. .unthrottle = visor_unthrottle,
  214. .attach = clie_5_attach,
  215. .probe = visor_probe,
  216. .calc_num_ports = visor_calc_num_ports,
  217. .release = visor_release,
  218. .write = visor_write,
  219. .write_room = visor_write_room,
  220. .write_bulk_callback = visor_write_bulk_callback,
  221. .read_bulk_callback = visor_read_bulk_callback,
  222. .read_int_callback = visor_read_int_callback,
  223. };
  224. /* device info for the Sony Clie OS version 3.5 */
  225. static struct usb_serial_driver clie_3_5_device = {
  226. .driver = {
  227. .owner = THIS_MODULE,
  228. .name = "clie_3.5",
  229. },
  230. .description = "Sony Clie 3.5",
  231. .usb_driver = &visor_driver,
  232. .id_table = clie_id_3_5_table,
  233. .num_ports = 1,
  234. .open = visor_open,
  235. .close = visor_close,
  236. .throttle = visor_throttle,
  237. .unthrottle = visor_unthrottle,
  238. .attach = clie_3_5_startup,
  239. .write = visor_write,
  240. .write_room = visor_write_room,
  241. .write_bulk_callback = visor_write_bulk_callback,
  242. .read_bulk_callback = visor_read_bulk_callback,
  243. };
  244. struct visor_private {
  245. spinlock_t lock;
  246. int bytes_in;
  247. int bytes_out;
  248. int outstanding_urbs;
  249. unsigned char throttled;
  250. unsigned char actually_throttled;
  251. };
  252. /* number of outstanding urbs to prevent userspace DoS from happening */
  253. #define URB_UPPER_LIMIT 42
  254. static int stats;
  255. /******************************************************************************
  256. * Handspring Visor specific driver functions
  257. ******************************************************************************/
  258. static int visor_open(struct tty_struct *tty, struct usb_serial_port *port)
  259. {
  260. struct usb_serial *serial = port->serial;
  261. struct visor_private *priv = usb_get_serial_port_data(port);
  262. unsigned long flags;
  263. int result = 0;
  264. dbg("%s - port %d", __func__, port->number);
  265. if (!port->read_urb) {
  266. /* this is needed for some brain dead Sony devices */
  267. dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
  268. return -ENODEV;
  269. }
  270. spin_lock_irqsave(&priv->lock, flags);
  271. priv->bytes_in = 0;
  272. priv->bytes_out = 0;
  273. priv->throttled = 0;
  274. spin_unlock_irqrestore(&priv->lock, flags);
  275. /* Start reading from the device */
  276. usb_fill_bulk_urb(port->read_urb, serial->dev,
  277. usb_rcvbulkpipe(serial->dev,
  278. port->bulk_in_endpointAddress),
  279. port->read_urb->transfer_buffer,
  280. port->read_urb->transfer_buffer_length,
  281. visor_read_bulk_callback, port);
  282. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  283. if (result) {
  284. dev_err(&port->dev,
  285. "%s - failed submitting read urb, error %d\n",
  286. __func__, result);
  287. goto exit;
  288. }
  289. if (port->interrupt_in_urb) {
  290. dbg("%s - adding interrupt input for treo", __func__);
  291. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  292. if (result)
  293. dev_err(&port->dev,
  294. "%s - failed submitting interrupt urb, error %d\n",
  295. __func__, result);
  296. }
  297. exit:
  298. return result;
  299. }
  300. static void visor_close(struct usb_serial_port *port)
  301. {
  302. struct visor_private *priv = usb_get_serial_port_data(port);
  303. unsigned char *transfer_buffer;
  304. dbg("%s - port %d", __func__, port->number);
  305. /* shutdown our urbs */
  306. usb_kill_urb(port->read_urb);
  307. usb_kill_urb(port->interrupt_in_urb);
  308. mutex_lock(&port->serial->disc_mutex);
  309. if (!port->serial->disconnected) {
  310. /* Try to send shutdown message, unless the device is gone */
  311. transfer_buffer = kmalloc(0x12, GFP_KERNEL);
  312. if (transfer_buffer) {
  313. usb_control_msg(port->serial->dev,
  314. usb_rcvctrlpipe(port->serial->dev, 0),
  315. VISOR_CLOSE_NOTIFICATION, 0xc2,
  316. 0x0000, 0x0000,
  317. transfer_buffer, 0x12, 300);
  318. kfree(transfer_buffer);
  319. }
  320. }
  321. mutex_unlock(&port->serial->disc_mutex);
  322. if (stats)
  323. dev_info(&port->dev, "Bytes In = %d Bytes Out = %d\n",
  324. priv->bytes_in, priv->bytes_out);
  325. }
  326. static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
  327. const unsigned char *buf, int count)
  328. {
  329. struct visor_private *priv = usb_get_serial_port_data(port);
  330. struct usb_serial *serial = port->serial;
  331. struct urb *urb;
  332. unsigned char *buffer;
  333. unsigned long flags;
  334. int status;
  335. dbg("%s - port %d", __func__, port->number);
  336. spin_lock_irqsave(&priv->lock, flags);
  337. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  338. spin_unlock_irqrestore(&priv->lock, flags);
  339. dbg("%s - write limit hit\n", __func__);
  340. return 0;
  341. }
  342. priv->outstanding_urbs++;
  343. spin_unlock_irqrestore(&priv->lock, flags);
  344. buffer = kmalloc(count, GFP_ATOMIC);
  345. if (!buffer) {
  346. dev_err(&port->dev, "out of memory\n");
  347. count = -ENOMEM;
  348. goto error_no_buffer;
  349. }
  350. urb = usb_alloc_urb(0, GFP_ATOMIC);
  351. if (!urb) {
  352. dev_err(&port->dev, "no more free urbs\n");
  353. count = -ENOMEM;
  354. goto error_no_urb;
  355. }
  356. memcpy(buffer, buf, count);
  357. usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
  358. usb_fill_bulk_urb(urb, serial->dev,
  359. usb_sndbulkpipe(serial->dev,
  360. port->bulk_out_endpointAddress),
  361. buffer, count,
  362. visor_write_bulk_callback, port);
  363. /* send it down the pipe */
  364. status = usb_submit_urb(urb, GFP_ATOMIC);
  365. if (status) {
  366. dev_err(&port->dev,
  367. "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  368. __func__, status);
  369. count = status;
  370. goto error;
  371. } else {
  372. spin_lock_irqsave(&priv->lock, flags);
  373. priv->bytes_out += count;
  374. spin_unlock_irqrestore(&priv->lock, flags);
  375. }
  376. /* we are done with this urb, so let the host driver
  377. * really free it when it is finished with it */
  378. usb_free_urb(urb);
  379. return count;
  380. error:
  381. usb_free_urb(urb);
  382. error_no_urb:
  383. kfree(buffer);
  384. error_no_buffer:
  385. spin_lock_irqsave(&priv->lock, flags);
  386. --priv->outstanding_urbs;
  387. spin_unlock_irqrestore(&priv->lock, flags);
  388. return count;
  389. }
  390. static int visor_write_room(struct tty_struct *tty)
  391. {
  392. struct usb_serial_port *port = tty->driver_data;
  393. struct visor_private *priv = usb_get_serial_port_data(port);
  394. unsigned long flags;
  395. dbg("%s - port %d", __func__, port->number);
  396. /*
  397. * We really can take anything the user throws at us
  398. * but let's pick a nice big number to tell the tty
  399. * layer that we have lots of free space, unless we don't.
  400. */
  401. spin_lock_irqsave(&priv->lock, flags);
  402. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  403. spin_unlock_irqrestore(&priv->lock, flags);
  404. dbg("%s - write limit hit\n", __func__);
  405. return 0;
  406. }
  407. spin_unlock_irqrestore(&priv->lock, flags);
  408. return 2048;
  409. }
  410. static void visor_write_bulk_callback(struct urb *urb)
  411. {
  412. struct usb_serial_port *port = urb->context;
  413. struct visor_private *priv = usb_get_serial_port_data(port);
  414. int status = urb->status;
  415. unsigned long flags;
  416. /* free up the transfer buffer, as usb_free_urb() does not do this */
  417. kfree(urb->transfer_buffer);
  418. dbg("%s - port %d", __func__, port->number);
  419. if (status)
  420. dbg("%s - nonzero write bulk status received: %d",
  421. __func__, status);
  422. spin_lock_irqsave(&priv->lock, flags);
  423. --priv->outstanding_urbs;
  424. spin_unlock_irqrestore(&priv->lock, flags);
  425. usb_serial_port_softint(port);
  426. }
  427. static void visor_read_bulk_callback(struct urb *urb)
  428. {
  429. struct usb_serial_port *port = urb->context;
  430. struct visor_private *priv = usb_get_serial_port_data(port);
  431. unsigned char *data = urb->transfer_buffer;
  432. int status = urb->status;
  433. struct tty_struct *tty;
  434. int result;
  435. int available_room = 0;
  436. dbg("%s - port %d", __func__, port->number);
  437. if (status) {
  438. dbg("%s - nonzero read bulk status received: %d",
  439. __func__, status);
  440. return;
  441. }
  442. usb_serial_debug_data(debug, &port->dev, __func__,
  443. urb->actual_length, data);
  444. if (urb->actual_length) {
  445. tty = tty_port_tty_get(&port->port);
  446. if (tty) {
  447. available_room = tty_buffer_request_room(tty,
  448. urb->actual_length);
  449. if (available_room) {
  450. tty_insert_flip_string(tty, data,
  451. available_room);
  452. tty_flip_buffer_push(tty);
  453. }
  454. tty_kref_put(tty);
  455. }
  456. spin_lock(&priv->lock);
  457. priv->bytes_in += available_room;
  458. } else {
  459. spin_lock(&priv->lock);
  460. }
  461. /* Continue trying to always read if we should */
  462. if (!priv->throttled) {
  463. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  464. usb_rcvbulkpipe(port->serial->dev,
  465. port->bulk_in_endpointAddress),
  466. port->read_urb->transfer_buffer,
  467. port->read_urb->transfer_buffer_length,
  468. visor_read_bulk_callback, port);
  469. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  470. if (result)
  471. dev_err(&port->dev,
  472. "%s - failed resubmitting read urb, error %d\n",
  473. __func__, result);
  474. } else
  475. priv->actually_throttled = 1;
  476. spin_unlock(&priv->lock);
  477. }
  478. static void visor_read_int_callback(struct urb *urb)
  479. {
  480. struct usb_serial_port *port = urb->context;
  481. int status = urb->status;
  482. int result;
  483. switch (status) {
  484. case 0:
  485. /* success */
  486. break;
  487. case -ECONNRESET:
  488. case -ENOENT:
  489. case -ESHUTDOWN:
  490. /* this urb is terminated, clean up */
  491. dbg("%s - urb shutting down with status: %d",
  492. __func__, status);
  493. return;
  494. default:
  495. dbg("%s - nonzero urb status received: %d",
  496. __func__, status);
  497. goto exit;
  498. }
  499. /*
  500. * This information is still unknown what it can be used for.
  501. * If anyone has an idea, please let the author know...
  502. *
  503. * Rumor has it this endpoint is used to notify when data
  504. * is ready to be read from the bulk ones.
  505. */
  506. usb_serial_debug_data(debug, &port->dev, __func__,
  507. urb->actual_length, urb->transfer_buffer);
  508. exit:
  509. result = usb_submit_urb(urb, GFP_ATOMIC);
  510. if (result)
  511. dev_err(&urb->dev->dev,
  512. "%s - Error %d submitting interrupt urb\n",
  513. __func__, result);
  514. }
  515. static void visor_throttle(struct tty_struct *tty)
  516. {
  517. struct usb_serial_port *port = tty->driver_data;
  518. struct visor_private *priv = usb_get_serial_port_data(port);
  519. unsigned long flags;
  520. dbg("%s - port %d", __func__, port->number);
  521. spin_lock_irqsave(&priv->lock, flags);
  522. priv->throttled = 1;
  523. spin_unlock_irqrestore(&priv->lock, flags);
  524. }
  525. static void visor_unthrottle(struct tty_struct *tty)
  526. {
  527. struct usb_serial_port *port = tty->driver_data;
  528. struct visor_private *priv = usb_get_serial_port_data(port);
  529. unsigned long flags;
  530. int result;
  531. dbg("%s - port %d", __func__, port->number);
  532. spin_lock_irqsave(&priv->lock, flags);
  533. priv->throttled = 0;
  534. priv->actually_throttled = 0;
  535. spin_unlock_irqrestore(&priv->lock, flags);
  536. port->read_urb->dev = port->serial->dev;
  537. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  538. if (result)
  539. dev_err(&port->dev,
  540. "%s - failed submitting read urb, error %d\n",
  541. __func__, result);
  542. }
  543. static int palm_os_3_probe(struct usb_serial *serial,
  544. const struct usb_device_id *id)
  545. {
  546. struct device *dev = &serial->dev->dev;
  547. struct visor_connection_info *connection_info;
  548. unsigned char *transfer_buffer;
  549. char *string;
  550. int retval = 0;
  551. int i;
  552. int num_ports = 0;
  553. dbg("%s", __func__);
  554. transfer_buffer = kmalloc(sizeof(*connection_info), GFP_KERNEL);
  555. if (!transfer_buffer) {
  556. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  557. sizeof(*connection_info));
  558. return -ENOMEM;
  559. }
  560. /* send a get connection info request */
  561. retval = usb_control_msg(serial->dev,
  562. usb_rcvctrlpipe(serial->dev, 0),
  563. VISOR_GET_CONNECTION_INFORMATION,
  564. 0xc2, 0x0000, 0x0000, transfer_buffer,
  565. sizeof(*connection_info), 300);
  566. if (retval < 0) {
  567. dev_err(dev, "%s - error %d getting connection information\n",
  568. __func__, retval);
  569. goto exit;
  570. }
  571. if (retval == sizeof(*connection_info)) {
  572. connection_info = (struct visor_connection_info *)
  573. transfer_buffer;
  574. num_ports = le16_to_cpu(connection_info->num_ports);
  575. for (i = 0; i < num_ports; ++i) {
  576. switch (
  577. connection_info->connections[i].port_function_id) {
  578. case VISOR_FUNCTION_GENERIC:
  579. string = "Generic";
  580. break;
  581. case VISOR_FUNCTION_DEBUGGER:
  582. string = "Debugger";
  583. break;
  584. case VISOR_FUNCTION_HOTSYNC:
  585. string = "HotSync";
  586. break;
  587. case VISOR_FUNCTION_CONSOLE:
  588. string = "Console";
  589. break;
  590. case VISOR_FUNCTION_REMOTE_FILE_SYS:
  591. string = "Remote File System";
  592. break;
  593. default:
  594. string = "unknown";
  595. break;
  596. }
  597. dev_info(dev, "%s: port %d, is for %s use\n",
  598. serial->type->description,
  599. connection_info->connections[i].port, string);
  600. }
  601. }
  602. /*
  603. * Handle devices that report invalid stuff here.
  604. */
  605. if (num_ports == 0 || num_ports > 2) {
  606. dev_warn(dev, "%s: No valid connect info available\n",
  607. serial->type->description);
  608. num_ports = 2;
  609. }
  610. dev_info(dev, "%s: Number of ports: %d\n", serial->type->description,
  611. num_ports);
  612. /*
  613. * save off our num_ports info so that we can use it in the
  614. * calc_num_ports callback
  615. */
  616. usb_set_serial_data(serial, (void *)(long)num_ports);
  617. /* ask for the number of bytes available, but ignore the
  618. response as it is broken */
  619. retval = usb_control_msg(serial->dev,
  620. usb_rcvctrlpipe(serial->dev, 0),
  621. VISOR_REQUEST_BYTES_AVAILABLE,
  622. 0xc2, 0x0000, 0x0005, transfer_buffer,
  623. 0x02, 300);
  624. if (retval < 0)
  625. dev_err(dev, "%s - error %d getting bytes available request\n",
  626. __func__, retval);
  627. retval = 0;
  628. exit:
  629. kfree(transfer_buffer);
  630. return retval;
  631. }
  632. static int palm_os_4_probe(struct usb_serial *serial,
  633. const struct usb_device_id *id)
  634. {
  635. struct device *dev = &serial->dev->dev;
  636. struct palm_ext_connection_info *connection_info;
  637. unsigned char *transfer_buffer;
  638. int retval;
  639. dbg("%s", __func__);
  640. transfer_buffer = kmalloc(sizeof(*connection_info), GFP_KERNEL);
  641. if (!transfer_buffer) {
  642. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  643. sizeof(*connection_info));
  644. return -ENOMEM;
  645. }
  646. retval = usb_control_msg(serial->dev,
  647. usb_rcvctrlpipe(serial->dev, 0),
  648. PALM_GET_EXT_CONNECTION_INFORMATION,
  649. 0xc2, 0x0000, 0x0000, transfer_buffer,
  650. sizeof(*connection_info), 300);
  651. if (retval < 0)
  652. dev_err(dev, "%s - error %d getting connection info\n",
  653. __func__, retval);
  654. else
  655. usb_serial_debug_data(debug, &serial->dev->dev, __func__,
  656. retval, transfer_buffer);
  657. kfree(transfer_buffer);
  658. return 0;
  659. }
  660. static int visor_probe(struct usb_serial *serial,
  661. const struct usb_device_id *id)
  662. {
  663. int retval = 0;
  664. int (*startup)(struct usb_serial *serial,
  665. const struct usb_device_id *id);
  666. dbg("%s", __func__);
  667. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  668. dev_err(&serial->dev->dev, "active config #%d != 1 ??\n",
  669. serial->dev->actconfig->desc.bConfigurationValue);
  670. return -ENODEV;
  671. }
  672. if (id->driver_info) {
  673. startup = (void *)id->driver_info;
  674. retval = startup(serial, id);
  675. }
  676. return retval;
  677. }
  678. static int visor_calc_num_ports(struct usb_serial *serial)
  679. {
  680. int num_ports = (int)(long)(usb_get_serial_data(serial));
  681. if (num_ports)
  682. usb_set_serial_data(serial, NULL);
  683. return num_ports;
  684. }
  685. static int generic_startup(struct usb_serial *serial)
  686. {
  687. struct usb_serial_port **ports = serial->port;
  688. struct visor_private *priv;
  689. int i;
  690. for (i = 0; i < serial->num_ports; ++i) {
  691. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  692. if (!priv) {
  693. while (i-- != 0) {
  694. priv = usb_get_serial_port_data(ports[i]);
  695. usb_set_serial_port_data(ports[i], NULL);
  696. kfree(priv);
  697. }
  698. return -ENOMEM;
  699. }
  700. spin_lock_init(&priv->lock);
  701. usb_set_serial_port_data(ports[i], priv);
  702. }
  703. return 0;
  704. }
  705. static int clie_3_5_startup(struct usb_serial *serial)
  706. {
  707. struct device *dev = &serial->dev->dev;
  708. int result;
  709. u8 data;
  710. dbg("%s", __func__);
  711. /*
  712. * Note that PEG-300 series devices expect the following two calls.
  713. */
  714. /* get the config number */
  715. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  716. USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
  717. 0, 0, &data, 1, 3000);
  718. if (result < 0) {
  719. dev_err(dev, "%s: get config number failed: %d\n",
  720. __func__, result);
  721. return result;
  722. }
  723. if (result != 1) {
  724. dev_err(dev, "%s: get config number bad return length: %d\n",
  725. __func__, result);
  726. return -EIO;
  727. }
  728. /* get the interface number */
  729. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  730. USB_REQ_GET_INTERFACE,
  731. USB_DIR_IN | USB_RECIP_INTERFACE,
  732. 0, 0, &data, 1, 3000);
  733. if (result < 0) {
  734. dev_err(dev, "%s: get interface number failed: %d\n",
  735. __func__, result);
  736. return result;
  737. }
  738. if (result != 1) {
  739. dev_err(dev,
  740. "%s: get interface number bad return length: %d\n",
  741. __func__, result);
  742. return -EIO;
  743. }
  744. return generic_startup(serial);
  745. }
  746. static int treo_attach(struct usb_serial *serial)
  747. {
  748. struct usb_serial_port *swap_port;
  749. /* Only do this endpoint hack for the Handspring devices with
  750. * interrupt in endpoints, which for now are the Treo devices. */
  751. if (!((le16_to_cpu(serial->dev->descriptor.idVendor)
  752. == HANDSPRING_VENDOR_ID) ||
  753. (le16_to_cpu(serial->dev->descriptor.idVendor)
  754. == KYOCERA_VENDOR_ID)) ||
  755. (serial->num_interrupt_in == 0))
  756. goto generic_startup;
  757. dbg("%s", __func__);
  758. /*
  759. * It appears that Treos and Kyoceras want to use the
  760. * 1st bulk in endpoint to communicate with the 2nd bulk out endpoint,
  761. * so let's swap the 1st and 2nd bulk in and interrupt endpoints.
  762. * Note that swapping the bulk out endpoints would break lots of
  763. * apps that want to communicate on the second port.
  764. */
  765. #define COPY_PORT(dest, src) \
  766. do { \
  767. dest->read_urb = src->read_urb; \
  768. dest->bulk_in_endpointAddress = src->bulk_in_endpointAddress;\
  769. dest->bulk_in_buffer = src->bulk_in_buffer; \
  770. dest->interrupt_in_urb = src->interrupt_in_urb; \
  771. dest->interrupt_in_endpointAddress = \
  772. src->interrupt_in_endpointAddress;\
  773. dest->interrupt_in_buffer = src->interrupt_in_buffer; \
  774. } while (0);
  775. swap_port = kmalloc(sizeof(*swap_port), GFP_KERNEL);
  776. if (!swap_port)
  777. return -ENOMEM;
  778. COPY_PORT(swap_port, serial->port[0]);
  779. COPY_PORT(serial->port[0], serial->port[1]);
  780. COPY_PORT(serial->port[1], swap_port);
  781. kfree(swap_port);
  782. generic_startup:
  783. return generic_startup(serial);
  784. }
  785. static int clie_5_attach(struct usb_serial *serial)
  786. {
  787. dbg("%s", __func__);
  788. /* TH55 registers 2 ports.
  789. Communication in from the UX50/TH55 uses bulk_in_endpointAddress
  790. from port 0. Communication out to the UX50/TH55 uses
  791. bulk_out_endpointAddress from port 1
  792. Lets do a quick and dirty mapping
  793. */
  794. /* some sanity check */
  795. if (serial->num_ports < 2)
  796. return -1;
  797. /* port 0 now uses the modified endpoint Address */
  798. serial->port[0]->bulk_out_endpointAddress =
  799. serial->port[1]->bulk_out_endpointAddress;
  800. return generic_startup(serial);
  801. }
  802. static void visor_release(struct usb_serial *serial)
  803. {
  804. struct visor_private *priv;
  805. int i;
  806. dbg("%s", __func__);
  807. for (i = 0; i < serial->num_ports; i++) {
  808. priv = usb_get_serial_port_data(serial->port[i]);
  809. kfree(priv);
  810. }
  811. }
  812. static int __init visor_init(void)
  813. {
  814. int i, retval;
  815. /* Only if parameters were passed to us */
  816. if (vendor > 0 && product > 0) {
  817. struct usb_device_id usb_dev_temp[] = {
  818. {
  819. USB_DEVICE(vendor, product),
  820. .driver_info =
  821. (kernel_ulong_t) &palm_os_4_probe
  822. }
  823. };
  824. /* Find the last entry in id_table */
  825. for (i = 0;; i++) {
  826. if (id_table[i].idVendor == 0) {
  827. id_table[i] = usb_dev_temp[0];
  828. break;
  829. }
  830. }
  831. /* Find the last entry in id_table_combined */
  832. for (i = 0;; i++) {
  833. if (id_table_combined[i].idVendor == 0) {
  834. id_table_combined[i] = usb_dev_temp[0];
  835. break;
  836. }
  837. }
  838. printk(KERN_INFO KBUILD_MODNAME
  839. ": Untested USB device specified at time of module insertion\n");
  840. printk(KERN_INFO KBUILD_MODNAME
  841. ": Warning: This is not guaranteed to work\n");
  842. printk(KERN_INFO KBUILD_MODNAME
  843. ": Using a newer kernel is preferred to this method\n");
  844. printk(KERN_INFO KBUILD_MODNAME
  845. ": Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x\n",
  846. vendor, product);
  847. }
  848. retval = usb_serial_register(&handspring_device);
  849. if (retval)
  850. goto failed_handspring_register;
  851. retval = usb_serial_register(&clie_3_5_device);
  852. if (retval)
  853. goto failed_clie_3_5_register;
  854. retval = usb_serial_register(&clie_5_device);
  855. if (retval)
  856. goto failed_clie_5_register;
  857. retval = usb_register(&visor_driver);
  858. if (retval)
  859. goto failed_usb_register;
  860. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  861. return 0;
  862. failed_usb_register:
  863. usb_serial_deregister(&clie_5_device);
  864. failed_clie_5_register:
  865. usb_serial_deregister(&clie_3_5_device);
  866. failed_clie_3_5_register:
  867. usb_serial_deregister(&handspring_device);
  868. failed_handspring_register:
  869. return retval;
  870. }
  871. static void __exit visor_exit (void)
  872. {
  873. usb_deregister(&visor_driver);
  874. usb_serial_deregister(&handspring_device);
  875. usb_serial_deregister(&clie_3_5_device);
  876. usb_serial_deregister(&clie_5_device);
  877. }
  878. module_init(visor_init);
  879. module_exit(visor_exit);
  880. MODULE_AUTHOR(DRIVER_AUTHOR);
  881. MODULE_DESCRIPTION(DRIVER_DESC);
  882. MODULE_LICENSE("GPL");
  883. module_param(debug, bool, S_IRUGO | S_IWUSR);
  884. MODULE_PARM_DESC(debug, "Debug enabled or not");
  885. module_param(stats, bool, S_IRUGO | S_IWUSR);
  886. MODULE_PARM_DESC(stats, "Enables statistics or not");
  887. module_param(vendor, ushort, 0);
  888. MODULE_PARM_DESC(vendor, "User specified vendor ID");
  889. module_param(product, ushort, 0);
  890. MODULE_PARM_DESC(product, "User specified product ID");