visor.c 32 KB

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